|
| 1 | +package internal |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/kubeslice/kubeslice-cli/util" |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | + "github.com/stretchr/testify/require" |
| 12 | +) |
| 13 | + |
| 14 | +func TestCreateServiceExportConfig(t *testing.T) { |
| 15 | + cluster := &Cluster{ |
| 16 | + Name: "controller", |
| 17 | + ContextName: "controller-ctx", |
| 18 | + KubeConfigPath: "/path/config", |
| 19 | + } |
| 20 | + |
| 21 | + tests := []struct { |
| 22 | + name string |
| 23 | + namespace string |
| 24 | + filename string |
| 25 | + mockApplyFile func(string, string, *Cluster) |
| 26 | + expectFatal bool |
| 27 | + fatalContains string |
| 28 | + }{ |
| 29 | + { |
| 30 | + name: "create service export config", |
| 31 | + namespace: "test-namespace", |
| 32 | + filename: "service-export.yaml", |
| 33 | + mockApplyFile: func(fileName, namespace string, c *Cluster) { |
| 34 | + assert.Equal(t, "service-export.yaml", fileName) |
| 35 | + assert.Equal(t, "test-namespace", namespace) |
| 36 | + assert.Equal(t, cluster, c) |
| 37 | + }, |
| 38 | + }, |
| 39 | + { |
| 40 | + name: "create with different namespace", |
| 41 | + namespace: "custom-namespace", |
| 42 | + filename: "custom-export.yaml", |
| 43 | + mockApplyFile: func(fileName, namespace string, c *Cluster) { |
| 44 | + assert.Equal(t, "custom-export.yaml", fileName) |
| 45 | + assert.Equal(t, "custom-namespace", namespace) |
| 46 | + }, |
| 47 | + }, |
| 48 | + { |
| 49 | + name: "apply file fails", |
| 50 | + namespace: "test-namespace", |
| 51 | + filename: "service-export.yaml", |
| 52 | + mockApplyFile: func(fileName, namespace string, c *Cluster) { |
| 53 | + util.Fatalf("Process failed %v", errors.New("apply failed")) |
| 54 | + }, |
| 55 | + expectFatal: true, |
| 56 | + fatalContains: "apply failed", |
| 57 | + }, |
| 58 | + } |
| 59 | + |
| 60 | + for _, tt := range tests { |
| 61 | + t.Run(tt.name, func(t *testing.T) { |
| 62 | + cleanup := util.NewTestEnvironment() |
| 63 | + defer cleanup() |
| 64 | + |
| 65 | + fakeOutput := util.Output.(*util.FakeOutput) |
| 66 | + |
| 67 | + applyCalled := false |
| 68 | + originalApply := applyFileFuncServiceExport |
| 69 | + applyFileFuncServiceExport = func(fileName, namespace string, cluster *Cluster) { |
| 70 | + applyCalled = true |
| 71 | + if tt.mockApplyFile != nil { |
| 72 | + tt.mockApplyFile(fileName, namespace, cluster) |
| 73 | + } |
| 74 | + } |
| 75 | + defer func() { applyFileFuncServiceExport = originalApply }() |
| 76 | + |
| 77 | + CreateServiceExportConfig(tt.namespace, cluster, tt.filename) |
| 78 | + |
| 79 | + assert.True(t, applyCalled, "ApplyFile should be called") |
| 80 | + |
| 81 | + if tt.expectFatal { |
| 82 | + require.NotEmpty(t, fakeOutput.FatalCalls) |
| 83 | + assert.Contains(t, fmt.Sprint(fakeOutput.FatalCalls[0]), tt.fatalContains) |
| 84 | + } else { |
| 85 | + assert.Empty(t, fakeOutput.FatalCalls) |
| 86 | + } |
| 87 | + }) |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +func TestGetServiceExportConfig(t *testing.T) { |
| 92 | + cluster := &Cluster{Name: "controller", ContextName: "ctrl-ctx"} |
| 93 | + |
| 94 | + tests := []struct { |
| 95 | + name string |
| 96 | + mockGet func(string, string, string, *Cluster, string) |
| 97 | + expectFatal bool |
| 98 | + fatalContains string |
| 99 | + }{ |
| 100 | + { |
| 101 | + name: "successful get", |
| 102 | + mockGet: func(resourceType, resourceName, namespace string, c *Cluster, outputFormat string) { |
| 103 | + assert.Equal(t, ServiceExportConfigObject, resourceType) |
| 104 | + assert.Equal(t, "my-export", resourceName) |
| 105 | + assert.Equal(t, "test-namespace", namespace) |
| 106 | + assert.Equal(t, cluster, c) |
| 107 | + assert.Equal(t, "", outputFormat) |
| 108 | + }, |
| 109 | + }, |
| 110 | + { |
| 111 | + name: "get fails", |
| 112 | + mockGet: func(resourceType, resourceName, namespace string, c *Cluster, outputFormat string) { |
| 113 | + util.Fatalf("Process failed %v", errors.New("get failed")) |
| 114 | + }, |
| 115 | + expectFatal: true, |
| 116 | + fatalContains: "get failed", |
| 117 | + }, |
| 118 | + } |
| 119 | + |
| 120 | + for _, tt := range tests { |
| 121 | + t.Run(tt.name, func(t *testing.T) { |
| 122 | + cleanup := util.NewTestEnvironment() |
| 123 | + defer cleanup() |
| 124 | + |
| 125 | + fakeClock := util.SystemClock.(*util.FakeClock) |
| 126 | + fakeOutput := util.Output.(*util.FakeOutput) |
| 127 | + |
| 128 | + originalGet := getKubectlResourcesFuncServiceExport |
| 129 | + getKubectlResourcesFuncServiceExport = tt.mockGet |
| 130 | + defer func() { getKubectlResourcesFuncServiceExport = originalGet }() |
| 131 | + |
| 132 | + GetServiceExportConfig("my-export", "test-namespace", cluster) |
| 133 | + |
| 134 | + if tt.expectFatal { |
| 135 | + require.NotEmpty(t, fakeOutput.FatalCalls) |
| 136 | + assert.Contains(t, fmt.Sprint(fakeOutput.FatalCalls[0]), tt.fatalContains) |
| 137 | + } else { |
| 138 | + assert.Empty(t, fakeOutput.FatalCalls) |
| 139 | + require.Len(t, fakeClock.SleepCalls, 1) |
| 140 | + assert.Equal(t, 200*time.Millisecond, fakeClock.SleepCalls[0]) |
| 141 | + } |
| 142 | + }) |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +func TestDeleteServiceExportConfig(t *testing.T) { |
| 147 | + cluster := &Cluster{Name: "controller"} |
| 148 | + |
| 149 | + tests := []struct { |
| 150 | + name string |
| 151 | + mockDelete func(string, string, string, *Cluster) |
| 152 | + expectFatal bool |
| 153 | + fatalContains string |
| 154 | + }{ |
| 155 | + { |
| 156 | + name: "successful delete", |
| 157 | + mockDelete: func(resourceType, resourceName, namespace string, c *Cluster) { |
| 158 | + assert.Equal(t, ServiceExportConfigObject, resourceType) |
| 159 | + assert.Equal(t, "my-export", resourceName) |
| 160 | + assert.Equal(t, "test-namespace", namespace) |
| 161 | + }, |
| 162 | + }, |
| 163 | + { |
| 164 | + name: "delete fails", |
| 165 | + mockDelete: func(resourceType, resourceName, namespace string, c *Cluster) { |
| 166 | + util.Fatalf("Process failed %v", errors.New("delete failed")) |
| 167 | + }, |
| 168 | + expectFatal: true, |
| 169 | + fatalContains: "delete failed", |
| 170 | + }, |
| 171 | + } |
| 172 | + |
| 173 | + for _, tt := range tests { |
| 174 | + t.Run(tt.name, func(t *testing.T) { |
| 175 | + cleanup := util.NewTestEnvironment() |
| 176 | + defer cleanup() |
| 177 | + |
| 178 | + fakeClock := util.SystemClock.(*util.FakeClock) |
| 179 | + fakeOutput := util.Output.(*util.FakeOutput) |
| 180 | + |
| 181 | + originalDelete := deleteKubectlResourcesFuncServiceExport |
| 182 | + deleteKubectlResourcesFuncServiceExport = tt.mockDelete |
| 183 | + defer func() { deleteKubectlResourcesFuncServiceExport = originalDelete }() |
| 184 | + |
| 185 | + DeleteServiceExportConfig("my-export", "test-namespace", cluster) |
| 186 | + |
| 187 | + if tt.expectFatal { |
| 188 | + require.NotEmpty(t, fakeOutput.FatalCalls) |
| 189 | + assert.Contains(t, fmt.Sprint(fakeOutput.FatalCalls[0]), tt.fatalContains) |
| 190 | + } else { |
| 191 | + assert.Empty(t, fakeOutput.FatalCalls) |
| 192 | + require.Len(t, fakeClock.SleepCalls, 1) |
| 193 | + assert.Equal(t, 200*time.Millisecond, fakeClock.SleepCalls[0]) |
| 194 | + } |
| 195 | + }) |
| 196 | + } |
| 197 | +} |
| 198 | + |
| 199 | +func TestEditServiceExportConfig(t *testing.T) { |
| 200 | + cluster := &Cluster{Name: "controller"} |
| 201 | + |
| 202 | + tests := []struct { |
| 203 | + name string |
| 204 | + mockEdit func(string, string, string, *Cluster) |
| 205 | + expectFatal bool |
| 206 | + fatalContains string |
| 207 | + }{ |
| 208 | + { |
| 209 | + name: "successful edit", |
| 210 | + mockEdit: func(resourceType, resourceName, namespace string, c *Cluster) { |
| 211 | + assert.Equal(t, ServiceExportConfigObject, resourceType) |
| 212 | + assert.Equal(t, "my-export", resourceName) |
| 213 | + assert.Equal(t, "test-namespace", namespace) |
| 214 | + }, |
| 215 | + }, |
| 216 | + { |
| 217 | + name: "edit fails", |
| 218 | + mockEdit: func(resourceType, resourceName, namespace string, c *Cluster) { |
| 219 | + util.Fatalf("Process failed %v", errors.New("edit failed")) |
| 220 | + }, |
| 221 | + expectFatal: true, |
| 222 | + fatalContains: "edit failed", |
| 223 | + }, |
| 224 | + } |
| 225 | + |
| 226 | + for _, tt := range tests { |
| 227 | + t.Run(tt.name, func(t *testing.T) { |
| 228 | + cleanup := util.NewTestEnvironment() |
| 229 | + defer cleanup() |
| 230 | + |
| 231 | + fakeClock := util.SystemClock.(*util.FakeClock) |
| 232 | + fakeOutput := util.Output.(*util.FakeOutput) |
| 233 | + |
| 234 | + originalEdit := editKubectlResourcesFuncServiceExport |
| 235 | + editKubectlResourcesFuncServiceExport = tt.mockEdit |
| 236 | + defer func() { editKubectlResourcesFuncServiceExport = originalEdit }() |
| 237 | + |
| 238 | + EditServiceExportConfig("my-export", "test-namespace", cluster) |
| 239 | + |
| 240 | + if tt.expectFatal { |
| 241 | + require.NotEmpty(t, fakeOutput.FatalCalls) |
| 242 | + assert.Contains(t, fmt.Sprint(fakeOutput.FatalCalls[0]), tt.fatalContains) |
| 243 | + } else { |
| 244 | + assert.Empty(t, fakeOutput.FatalCalls) |
| 245 | + require.Len(t, fakeClock.SleepCalls, 1) |
| 246 | + assert.Equal(t, 200*time.Millisecond, fakeClock.SleepCalls[0]) |
| 247 | + } |
| 248 | + }) |
| 249 | + } |
| 250 | +} |
| 251 | + |
| 252 | +func TestDescribeServiceExportConfig(t *testing.T) { |
| 253 | + cluster := &Cluster{Name: "controller"} |
| 254 | + |
| 255 | + tests := []struct { |
| 256 | + name string |
| 257 | + mockDescribe func(string, string, string, *Cluster) |
| 258 | + expectFatal bool |
| 259 | + fatalContains string |
| 260 | + }{ |
| 261 | + { |
| 262 | + name: "successful describe", |
| 263 | + mockDescribe: func(resourceType, resourceName, namespace string, c *Cluster) { |
| 264 | + assert.Equal(t, ServiceExportConfigObject, resourceType) |
| 265 | + assert.Equal(t, "my-export", resourceName) |
| 266 | + assert.Equal(t, "test-namespace", namespace) |
| 267 | + }, |
| 268 | + }, |
| 269 | + { |
| 270 | + name: "describe fails", |
| 271 | + mockDescribe: func(resourceType, resourceName, namespace string, c *Cluster) { |
| 272 | + util.Fatalf("Process failed %v", errors.New("describe failed")) |
| 273 | + }, |
| 274 | + expectFatal: true, |
| 275 | + fatalContains: "describe failed", |
| 276 | + }, |
| 277 | + } |
| 278 | + |
| 279 | + for _, tt := range tests { |
| 280 | + t.Run(tt.name, func(t *testing.T) { |
| 281 | + cleanup := util.NewTestEnvironment() |
| 282 | + defer cleanup() |
| 283 | + |
| 284 | + fakeClock := util.SystemClock.(*util.FakeClock) |
| 285 | + fakeOutput := util.Output.(*util.FakeOutput) |
| 286 | + |
| 287 | + originalDescribe := describeKubectlResourcesFuncServiceExport |
| 288 | + describeKubectlResourcesFuncServiceExport = tt.mockDescribe |
| 289 | + defer func() { describeKubectlResourcesFuncServiceExport = originalDescribe }() |
| 290 | + |
| 291 | + DescribeServiceExportConfig("my-export", "test-namespace", cluster) |
| 292 | + |
| 293 | + if tt.expectFatal { |
| 294 | + require.NotEmpty(t, fakeOutput.FatalCalls) |
| 295 | + assert.Contains(t, fmt.Sprint(fakeOutput.FatalCalls[0]), tt.fatalContains) |
| 296 | + } else { |
| 297 | + assert.Empty(t, fakeOutput.FatalCalls) |
| 298 | + require.Len(t, fakeClock.SleepCalls, 1) |
| 299 | + assert.Equal(t, 200*time.Millisecond, fakeClock.SleepCalls[0]) |
| 300 | + } |
| 301 | + }) |
| 302 | + } |
| 303 | +} |
0 commit comments