Skip to content

Commit

Permalink
Updating the Unit tests for Code Coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
harshitap26 committed Sep 23, 2024
1 parent 7db0758 commit 30b74b4
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 7 deletions.
4 changes: 3 additions & 1 deletion service/features/service.feature
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,17 @@ Feature: VxFlex OS CSI interface
Scenario Outline: Identity GetPluginInfo bad call
Given a VxFlexOS service
When I call GetPluginInfo
When I call BeforeServe
And I induce error <error>
When I call BeforeServe
Then configMap is updated
Then a valid GetPlugInfoResponse is returned
Examples:
| error |
| "UpdateConfigMapUnmarshalError" |
| "GetIPAddressByInterfaceError" |
| "UpdateConfigK8sClientError" |
| "UpdateConfigFormatError" |
| "ConfigMapNotFoundError" |

Scenario Outline: Dynamic log config change
Given a VxFlexOS service
Expand Down
44 changes: 44 additions & 0 deletions service/service_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,50 @@ func TestFindNetworkInterfaceIPs(t *testing.T) {
}
},
},
{
name: "Error unmarshalling ConfigMap params",
expectedError: errors.New("error converting YAML to JSON: yaml: line 1: did not find expected node content"),
client: fake.NewSimpleClientset(),
configMapData: map[string]string{
"driver-config-params.yaml": `[interfaces:`,
},
createConfigMap: func(data map[string]string, clientSet kubernetes.Interface) {
configMap := &v1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: DriverConfigMap,
Namespace: DriverNamespace,
},
Data: data,
}
// Create a ConfigMap using fake ClientSet
_, err := clientSet.CoreV1().ConfigMaps(DriverNamespace).Create(context.TODO(), configMap, metav1.CreateOptions{})
if err != nil {
Log.Fatalf("failed to create configMaps: %v", err)
}
},
},
{
name: "Error getting the Network Interface IPs",
expectedError: fmt.Errorf("failed to get the Network Interface IPs"),
client: fake.NewSimpleClientset(),
configMapData: map[string]string{
"params-yaml": ``,
},
createConfigMap: func(data map[string]string, clientSet kubernetes.Interface) {
configMap := &v1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: DriverConfigMap,
Namespace: DriverNamespace,
},
Data: data,
}
// Create a ConfigMap using fake ClientSet
_, err := clientSet.CoreV1().ConfigMaps(DriverNamespace).Create(context.TODO(), configMap, metav1.CreateOptions{})
if err != nil {
Log.Fatalf("failed to create configMaps: %v", err)
}
},
},
}

for _, tt := range tests {
Expand Down
22 changes: 16 additions & 6 deletions service/step_defs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1348,6 +1348,10 @@ func (f *feature) iInduceError(errtype string) error {
stepHandlersErrors.GetIPAddressByInterfaceError = true
case "UpdateConfigK8sClientError":
stepHandlersErrors.UpdateConfigK8sClientError = true
case "UpdateConfigFormatError":
stepHandlersErrors.UpdateConfigFormatError = true
case "ConfigMapNotFoundError":
stepHandlersErrors.ConfigMapNotFoundError = true
default:
fmt.Println("Ensure that the error is handled in the handlers section.")
}
Expand Down Expand Up @@ -3143,14 +3147,15 @@ func (f *feature) theConfigMapIsUpdated() error {
data := `interfaceNames:
worker1: "eth1"
worker2: "eth2"`

if stepHandlersErrors.UpdateConfigMapUnmarshalError {
data = `[interfaces:`
} else if stepHandlersErrors.UpdateConfigFormatError {
data = `interfaceName:`
}

configMapData := map[string]string{
"driver-config-params.yaml": data,
}

configMap := &v1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: DriverConfigMap,
Expand All @@ -3167,10 +3172,12 @@ func (f *feature) theConfigMapIsUpdated() error {
Log.Errorf("Error writing to temp file: %v", err)
}

// Create a ConfigMap using fake ClientSet
_, err = clientSet.CoreV1().ConfigMaps(DriverNamespace).Create(context.TODO(), configMap, metav1.CreateOptions{})
if err != nil {
Log.Errorf("failed to create configmap: %v", err)
if !stepHandlersErrors.ConfigMapNotFoundError {
// Create a ConfigMap using fake ClientSet
_, err = clientSet.CoreV1().ConfigMaps(DriverNamespace).Create(context.TODO(), configMap, metav1.CreateOptions{})
if err != nil {
Log.Errorf("failed to create configmap: %v", err)
}
}

// Mocking the GetIPAddressByInterface function
Expand Down Expand Up @@ -3219,6 +3226,9 @@ func (f *feature) iCallBeforeServe() error {
if perr != nil {
f.err = perr
}
if stepHandlersErrors.UpdateConfigK8sClientError {
K8sClientset = nil
}
f.err = f.service.BeforeServe(ctx, nil, listener)
listener.Close()
return nil
Expand Down
4 changes: 4 additions & 0 deletions service/step_handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ var (
UpdateConfigMapUnmarshalError bool
GetIPAddressByInterfaceError bool
UpdateConfigK8sClientError bool
UpdateConfigFormatError bool
ConfigMapNotFoundError bool
}
)

Expand Down Expand Up @@ -232,6 +234,8 @@ func getHandler() http.Handler {
stepHandlersErrors.UpdateConfigMapUnmarshalError = false
stepHandlersErrors.GetIPAddressByInterfaceError = false
stepHandlersErrors.UpdateConfigK8sClientError = false
stepHandlersErrors.UpdateConfigFormatError = false
stepHandlersErrors.ConfigMapNotFoundError = false
sdcMappings = sdcMappings[:0]
sdcMappingsID = ""
return handler
Expand Down

0 comments on commit 30b74b4

Please sign in to comment.