Skip to content

Commit

Permalink
test(kubernetes): validate list output type
Browse files Browse the repository at this point in the history
  • Loading branch information
kangasta committed Oct 18, 2023
1 parent 9711470 commit c618de4
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 82 deletions.
30 changes: 30 additions & 0 deletions internal/commands/kubernetes/list_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package kubernetes

import (
"testing"

"github.com/UpCloudLtd/upcloud-cli/v2/internal/commands"
"github.com/UpCloudLtd/upcloud-cli/v2/internal/config"
smock "github.com/UpCloudLtd/upcloud-cli/v2/internal/mock"
"github.com/UpCloudLtd/upcloud-cli/v2/internal/mockexecute"
"github.com/UpCloudLtd/upcloud-cli/v2/internal/testutils"

"github.com/UpCloudLtd/upcloud-go-api/v6/upcloud"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)

func TestList_MarshaledOutput(t *testing.T) {
mService := smock.Service{}
mService.On("GetKubernetesClusters", mock.Anything).Return([]upcloud.KubernetesCluster{testCluster}, nil)

conf := config.New()
conf.Viper().Set(config.KeyOutput, config.ValueOutputJSON)

command := commands.BuildCommand(ListCommand(), nil, conf)

output, err := mockexecute.MockExecute(command, &mService, conf)

assert.Nil(t, err)
testutils.AssertOutputIsList(t, output)
}
165 changes: 83 additions & 82 deletions internal/commands/kubernetes/show_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,97 +15,98 @@ import (
"github.com/stretchr/testify/mock"
)

