39 lines
1.0 KiB
Go
39 lines
1.0 KiB
Go
package api
|
|
|
|
import (
|
|
"errors"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
type stubPolicyLoader struct {
|
|
calls int
|
|
err error
|
|
}
|
|
|
|
func (loader *stubPolicyLoader) LoadPolicy() error {
|
|
loader.calls++
|
|
return loader.err
|
|
}
|
|
|
|
func TestReloadPoliciesRefreshesEveryRuntimeEnforcer(t *testing.T) {
|
|
first, second := &stubPolicyLoader{}, &stubPolicyLoader{}
|
|
if err := reloadPolicies(map[string]policyLoader{"*": first, "tenant": second}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if first.calls != 1 || second.calls != 1 {
|
|
t.Fatalf("reload calls first=%d second=%d", first.calls, second.calls)
|
|
}
|
|
}
|
|
|
|
func TestReloadPoliciesFailsClosed(t *testing.T) {
|
|
failure := &stubPolicyLoader{err: errors.New("adapter unavailable")}
|
|
err := reloadPolicies(map[string]policyLoader{"*": failure})
|
|
if err == nil || !strings.Contains(err.Error(), "adapter unavailable") || failure.calls != 1 {
|
|
t.Fatalf("unexpected reload failure: %v calls=%d", err, failure.calls)
|
|
}
|
|
if err := reloadPolicies(map[string]policyLoader{"*": nil}); err == nil {
|
|
t.Fatal("nil enforcer was accepted")
|
|
}
|
|
}
|