Skip to content

Commit

Permalink
Increase unit test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
wongni committed Jun 21, 2022
1 parent db9cb9c commit 5b3a347
Show file tree
Hide file tree
Showing 4 changed files with 192 additions and 3 deletions.
7 changes: 7 additions & 0 deletions pkg/dependencies/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package dependencies_test

import (
"context"
"encoding/base64"
"os"
"testing"

Expand All @@ -11,6 +12,7 @@ import (
"github.com/aws/eks-anywhere/pkg/cluster"
"github.com/aws/eks-anywhere/pkg/config"
"github.com/aws/eks-anywhere/pkg/dependencies"
"github.com/aws/eks-anywhere/pkg/providers/cloudstack/decoder"
)

type factoryTest struct {
Expand Down Expand Up @@ -74,6 +76,10 @@ func TestFactoryBuildWithClusterManagerWithoutCliConfig(t *testing.T) {
}

func TestFactoryBuildWithMultipleDependencies(t *testing.T) {
configString := test.ReadFile(t, "testdata/cloudstack_config_multiple_profiles.ini")
encodedConfig := base64.StdEncoding.EncodeToString([]byte(configString))
t.Setenv(decoder.EksacloudStackCloudConfigB64SecretKey, encodedConfig)

tt := newTest(t)
deps, err := dependencies.NewFactory().
UseExecutableImage("image:1").
Expand All @@ -92,6 +98,7 @@ func TestFactoryBuildWithMultipleDependencies(t *testing.T) {
WithCAPIManager().
WithManifestReader().
WithUnAuthKubeClient().
WithCmk().
Build(context.Background())

tt.Expect(err).To(BeNil())
Expand Down
11 changes: 11 additions & 0 deletions pkg/dependencies/testdata/cloudstack_config_multiple_profiles.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[Global]
verify-ssl = false
api-key = test-key1
secret-key = test-secret1
api-url = http://127.16.0.1:8080/client/api

[Instance2]
verify-ssl = true
api-key = test-key2
secret-key = test-secret2
api-url = http://127.16.0.2:8080/client/api
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
apiVersion: anywhere.eks.amazonaws.com/v1alpha1
kind: Cluster
metadata:
name: test
namespace: test-namespace
spec:
clusterNetwork:
cni: cilium
pods:
cidrBlocks:
- 192.168.0.0/16
services:
cidrBlocks:
- 10.96.0.0/12
controlPlaneConfiguration:
count: 3
endpoint:
host: 1.2.3.4
machineGroupRef:
kind: CloudStackMachineConfig
name: test-cp
datacenterRef:
kind: CloudStackDatacenterConfig
name: test
externalEtcdConfiguration:
count: 3
machineGroupRef:
kind: CloudStackMachineConfig
name: test-etcd
kubernetesVersion: "1.21"
workerNodeGroupConfigurations:
- count: 3
machineGroupRef:
kind: CloudStackMachineConfig
name: test
---
apiVersion: anywhere.eks.amazonaws.com/v1alpha1
kind: CloudStackDatacenterConfig
metadata:
name: test
namespace: test-namespace
spec:
account: "admin"
domain: "domain1"
zones:
- name: "zone1"
network:
name: "net1"
managementApiEndpoint: "http://127.16.0.1:8080/client/api"
availabilityZones:
- name: "zone2"
account: "admin"
domain: "domain2"
managementApiEndpoint: "http://127.16.0.2:8080/client/api"
zone:
name: "zone2"
network:
name: "net2"

---
apiVersion: anywhere.eks.amazonaws.com/v1alpha1
kind: CloudStackMachineConfig
metadata:
name: test-cp
namespace: test-namespace
spec:
computeOffering:
name: "m4-large"
users:
- name: "mySshUsername"
sshAuthorizedKeys: # The key below was manually generated and not used in any production systems
- "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQC1BK73XhIzjX+meUr7pIYh6RHbvI3tmHeQIXY5lv7aztN1UoX+bhPo3dwo2sfSQn5kuxgQdnxIZ/CTzy0p0GkEYVv3gwspCeurjmu0XmrdmaSGcGxCEWT/65NtvYrQtUE5ELxJ+N/aeZNlK2B7IWANnw/82913asXH4VksV1NYNduP0o1/G4XcwLLSyVFB078q/oEnmvdNIoS61j4/o36HVtENJgYr0idcBvwJdvcGxGnPaqOhx477t+kfJAa5n5dSA5wilIaoXH5i1Tf/HsTCM52L+iNCARvQzJYZhzbWI1MDQwzILtIBEQCJsl2XSqIupleY8CxqQ6jCXt2mhae+wPc3YmbO5rFvr2/EvC57kh3yDs1Nsuj8KOvD78KeeujbR8n8pScm3WDp62HFQ8lEKNdeRNj6kB8WnuaJvPnyZfvzOhwG65/9w13IBl7B1sWxbFnq2rMpm5uHVK7mAmjL0Tt8zoDhcE1YJEnp9xte3/pvmKPkST5Q/9ZtR9P5sI+02jY0fvPkPyC03j2gsPixG7rpOCwpOdbny4dcj0TDeeXJX8er+oVfJuLYz0pNWJcT2raDdFfcqvYA0B0IyNYlj5nWX4RuEcyT3qocLReWPnZojetvAG/H8XwOh7fEVGqHAKOVSnPXCSQJPl6s0H12jPJBDJMTydtYPEszl4/CeQ== testemail@test.com"
template:
name: "centos7-k8s-118"
diskOffering:
name: "Small"
mountPath: "/data-small"
device: "/dev/vdb"
filesystem: "ext4"
label: "data_disk"
symlinks:
/var/log/kubernetes: /data-small/var/log/kubernetes
affinityGroupIds:
- control-plane-anti-affinity
---
apiVersion: anywhere.eks.amazonaws.com/v1alpha1
kind: CloudStackMachineConfig
metadata:
name: test
namespace: test-namespace
spec:
computeOffering:
name: "m4-large"
users:
- name: "mySshUsername"
sshAuthorizedKeys: # The key below was manually generated and not used in any production systems
- "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQC1BK73XhIzjX+meUr7pIYh6RHbvI3tmHeQIXY5lv7aztN1UoX+bhPo3dwo2sfSQn5kuxgQdnxIZ/CTzy0p0GkEYVv3gwspCeurjmu0XmrdmaSGcGxCEWT/65NtvYrQtUE5ELxJ+N/aeZNlK2B7IWANnw/82913asXH4VksV1NYNduP0o1/G4XcwLLSyVFB078q/oEnmvdNIoS61j4/o36HVtENJgYr0idcBvwJdvcGxGnPaqOhx477t+kfJAa5n5dSA5wilIaoXH5i1Tf/HsTCM52L+iNCARvQzJYZhzbWI1MDQwzILtIBEQCJsl2XSqIupleY8CxqQ6jCXt2mhae+wPc3YmbO5rFvr2/EvC57kh3yDs1Nsuj8KOvD78KeeujbR8n8pScm3WDp62HFQ8lEKNdeRNj6kB8WnuaJvPnyZfvzOhwG65/9w13IBl7B1sWxbFnq2rMpm5uHVK7mAmjL0Tt8zoDhcE1YJEnp9xte3/pvmKPkST5Q/9ZtR9P5sI+02jY0fvPkPyC03j2gsPixG7rpOCwpOdbny4dcj0TDeeXJX8er+oVfJuLYz0pNWJcT2raDdFfcqvYA0B0IyNYlj5nWX4RuEcyT3qocLReWPnZojetvAG/H8XwOh7fEVGqHAKOVSnPXCSQJPl6s0H12jPJBDJMTydtYPEszl4/CeQ== testemail@test.com"
template:
name: "centos7-k8s-118"
diskOffering:
name: "Small"
mountPath: "/data-small"
device: "/dev/vdb"
filesystem: "ext4"
label: "data_disk"
symlinks:
/var/log/pods: /data-small/var/log/pods
/var/log/containers: /data-small/var/log/containers
affinityGroupIds:
- worker-affinity
userCustomDetails:
foo: bar
---
apiVersion: anywhere.eks.amazonaws.com/v1alpha1
kind: CloudStackMachineConfig
metadata:
name: test-etcd
namespace: test-namespace
spec:
computeOffering:
name: "m4-large"
users:
- name: "mySshUsername"
sshAuthorizedKeys: # The key below was manually generated and not used in any production systems
- "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQC1BK73XhIzjX+meUr7pIYh6RHbvI3tmHeQIXY5lv7aztN1UoX+bhPo3dwo2sfSQn5kuxgQdnxIZ/CTzy0p0GkEYVv3gwspCeurjmu0XmrdmaSGcGxCEWT/65NtvYrQtUE5ELxJ+N/aeZNlK2B7IWANnw/82913asXH4VksV1NYNduP0o1/G4XcwLLSyVFB078q/oEnmvdNIoS61j4/o36HVtENJgYr0idcBvwJdvcGxGnPaqOhx477t+kfJAa5n5dSA5wilIaoXH5i1Tf/HsTCM52L+iNCARvQzJYZhzbWI1MDQwzILtIBEQCJsl2XSqIupleY8CxqQ6jCXt2mhae+wPc3YmbO5rFvr2/EvC57kh3yDs1Nsuj8KOvD78KeeujbR8n8pScm3WDp62HFQ8lEKNdeRNj6kB8WnuaJvPnyZfvzOhwG65/9w13IBl7B1sWxbFnq2rMpm5uHVK7mAmjL0Tt8zoDhcE1YJEnp9xte3/pvmKPkST5Q/9ZtR9P5sI+02jY0fvPkPyC03j2gsPixG7rpOCwpOdbny4dcj0TDeeXJX8er+oVfJuLYz0pNWJcT2raDdFfcqvYA0B0IyNYlj5nWX4RuEcyT3qocLReWPnZojetvAG/H8XwOh7fEVGqHAKOVSnPXCSQJPl6s0H12jPJBDJMTydtYPEszl4/CeQ== testemail@test.com"
template:
name: "centos7-k8s-118"
diskOffering:
name: "Small"
mountPath: "/data-small"
device: "/dev/vdb"
filesystem: "ext4"
label: "data_disk"
symlinks:
/var/lib/: /data-small/var/lib
affinityGroupIds:
- etcd-affinity

---
38 changes: 35 additions & 3 deletions pkg/providers/cloudstack/validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ import (
)

const (
testClusterConfigMainFilename = "cluster_main.yaml"
testDataDir = "testdata"
testClusterConfigMainFilename = "cluster_main.yaml"
testClusterConfigMainWithAZsFilename = "cluster_main_with_availability_zones.yaml"
testDataDir = "testdata"
)

var testTemplate = v1alpha1.CloudStackResourceIdentifier{
Expand Down Expand Up @@ -58,6 +59,27 @@ func TestValidateCloudStackDatacenterConfig(t *testing.T) {
}
}

func TestValidateCloudStackDatacenterConfigWithAZ(t *testing.T) {
ctx := context.Background()
setupContext()
cmk := mocks.NewMockProviderCmkClient(gomock.NewController(t))
cmk2 := mocks.NewMockProviderCmkClient(gomock.NewController(t))
validator := NewValidator(CmkClientMap{decoder.CloudStackGlobalAZ: cmk, "zone2": cmk2})

datacenterConfig, err := v1alpha1.GetCloudStackDatacenterConfig(path.Join(testDataDir, testClusterConfigMainWithAZsFilename))
if err != nil {
t.Fatalf("unable to get datacenter config from file")
}

setupMockForDatacenterConfigValidation(cmk, ctx, datacenterConfig)
setupMockForAvailabilityZonesValidation(cmk2, ctx, datacenterConfig.Spec.AvailabilityZones)

err = validator.ValidateCloudStackDatacenterConfig(ctx, datacenterConfig)
if err != nil {
t.Fatalf("failed to validate CloudStackDataCenterConfig: %v", err)
}
}

func TestValidateCloudStackConnection(t *testing.T) {
ctx := context.Background()
cmk := mocks.NewMockProviderCmkClient(gomock.NewController(t))
Expand Down Expand Up @@ -588,7 +610,17 @@ func setupMockForDatacenterConfigValidation(cmk *mocks.MockProviderCmkClient, ct
cmk.EXPECT().ValidateDomainPresent(ctx, datacenterConfig.Spec.Domain).AnyTimes().Return(v1alpha1.CloudStackResourceIdentifier{Id: "5300cdac-74d5-11ec-8696-c81f66d3e965", Name: datacenterConfig.Spec.Domain}, nil)
cmk.EXPECT().ValidateAccountPresent(ctx, gomock.Any(), gomock.Any()).AnyTimes().Return(nil)
cmk.EXPECT().ValidateNetworkPresent(ctx, gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes().Return(nil)
cmk.EXPECT().GetManagementApiEndpoint(ctx).AnyTimes().Return("http://127.16.0.1:8080/client/api")
cmk.EXPECT().GetManagementApiEndpoint(ctx).AnyTimes().Return(datacenterConfig.Spec.ManagementApiEndpoint)
}

func setupMockForAvailabilityZonesValidation(cmk *mocks.MockProviderCmkClient, ctx context.Context, azs []v1alpha1.CloudStackAvailabilityZone) {
for _, az := range azs {
cmk.EXPECT().ValidateZonePresent(ctx, az.Zone).AnyTimes().Return("4e3b338d-87a6-4189-b931-a1747edeea82", nil)
cmk.EXPECT().ValidateDomainPresent(ctx, az.Domain).AnyTimes().Return(v1alpha1.CloudStackResourceIdentifier{Id: "5300cdac-74d5-11ec-8696-c81f66d3e962", Name: az.Domain}, nil)
cmk.EXPECT().ValidateAccountPresent(ctx, az.Account, gomock.Any()).AnyTimes().Return(nil)
cmk.EXPECT().ValidateNetworkPresent(ctx, gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes().Return(nil)
cmk.EXPECT().GetManagementApiEndpoint(ctx).AnyTimes().Return(az.ManagementApiEndpoint)
}
}

func TestSetupAndValidateCreateClusterCPMachineGroupRefNil(t *testing.T) {
Expand Down

0 comments on commit 5b3a347

Please sign in to comment.