| Serge Bazanski | e6030f6 | 2020-06-03 17:52:59 +0200 | [diff] [blame] | 1 | // Copyright 2020 The Monogon Project Authors. |
| 2 | // |
| 3 | // SPDX-License-Identifier: Apache-2.0 |
| 4 | // |
| 5 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | // you may not use this file except in compliance with the License. |
| 7 | // You may obtain a copy of the License at |
| 8 | // |
| 9 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | // |
| 11 | // Unless required by applicable law or agreed to in writing, software |
| 12 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | // See the License for the specific language governing permissions and |
| 15 | // limitations under the License. |
| 16 | |
| 17 | package reconciler |
| 18 | |
| 19 | import ( |
| 20 | "context" |
| 21 | "fmt" |
| 22 | "testing" |
| 23 | |
| Jan Schär | 69f5f4e | 2024-05-15 10:32:07 +0200 | [diff] [blame] | 24 | apiequality "k8s.io/apimachinery/pkg/api/equality" |
| 25 | apierrors "k8s.io/apimachinery/pkg/api/errors" |
| 26 | apivalidation "k8s.io/apimachinery/pkg/api/validation" |
| Serge Bazanski | e6030f6 | 2020-06-03 17:52:59 +0200 | [diff] [blame] | 27 | meta "k8s.io/apimachinery/pkg/apis/meta/v1" |
| Jan Schär | 69f5f4e | 2024-05-15 10:32:07 +0200 | [diff] [blame] | 28 | "k8s.io/apimachinery/pkg/runtime" |
| 29 | "k8s.io/apimachinery/pkg/runtime/schema" |
| 30 | "k8s.io/apimachinery/pkg/util/validation/field" |
| 31 | installnode "k8s.io/kubernetes/pkg/apis/node/install" |
| 32 | installpolicy "k8s.io/kubernetes/pkg/apis/policy/install" |
| 33 | installrbac "k8s.io/kubernetes/pkg/apis/rbac/install" |
| 34 | installstorage "k8s.io/kubernetes/pkg/apis/storage/install" |
| 35 | |
| Tim Windelschmidt | 9f21f53 | 2024-05-07 15:14:20 +0200 | [diff] [blame] | 36 | "source.monogon.dev/osbase/supervisor" |
| Serge Bazanski | e6030f6 | 2020-06-03 17:52:59 +0200 | [diff] [blame] | 37 | ) |
| 38 | |
| Jan Schär | 69f5f4e | 2024-05-15 10:32:07 +0200 | [diff] [blame] | 39 | // TestExpectedUniqueNames ensures that all the Expected objects of any |
| 40 | // given resource type have a unique name. |
| 41 | func TestExpectedUniqueNames(t *testing.T) { |
| 42 | for reconciler, r := range allResources(nil) { |
| 43 | names := make(map[string]bool) |
| 44 | for _, v := range r.Expected() { |
| 45 | if names[v.GetName()] { |
| 46 | t.Errorf("reconciler %q: duplicate name %q", reconciler, v.GetName()) |
| 47 | continue |
| 48 | } |
| 49 | names[v.GetName()] = true |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 54 | // TestExpectedLabeledCorrectly ensures that all the Expected objects of all |
| 55 | // resource types have a Kubernetes metadata label that signifies it's a |
| 56 | // builtin object, to be retrieved afterwards. This contract must be met in |
| 57 | // order for the reconciler to not keep overwriting objects (and possibly |
| 58 | // failing), when a newly created object is not then retrievable using a |
| Jan Schär | 7f72748 | 2024-03-25 13:03:51 +0100 | [diff] [blame] | 59 | // selector corresponding to this label. |
| Serge Bazanski | e6030f6 | 2020-06-03 17:52:59 +0200 | [diff] [blame] | 60 | func TestExpectedLabeledCorrectly(t *testing.T) { |
| 61 | for reconciler, r := range allResources(nil) { |
| Jan Schär | 69f5f4e | 2024-05-15 10:32:07 +0200 | [diff] [blame] | 62 | for _, v := range r.Expected() { |
| Jan Schär | 7f72748 | 2024-03-25 13:03:51 +0100 | [diff] [blame] | 63 | if data := v.GetLabels()[BuiltinLabelKey]; data != BuiltinLabelValue { |
| Jan Schär | 69f5f4e | 2024-05-15 10:32:07 +0200 | [diff] [blame] | 64 | t.Errorf("reconciler %q, object %q: %q=%q, wanted =%q", reconciler, v.GetName(), BuiltinLabelKey, data, BuiltinLabelValue) |
| Serge Bazanski | e6030f6 | 2020-06-03 17:52:59 +0200 | [diff] [blame] | 65 | continue |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | |
| Jan Schär | 69f5f4e | 2024-05-15 10:32:07 +0200 | [diff] [blame] | 71 | // TestExpectedDefaulted ensures that all the Expected objects of all |
| 72 | // resource types have defaults already applied. If this were not the case, |
| 73 | // the reconciler would think that the object has changed and try to update it |
| 74 | // in each iteration. If this test fails, the most likely fix is to add the |
| 75 | // missing default values to the expected objects. |
| 76 | func TestExpectedDefaulted(t *testing.T) { |
| 77 | scheme := runtime.NewScheme() |
| 78 | installnode.Install(scheme) |
| 79 | installpolicy.Install(scheme) |
| 80 | installrbac.Install(scheme) |
| 81 | installstorage.Install(scheme) |
| 82 | |
| 83 | for reconciler, r := range allResources(nil) { |
| 84 | for _, v := range r.Expected() { |
| 85 | v_defaulted := v.(runtime.Object).DeepCopyObject() |
| 86 | if _, ok := scheme.IsUnversioned(v_defaulted); !ok { |
| 87 | t.Errorf("reconciler %q: type not installed in scheme", reconciler) |
| 88 | } |
| 89 | scheme.Default(v_defaulted) |
| 90 | if !apiequality.Semantic.DeepEqual(v, v_defaulted) { |
| 91 | t.Errorf("reconciler %q, object %q changed after defaulting\ngot: %+v\nwanted: %+v", reconciler, v.GetName(), v, v_defaulted) |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | |
| Jan Schär | 7f72748 | 2024-03-25 13:03:51 +0100 | [diff] [blame] | 97 | // testObject is the object type managed by testResource. |
| 98 | type testObject struct { |
| 99 | meta.ObjectMeta |
| Jan Schär | 69f5f4e | 2024-05-15 10:32:07 +0200 | [diff] [blame] | 100 | Val int |
| Jan Schär | 7f72748 | 2024-03-25 13:03:51 +0100 | [diff] [blame] | 101 | } |
| 102 | |
| Jan Schär | 69f5f4e | 2024-05-15 10:32:07 +0200 | [diff] [blame] | 103 | func makeTestObject(name string, val int) *testObject { |
| Jan Schär | 7f72748 | 2024-03-25 13:03:51 +0100 | [diff] [blame] | 104 | return &testObject{ |
| 105 | ObjectMeta: meta.ObjectMeta{ |
| 106 | Name: name, |
| 107 | Labels: builtinLabels(nil), |
| 108 | }, |
| Jan Schär | 69f5f4e | 2024-05-15 10:32:07 +0200 | [diff] [blame] | 109 | Val: val, |
| Jan Schär | 7f72748 | 2024-03-25 13:03:51 +0100 | [diff] [blame] | 110 | } |
| 111 | } |
| 112 | |
| 113 | // testResource is a resource type used for testing. It simulates a target |
| 114 | // (ie. k8s apiserver mock) that always acts nominally (all resources are |
| 115 | // created, deleted as requested, and the state is consistent with requests). |
| Serge Bazanski | e6030f6 | 2020-06-03 17:52:59 +0200 | [diff] [blame] | 116 | type testResource struct { |
| 117 | // current is the simulated state of resources in the target. |
| Jan Schär | 7f72748 | 2024-03-25 13:03:51 +0100 | [diff] [blame] | 118 | current map[string]*testObject |
| Serge Bazanski | e6030f6 | 2020-06-03 17:52:59 +0200 | [diff] [blame] | 119 | // expected is what this type will report as the Expected() resources. |
| Jan Schär | 7f72748 | 2024-03-25 13:03:51 +0100 | [diff] [blame] | 120 | expected map[string]*testObject |
| Serge Bazanski | e6030f6 | 2020-06-03 17:52:59 +0200 | [diff] [blame] | 121 | } |
| 122 | |
| Jan Schär | 7f72748 | 2024-03-25 13:03:51 +0100 | [diff] [blame] | 123 | func (r *testResource) List(ctx context.Context) ([]meta.Object, error) { |
| 124 | var cur []meta.Object |
| 125 | for _, v := range r.current { |
| 126 | v_copy := *v |
| 127 | cur = append(cur, &v_copy) |
| Serge Bazanski | e6030f6 | 2020-06-03 17:52:59 +0200 | [diff] [blame] | 128 | } |
| Jan Schär | 7f72748 | 2024-03-25 13:03:51 +0100 | [diff] [blame] | 129 | return cur, nil |
| Serge Bazanski | e6030f6 | 2020-06-03 17:52:59 +0200 | [diff] [blame] | 130 | } |
| 131 | |
| Jan Schär | 7f72748 | 2024-03-25 13:03:51 +0100 | [diff] [blame] | 132 | func (r *testResource) Create(ctx context.Context, el meta.Object) error { |
| 133 | r.current[el.GetName()] = el.(*testObject) |
| Serge Bazanski | e6030f6 | 2020-06-03 17:52:59 +0200 | [diff] [blame] | 134 | return nil |
| 135 | } |
| 136 | |
| Jan Schär | 69f5f4e | 2024-05-15 10:32:07 +0200 | [diff] [blame] | 137 | func (r *testResource) Update(ctx context.Context, el meta.Object) error { |
| 138 | r.current[el.GetName()] = el.(*testObject) |
| 139 | return nil |
| 140 | } |
| 141 | |
| 142 | func (r *testResource) Delete(ctx context.Context, name string, opts meta.DeleteOptions) error { |
| Serge Bazanski | e6030f6 | 2020-06-03 17:52:59 +0200 | [diff] [blame] | 143 | delete(r.current, name) |
| 144 | return nil |
| 145 | } |
| 146 | |
| Jan Schär | 7f72748 | 2024-03-25 13:03:51 +0100 | [diff] [blame] | 147 | func (r *testResource) Expected() []meta.Object { |
| 148 | var exp []meta.Object |
| 149 | for _, v := range r.expected { |
| 150 | v_copy := *v |
| 151 | exp = append(exp, &v_copy) |
| Serge Bazanski | e6030f6 | 2020-06-03 17:52:59 +0200 | [diff] [blame] | 152 | } |
| 153 | return exp |
| 154 | } |
| 155 | |
| Jan Schär | 7f72748 | 2024-03-25 13:03:51 +0100 | [diff] [blame] | 156 | // newTestResource creates a test resource with a list of expected objects. |
| 157 | func newTestResource(want ...*testObject) *testResource { |
| 158 | expected := make(map[string]*testObject) |
| Serge Bazanski | e6030f6 | 2020-06-03 17:52:59 +0200 | [diff] [blame] | 159 | for _, w := range want { |
| Jan Schär | 7f72748 | 2024-03-25 13:03:51 +0100 | [diff] [blame] | 160 | expected[w.GetName()] = w |
| Serge Bazanski | e6030f6 | 2020-06-03 17:52:59 +0200 | [diff] [blame] | 161 | } |
| 162 | return &testResource{ |
| Jan Schär | 7f72748 | 2024-03-25 13:03:51 +0100 | [diff] [blame] | 163 | current: make(map[string]*testObject), |
| Serge Bazanski | e6030f6 | 2020-06-03 17:52:59 +0200 | [diff] [blame] | 164 | expected: expected, |
| 165 | } |
| 166 | } |
| 167 | |
| Jan Schär | 7f72748 | 2024-03-25 13:03:51 +0100 | [diff] [blame] | 168 | // currentDiff returns a human-readable string showing the difference between |
| 169 | // the current state and the given objects. If no difference is |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 170 | // present, the returned string is empty. |
| Jan Schär | 7f72748 | 2024-03-25 13:03:51 +0100 | [diff] [blame] | 171 | func (r *testResource) currentDiff(want ...*testObject) string { |
| 172 | expected := make(map[string]*testObject) |
| Serge Bazanski | e6030f6 | 2020-06-03 17:52:59 +0200 | [diff] [blame] | 173 | for _, w := range want { |
| Jan Schär | 7f72748 | 2024-03-25 13:03:51 +0100 | [diff] [blame] | 174 | if _, ok := r.current[w.GetName()]; !ok { |
| 175 | return fmt.Sprintf("%q missing in current", w.GetName()) |
| Serge Bazanski | e6030f6 | 2020-06-03 17:52:59 +0200 | [diff] [blame] | 176 | } |
| Jan Schär | 7f72748 | 2024-03-25 13:03:51 +0100 | [diff] [blame] | 177 | expected[w.GetName()] = w |
| Serge Bazanski | e6030f6 | 2020-06-03 17:52:59 +0200 | [diff] [blame] | 178 | } |
| 179 | for _, g := range r.current { |
| Jan Schär | 7f72748 | 2024-03-25 13:03:51 +0100 | [diff] [blame] | 180 | if _, ok := expected[g.GetName()]; !ok { |
| 181 | return fmt.Sprintf("%q spurious in current", g.GetName()) |
| Serge Bazanski | e6030f6 | 2020-06-03 17:52:59 +0200 | [diff] [blame] | 182 | } |
| 183 | } |
| 184 | return "" |
| 185 | } |
| 186 | |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 187 | // TestBasicReconciliation ensures that the reconcile function does manipulate |
| 188 | // a target state based on a set of expected resources. |
| Serge Bazanski | e6030f6 | 2020-06-03 17:52:59 +0200 | [diff] [blame] | 189 | func TestBasicReconciliation(t *testing.T) { |
| Jan Schär | 69f5f4e | 2024-05-15 10:32:07 +0200 | [diff] [blame] | 190 | // This needs to run in a TestHarness to make logging work. |
| 191 | supervisor.TestHarness(t, func(ctx context.Context) error { |
| 192 | r := newTestResource(makeTestObject("foo", 0), makeTestObject("bar", 0), makeTestObject("baz", 0)) |
| 193 | rname := "testresource" |
| Serge Bazanski | e6030f6 | 2020-06-03 17:52:59 +0200 | [diff] [blame] | 194 | |
| Jan Schär | 69f5f4e | 2024-05-15 10:32:07 +0200 | [diff] [blame] | 195 | // nothing should have happened yet (testing the test) |
| 196 | if diff := r.currentDiff(); diff != "" { |
| 197 | return fmt.Errorf("wrong state after creation: %s", diff) |
| 198 | } |
| Serge Bazanski | e6030f6 | 2020-06-03 17:52:59 +0200 | [diff] [blame] | 199 | |
| Jan Schär | 69f5f4e | 2024-05-15 10:32:07 +0200 | [diff] [blame] | 200 | if err := reconcile(ctx, r, rname); err != nil { |
| 201 | return fmt.Errorf("reconcile: %v", err) |
| 202 | } |
| 203 | // everything requested should have been created |
| 204 | if diff := r.currentDiff(makeTestObject("foo", 0), makeTestObject("bar", 0), makeTestObject("baz", 0)); diff != "" { |
| 205 | return fmt.Errorf("wrong state after reconciliation: %s", diff) |
| 206 | } |
| Serge Bazanski | e6030f6 | 2020-06-03 17:52:59 +0200 | [diff] [blame] | 207 | |
| Jan Schär | 69f5f4e | 2024-05-15 10:32:07 +0200 | [diff] [blame] | 208 | delete(r.expected, "foo") |
| 209 | if err := reconcile(ctx, r, rname); err != nil { |
| 210 | return fmt.Errorf("reconcile: %v", err) |
| 211 | } |
| 212 | // foo should now be missing |
| 213 | if diff := r.currentDiff(makeTestObject("bar", 0), makeTestObject("baz", 0)); diff != "" { |
| 214 | return fmt.Errorf("wrong state after deleting foo: %s", diff) |
| 215 | } |
| 216 | |
| 217 | r.expected["bar"] = makeTestObject("bar", 1) |
| 218 | if err := reconcile(ctx, r, rname); err != nil { |
| 219 | return fmt.Errorf("reconcile: %v", err) |
| 220 | } |
| 221 | // bar should be updated |
| 222 | if diff := r.currentDiff(makeTestObject("bar", 1), makeTestObject("baz", 0)); diff != "" { |
| 223 | return fmt.Errorf("wrong state after deleting foo: %s", diff) |
| 224 | } |
| 225 | |
| 226 | return nil |
| 227 | }) |
| 228 | } |
| 229 | |
| 230 | func TestIsImmutableError(t *testing.T) { |
| 231 | gk := schema.GroupKind{Group: "someGroup", Kind: "someKind"} |
| 232 | cases := []struct { |
| 233 | err error |
| 234 | isImmutable bool |
| 235 | }{ |
| 236 | {fmt.Errorf("something wrong"), false}, |
| 237 | {apierrors.NewApplyConflict(nil, "conflict"), false}, |
| 238 | {apierrors.NewInvalid(gk, "name", field.ErrorList{}), false}, |
| 239 | {apierrors.NewInvalid(gk, "name", field.ErrorList{ |
| 240 | field.Invalid(field.NewPath("field1"), true, apivalidation.FieldImmutableErrorMsg), |
| 241 | field.Invalid(field.NewPath("field2"), true, "some other error"), |
| 242 | }), false}, |
| 243 | {apierrors.NewInvalid(gk, "name", field.ErrorList{ |
| 244 | field.Invalid(field.NewPath("field1"), true, apivalidation.FieldImmutableErrorMsg), |
| 245 | }), true}, |
| 246 | {apierrors.NewInvalid(gk, "name", field.ErrorList{ |
| 247 | field.Invalid(field.NewPath("field1"), true, apivalidation.FieldImmutableErrorMsg), |
| 248 | field.Invalid(field.NewPath("field2"), true, apivalidation.FieldImmutableErrorMsg), |
| 249 | }), true}, |
| Serge Bazanski | e6030f6 | 2020-06-03 17:52:59 +0200 | [diff] [blame] | 250 | } |
| Jan Schär | 69f5f4e | 2024-05-15 10:32:07 +0200 | [diff] [blame] | 251 | for _, c := range cases { |
| 252 | actual := isImmutableError(c.err) |
| 253 | if actual != c.isImmutable { |
| 254 | t.Errorf("Expected %v, got %v for error: %v", c.isImmutable, actual, c.err) |
| 255 | } |
| Serge Bazanski | e6030f6 | 2020-06-03 17:52:59 +0200 | [diff] [blame] | 256 | } |
| 257 | } |