Skip to content

Commit

Permalink
nodegroups:unit-tests
Browse files Browse the repository at this point in the history
Signed-off-by: Talor Itzhak <[email protected]>
  • Loading branch information
Tal-or committed Jan 7, 2025
1 parent d8172f9 commit 9c0890f
Showing 1 changed file with 199 additions and 0 deletions.
199 changes: 199 additions & 0 deletions pkg/nodegroups/nodegroups_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
package nodegroups

import (
"strings"
"testing"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/k8stopologyawareschedwg/deployer/pkg/deployer/platform"
nropv1 "github.com/openshift-kni/numaresources-operator/api/numaresourcesoperator/v1"
)

func TestValidate(t *testing.T) {
type testCase struct {
name string
nodeGroups []nropv1.NodeGroup
expectedError bool
expectedErrorMessage string
platf platform.Platform
}

emptyString := ""
poolName := "poolname-test"
config := nropv1.DefaultNodeGroupConfig()

testCases := []testCase{
{
name: "both source pools are nil",
nodeGroups: []nropv1.NodeGroup{
{
MachineConfigPoolSelector: nil,
PoolName: nil,
},
{
MachineConfigPoolSelector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"test": "test",
},
},
},
},
expectedError: true,
expectedErrorMessage: "missing any pool specifier",
platf: platform.OpenShift,
},
{
name: "both source pools are set",
nodeGroups: []nropv1.NodeGroup{
{
MachineConfigPoolSelector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"test": "test",
},
},
PoolName: &poolName,
},
},
expectedError: true,
expectedErrorMessage: "must have only a single specifier set",
platf: platform.OpenShift,
},
{
name: "with duplicates - mcp selector",
nodeGroups: []nropv1.NodeGroup{
{
MachineConfigPoolSelector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"test": "test",
},
},
},
{
MachineConfigPoolSelector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"test": "test",
},
},
},
},
expectedError: true,
expectedErrorMessage: "has duplicates",
platf: platform.OpenShift,
},
{
name: "with duplicates - pool name",
nodeGroups: []nropv1.NodeGroup{
{
PoolName: &poolName,
},
{
PoolName: &poolName,
},
},
expectedError: true,
expectedErrorMessage: "has duplicates",
platf: platform.OpenShift,
},
{
name: "bad MCP selector",
nodeGroups: []nropv1.NodeGroup{
{
MachineConfigPoolSelector: &metav1.LabelSelector{
MatchExpressions: []metav1.LabelSelectorRequirement{
{
Key: "test",
Operator: "bad-operator",
Values: []string{"test"},
},
},
},
},
},
expectedError: true,
expectedErrorMessage: "not a valid label selector operator",
platf: platform.OpenShift,
},
{
name: "correct values",
nodeGroups: []nropv1.NodeGroup{
{
MachineConfigPoolSelector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"test": "test",
},
},
},
{
MachineConfigPoolSelector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"test1": "test1",
},
},
},
{
PoolName: &poolName,
},
},
platf: platform.OpenShift,
},
{
name: "MCP selector set on Hypershift platform",
nodeGroups: []nropv1.NodeGroup{
{
MachineConfigPoolSelector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"test": "test",
},
},
PoolName: &poolName,
},
},
expectedError: true,
expectedErrorMessage: "MachineConfigPoolSelector on Hypershift platform",
platf: platform.HyperShift,
},
{
name: "empty PoolName on Hypershift platform",
nodeGroups: []nropv1.NodeGroup{
{
Config: &config,
},
},
expectedError: true,
expectedErrorMessage: "must specify PoolName on Hypershift platform",
platf: platform.HyperShift,
},
{
name: "empty pool name",
nodeGroups: []nropv1.NodeGroup{
{
PoolName: &emptyString,
},
},
expectedError: true,
expectedErrorMessage: "cannot be empty",
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
manager, err := NewForPlatform(tc.platf)
if err != nil {
t.Errorf(err.Error())

Check failure on line 183 in pkg/nodegroups/nodegroups_test.go

View workflow job for this annotation

GitHub Actions / build

printf: non-constant format string in call to (*testing.common).Errorf (govet)

Check failure on line 183 in pkg/nodegroups/nodegroups_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint

printf: non-constant format string in call to (*testing.common).Errorf (govet)
}
err = manager.Validate(tc.nodeGroups)
if err == nil && tc.expectedError {
t.Errorf("expected error, succeeded")
}
if err != nil && !tc.expectedError {
t.Errorf("expected success, failed")
}
if tc.expectedErrorMessage != "" {
if !strings.Contains(err.Error(), tc.expectedErrorMessage) {
t.Errorf("unexpected error: %v (expected %q)", err, tc.expectedErrorMessage)
}
}
})
}
}

0 comments on commit 9c0890f

Please sign in to comment.