Skip to content

Commit

Permalink
Testing done correctly for errors and golint fixes
Browse files Browse the repository at this point in the history
Signed-off-by: Abhishek-kumar09 <abhimait1909@gmail.com>
  • Loading branch information
Abhishek-kumar09 committed Sep 6, 2021
1 parent 42a7937 commit acf45c5
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 19 deletions.
5 changes: 2 additions & 3 deletions pkg/blockdevice/blockdevice.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,8 @@ func createTreeByNode(k *client.K8sClient, bdNames []string) error {
}
if len(rows) == 0 {
return util.HandleEmptyTableError("Block Device", k.Ns, "")
} else {
// Show the output using cli-runtime
util.TablePrinter(util.BDTreeListColumnDefinations, rows, printers.PrintOptions{Wide: true})
}
// Show the output using cli-runtime
util.TablePrinter(util.BDTreeListColumnDefinations, rows, printers.PrintOptions{Wide: true})
return nil
}
3 changes: 1 addition & 2 deletions pkg/storage/lvmlocalpv.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ func GetVolumeGroups(c *client.K8sClient, vgs []string) ([]metav1.TableColumnDef
}
// 3. Actually print the table or return an error
if len(rows) == 0 {
// TODO: Improve this in issue #56
return nil, nil, fmt.Errorf("no lvm volumegroups found")
return nil, nil, util.HandleEmptyTableError("lvm Volumegroups", c.Ns, "")
}
return util.LVMvolgroupListColumnDefinitions, rows, nil
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,8 @@ func Get(pools []string, openebsNS string, casType string) error {
}
if len(rows) == 0 {
return util.HandleEmptyTableError("Storage", openebsNS, casType)
} else {
util.TablePrinter(header, rows, printers.PrintOptions{Wide: true})
}
util.TablePrinter(header, rows, printers.PrintOptions{Wide: true})
} else if casType != "" {
return fmt.Errorf("cas-type %s is not supported", casType)
} else {
Expand Down
2 changes: 1 addition & 1 deletion pkg/storage/zfslocalpv.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func GetZFSPools(c *client.K8sClient, zfsnodes []string) ([]metav1.TableColumnDe
}
// 3. Actually print the table or return an error
if len(rows) == 0 {
return nil, nil, fmt.Errorf("no zfspools found")
return nil, nil, util.HandleEmptyTableError("zfs pools", c.Ns, "")
}
return util.ZFSPoolListColumnDefinitions, rows, nil
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/util/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ func CheckErr(err error, handleErr func(string)) {
handleErr(err.Error())
}

// Handle Empty Table Error
// Handle Empty handles error when resources or set of resources are not found
func HandleEmptyTableError(resource string, ns string, casType string) error {
if ns == "" && casType == "" {
return fmt.Errorf("no %s found in your cluster", resource)
} else if ns != "" && casType != "" {
return fmt.Errorf("no %s %s found in %s namespace", casType, resource, ns)
} else if casType != "" {
return fmt.Errorf("unknown cas-type %s", casType)
} else if casType != "" && !IsValidCasType(casType) {
return fmt.Errorf("cas-type %s not supported", casType)
} else {
return fmt.Errorf("no %s found in %s namespace", resource, ns)
}
Expand Down
26 changes: 21 additions & 5 deletions pkg/util/error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,9 @@ func TestHandleEmptyTableError(t *testing.T) {
casType string
}
tests := []struct {
name string
args args
name string
args args
expected error
}{
{
"No Namespace and cas",
Expand All @@ -75,14 +76,25 @@ func TestHandleEmptyTableError(t *testing.T) {
ns: "",
casType: "",
},
fmt.Errorf("no ResourceType found in your cluster"),
},
{
"Wrong cas or Namespace",
args{
resource: "ResourceType",
ns: "InValidNamespace",
ns: "InValid",
casType: "jiva",
},
fmt.Errorf("no jiva ResourceType found in InValid namespace"),
},
{
"",
args{
resource: "ResourceType",
ns: "invalid",
casType: "",
},
fmt.Errorf("no ResourceType found in invalid namespace"),
},
{
"Wrong cas type in all namespace",
Expand All @@ -91,19 +103,23 @@ func TestHandleEmptyTableError(t *testing.T) {
ns: "",
casType: "invalid",
},
fmt.Errorf("cas-type invalid not supported"),
},
{
"Wrong Namespace and all cas types",
args{
resource: "ResourceType",
ns: "InValidNamespace",
ns: "InValid",
casType: "",
},
fmt.Errorf("no ResourceType found in InValid namespace"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_ = HandleEmptyTableError(tt.args.resource, tt.args.ns, tt.args.casType)
if tResult := HandleEmptyTableError(tt.args.resource, tt.args.ns, tt.args.casType); tResult.Error() != tt.expected.Error() {
t.Errorf("HandleEmptyTableError(): expected: %s, got: %s", tt.expected, tResult)
}
})
}
}
5 changes: 2 additions & 3 deletions pkg/volume/volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,11 @@ func Get(vols []string, openebsNS, casType string) error {
}
}

// 3. Return Error or Print volumes from rows
if len(rows) == 0 {
return util.HandleEmptyTableError("Volume", openebsNS, casType)
} else {
// 3. Print the volumes from rows
util.TablePrinter(util.VolumeListColumnDefinations, rows, printers.PrintOptions{Wide: true})
}
util.TablePrinter(util.VolumeListColumnDefinations, rows, printers.PrintOptions{Wide: true})
return nil
}

Expand Down

0 comments on commit acf45c5

Please sign in to comment.