Skip to content

Commit

Permalink
Add validations and unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
adiantum committed Jun 6, 2024
1 parent 70b8ec8 commit 1affcb4
Show file tree
Hide file tree
Showing 13 changed files with 401 additions and 9 deletions.
10 changes: 5 additions & 5 deletions pkg/api/v1alpha1/nutanixdatacenterconfig_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,19 +185,19 @@ func (in *NutanixDatacenterConfig) Validate() error {
return nil
}

func createValidateNutanixResourceFunc(msgPrefix, entityName, mfstName string) (func (*NutanixResourceIdentifier) error) {
return func (ntnxRId *NutanixResourceIdentifier) error {
func createValidateNutanixResourceFunc(msgPrefix, entityName, mfstName string) func(*NutanixResourceIdentifier) error {
return func(ntnxRId *NutanixResourceIdentifier) error {
if ntnxRId.Type != NutanixIdentifierName && ntnxRId.Type != NutanixIdentifierUUID {
return fmt.Errorf("%s: invalid identifier type for %s: %s", msgPrefix, entityName, ntnxRId.Type)

Check warning on line 191 in pkg/api/v1alpha1/nutanixdatacenterconfig_types.go

View check run for this annotation

Codecov / codecov/patch

pkg/api/v1alpha1/nutanixdatacenterconfig_types.go#L191

Added line #L191 was not covered by tests
}

if ntnxRId.Type == NutanixIdentifierName && (ntnxRId.Name == nil || *ntnxRId.Name == "") {
return fmt.Errorf("%s: missing %s name: %s", msgPrefix, entityName, mfstName)

Check warning on line 195 in pkg/api/v1alpha1/nutanixdatacenterconfig_types.go

View check run for this annotation

Codecov / codecov/patch

pkg/api/v1alpha1/nutanixdatacenterconfig_types.go#L195

Added line #L195 was not covered by tests
} else if ntnxRId.Type == NutanixIdentifierUUID && (ntnxRId.UUID == nil || *ntnxRId.UUID == "") {
return fmt.Errorf("%s: missing %s UUID: %s", msgPrefix, entityName, mfstName)
}
return nil

return nil
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
apiVersion: anywhere.eks.amazonaws.com/v1alpha1
kind: NutanixDatacenterConfig
metadata:
name: eksa-unit-test
namespace: default
spec:
endpoint: "prism.nutanix.com"
port: 9440
credentialRef:
name: eksa-unit-test
kind: Secret
failureDomains:
- name: "pe1"
cluster:
type: name
name: "prism-cluster-1"
subnets:
- name: "prism-subnet-1"
type: "name"
- uuid: ""
type: "uuid"
- name: "pe2"
cluster:
type: "uuid"
uuid: "468b7b36-d15b-406a-90f7-46d1560c4f4e"
subnets:
- name: "prism-subnet-1"
type: "name"
- uuid: "3e716c09-0613-46f3-b46a-beb89aa02295"
type: "uuid"
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
apiVersion: anywhere.eks.amazonaws.com/v1alpha1
kind: NutanixDatacenterConfig
metadata:
name: eksa-unit-test
namespace: default
spec:
endpoint: "prism.nutanix.com"
port: 9440
credentialRef:
name: eksa-unit-test
kind: Secret
failureDomains:
- name: "pe1"
cluster:
type: name
name: "prism-cluster-1"
subnets:
- name: "prism-subnet-1"
type: "name"
- uuid: "3e716c09-0613-46f3-b46a-beb89aa02295"
type: "uuid"
- name: "pe2"
cluster:
type: "uuid"
uuid: "468b7b36-d15b-406a-90f7-46d1560c4f4e"
subnets:
- name: "prism-subnet-1"
type: "name"
- uuid: "3e716c09-0613-46f3-b46a-beb89aa02295"
type: "uuid"
13 changes: 11 additions & 2 deletions pkg/providers/nutanix/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,17 @@ func (p *Provider) SetupAndValidateDeleteCluster(ctx context.Context, cluster *t
}

// SetupAndValidateUpgradeCluster - Performs necessary setup and validations for upgrade cluster operation.
func (p *Provider) SetupAndValidateUpgradeCluster(ctx context.Context, _ *types.Cluster, clusterSpec *cluster.Spec, _ *cluster.Spec) error {
func (p *Provider) SetupAndValidateUpgradeCluster(ctx context.Context, cluster *types.Cluster, clusterSpec *cluster.Spec, _ *cluster.Spec) error {
// Get current Nutanix Datacenter Config
curDc, err := p.kubectlClient.GetEksaNutanixDatacenterConfig(ctx, p.datacenterConfig.Name, cluster.KubeconfigFile, clusterSpec.Cluster.Namespace)
if err != nil {
return fmt.Errorf("failed setup and validations: %v", err)

Check warning on line 233 in pkg/providers/nutanix/provider.go

View check run for this annotation

Codecov / codecov/patch

pkg/providers/nutanix/provider.go#L233

Added line #L233 was not covered by tests
}

if len(curDc.Spec.FailureDomains) != len(p.datacenterConfig.Spec.FailureDomains) {
return fmt.Errorf("failed setup and validations: failure domains upgrade doesn't supported in current release")

Check warning on line 237 in pkg/providers/nutanix/provider.go

View check run for this annotation

Codecov / codecov/patch

pkg/providers/nutanix/provider.go#L237

Added line #L237 was not covered by tests
}

if err := p.SetupAndValidateUpgradeManagementComponents(ctx, clusterSpec); err != nil {
return err
}
Expand Down Expand Up @@ -419,7 +429,6 @@ func needsNewEtcdTemplate(oldSpec, newSpec *cluster.Spec, oldNmc, newNmc *v1alph
if oldSpec.Bundles.Spec.Number != newSpec.Bundles.Spec.Number {
return true
}

return AnyImmutableFieldChanged(oldNmc, newNmc)
}

Expand Down
18 changes: 16 additions & 2 deletions pkg/providers/nutanix/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,20 @@ func TestNutanixProviderSetupAndValidateDeleteCluster(t *testing.T) {
}

func TestNutanixProviderSetupAndValidateUpgradeCluster(t *testing.T) {
provider := testDefaultNutanixProvider(t)
ctrl := gomock.NewController(t)
executable := mockexecutables.NewMockExecutable(ctrl)
executable.EXPECT().ExecuteWithStdin(gomock.Any(), gomock.Any(), gomock.Any()).Return(bytes.Buffer{}, nil).AnyTimes()
executable.EXPECT().Execute(gomock.Any(), "get",
"--ignore-not-found", "-o", "json", "--kubeconfig", "testdata/kubeconfig.yaml", "nutanixdatacenterconfigs.anywhere.eks.amazonaws.com", "--namespace", "default", "eksa-unit-test").Return(*bytes.NewBufferString(nutanixDatacenterConfigSpecJSON), nil).AnyTimes()
kubectl := executables.NewKubectl(executable)
mockClient := mocknutanix.NewMockClient(ctrl)
mockCertValidator := mockCrypto.NewMockTlsValidator(ctrl)
mockTransport := mocknutanix.NewMockRoundTripper(ctrl)
mockTransport.EXPECT().RoundTrip(gomock.Any()).Return(&http.Response{}, nil).AnyTimes()
mockHTTPClient := &http.Client{Transport: mockTransport}
mockWriter := filewritermocks.NewMockFileWriter(ctrl)
provider := testNutanixProvider(t, mockClient, kubectl, mockCertValidator, mockHTTPClient, mockWriter)

tests := []struct {
name string
clusterConfFile string
Expand Down Expand Up @@ -558,7 +571,8 @@ func TestNutanixProviderSetupAndValidateUpgradeCluster(t *testing.T) {

for _, tt := range tests {
clusterSpec := test.NewFullClusterSpec(t, tt.clusterConfFile)
err := provider.SetupAndValidateUpgradeCluster(context.Background(), &types.Cluster{Name: "eksa-unit-test"}, clusterSpec, clusterSpec)
cluster := &types.Cluster{Name: "eksa-unit-test", KubeconfigFile: "testdata/kubeconfig.yaml"}
err := provider.SetupAndValidateUpgradeCluster(context.Background(), cluster, clusterSpec, clusterSpec)
if tt.expectErr {
assert.Error(t, err, tt.name)
thenErrorExpected(t, tt.expectErrStr, err)
Expand Down
3 changes: 3 additions & 0 deletions pkg/providers/nutanix/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -699,6 +699,9 @@ func TestTemplateBuilderFailureDomains(t *testing.T) {
clusterSpec := test.NewFullClusterSpec(t, tc.Input)

machineCfg := clusterSpec.NutanixMachineConfig(clusterSpec.Cluster.Spec.ControlPlaneConfiguration.MachineGroupRef.Name)

t.Setenv(constants.EksaNutanixUsernameKey, "admin")
t.Setenv(constants.EksaNutanixPasswordKey, "password")
creds := GetCredsFromEnv()

bldr := NewNutanixTemplateBuilder(&clusterSpec.NutanixDatacenter.Spec, &machineCfg.Spec, nil,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
apiVersion: anywhere.eks.amazonaws.com/v1alpha1
kind: NutanixDatacenterConfig
metadata:
name: eksa-unit-test
namespace: default
spec:
endpoint: "prism.nutanix.com"
port: 9440
credentialRef:
kind: Secret
name: "nutanix-credentials"
insecure: true
failureDomains:
- name: "pe1"
cluster:
type: name
name: "prism-cluster"
subnets:
- type: uuid
uuid: "2d166190-7759-4dc6-b835-923262d6b497"
- name: "pe2"
cluster:
type: uuid
uuid: "4d69ca7d-022f-49d1-a454-74535993bda4"
subnets:
- type: name
name: "prism-subnet"
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
apiVersion: anywhere.eks.amazonaws.com/v1alpha1
kind: NutanixDatacenterConfig
metadata:
name: eksa-unit-test
namespace: default
spec:
endpoint: "prism.nutanix.com"
port: 9440
credentialRef:
kind: Secret
name: "nutanix-credentials"
insecure: true
failureDomains:
- name: "pe1"
cluster:
type: name
name: "prism-cluster"
subnets:
- type: uuid
uuid: "2d166190-7759-4dc6-b835-923262d6b497"
- name: "pe2"
cluster:
type: uuid
uuid: "4d69ca7d-022f-49d1-a454-00005993bda4"
subnets:
- type: name
name: "prism-subnet"
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
apiVersion: anywhere.eks.amazonaws.com/v1alpha1
kind: NutanixDatacenterConfig
metadata:
name: eksa-unit-test
namespace: default
spec:
endpoint: "prism.nutanix.com"
port: 9440
credentialRef:
kind: Secret
name: "nutanix-credentials"
insecure: true
failureDomains:
- name: "FIZZBUZZ!!!!"
cluster:
type: name
name: "prism-cluster"
subnets:
- type: uuid
uuid: "2d166190-7759-4dc6-b835-923262d6b497"
- name: "pe2"
cluster:
type: uuid
uuid: "4d69ca7d-022f-49d1-a454-74535993bda4"
subnets:
- type: name
name: "prism-subnet"
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
apiVersion: anywhere.eks.amazonaws.com/v1alpha1
kind: NutanixDatacenterConfig
metadata:
name: eksa-unit-test
namespace: default
spec:
endpoint: "prism.nutanix.com"
port: 9440
credentialRef:
kind: Secret
name: "nutanix-credentials"
insecure: true
failureDomains:
- name: "pe1"
cluster:
type: name
name: "prism-cluster"
subnets:
- type: uuid
uuid: "2d166190-7759-4dc6-b835-000062d6b497"
- name: "pe2"
cluster:
type: uuid
uuid: "4d69ca7d-022f-49d1-a454-74535993bda4"
subnets:
- type: name
name: "prism-subnet"
Original file line number Diff line number Diff line change
Expand Up @@ -601,3 +601,31 @@ spec:
- kind: Secret
name: test-nutanix-ccm-secret
strategy: Reconcile
---
apiVersion: v1
kind: Secret
metadata:
name: "test-nutanix-ccm-secret"
namespace: "eksa-system"
stringData:
nutanix-ccm-secret.yaml: |
apiVersion: v1
kind: Secret
metadata:
name: nutanix-creds
namespace: kube-system
stringData:
credentials: |-
[
{
"type": "basic_auth",
"data": {
"prismCentral": {
"username": "admin",
"password": "password"
},
"prismElements": null
}
}
]
type: addons.cluster.x-k8s.io/resource-set
32 changes: 32 additions & 0 deletions pkg/providers/nutanix/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"net/http"
"regexp"
"strconv"
"strings"

Expand Down Expand Up @@ -126,6 +127,37 @@ func (v *Validator) ValidateDatacenterConfig(ctx context.Context, client Client,
return err
}

if err := v.validateFailureDomains(ctx, client, config); err != nil {
return err
}

return nil
}

func (v *Validator) validateFailureDomains(ctx context.Context, client Client, config *anywherev1.NutanixDatacenterConfig) error {
regexName, err := regexp.Compile("^[a-z0-9]([-a-z0-9]*[a-z0-9])?$")
if err != nil {
return err

Check warning on line 140 in pkg/providers/nutanix/validator.go

View check run for this annotation

Codecov / codecov/patch

pkg/providers/nutanix/validator.go#L140

Added line #L140 was not covered by tests
}

for _, fd := range config.Spec.FailureDomains {
if res := regexName.MatchString(fd.Name); !res {
errorStr := `failure domain name should contains only small letters, digits, and hyphens.
It should start with small letter or digit`
return fmt.Errorf(errorStr)
}

if err := v.validateClusterConfig(ctx, client, fd.Cluster); err != nil {
return err
}

for _, subnet := range fd.Subnets {
if err := v.validateSubnetConfig(ctx, client, subnet); err != nil {
return err
}
}
}

return nil
}

Expand Down
Loading

0 comments on commit 1affcb4

Please sign in to comment.