|
| 1 | +package controller |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + "testing" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/bpineau/katafygio/config" |
| 9 | + "github.com/bpineau/katafygio/pkg/event" |
| 10 | + "github.com/bpineau/katafygio/pkg/log" |
| 11 | + |
| 12 | + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" |
| 13 | + fakecontroller "k8s.io/client-go/tools/cache/testing" |
| 14 | +) |
| 15 | + |
| 16 | +type mockNotifier struct { |
| 17 | + evts []*event.Notification |
| 18 | +} |
| 19 | + |
| 20 | +func (m *mockNotifier) Send(ev *event.Notification) { |
| 21 | + m.evts = append(m.evts, ev) |
| 22 | +} |
| 23 | + |
| 24 | +func (m *mockNotifier) ReadChan() <-chan event.Notification { |
| 25 | + return make(chan event.Notification) |
| 26 | +} |
| 27 | + |
| 28 | +var ( |
| 29 | + obj1 = &unstructured.Unstructured{ |
| 30 | + Object: map[string]interface{}{ |
| 31 | + "apiVersion": "v1", |
| 32 | + "kind": "Foo1", |
| 33 | + "metadata": map[string]interface{}{ |
| 34 | + "name": "Bar1", |
| 35 | + "namespace": "ns1", |
| 36 | + "resourceVersion": 1, |
| 37 | + "uid": "00000000-0000-0000-0000-000000000042", |
| 38 | + "selfLink": "shouldnotbethere", |
| 39 | + }, |
| 40 | + "status": "shouldnotbethere", |
| 41 | + }, |
| 42 | + } |
| 43 | + |
| 44 | + obj2 = &unstructured.Unstructured{ |
| 45 | + Object: map[string]interface{}{ |
| 46 | + "apiVersion": "v1", |
| 47 | + "kind": "Foo2", |
| 48 | + "metadata": map[string]interface{}{ |
| 49 | + "name": "Bar2", |
| 50 | + "namespace": "ns2", |
| 51 | + "resourceVersion": 1, |
| 52 | + "uid": "00000000-0000-0000-0000-000000000042", |
| 53 | + "selfLink": "shouldnotbethere", |
| 54 | + }, |
| 55 | + "status": "shouldnotbethere", |
| 56 | + }, |
| 57 | + } |
| 58 | + |
| 59 | + obj3 = &unstructured.Unstructured{ |
| 60 | + Object: map[string]interface{}{ |
| 61 | + "apiVersion": "v1", |
| 62 | + "kind": "Foo3", |
| 63 | + "metadata": map[string]interface{}{ |
| 64 | + "name": "Bar3", |
| 65 | + "namespace": "ns3", |
| 66 | + "resourceVersion": 1, |
| 67 | + "uid": "00000000-0000-0000-0000-000000000042", |
| 68 | + "selfLink": "shouldnotbethere", |
| 69 | + }, |
| 70 | + "status": "shouldnotbethere", |
| 71 | + }, |
| 72 | + } |
| 73 | +) |
| 74 | + |
| 75 | +func TestController(t *testing.T) { |
| 76 | + |
| 77 | + conf := &config.KfConfig{ |
| 78 | + Logger: log.New("info", "", "test"), |
| 79 | + ExcludeObject: []string{"pod:ns3/Bar3"}, |
| 80 | + |
| 81 | + // label filters can't be tested due to the way we inject objets in tests |
| 82 | + Filter: "label1=something", |
| 83 | + } |
| 84 | + |
| 85 | + client := fakecontroller.NewFakeControllerSource() |
| 86 | + |
| 87 | + evt := new(mockNotifier) |
| 88 | + f := new(Factory) |
| 89 | + ctrl := f.NewController(client, evt, "pod", conf) |
| 90 | + |
| 91 | + // this will trigger a deletion event |
| 92 | + idx := ctrl.(*Controller).informer.GetIndexer() |
| 93 | + err := idx.Add(obj1) |
| 94 | + if err != nil { |
| 95 | + t.Errorf("failed to inject an object in indexer: %v", err) |
| 96 | + } |
| 97 | + |
| 98 | + client.Add(obj2) |
| 99 | + client.Add(obj3) |
| 100 | + |
| 101 | + ctrl.Start() |
| 102 | + // wait until queue is drained |
| 103 | + for ctrl.(*Controller).queue.Len() > 0 { |
| 104 | + time.Sleep(10 * time.Millisecond) |
| 105 | + } |
| 106 | + ctrl.Stop() |
| 107 | + |
| 108 | + gotFoo2 := false |
| 109 | + for _, ev := range evt.evts { |
| 110 | + // ensure cleanup filters works as expected |
| 111 | + if strings.Contains(ev.Object, "shouldnotbethere") { |
| 112 | + t.Error("object cleanup filters didn't work") |
| 113 | + } |
| 114 | + |
| 115 | + // ensure deletion notifications pops up as expected |
| 116 | + if strings.Compare(ev.Key, "ns1/Bar1") == 0 && ev.Action != event.Delete { |
| 117 | + t.Error("deletion notification failed") |
| 118 | + } |
| 119 | + |
| 120 | + if strings.Compare(ev.Key, "ns2/Bar2") == 0 { |
| 121 | + gotFoo2 = true |
| 122 | + } |
| 123 | + |
| 124 | + // ensure objet filter works as expected |
| 125 | + if strings.Compare(ev.Key, "ns3/Bar3") == 0 { |
| 126 | + t.Error("execludedobject filter failed") |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + if !gotFoo2 { |
| 131 | + t.Errorf("we should have notified obj2") |
| 132 | + } |
| 133 | +} |
0 commit comments