|
| 1 | +package migrateokdfeatureset |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + configv1 "github.com/openshift/api/config/v1" |
| 8 | + configv1fake "github.com/openshift/client-go/config/clientset/versioned/fake" |
| 9 | + "github.com/openshift/cluster-config-operator/pkg/version" |
| 10 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 11 | + "k8s.io/apimachinery/pkg/types" |
| 12 | + kubetesting "k8s.io/client-go/testing" |
| 13 | +) |
| 14 | + |
| 15 | +func TestOKDFeatureSetMigrationController_syncFeatureGate(t *testing.T) { |
| 16 | + tests := []struct { |
| 17 | + name string |
| 18 | + featureGate *configv1.FeatureGate |
| 19 | + |
| 20 | + changeVerifier func(t *testing.T, actions []kubetesting.Action) |
| 21 | + }{ |
| 22 | + { |
| 23 | + name: "migrate-empty-featureset-to-okd", |
| 24 | + featureGate: &configv1.FeatureGate{ |
| 25 | + ObjectMeta: metav1.ObjectMeta{Name: "cluster"}, |
| 26 | + Spec: configv1.FeatureGateSpec{ |
| 27 | + FeatureGateSelection: configv1.FeatureGateSelection{ |
| 28 | + FeatureSet: "", |
| 29 | + }, |
| 30 | + }, |
| 31 | + }, |
| 32 | + changeVerifier: func(t *testing.T, actions []kubetesting.Action) { |
| 33 | + if len(actions) != 1 { |
| 34 | + t.Fatalf("expected 1 action, got %d: %v", len(actions), actions) |
| 35 | + } |
| 36 | + patchAction := actions[0].(kubetesting.PatchAction) |
| 37 | + if patchAction.GetPatchType() != types.ApplyPatchType { |
| 38 | + t.Fatalf("unexpected patch type: %v", patchAction.GetPatchType()) |
| 39 | + } |
| 40 | + applied := string(patchAction.GetPatch()) |
| 41 | + expectedApplied := `{"kind":"FeatureGate","apiVersion":"config.openshift.io/v1","metadata":{"name":"cluster"},"spec":{"featureSet":"OKD"}}` |
| 42 | + if applied != expectedApplied { |
| 43 | + t.Fatalf("unexpected patch:\ngot: %s\nwant: %s", applied, expectedApplied) |
| 44 | + } |
| 45 | + }, |
| 46 | + }, |
| 47 | + { |
| 48 | + name: "migrate-default-featureset-to-okd", |
| 49 | + featureGate: &configv1.FeatureGate{ |
| 50 | + ObjectMeta: metav1.ObjectMeta{Name: "cluster"}, |
| 51 | + Spec: configv1.FeatureGateSpec{ |
| 52 | + FeatureGateSelection: configv1.FeatureGateSelection{ |
| 53 | + FeatureSet: configv1.Default, |
| 54 | + }, |
| 55 | + }, |
| 56 | + }, |
| 57 | + changeVerifier: func(t *testing.T, actions []kubetesting.Action) { |
| 58 | + if len(actions) != 1 { |
| 59 | + t.Fatalf("expected 1 action, got %d: %v", len(actions), actions) |
| 60 | + } |
| 61 | + patchAction := actions[0].(kubetesting.PatchAction) |
| 62 | + if patchAction.GetPatchType() != types.ApplyPatchType { |
| 63 | + t.Fatalf("unexpected patch type: %v", patchAction.GetPatchType()) |
| 64 | + } |
| 65 | + applied := string(patchAction.GetPatch()) |
| 66 | + expectedApplied := `{"kind":"FeatureGate","apiVersion":"config.openshift.io/v1","metadata":{"name":"cluster"},"spec":{"featureSet":"OKD"}}` |
| 67 | + if applied != expectedApplied { |
| 68 | + t.Fatalf("unexpected patch:\ngot: %s\nwant: %s", applied, expectedApplied) |
| 69 | + } |
| 70 | + }, |
| 71 | + }, |
| 72 | + { |
| 73 | + name: "leave-techpreview-unchanged", |
| 74 | + featureGate: &configv1.FeatureGate{ |
| 75 | + ObjectMeta: metav1.ObjectMeta{Name: "cluster"}, |
| 76 | + Spec: configv1.FeatureGateSpec{ |
| 77 | + FeatureGateSelection: configv1.FeatureGateSelection{ |
| 78 | + FeatureSet: configv1.TechPreviewNoUpgrade, |
| 79 | + }, |
| 80 | + }, |
| 81 | + }, |
| 82 | + changeVerifier: func(t *testing.T, actions []kubetesting.Action) { |
| 83 | + if len(actions) != 0 { |
| 84 | + t.Fatalf("expected no actions, got %d: %v", len(actions), actions) |
| 85 | + } |
| 86 | + }, |
| 87 | + }, |
| 88 | + { |
| 89 | + name: "leave-okd-unchanged", |
| 90 | + featureGate: &configv1.FeatureGate{ |
| 91 | + ObjectMeta: metav1.ObjectMeta{Name: "cluster"}, |
| 92 | + Spec: configv1.FeatureGateSpec{ |
| 93 | + FeatureGateSelection: configv1.FeatureGateSelection{ |
| 94 | + FeatureSet: configv1.OKD, |
| 95 | + }, |
| 96 | + }, |
| 97 | + }, |
| 98 | + changeVerifier: func(t *testing.T, actions []kubetesting.Action) { |
| 99 | + if len(actions) != 0 { |
| 100 | + t.Fatalf("expected no actions, got %d: %v", len(actions), actions) |
| 101 | + } |
| 102 | + }, |
| 103 | + }, |
| 104 | + { |
| 105 | + name: "leave-custompoupgrade-unchanged", |
| 106 | + featureGate: &configv1.FeatureGate{ |
| 107 | + ObjectMeta: metav1.ObjectMeta{Name: "cluster"}, |
| 108 | + Spec: configv1.FeatureGateSpec{ |
| 109 | + FeatureGateSelection: configv1.FeatureGateSelection{ |
| 110 | + FeatureSet: configv1.CustomNoUpgrade, |
| 111 | + }, |
| 112 | + }, |
| 113 | + }, |
| 114 | + changeVerifier: func(t *testing.T, actions []kubetesting.Action) { |
| 115 | + if len(actions) != 0 { |
| 116 | + t.Fatalf("expected no actions, got %d: %v", len(actions), actions) |
| 117 | + } |
| 118 | + }, |
| 119 | + }, |
| 120 | + } |
| 121 | + for _, tt := range tests { |
| 122 | + t.Run(tt.name, func(t *testing.T) { |
| 123 | + // Skip tests if not running in SCOS mode |
| 124 | + if !version.IsSCOS() { |
| 125 | + t.Skipf("Skipping test %s because version.IsSCOS() is false", tt.name) |
| 126 | + } |
| 127 | + |
| 128 | + ctx := context.Background() |
| 129 | + _, cancel := context.WithCancel(ctx) |
| 130 | + defer cancel() |
| 131 | + |
| 132 | + fakeClient := configv1fake.NewSimpleClientset(tt.featureGate) |
| 133 | + |
| 134 | + c := OKDFeatureSetMigrationController{ |
| 135 | + featureGatesClient: fakeClient.ConfigV1(), |
| 136 | + } |
| 137 | + if err := c.syncFeatureGate(ctx, tt.featureGate); err != nil { |
| 138 | + t.Fatal(err) |
| 139 | + } |
| 140 | + |
| 141 | + tt.changeVerifier(t, fakeClient.Actions()) |
| 142 | + }) |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +// Note: Testing the non-SCOS code path (early return in sync) is not necessary |
| 147 | +// as it's a trivial check. The version.IsSCOS() function is tested in the version package. |
| 148 | +// All meaningful functionality is tested in the syncFeatureGate tests above. |
0 commit comments