|
| 1 | +package resourceapply |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/davecgh/go-spew/spew" |
| 7 | + "k8s.io/apimachinery/pkg/api/equality" |
| 8 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 9 | + "k8s.io/apimachinery/pkg/runtime" |
| 10 | + "k8s.io/apimachinery/pkg/util/diff" |
| 11 | + "k8s.io/client-go/kubernetes/fake" |
| 12 | + clienttesting "k8s.io/client-go/testing" |
| 13 | + |
| 14 | + corev1 "k8s.io/api/core/v1" |
| 15 | +) |
| 16 | + |
| 17 | +func TestApplyConfigMap(t *testing.T) { |
| 18 | + tests := []struct { |
| 19 | + name string |
| 20 | + existing []runtime.Object |
| 21 | + input *corev1.ConfigMap |
| 22 | + |
| 23 | + expectedModified bool |
| 24 | + verifyActions func(actions []clienttesting.Action, t *testing.T) |
| 25 | + }{ |
| 26 | + { |
| 27 | + name: "create", |
| 28 | + input: &corev1.ConfigMap{ |
| 29 | + ObjectMeta: metav1.ObjectMeta{Namespace: "one-ns", Name: "foo"}, |
| 30 | + }, |
| 31 | + |
| 32 | + expectedModified: true, |
| 33 | + verifyActions: func(actions []clienttesting.Action, t *testing.T) { |
| 34 | + if len(actions) != 2 { |
| 35 | + t.Fatal(spew.Sdump(actions)) |
| 36 | + } |
| 37 | + if !actions[0].Matches("get", "configmaps") || actions[0].(clienttesting.GetAction).GetName() != "foo" { |
| 38 | + t.Error(spew.Sdump(actions)) |
| 39 | + } |
| 40 | + if !actions[1].Matches("create", "configmaps") { |
| 41 | + t.Error(spew.Sdump(actions)) |
| 42 | + } |
| 43 | + expected := &corev1.ConfigMap{ |
| 44 | + ObjectMeta: metav1.ObjectMeta{Namespace: "one-ns", Name: "foo"}, |
| 45 | + } |
| 46 | + actual := actions[1].(clienttesting.CreateAction).GetObject().(*corev1.ConfigMap) |
| 47 | + if !equality.Semantic.DeepEqual(expected, actual) { |
| 48 | + t.Error(diff.ObjectDiff(expected, actual)) |
| 49 | + } |
| 50 | + }, |
| 51 | + }, |
| 52 | + { |
| 53 | + name: "skip on extra label", |
| 54 | + existing: []runtime.Object{ |
| 55 | + &corev1.ConfigMap{ |
| 56 | + ObjectMeta: metav1.ObjectMeta{Namespace: "one-ns", Name: "foo", Labels: map[string]string{"extra": "leave-alone"}}, |
| 57 | + }, |
| 58 | + }, |
| 59 | + input: &corev1.ConfigMap{ |
| 60 | + ObjectMeta: metav1.ObjectMeta{Namespace: "one-ns", Name: "foo"}, |
| 61 | + }, |
| 62 | + |
| 63 | + expectedModified: false, |
| 64 | + verifyActions: func(actions []clienttesting.Action, t *testing.T) { |
| 65 | + if len(actions) != 1 { |
| 66 | + t.Fatal(spew.Sdump(actions)) |
| 67 | + } |
| 68 | + if !actions[0].Matches("get", "configmaps") || actions[0].(clienttesting.GetAction).GetName() != "foo" { |
| 69 | + t.Error(spew.Sdump(actions)) |
| 70 | + } |
| 71 | + }, |
| 72 | + }, |
| 73 | + { |
| 74 | + name: "update on missing label", |
| 75 | + existing: []runtime.Object{ |
| 76 | + &corev1.ConfigMap{ |
| 77 | + ObjectMeta: metav1.ObjectMeta{Namespace: "one-ns", Name: "foo", Labels: map[string]string{"extra": "leave-alone"}}, |
| 78 | + }, |
| 79 | + }, |
| 80 | + input: &corev1.ConfigMap{ |
| 81 | + ObjectMeta: metav1.ObjectMeta{Namespace: "one-ns", Name: "foo", Labels: map[string]string{"new": "merge"}}, |
| 82 | + }, |
| 83 | + |
| 84 | + expectedModified: true, |
| 85 | + verifyActions: func(actions []clienttesting.Action, t *testing.T) { |
| 86 | + if len(actions) != 2 { |
| 87 | + t.Fatal(spew.Sdump(actions)) |
| 88 | + } |
| 89 | + if !actions[0].Matches("get", "configmaps") || actions[0].(clienttesting.GetAction).GetName() != "foo" { |
| 90 | + t.Error(spew.Sdump(actions)) |
| 91 | + } |
| 92 | + if !actions[1].Matches("update", "configmaps") { |
| 93 | + t.Error(spew.Sdump(actions)) |
| 94 | + } |
| 95 | + expected := &corev1.ConfigMap{ |
| 96 | + ObjectMeta: metav1.ObjectMeta{Namespace: "one-ns", Name: "foo", Labels: map[string]string{"extra": "leave-alone", "new": "merge"}}, |
| 97 | + } |
| 98 | + actual := actions[1].(clienttesting.UpdateAction).GetObject().(*corev1.ConfigMap) |
| 99 | + if !equality.Semantic.DeepEqual(expected, actual) { |
| 100 | + t.Error(diff.ObjectDiff(expected, actual)) |
| 101 | + } |
| 102 | + }, |
| 103 | + }, |
| 104 | + { |
| 105 | + name: "update on mismatch data", |
| 106 | + existing: []runtime.Object{ |
| 107 | + &corev1.ConfigMap{ |
| 108 | + ObjectMeta: metav1.ObjectMeta{Namespace: "one-ns", Name: "foo", Labels: map[string]string{"extra": "leave-alone"}}, |
| 109 | + }, |
| 110 | + }, |
| 111 | + input: &corev1.ConfigMap{ |
| 112 | + ObjectMeta: metav1.ObjectMeta{Namespace: "one-ns", Name: "foo"}, |
| 113 | + Data: map[string]string{ |
| 114 | + "configmap": "value", |
| 115 | + }, |
| 116 | + }, |
| 117 | + |
| 118 | + expectedModified: true, |
| 119 | + verifyActions: func(actions []clienttesting.Action, t *testing.T) { |
| 120 | + if len(actions) != 2 { |
| 121 | + t.Fatal(spew.Sdump(actions)) |
| 122 | + } |
| 123 | + if !actions[0].Matches("get", "configmaps") || actions[0].(clienttesting.GetAction).GetName() != "foo" { |
| 124 | + t.Error(spew.Sdump(actions)) |
| 125 | + } |
| 126 | + if !actions[1].Matches("update", "configmaps") { |
| 127 | + t.Error(spew.Sdump(actions)) |
| 128 | + } |
| 129 | + expected := &corev1.ConfigMap{ |
| 130 | + ObjectMeta: metav1.ObjectMeta{Namespace: "one-ns", Name: "foo", Labels: map[string]string{"extra": "leave-alone"}}, |
| 131 | + Data: map[string]string{ |
| 132 | + "configmap": "value", |
| 133 | + }, |
| 134 | + } |
| 135 | + actual := actions[1].(clienttesting.UpdateAction).GetObject().(*corev1.ConfigMap) |
| 136 | + if !equality.Semantic.DeepEqual(expected, actual) { |
| 137 | + t.Error(diff.ObjectDiff(expected, actual)) |
| 138 | + } |
| 139 | + }, |
| 140 | + }, |
| 141 | + } |
| 142 | + |
| 143 | + for _, test := range tests { |
| 144 | + t.Run(test.name, func(t *testing.T) { |
| 145 | + client := fake.NewSimpleClientset(test.existing...) |
| 146 | + actualModified, err := ApplyConfigMap(client.CoreV1(), test.input) |
| 147 | + if err != nil { |
| 148 | + t.Fatal(err) |
| 149 | + } |
| 150 | + if test.expectedModified != actualModified { |
| 151 | + t.Errorf("expected %v, got %v", test.expectedModified, actualModified) |
| 152 | + } |
| 153 | + test.verifyActions(client.Actions(), t) |
| 154 | + }) |
| 155 | + } |
| 156 | +} |
0 commit comments