|
| 1 | +package dsc |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/go-logr/logr" |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | + corev1 "k8s.io/api/core/v1" |
| 12 | + "k8s.io/apimachinery/pkg/api/errors" |
| 13 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 14 | + "k8s.io/apimachinery/pkg/runtime" |
| 15 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 16 | + "sigs.k8s.io/controller-runtime/pkg/client/fake" |
| 17 | +) |
| 18 | + |
| 19 | +func TestDSCConfigReader_ReadDSCConfig(t *testing.T) { |
| 20 | + tests := []struct { |
| 21 | + name string |
| 22 | + namespace string |
| 23 | + configMapData map[string]string |
| 24 | + expectError bool |
| 25 | + expectedOnline bool |
| 26 | + expectedCode bool |
| 27 | + }{ |
| 28 | + { |
| 29 | + name: "ConfigMap not found - should use defaults", |
| 30 | + namespace: "test-namespace", |
| 31 | + configMapData: nil, |
| 32 | + expectError: false, |
| 33 | + expectedOnline: false, // Default value |
| 34 | + expectedCode: false, // Default value |
| 35 | + }, |
| 36 | + { |
| 37 | + name: "Valid configuration with both settings enabled", |
| 38 | + namespace: "test-namespace", |
| 39 | + configMapData: map[string]string{ |
| 40 | + DSCPermitOnlineKey: "true", |
| 41 | + DSCPermitCodeExecutionKey: "true", |
| 42 | + }, |
| 43 | + expectError: false, |
| 44 | + expectedOnline: true, |
| 45 | + expectedCode: true, |
| 46 | + }, |
| 47 | + { |
| 48 | + name: "Valid configuration with both settings disabled", |
| 49 | + namespace: "test-namespace", |
| 50 | + configMapData: map[string]string{ |
| 51 | + DSCPermitOnlineKey: "false", |
| 52 | + DSCPermitCodeExecutionKey: "false", |
| 53 | + }, |
| 54 | + expectError: false, |
| 55 | + expectedOnline: false, |
| 56 | + expectedCode: false, |
| 57 | + }, |
| 58 | + { |
| 59 | + name: "Partial configuration - only permitOnline", |
| 60 | + namespace: "test-namespace", |
| 61 | + configMapData: map[string]string{ |
| 62 | + DSCPermitOnlineKey: "true", |
| 63 | + }, |
| 64 | + expectError: false, |
| 65 | + expectedOnline: true, |
| 66 | + expectedCode: false, // Should remain default |
| 67 | + }, |
| 68 | + { |
| 69 | + name: "Invalid boolean values - should use defaults", |
| 70 | + namespace: "test-namespace", |
| 71 | + configMapData: map[string]string{ |
| 72 | + DSCPermitOnlineKey: "invalid", |
| 73 | + DSCPermitCodeExecutionKey: "also-invalid", |
| 74 | + }, |
| 75 | + expectError: false, // Should not fail, just log error |
| 76 | + expectedOnline: false, // Should remain default |
| 77 | + expectedCode: false, // Should remain default |
| 78 | + }, |
| 79 | + { |
| 80 | + name: "Empty namespace - should skip reading", |
| 81 | + namespace: "", |
| 82 | + configMapData: nil, |
| 83 | + expectError: false, |
| 84 | + expectedOnline: false, // Should remain default |
| 85 | + expectedCode: false, // Should remain default |
| 86 | + }, |
| 87 | + } |
| 88 | + |
| 89 | + for _, tt := range tests { |
| 90 | + t.Run(tt.name, func(t *testing.T) { |
| 91 | + // Create fake client |
| 92 | + var objects []client.Object |
| 93 | + if tt.configMapData != nil { |
| 94 | + configMap := &corev1.ConfigMap{ |
| 95 | + ObjectMeta: metav1.ObjectMeta{ |
| 96 | + Name: DSCConfigMapName, |
| 97 | + Namespace: tt.namespace, |
| 98 | + }, |
| 99 | + Data: tt.configMapData, |
| 100 | + } |
| 101 | + objects = append(objects, configMap) |
| 102 | + } |
| 103 | + |
| 104 | + scheme := runtime.NewScheme() |
| 105 | + require.NoError(t, corev1.AddToScheme(scheme)) |
| 106 | + fakeClient := fake.NewClientBuilder().WithScheme(scheme).WithObjects(objects...).Build() |
| 107 | + |
| 108 | + // Create DSCConfigReader |
| 109 | + reader := NewDSCConfigReader(fakeClient, tt.namespace) |
| 110 | + |
| 111 | + // Create a test logger |
| 112 | + log := logr.Discard() |
| 113 | + |
| 114 | + // Test ReadDSCConfig |
| 115 | + dscConfig, err := reader.ReadDSCConfig(context.Background(), &log) |
| 116 | + |
| 117 | + // Assert error expectations |
| 118 | + if tt.expectError { |
| 119 | + assert.Error(t, err) |
| 120 | + } else { |
| 121 | + assert.NoError(t, err) |
| 122 | + } |
| 123 | + |
| 124 | + // Assert configuration values |
| 125 | + assert.Equal(t, tt.expectedOnline, dscConfig.AllowOnline, "AllowOnline should match expected value") |
| 126 | + assert.Equal(t, tt.expectedCode, dscConfig.AllowCodeExecution, "AllowCodeExecution should match expected value") |
| 127 | + }) |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +func TestDSCConfigReader_ReadDSCConfig_ConfigMapNotFound(t *testing.T) { |
| 132 | + // Create fake client without any ConfigMaps |
| 133 | + scheme := runtime.NewScheme() |
| 134 | + require.NoError(t, corev1.AddToScheme(scheme)) |
| 135 | + fakeClient := fake.NewClientBuilder().WithScheme(scheme).Build() |
| 136 | + |
| 137 | + // Create DSCConfigReader |
| 138 | + reader := NewDSCConfigReader(fakeClient, "test-namespace") |
| 139 | + |
| 140 | + // Create a test logger |
| 141 | + log := logr.Discard() |
| 142 | + |
| 143 | + // Test ReadDSCConfig |
| 144 | + dscConfig, err := reader.ReadDSCConfig(context.Background(), &log) |
| 145 | + |
| 146 | + // Should not return error when ConfigMap is not found |
| 147 | + assert.NoError(t, err) |
| 148 | + |
| 149 | + // Values should be at defaults |
| 150 | + assert.False(t, dscConfig.AllowOnline) |
| 151 | + assert.False(t, dscConfig.AllowCodeExecution) |
| 152 | +} |
| 153 | + |
| 154 | +func TestDSCConfigReader_ReadDSCConfig_ClientError(t *testing.T) { |
| 155 | + // Create a mock client that returns an error |
| 156 | + mockClient := &mockClient{shouldError: true} |
| 157 | + |
| 158 | + // Create DSCConfigReader |
| 159 | + reader := NewDSCConfigReader(mockClient, "test-namespace") |
| 160 | + |
| 161 | + // Create a test logger |
| 162 | + log := logr.Discard() |
| 163 | + |
| 164 | + // Test ReadDSCConfig |
| 165 | + _, err := reader.ReadDSCConfig(context.Background(), &log) |
| 166 | + |
| 167 | + // Should return error when client fails |
| 168 | + assert.Error(t, err) |
| 169 | + assert.Contains(t, err.Error(), "error reading DSC ConfigMap") |
| 170 | +} |
| 171 | + |
| 172 | +// mockClient is a simple mock that can simulate client errors |
| 173 | +type mockClient struct { |
| 174 | + client.Client |
| 175 | + shouldError bool |
| 176 | +} |
| 177 | + |
| 178 | +func (m *mockClient) Get(ctx context.Context, key client.ObjectKey, obj client.Object, opts ...client.GetOption) error { |
| 179 | + if m.shouldError { |
| 180 | + return errors.NewInternalError(fmt.Errorf("mock client error")) |
| 181 | + } |
| 182 | + return nil |
| 183 | +} |
| 184 | + |
| 185 | +func TestNewDSCConfigReader(t *testing.T) { |
| 186 | + // Create a fake client |
| 187 | + scheme := runtime.NewScheme() |
| 188 | + require.NoError(t, corev1.AddToScheme(scheme)) |
| 189 | + fakeClient := fake.NewClientBuilder().WithScheme(scheme).Build() |
| 190 | + |
| 191 | + // Test NewDSCConfigReader |
| 192 | + reader := NewDSCConfigReader(fakeClient, "test-namespace") |
| 193 | + |
| 194 | + // Assert reader is properly initialised |
| 195 | + assert.NotNil(t, reader) |
| 196 | + assert.Equal(t, fakeClient, reader.Client) |
| 197 | + assert.Equal(t, "test-namespace", reader.Namespace) |
| 198 | +} |
0 commit comments