Skip to content

Commit 7c26ad5

Browse files
fix(golangci-lint): Fix errors caught on golangci-lint, add testdata (#52)
* Add unit test for Jiva Signed-off-by: Abhinandan-Purkait <[email protected]>
1 parent e2af1c0 commit 7c26ad5

File tree

8 files changed

+307
-81
lines changed

8 files changed

+307
-81
lines changed

pkg/blockdevice/blockdevice.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,7 @@ func createTreeByNode(k *client.K8sClient, bdNames []string) error {
5454
// 2. Create a map out of the list of bds, by their node names.
5555
var nodeBDlistMap = map[string][]v1alpha1.BlockDevice{}
5656
for _, bd := range bdList.Items {
57-
if _, ok := nodeBDlistMap[bd.Spec.NodeAttributes.NodeName]; ok {
58-
// Append to the node if nodeName exists
59-
nodeBDlistMap[bd.Spec.NodeAttributes.NodeName] = append(nodeBDlistMap[bd.Spec.NodeAttributes.NodeName], bd)
60-
} else {
61-
// Create new nodeName with node name and add the bd, if node does not exist
62-
nodeBDlistMap[bd.Spec.NodeAttributes.NodeName] = []v1alpha1.BlockDevice{bd}
63-
}
57+
nodeBDlistMap[bd.Spec.NodeAttributes.NodeName] = append(nodeBDlistMap[bd.Spec.NodeAttributes.NodeName], bd)
6458
}
6559
var rows []metav1.TableRow
6660
if len(nodeBDlistMap) == 0 {

pkg/blockdevice/blockdevice_test.go

Lines changed: 0 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -17,74 +17,12 @@ limitations under the License.
1717
package blockdevice
1818

1919
import (
20-
"github.com/openebs/api/v2/pkg/apis/openebs.io/v1alpha1"
2120
openebsFakeClientset "github.com/openebs/api/v2/pkg/client/clientset/versioned/fake"
2221
"github.com/openebs/openebsctl/pkg/client"
23-
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2422
"k8s.io/client-go/kubernetes/fake"
2523
"testing"
2624
)
2725

28-
var (
29-
bd1 = v1alpha1.BlockDevice{
30-
TypeMeta: metav1.TypeMeta{},
31-
ObjectMeta: metav1.ObjectMeta{Name: "some-fake-bd-1"},
32-
Spec: v1alpha1.DeviceSpec{
33-
Path: "/dev/sdb",
34-
Capacity: v1alpha1.DeviceCapacity{Storage: uint64(132131321)},
35-
FileSystem: v1alpha1.FileSystemInfo{
36-
Type: "zfs_member",
37-
Mountpoint: "/var/some-fake-point",
38-
},
39-
NodeAttributes: v1alpha1.NodeAttribute{
40-
NodeName: "fake-node-1",
41-
},
42-
},
43-
Status: v1alpha1.DeviceStatus{
44-
ClaimState: "Claimed",
45-
State: "Active",
46-
},
47-
}
48-
bd2 = v1alpha1.BlockDevice{
49-
TypeMeta: metav1.TypeMeta{},
50-
ObjectMeta: metav1.ObjectMeta{Name: "some-fake-bd-2"},
51-
Spec: v1alpha1.DeviceSpec{
52-
Path: "/dev/sdb",
53-
Capacity: v1alpha1.DeviceCapacity{Storage: uint64(132131321)},
54-
FileSystem: v1alpha1.FileSystemInfo{
55-
Type: "zfs_member",
56-
Mountpoint: "/var/some-fake-point",
57-
},
58-
NodeAttributes: v1alpha1.NodeAttribute{
59-
NodeName: "fake-node-1",
60-
},
61-
},
62-
Status: v1alpha1.DeviceStatus{
63-
ClaimState: "Claimed",
64-
State: "Active",
65-
},
66-
}
67-
bd3 = v1alpha1.BlockDevice{
68-
TypeMeta: metav1.TypeMeta{},
69-
ObjectMeta: metav1.ObjectMeta{Name: "some-fake-bd-3", Namespace: "fake-ns"},
70-
Spec: v1alpha1.DeviceSpec{
71-
Path: "/dev/sdb",
72-
Capacity: v1alpha1.DeviceCapacity{Storage: uint64(132131321)},
73-
FileSystem: v1alpha1.FileSystemInfo{
74-
Type: "lvm_member",
75-
Mountpoint: "/var/some-fake-point",
76-
},
77-
NodeAttributes: v1alpha1.NodeAttribute{
78-
NodeName: "fake-node-2",
79-
},
80-
},
81-
Status: v1alpha1.DeviceStatus{
82-
ClaimState: "Claimed",
83-
State: "Active",
84-
},
85-
}
86-
)
87-
8826
func Test_createTreeByNode(t *testing.T) {
8927
k8sCS := fake.NewSimpleClientset()
9028
type args struct {

pkg/blockdevice/testdata_test.go

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
Copyright 2020-2021 The OpenEBS Authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package blockdevice
18+
19+
import (
20+
"github.com/openebs/api/v2/pkg/apis/openebs.io/v1alpha1"
21+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
22+
)
23+
24+
var (
25+
bd1 = v1alpha1.BlockDevice{
26+
TypeMeta: metav1.TypeMeta{},
27+
ObjectMeta: metav1.ObjectMeta{Name: "some-fake-bd-1"},
28+
Spec: v1alpha1.DeviceSpec{
29+
Path: "/dev/sdb",
30+
Capacity: v1alpha1.DeviceCapacity{Storage: uint64(132131321)},
31+
FileSystem: v1alpha1.FileSystemInfo{
32+
Type: "zfs_member",
33+
Mountpoint: "/var/some-fake-point",
34+
},
35+
NodeAttributes: v1alpha1.NodeAttribute{
36+
NodeName: "fake-node-1",
37+
},
38+
},
39+
Status: v1alpha1.DeviceStatus{
40+
ClaimState: "Claimed",
41+
State: "Active",
42+
},
43+
}
44+
bd2 = v1alpha1.BlockDevice{
45+
TypeMeta: metav1.TypeMeta{},
46+
ObjectMeta: metav1.ObjectMeta{Name: "some-fake-bd-2"},
47+
Spec: v1alpha1.DeviceSpec{
48+
Path: "/dev/sdb",
49+
Capacity: v1alpha1.DeviceCapacity{Storage: uint64(132131321)},
50+
FileSystem: v1alpha1.FileSystemInfo{
51+
Type: "zfs_member",
52+
Mountpoint: "/var/some-fake-point",
53+
},
54+
NodeAttributes: v1alpha1.NodeAttribute{
55+
NodeName: "fake-node-1",
56+
},
57+
},
58+
Status: v1alpha1.DeviceStatus{
59+
ClaimState: "Claimed",
60+
State: "Active",
61+
},
62+
}
63+
bd3 = v1alpha1.BlockDevice{
64+
TypeMeta: metav1.TypeMeta{},
65+
ObjectMeta: metav1.ObjectMeta{Name: "some-fake-bd-3", Namespace: "fake-ns"},
66+
Spec: v1alpha1.DeviceSpec{
67+
Path: "/dev/sdb",
68+
Capacity: v1alpha1.DeviceCapacity{Storage: uint64(132131321)},
69+
FileSystem: v1alpha1.FileSystemInfo{
70+
Type: "lvm_member",
71+
Mountpoint: "/var/some-fake-point",
72+
},
73+
NodeAttributes: v1alpha1.NodeAttribute{
74+
NodeName: "fake-node-2",
75+
},
76+
},
77+
Status: v1alpha1.DeviceStatus{
78+
ClaimState: "Claimed",
79+
State: "Active",
80+
},
81+
}
82+
)

pkg/client/k8s.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func NewK8sClient(ns string) (*K8sClient, error) {
8787
if err != nil {
8888
return nil, errors.Wrap(err, "failed to build OpenEBS clientset")
8989
}
90-
lv, err := getLVMclient(config)
90+
lv, _ := getLVMclient(config)
9191
return &K8sClient{
9292
Ns: ns,
9393
K8sCS: k8sCS,
@@ -222,7 +222,7 @@ func (k K8sClient) GetPVs(volNames []string, labelselector string) (*corev1.Pers
222222
volMap[vol.Name] = vol
223223
}
224224
var list []corev1.PersistentVolume
225-
if volNames == nil || len(volNames) == 0 {
225+
if len(volNames) == 0 {
226226
return pvs, nil
227227
}
228228
for _, name := range volNames {
@@ -256,7 +256,7 @@ func (k K8sClient) GetPVCs(namespace string, pvcNames []string, labelselector st
256256
if err != nil {
257257
return nil, err
258258
}
259-
if pvcNames == nil || len(pvcNames) == 0 {
259+
if len(pvcNames) == 0 {
260260
return pvcs, nil
261261
}
262262
pvcNamePVCmap := make(map[string]corev1.PersistentVolumeClaim)
@@ -296,7 +296,7 @@ func (k K8sClient) GetBDs(bdNames []string, labelselector string) (*v1alpha1.Blo
296296
if err != nil {
297297
return nil, errors.Wrapf(err, "Error while getting block device")
298298
}
299-
if bdNames == nil || len(bdNames) == 0 {
299+
if len(bdNames) == 0 {
300300
return bds, nil
301301
}
302302
bdNameBDmap := make(map[string]v1alpha1.BlockDevice)
@@ -340,7 +340,7 @@ func (k K8sClient) GetCVs(volNames []string, rType util.ReturnType, labelSelecto
340340
return nil, nil, errors.Wrapf(err, "Error while getting volumes")
341341
}
342342
var list []cstorv1.CStorVolume
343-
if volNames == nil || len(volNames) == 0 {
343+
if len(volNames) == 0 {
344344
list = cVols.Items
345345
} else {
346346
csMap := make(map[string]cstorv1.CStorVolume)
@@ -539,7 +539,7 @@ func (k K8sClient) GetCSPIs(cspiNames []string, labelselector string) (*cstorv1.
539539
if err != nil {
540540
return nil, errors.Wrapf(err, "Error while getting cspi")
541541
}
542-
if cspiNames == nil || len(cspiNames) == 0 {
542+
if len(cspiNames) == 0 {
543543
return cspi, nil
544544
}
545545
poolMap := make(map[string]cstorv1.CStorPoolInstance)
@@ -590,7 +590,7 @@ func (k K8sClient) GetJVs(volNames []string, rType util.ReturnType, labelSelecto
590590
return nil, nil, err
591591
}
592592
var list []jiva.JivaVolume
593-
if volNames == nil || len(volNames) == 0 {
593+
if len(volNames) == 0 {
594594
list = jvs.Items
595595
} else {
596596
jvsMap := make(map[string]jiva.JivaVolume)
@@ -653,7 +653,7 @@ func (k K8sClient) GetZFSVols(volNames []string, rType util.ReturnType, labelSel
653653
return nil, nil, err
654654
}
655655
var list []zfs.ZFSVolume
656-
if volNames == nil || len(volNames) == 0 {
656+
if len(volNames) == 0 {
657657
list = zvols.Items
658658
} else {
659659
zvsMap := make(map[string]zfs.ZFSVolume)

pkg/client/lvmlocalpv.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func (k K8sClient) GetLVMvol(lVols []string, rType util.ReturnType, labelSelecto
5050
return nil, nil, err
5151
}
5252
var list []lvm.LVMVolume
53-
if lVols == nil || len(lVols) == 0 {
53+
if len(lVols) == 0 {
5454
list = lvs.Items
5555
} else {
5656
lvsMap := make(map[string]lvm.LVMVolume)

pkg/persistentvolumeclaim/jiva.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func DescribeJivaVolumeClaim(c *client.K8sClient, pvc *corev1.PersistentVolumeCl
5959
CasType: util.JivaCasType,
6060
BoundVolume: pvc.Spec.VolumeName,
6161
StorageClassName: *pvc.Spec.StorageClassName,
62-
Size: util.ConvertToIBytes(jv.Spec.Capacity),
62+
Size: pvc.Spec.Resources.Requests.Storage().String(),
6363
}
6464
if jv != nil {
6565
jivaPvcInfo.AttachedToNode = jv.Labels["nodeID"]

pkg/util/util.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,8 @@ func TablePrinter(columns []metav1.TableColumnDefinition, rows []metav1.TableRow
100100
}
101101
out := bytes.NewBuffer([]byte{})
102102
printer := printers.NewTablePrinter(options)
103-
printer.PrintObj(table, out)
104-
fmt.Printf(out.String())
103+
_ = printer.PrintObj(table, out)
104+
fmt.Printf("%s",out.String())
105105
}
106106

107107
// TemplatePrinter uses cli-runtime TemplatePrinter to print by template without extra type

0 commit comments

Comments
 (0)