Skip to content

Commit

Permalink
Add unit-tests and fix failed
Browse files Browse the repository at this point in the history
  • Loading branch information
adiantum committed Oct 10, 2024
1 parent 0ea1830 commit 2941c32
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 3 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ require (
go.opentelemetry.io/otel/trace v1.20.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/mod v0.14.0 // indirect
golang.org/x/sync v0.6.0 // indirect
golang.org/x/sync v0.6.0
golang.org/x/sys v0.18.0 // indirect
golang.org/x/term v0.18.0 // indirect
golang.org/x/time v0.5.0 // indirect
Expand Down
12 changes: 10 additions & 2 deletions pkg/providers/nutanix/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"net/http"
"regexp"
"slices"
"strconv"
"strings"

Expand Down Expand Up @@ -174,7 +173,7 @@ func (v *Validator) validateFailureDomains(ctx context.Context, client Client, s
func (v *Validator) validateWorkerMachineGroup(spec *cluster.Spec, workerMachineGroupName string) error {
workerMachineGroupNames := getWorkerMachineGroupNames(spec)

if !slices.Contains(workerMachineGroupNames, workerMachineGroupName) {
if !sliceContains(workerMachineGroupNames, workerMachineGroupName) {
return fmt.Errorf("worker machine group %s not found in the cluster worker node group definitions", workerMachineGroupName)
}

Expand Down Expand Up @@ -588,3 +587,12 @@ func findProjectUUIDByName(ctx context.Context, v3Client Client, projectName str

return res.Entities[0].Metadata.UUID, nil
}

func sliceContains(slice []string, element string) bool {
for _, sliceElement := range slice {
if sliceElement == element {
return true
}
}
return false
}
42 changes: 42 additions & 0 deletions pkg/providers/nutanix/validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ var nutanixDatacenterConfigSpecWithFailureDomainInvalidCluster string
//go:embed testdata/datacenterConfig_with_failure_domains_invalid_subnet.yaml
var nutanixDatacenterConfigSpecWithFailureDomainInvalidSubnet string

//go:embed testdata/datacenterConfig_with_failure_domains_invalid_wg.yaml
var nutanixDatacenterConfigSpecWithFailureDomainInvalidWorkerMachineGroups string

func fakeClusterList() *v3.ClusterListIntentResponse {
return &v3.ClusterListIntentResponse{
Entities: []*v3.ClusterIntentResponse{
Expand Down Expand Up @@ -217,6 +220,40 @@ func fakeProjectList() *v3.ProjectListResponse {
}
}

func TestSliceContainsFunc(t *testing.T) {
tests := []struct {
name string
slice []string
value string
expected bool
}{
{
name: "empty slice",
slice: []string{},
value: "test",
expected: false,
},
{
name: "slice contains value",
slice: []string{"test", "test1", "test2"},
value: "test",
expected: true,
},
{
name: "slice does not contain value",
slice: []string{"test", "test2", "test3"},
value: "test1",
expected: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.expected, sliceContains(tt.slice, tt.value))
})
}
}

func TestNutanixValidatorValidateMachineConfig(t *testing.T) {
ctrl := gomock.NewController(t)

Expand Down Expand Up @@ -721,6 +758,11 @@ func TestNutanixValidatorValidateDatacenterConfig(t *testing.T) {
dcConfFile: nutanixDatacenterConfigSpecWithFailureDomainInvalidSubnet,
expectErr: true,
},
{
name: "failure domains with invalid workerMachineGroups",
dcConfFile: nutanixDatacenterConfigSpecWithFailureDomainInvalidWorkerMachineGroups,
expectErr: true,
},
}

ctrl := gomock.NewController(t)
Expand Down

0 comments on commit 2941c32

Please sign in to comment.