|
| 1 | +package kubernetes |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/UpCloudLtd/upcloud-cli/v2/internal/commands" |
| 7 | + "github.com/UpCloudLtd/upcloud-cli/v2/internal/config" |
| 8 | + smock "github.com/UpCloudLtd/upcloud-cli/v2/internal/mock" |
| 9 | + "github.com/UpCloudLtd/upcloud-cli/v2/internal/mockexecute" |
| 10 | + |
| 11 | + "github.com/UpCloudLtd/upcloud-go-api/v6/upcloud" |
| 12 | + "github.com/UpCloudLtd/upcloud-go-api/v6/upcloud/request" |
| 13 | + "github.com/stretchr/testify/assert" |
| 14 | + "github.com/stretchr/testify/require" |
| 15 | +) |
| 16 | + |
| 17 | +func TestModifyKubernetesCluster(t *testing.T) { |
| 18 | + clusterUUID := "28c80353-98fd-4221-85e0-82d7603756ba" |
| 19 | + |
| 20 | + for _, test := range []struct { |
| 21 | + name string |
| 22 | + args []string |
| 23 | + expected request.ModifyKubernetesClusterRequest |
| 24 | + errorMsg string |
| 25 | + }{ |
| 26 | + { |
| 27 | + name: "no args", |
| 28 | + args: []string{clusterUUID}, |
| 29 | + expected: request.ModifyKubernetesClusterRequest{ClusterUUID: clusterUUID, Cluster: request.ModifyKubernetesCluster{ControlPlaneIPFilter: []string{}}}, |
| 30 | + }, |
| 31 | + { |
| 32 | + name: "one IP", |
| 33 | + args: []string{ |
| 34 | + clusterUUID, |
| 35 | + "--kubernetes-api-allow-ip", "10.144.1.100", |
| 36 | + }, |
| 37 | + expected: request.ModifyKubernetesClusterRequest{ |
| 38 | + ClusterUUID: clusterUUID, |
| 39 | + Cluster: request.ModifyKubernetesCluster{ControlPlaneIPFilter: []string{"10.144.1.100"}}, |
| 40 | + }, |
| 41 | + }, |
| 42 | + { |
| 43 | + name: "IP and CIDR", |
| 44 | + args: []string{ |
| 45 | + clusterUUID, |
| 46 | + "--kubernetes-api-allow-ip", "10.144.1.100", |
| 47 | + "--kubernetes-api-allow-ip", "10.144.2.0/24", |
| 48 | + }, |
| 49 | + expected: request.ModifyKubernetesClusterRequest{ |
| 50 | + ClusterUUID: clusterUUID, |
| 51 | + Cluster: request.ModifyKubernetesCluster{ControlPlaneIPFilter: []string{"10.144.1.100", "10.144.2.0/24"}}, |
| 52 | + }, |
| 53 | + }, |
| 54 | + } { |
| 55 | + t.Run(test.name, func(t *testing.T) { |
| 56 | + conf := config.New() |
| 57 | + testCmd := ModifyCommand() |
| 58 | + mService := new(smock.Service) |
| 59 | + |
| 60 | + mService.On("ModifyKubernetesCluster", &test.expected).Return(&upcloud.KubernetesCluster{}, nil) |
| 61 | + |
| 62 | + c := commands.BuildCommand(testCmd, nil, conf) |
| 63 | + |
| 64 | + c.Cobra().SetArgs(test.args) |
| 65 | + _, err := mockexecute.MockExecute(c, mService, conf) |
| 66 | + |
| 67 | + if test.errorMsg != "" { |
| 68 | + assert.EqualError(t, err, test.errorMsg) |
| 69 | + } else { |
| 70 | + require.NoError(t, err) |
| 71 | + mService.AssertNumberOfCalls(t, "ModifyKubernetesCluster", 1) |
| 72 | + } |
| 73 | + }) |
| 74 | + } |
| 75 | +} |
0 commit comments