|
| 1 | +/* |
| 2 | +Copyright 2018 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package internal |
| 18 | + |
| 19 | +import ( |
| 20 | + "testing" |
| 21 | + |
| 22 | + "k8s.io/apimachinery/pkg/runtime" |
| 23 | + "k8s.io/apimachinery/pkg/runtime/schema" |
| 24 | + "k8s.io/apimachinery/pkg/watch" |
| 25 | +) |
| 26 | + |
| 27 | +type testKind struct { |
| 28 | + gvk schema.GroupVersionKind |
| 29 | +} |
| 30 | + |
| 31 | +func (o *testKind) SetGroupVersionKind(kind schema.GroupVersionKind) { |
| 32 | + o.gvk = kind |
| 33 | +} |
| 34 | + |
| 35 | +func (o *testKind) GroupVersionKind() schema.GroupVersionKind { |
| 36 | + return o.gvk |
| 37 | +} |
| 38 | + |
| 39 | +type testType struct { |
| 40 | + kind schema.ObjectKind |
| 41 | + label string |
| 42 | +} |
| 43 | + |
| 44 | +func newTestType(label string) testType { |
| 45 | + return testType{ |
| 46 | + kind: &testKind{ |
| 47 | + gvk: schema.GroupVersionKind{ |
| 48 | + Group: "originalgroup", |
| 49 | + Version: "OriginalVersion", |
| 50 | + Kind: "OriginalKind", |
| 51 | + }, |
| 52 | + }, |
| 53 | + label: label, |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +func (obj testType) GetObjectKind() schema.ObjectKind { return obj.kind } |
| 58 | +func (obj testType) DeepCopyObject() runtime.Object { return obj } |
| 59 | + |
| 60 | +// TestGVKFixupWatcher tests that gvkFixupWatcher behaves like the watch that it |
| 61 | +// wraps and that it overrides the GVK. |
| 62 | +// Adapted from https://github.com/kubernetes/kubernetes/blob/adbda068c1808fcc8a64a94269e0766b5c46ec41/staging/src/k8s.io/apimachinery/pkg/watch/watch_test.go#L33-L78 |
| 63 | +func TestGVKFixupWatcher(t *testing.T) { |
| 64 | + f := watch.NewFake() |
| 65 | + // This is the GVK which we expect the wrapper to set on all the events |
| 66 | + expectedGVK := schema.GroupVersionKind{ |
| 67 | + Group: "testgroup", |
| 68 | + Version: "v999", |
| 69 | + Kind: "TestKind", |
| 70 | + } |
| 71 | + gvkfw := newGVKFixupWatcher(expectedGVK, f) |
| 72 | + |
| 73 | + table := []struct { |
| 74 | + t watch.EventType |
| 75 | + s testType |
| 76 | + }{ |
| 77 | + {watch.Added, newTestType("foo")}, |
| 78 | + {watch.Modified, newTestType("qux")}, |
| 79 | + {watch.Modified, newTestType("bar")}, |
| 80 | + {watch.Deleted, newTestType("bar")}, |
| 81 | + {watch.Error, newTestType("error: blah")}, |
| 82 | + } |
| 83 | + |
| 84 | + // Prove that f implements Interface by phrasing this as a function. |
| 85 | + consumer := func(w watch.Interface) { |
| 86 | + for _, expect := range table { |
| 87 | + got, ok := <-w.ResultChan() |
| 88 | + if !ok { |
| 89 | + t.Fatalf("closed early") |
| 90 | + } |
| 91 | + if e, a := expect.t, got.Type; e != a { |
| 92 | + t.Fatalf("Expected %v, got %v", e, a) |
| 93 | + } |
| 94 | + a, ok := got.Object.(testType) |
| 95 | + if !ok { |
| 96 | + t.Fatalf("Unexpected type: %T", got.Object) |
| 97 | + } |
| 98 | + if a.label != expect.s.label { |
| 99 | + t.Fatalf("Expected %#v, got %#v", expect.s, a) |
| 100 | + } |
| 101 | + if actualGVK := got.Object.GetObjectKind().GroupVersionKind(); expectedGVK != actualGVK { |
| 102 | + t.Fatalf("Expected %v, got %v", expectedGVK, actualGVK) |
| 103 | + } |
| 104 | + } |
| 105 | + _, stillOpen := <-w.ResultChan() |
| 106 | + if stillOpen { |
| 107 | + t.Fatal("Never stopped") |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + sender := func() { |
| 112 | + f.Add(newTestType("foo")) |
| 113 | + f.Action(watch.Modified, newTestType("qux")) |
| 114 | + f.Modify(newTestType("bar")) |
| 115 | + f.Delete(newTestType("bar")) |
| 116 | + f.Error(newTestType("error: blah")) |
| 117 | + f.Stop() |
| 118 | + } |
| 119 | + |
| 120 | + go sender() |
| 121 | + consumer(gvkfw) |
| 122 | +} |
0 commit comments