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 f1da853 commit fe850a8
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 @@ -178,7 +177,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 @@ -856,6 +855,15 @@ 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
}

func isRequestedGPUAssignable(gpu v3.GPU, requestedGpu anywherev1.NutanixGPUIdentifier) bool {
if requestedGpu.Type == anywherev1.NutanixGPUIdentifierDeviceID {
return (*gpu.DeviceID == *requestedGpu.DeviceID) && gpu.Assignable
Expand Down
42 changes: 42 additions & 0 deletions pkg/providers/nutanix/validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,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 @@ -219,6 +222,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 @@ -1460,6 +1497,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 fe850a8

Please sign in to comment.