Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ func (c *Config) Load() error {
if err != nil {
return err
}
enabled := make([]ModuleConfig, 0)
for _, mc := range c.Modules {
if v, ok := iconfig[mc.Name()]; ok {
cc := CommonConfig{}
Expand All @@ -79,9 +80,11 @@ func (c *Config) Load() error {
if err != nil {
return err
}
enabled = append(enabled, mc)
}
}
}
c.Modules = enabled
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion config/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ limits:
min: 1G
max: 1G
healthz:
enabled: true
enabled: false
ignoredNamespaces:
- "test1"
- "test3"
Expand Down
1 change: 1 addition & 0 deletions config/config.yaml.fail
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
...
135 changes: 134 additions & 1 deletion config/config_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,143 @@
package config

import "testing"
import (
"flag"
"reflect"
"testing"

"github.com/jasonrichardsmith/sentry/sentry"
"github.com/mitchellh/mapstructure"
"k8s.io/api/admission/v1beta1"
)

var (
ignored = map[string][]string{
"source": {"test1"},
}
decoders = map[string]mapstructure.DecodeHookFunc{"test": FakeHookFunc}
)

func TestNew(t *testing.T) {
c := New()
if c.Modules == nil || c.decoders == nil || c.ignored == nil {
t.Fatal("Not all config structures instantiated")
}
}

type FakeSentry struct{}

func (fs FakeSentry) Admit(v1beta1.AdmissionReview) (ar *v1beta1.AdmissionResponse) { return }

func (fs FakeSentry) Type() string { return "test" }

type FakeModuleConfig struct {
Allowed []string
}

func (f FakeModuleConfig) LoadSentry() sentry.Sentry { return FakeSentry{} }
func (f FakeModuleConfig) Name() string { return "source" }

type DisabledModuleConfig struct{}

func (d DisabledModuleConfig) LoadSentry() sentry.Sentry { return FakeSentry{} }
func (d DisabledModuleConfig) Name() string { return "healthz" }

func TestLoad(t *testing.T) {
c := New()
err := c.Load()
if err != nil {
t.Fatal(err)
}
if !flag.Parsed() {
t.Fatal("Expecting parsed flags")
}
c = New()
c.Register(FakeModuleConfig{})
err = c.Load()
if err == nil {
t.Fatal("expect pointer error")
}
c = New()
c.Register(&FakeModuleConfig{})
err = c.Load()
if err != nil {
t.Fatal(err)
}
if len(c.Modules) != 1 {
t.Fatal("Expected one module loaded")
}
if !reflect.DeepEqual(c.Ignored("source"), []string{"test1"}) {
t.Fatal("Expected one ignore value")
}
if !reflect.DeepEqual(c.ignored, ignored) {
t.Fatal("mismatched ignored")
}
c = New()
c.Register(&DisabledModuleConfig{})
err = c.Load()
if err != nil {
t.Fatal(err)
}
if len(c.Modules) != 0 {
t.Fatal("Expected no module loaded")
}

configfile = "config.yaml.fail"
err = c.Load()
if err == nil {
t.Fatal("Expected yaml Unmarshal error")
}
}

func TestDefaultLoad(t *testing.T) {
configfile = "config.yaml"
err := Load()
if err != nil {
t.Fatal(err)
}
c := New()
err = c.Load()
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(DefaultConfig, c) {
t.Fatal("expected matching configs")
}
}

func TestRegister(t *testing.T) {
c := New()
err := c.Register(&FakeModuleConfig{})
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(c.Modules[0], &FakeModuleConfig{}) {
t.Fatal("expected matching moduleconfigs")
}
err = c.Register(&FakeModuleConfig{})
if err != ERR_DUPLICATE_MODULENAME {
t.Fatal("Expected matching error")
}
err = c.Register(&DisabledModuleConfig{})
if err != nil {
t.Fatal(err)
}
if len(c.Modules) != 2 {
t.Fatal("Expected two modules registered")
}
}

func FakeHookFunc(f reflect.Type, t reflect.Type, data interface{}) (interface{}, error) {
return data, nil
}

func TestDecoder(t *testing.T) {
Decoder("test", FakeHookFunc)
if len(DefaultConfig.decoders) != 1 {
t.Fatal("Expected one decoder")
}
}

func TestIgnored(t *testing.T) {

}
16 changes: 16 additions & 0 deletions example/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,24 @@ import (
"k8s.io/apimachinery/pkg/runtime"
)

var (
faildecode = "{"
)

func TestType(t *testing.T) {
ts := ExampleSentry{}
if ts.Type() != "Pod" {
t.Fatal("Failed type test")
}
}

func TestName(t *testing.T) {
c := Config{}
if c.Name() != "example" {
t.Fatal("Failed name test")
}
}

func TestAdmit(t *testing.T) {
s := ExampleSentry{}
ar := v1beta1.AdmissionReview{
Expand All @@ -27,4 +38,9 @@ func TestAdmit(t *testing.T) {
if !resp.Allowed {
t.Fatal("expected passing review")
}
ar.Request.Object.Raw = []byte(faildecode)
resp = s.Admit(ar)
if resp.Allowed {
t.Fatal("expected passing from failed decode")
}
}