func TestShowCommand(t *testing.T) {
text.DisableColors()
cluster1 := upcloud.KubernetesCluster{
ControlPlaneIPFilter: []string{"10.144.1.100", "10.144.2.0/24"},
Name: "upcloud-go-sdk-unit-test",
Network: "03a98be3-7daa-443f-bb25-4bc6854b396c",
NetworkCIDR: "172.16.1.0/24",
NodeGroups: []upcloud.KubernetesNodeGroup{
{
Count: 4,
Labels: []upcloud.Label{
{
Key: "managedBy",
Value: "upcloud-go-sdk-unit-test",
},
{
Key: "another",
Value: "label-thing",
},
var testCluster = upcloud.KubernetesCluster{
ControlPlaneIPFilter: []string{"10.144.1.100", "10.144.2.0/24"},
Name: "upcloud-upctl-unit-test",
Network: "03a98be3-7daa-443f-bb25-4bc6854b396c",
NetworkCIDR: "172.16.1.0/24",
NodeGroups: []upcloud.KubernetesNodeGroup{
{
Count: 4,
Labels: []upcloud.Label{
{
Key: "managedBy",
Value: "upcloud-go-sdk-unit-test",
},
{
Key: "another",
Value: "label-thing",
},
},
Name: "upcloud-go-sdk-unit-test",
Plan: "2xCPU-4GB",
State: upcloud.KubernetesNodeGroupStateRunning,
KubeletArgs: []upcloud.KubernetesKubeletArg{
{
Key: "somekubeletkey",
Value: "somekubeletvalue",
},
},
Taints: []upcloud.KubernetesTaint{
{
Effect: "NoExecute",
Key: "sometaintkey",
Value: "sometaintvalue",
},
{
Effect: "NoExecute",
Key: "sometaintkey",
Value: "sometaintvalue",
},
Name: "upcloud-go-sdk-unit-test",
Plan: "2xCPU-4GB",
State: upcloud.KubernetesNodeGroupStateRunning,
KubeletArgs: []upcloud.KubernetesKubeletArg{
{
Key: "somekubeletkey",
Value: "somekubeletvalue",
},
{
Effect: "NoExecute",
Key: "sometaintkey",
Value: "sometaintvalue",
},
Taints: []upcloud.KubernetesTaint{
{
Effect: "NoExecute",
Key: "sometaintkey",
Value: "sometaintvalue",
},
{
Effect: "NoExecute",
Key: "sometaintkey",
Value: "sometaintvalue",
},
{
Effect: "NoExecute",
Key: "sometaintkey",
Value: "sometaintvalue",
},
},
Storage: "storage-uuid",
SSHKeys: []string{"somekey"},
UtilityNetworkAccess: true,
}, {
Count: 8,
Labels: []upcloud.Label{
{
Key: "managedBy",
Value: "upcloud-go-sdk-unit-test-2",
},
Storage: "storage-uuid",
SSHKeys: []string{"somekey"},
UtilityNetworkAccess: true,
}, {
Count: 8,
Labels: []upcloud.Label{
{
Key: "managedBy",
Value: "upcloud-go-sdk-unit-test-2",
},
{
Key: "another2",
Value: "label-thing-2",
},
{
Key: "another2",
Value: "label-thing-2",
},
Name: "upcloud-go-sdk-unit-test-2",
Plan: "4xCPU-8GB",
State: upcloud.KubernetesNodeGroupStatePending,
KubeletArgs: []upcloud.KubernetesKubeletArg{
{
Key: "somekubeletkey2",
Value: "somekubeletvalue2",
},
},
Name: "upcloud-go-sdk-unit-test-2",
Plan: "4xCPU-8GB",
State: upcloud.KubernetesNodeGroupStatePending,
KubeletArgs: []upcloud.KubernetesKubeletArg{
{
Key: "somekubeletkey2",
Value: "somekubeletvalue2",
},
Taints: []upcloud.KubernetesTaint{
{
Effect: "NoSchedule",
Key: "sometaintkey2",
Value: "sometaintvalue2",
},
},
Taints: []upcloud.KubernetesTaint{
{
Effect: "NoSchedule",
Key: "sometaintkey2",
Value: "sometaintvalue2",
},
Storage: "storage-uuid-2",
SSHKeys: []string{"somekey2"},
UtilityNetworkAccess: false,
},
Storage: "storage-uuid-2",
SSHKeys: []string{"somekey2"},
UtilityNetworkAccess: false,
},
State: upcloud.KubernetesClusterStateRunning,
UUID: "0ddab8f4-97c0-4222-91ba-85a4fff7499b",
Zone: "de-fra1",
}
},
State: upcloud.KubernetesClusterStateRunning,
UUID: "0ddab8f4-97c0-4222-91ba-85a4fff7499b",
Zone: "de-fra1",
}

func TestShowCommand(t *testing.T) {
text.DisableColors()

expected := `
Overview:
UUID: 0ddab8f4-97c0-4222-91ba-85a4fff7499b
Name: upcloud-go-sdk-unit-test
Name: upcloud-upctl-unit-test
Network UUID: 03a98be3-7daa-443f-bb25-4bc6854b396c
Network name: Test network
Network CIDR: 172.16.1.0/24
Expand All @@ -125,8 +126,8 @@ func TestShowCommand(t *testing.T) {
`

mService := smock.Service{}
mService.On("GetKubernetesClusters", mock.Anything).Return([]upcloud.KubernetesCluster{cluster1}, nil)
mService.On("GetKubernetesCluster", mock.Anything).Return(&cluster1, nil)
mService.On("GetKubernetesClusters", mock.Anything).Return([]upcloud.KubernetesCluster{testCluster}, nil)
mService.On("GetKubernetesCluster", mock.Anything).Return(&testCluster, nil)
mService.On("GetNetworkDetails", mock.Anything).Return(&upcloud.Network{Name: "Test network"}, nil)
mService.On("GetStorageDetails", mock.Anything).Return(&upcloud.StorageDetails{Storage: upcloud.Storage{Title: "Test storage"}}, nil)

Expand All @@ -139,7 +140,7 @@ func TestShowCommand(t *testing.T) {
t.Fatal(err)
}

command.Cobra().SetArgs([]string{cluster1.UUID})
command.Cobra().SetArgs([]string{testCluster.UUID})
output, err := mockexecute.MockExecute(command, &mService, conf)

assert.NoError(t, err)
Expand Down

0 comments on commit c618de4

Please sign in to comment.