Skip to content

Commit

Permalink
Add all planned unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
donatwork committed Dec 8, 2024
1 parent 8abae75 commit 1b36c0c
Showing 1 changed file with 69 additions and 9 deletions.
78 changes: 69 additions & 9 deletions service/preinit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes/fake"
)

Expand Down Expand Up @@ -53,7 +55,7 @@ func TestPreInit(t *testing.T) {
name string
connectionInfo []*ArrayConnectionData
connectionError error
modeLabels map[string]string
nodeInfo *corev1.Node
errorExpected bool
expectedResult string
}{
Expand Down Expand Up @@ -112,13 +114,65 @@ func TestPreInit(t *testing.T) {
errorExpected: false,
expectedResult: "192.168.1.1,192.168.1.2,192.168.2.1,192.168.2.2",
},

// {
// name: "",
// connectionInfo: []*ArrayConnectionData{},
// errorExpected: false,
// expectedResult: "",
// },
{
name: "should fail if zones configured but unable to fetch node labels",
connectionInfo: []*ArrayConnectionData{
{
Mdm: "192.168.1.1,192.168.1.2",
Zone: ZoneInfo{
LabelKey: "key1",
Name: "zone1",
},
},
},
errorExpected: true,
expectedResult: "rpc error: code = Internal desc = Unable to fetch the node labels. Error: nodes \"\" not found",
},
{
name: "should fail if node label not found for node",
connectionInfo: []*ArrayConnectionData{
{
Mdm: "192.168.1.1,192.168.1.2",
Zone: ZoneInfo{
LabelKey: "key1",
Name: "zone1",
},
},
},
nodeInfo: &corev1.Node{
ObjectMeta: metav1.ObjectMeta{
Name: "",
Labels: map[string]string{"label1": "value1", "label2": "value2"},
}},
errorExpected: true,
expectedResult: "No zone found, cannot configure this node",
},
{
name: "should configure MDMs for only array matching zone label",
connectionInfo: []*ArrayConnectionData{
{
Mdm: "192.168.1.1,192.168.1.2",
Zone: ZoneInfo{
LabelKey: "key1",
Name: "zone1",
},
},
{
Mdm: "192.168.2.1,192.168.2.2",
Zone: ZoneInfo{
LabelKey: "key1",
Name: "zone2",
},
},
},
nodeInfo: &corev1.Node{
ObjectMeta: metav1.ObjectMeta{
Name: "",
Labels: map[string]string{"key1": "zone1", "label2": "value2"},
}},
errorExpected: false,
expectedResult: "192.168.1.1,192.168.1.2",
},
}

for _, test := range tests {
Expand All @@ -128,7 +182,13 @@ func TestPreInit(t *testing.T) {

arrayConfigurationProviderImpl = mockArrayConfigurationProvider
fileWriterProviderImpl = mockFileWriterProvider
K8sClientset = fake.NewClientset()

if test.nodeInfo == nil {
K8sClientset = fake.NewClientset()
} else {
K8sClientset = fake.NewClientset(test.nodeInfo)
}

svc := NewPreInitService()

mockArrayConfigurationProvider.On("GetArrayConfiguration").Return(test.connectionInfo, test.connectionError)
Expand Down

0 comments on commit 1b36c0c

Please sign in to comment.