|
| 1 | +package controllers |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/stretchr/testify/assert" |
| 8 | + "github.com/stretchr/testify/require" |
| 9 | + core "k8s.io/api/core/v1" |
| 10 | + meta "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 11 | + "k8s.io/apimachinery/pkg/runtime" |
| 12 | + ctrl "sigs.k8s.io/controller-runtime" |
| 13 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 14 | + "sigs.k8s.io/controller-runtime/pkg/client/fake" |
| 15 | + |
| 16 | + "github.com/openshift/windows-machine-config-operator/pkg/cluster" |
| 17 | + "github.com/openshift/windows-machine-config-operator/pkg/secrets" |
| 18 | +) |
| 19 | + |
| 20 | +// newTestSecretReconciler returns a SecretReconciler backed by a fake client pre-seeded with initObjs. |
| 21 | +func newTestSecretReconciler(initObjs ...client.Object) *SecretReconciler { |
| 22 | + scheme := runtime.NewScheme() |
| 23 | + _ = core.AddToScheme(scheme) |
| 24 | + |
| 25 | + fc := fake.NewClientBuilder().WithScheme(scheme).WithObjects(initObjs...).Build() |
| 26 | + return &SecretReconciler{ |
| 27 | + instanceReconciler: instanceReconciler{ |
| 28 | + client: fc, |
| 29 | + log: ctrl.Log.WithName("test"), |
| 30 | + }, |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +// newUserDataSecret creates a minimal windows-user-data Secret in the given namespace. |
| 35 | +func newUserDataSecret(namespace, userData string) *core.Secret { |
| 36 | + return &core.Secret{ |
| 37 | + ObjectMeta: meta.ObjectMeta{ |
| 38 | + Name: secrets.UserDataSecret, |
| 39 | + Namespace: namespace, |
| 40 | + }, |
| 41 | + Data: map[string][]byte{ |
| 42 | + "userData": []byte(userData), |
| 43 | + }, |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +// TestIsUserDataSecret verifies that isUserDataSecret matches secrets in both the Machine API and |
| 48 | +// Cluster API namespaces, but not in unrelated namespaces. |
| 49 | +func TestIsUserDataSecret(t *testing.T) { |
| 50 | + tests := []struct { |
| 51 | + name string |
| 52 | + ns string |
| 53 | + expected bool |
| 54 | + }{ |
| 55 | + { |
| 56 | + name: "Machine API namespace matches", |
| 57 | + ns: cluster.MachineAPINamespace, |
| 58 | + expected: true, |
| 59 | + }, |
| 60 | + { |
| 61 | + name: "Cluster API namespace matches", |
| 62 | + ns: cluster.ClusterAPINamespace, |
| 63 | + expected: true, |
| 64 | + }, |
| 65 | + { |
| 66 | + name: "Unrelated namespace does not match", |
| 67 | + ns: "kube-system", |
| 68 | + expected: false, |
| 69 | + }, |
| 70 | + } |
| 71 | + for _, tt := range tests { |
| 72 | + t.Run(tt.name, func(t *testing.T) { |
| 73 | + s := newUserDataSecret(tt.ns, "data") |
| 74 | + assert.Equal(t, tt.expected, isUserDataSecret(s)) |
| 75 | + }) |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +// TestIsClusterAPIEnabled verifies that isClusterAPIEnabled returns true only when the |
| 80 | +// openshift-cluster-api namespace exists. |
| 81 | +func TestIsClusterAPIEnabled(t *testing.T) { |
| 82 | + ctx := context.Background() |
| 83 | + |
| 84 | + t.Run("Namespace absent returns false", func(t *testing.T) { |
| 85 | + r := newTestSecretReconciler() |
| 86 | + enabled, err := r.isClusterAPIEnabled(ctx) |
| 87 | + require.NoError(t, err) |
| 88 | + assert.False(t, enabled) |
| 89 | + }) |
| 90 | + |
| 91 | + t.Run("Namespace present returns true", func(t *testing.T) { |
| 92 | + ns := &core.Namespace{ObjectMeta: meta.ObjectMeta{Name: cluster.ClusterAPINamespace}} |
| 93 | + r := newTestSecretReconciler(ns) |
| 94 | + enabled, err := r.isClusterAPIEnabled(ctx) |
| 95 | + require.NoError(t, err) |
| 96 | + assert.True(t, enabled) |
| 97 | + }) |
| 98 | +} |
| 99 | + |
| 100 | +// TestEnsureCAPIUserDataSecret verifies the create / no-op / update behaviour of ensureCAPIUserDataSecret. |
| 101 | +func TestEnsureCAPIUserDataSecret(t *testing.T) { |
| 102 | + ctx := context.Background() |
| 103 | + const content = "<powershell>ssh-rsa AAAA...</powershell>" |
| 104 | + |
| 105 | + t.Run("Creates secret when absent in CAPI namespace", func(t *testing.T) { |
| 106 | + r := newTestSecretReconciler() |
| 107 | + src := newUserDataSecret(cluster.MachineAPINamespace, content) |
| 108 | + |
| 109 | + require.NoError(t, r.ensureCAPIUserDataSecret(ctx, src)) |
| 110 | + |
| 111 | + got := &core.Secret{} |
| 112 | + require.NoError(t, r.client.Get(ctx, |
| 113 | + client.ObjectKey{Name: secrets.UserDataSecret, Namespace: cluster.ClusterAPINamespace}, got)) |
| 114 | + assert.Equal(t, content, string(got.Data["userData"])) |
| 115 | + assert.Equal(t, cluster.ClusterAPINamespace, got.Namespace) |
| 116 | + }) |
| 117 | + |
| 118 | + t.Run("No-op when CAPI secret already matches", func(t *testing.T) { |
| 119 | + existing := newUserDataSecret(cluster.ClusterAPINamespace, content) |
| 120 | + existing.ResourceVersion = "42" |
| 121 | + r := newTestSecretReconciler(existing) |
| 122 | + src := newUserDataSecret(cluster.MachineAPINamespace, content) |
| 123 | + |
| 124 | + require.NoError(t, r.ensureCAPIUserDataSecret(ctx, src)) |
| 125 | + |
| 126 | + got := &core.Secret{} |
| 127 | + require.NoError(t, r.client.Get(ctx, |
| 128 | + client.ObjectKey{Name: secrets.UserDataSecret, Namespace: cluster.ClusterAPINamespace}, got)) |
| 129 | + // ResourceVersion must be unchanged (no update was issued). |
| 130 | + assert.Equal(t, "42", got.ResourceVersion) |
| 131 | + }) |
| 132 | + |
| 133 | + t.Run("Updates CAPI secret when content differs", func(t *testing.T) { |
| 134 | + existing := newUserDataSecret(cluster.ClusterAPINamespace, "old-data") |
| 135 | + existing.ResourceVersion = "1" |
| 136 | + r := newTestSecretReconciler(existing) |
| 137 | + src := newUserDataSecret(cluster.MachineAPINamespace, content) |
| 138 | + |
| 139 | + require.NoError(t, r.ensureCAPIUserDataSecret(ctx, src)) |
| 140 | + |
| 141 | + got := &core.Secret{} |
| 142 | + require.NoError(t, r.client.Get(ctx, |
| 143 | + client.ObjectKey{Name: secrets.UserDataSecret, Namespace: cluster.ClusterAPINamespace}, got)) |
| 144 | + assert.Equal(t, content, string(got.Data["userData"])) |
| 145 | + }) |
| 146 | +} |
0 commit comments