Skip to content

Commit e79ac87

Browse files
Merge pull request #56 from IBM/task/add-unitests-rest
add unitests for rest module
2 parents 0dc8b98 + 01fde82 commit e79ac87

4 files changed

Lines changed: 153 additions & 7 deletions

File tree

pkg/managers/managers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func GetManagers(namespace string, currentSystems map[string]*rest.FSRestClient)
8888
continue
8989
}
9090

91-
restClient, restErr := rest.NewFSRestClient(restConfig, &mgr)
91+
restClient, restErr := newSystems[fscName].NewFSRestClient(restConfig, &mgr)
9292
if err = CheckRestClientState(restClient, mgr, restErr); err != nil {
9393
continue
9494
}

pkg/rest/client.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func NewRequester(p Poster) *Requester {
6767
return &Requester{poster: p}
6868
}
6969

70-
func NewFSRestClient(config Config, driverManager *drivermanager.DriverManager) (*FSRestClient, error) {
70+
func (c *FSRestClient) NewFSRestClient(config Config, driverManager *drivermanager.DriverManager) (*FSRestClient, error) {
7171
tr := &http.Transport{
7272
// #nosec
7373
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
@@ -83,7 +83,7 @@ func NewFSRestClient(config Config, driverManager *drivermanager.DriverManager)
8383
Transport: tr,
8484
}
8585

86-
c := &FSRestClient{
86+
cl := &FSRestClient{
8787
Client: client,
8888
BaseURL: fmt.Sprintf("https://%s:7443/rest", config.Host),
8989
RestConfig: config,
@@ -92,11 +92,11 @@ func NewFSRestClient(config Config, driverManager *drivermanager.DriverManager)
9292
PostRequester: NewRequester(doRequest),
9393
}
9494

95-
if err := c.authenticate(); err != nil {
95+
if err := cl.authenticate(); err != nil {
9696
return nil, err
9797
}
9898

99-
return c, nil
99+
return cl, nil
100100
}
101101

102102
type authenResult map[string]interface{}

pkg/rest/systemcheck.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func (c *FSRestClient) CheckUserRole() (bool, error) {
6868
return false, nil
6969
}
7070

71-
func isHealth(status string) bool {
71+
func (c *FSRestClient) isHealth(status string) bool {
7272
switch status {
7373
case "starting", "service", "pending", "offline", "flushing", "deleting", "adding":
7474
return false
@@ -84,7 +84,7 @@ func (c *FSRestClient) CheckFlashsystemClusterState() (bool, error) {
8484

8585
iogrps := map[string]int{}
8686
for _, node := range nodes {
87-
if !isHealth(node["status"]) {
87+
if !c.isHealth(node["status"]) {
8888
log.Infof("The node %s id %s status %s is unhealthy.", node["name"], node["id"], node["status"])
8989
return false, nil
9090
}

pkg/rest/systemcheck_test.go

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package rest
1818

1919
import (
20+
drivermanager "github.com/IBM/ibm-storage-odf-block-driver/pkg/driver"
2021
"net/http"
2122
"testing"
2223
)
@@ -27,6 +28,12 @@ var c = FSRestClient{PostRequester: &Requester{
2728
return []byte(body), 200, nil
2829
},
2930
}}
31+
var manager1 = drivermanager.DriverManager{SystemName: "FS-system-name"}
32+
var config1 = Config{
33+
Host: "FS-Host",
34+
Username: "FS-Username",
35+
Password: "FS-Password",
36+
}
3037

3138
func TestNormalizeVersion(t *testing.T) {
3239

@@ -49,6 +56,24 @@ func TestNormalizeVersion(t *testing.T) {
4956
})
5057
}
5158

59+
func TestIsHealth(t *testing.T) {
60+
// Happy path
61+
t.Run("health check state", func(t *testing.T) {
62+
res := c.isHealth("completed")
63+
if res != true {
64+
t.Errorf("Health check should return true.")
65+
}
66+
})
67+
68+
// Unhappy path
69+
t.Run("health check state", func(t *testing.T) {
70+
res := c.isHealth("pending")
71+
if res != false {
72+
t.Errorf("Health check should return false.")
73+
}
74+
})
75+
}
76+
5277
func TestUserRole(t *testing.T) {
5378

5479
t.Run("Check User Administrator", func(t *testing.T) {
@@ -115,3 +140,124 @@ func TestCheckFlashsystemClusterState(t *testing.T) {
115140
}
116141
})
117142
}
143+
144+
func TestLssystem(t *testing.T) {
145+
// Happy path
146+
t.Run("run successful lssystem", func(t *testing.T) {
147+
body = `{"id": "0000020420E0E8DC", "name": "fab3p-159-c", "location": "local"}`
148+
_, err := c.Lssystem()
149+
if err != nil {
150+
t.Errorf("lssystem check should return without error")
151+
}
152+
})
153+
154+
// unhappy path
155+
t.Run("run failed lssystem", func(t *testing.T) {
156+
body = ``
157+
_, err := c.Lssystem()
158+
if err == nil {
159+
t.Errorf("lssystem check should return error ")
160+
}
161+
})
162+
}
163+
164+
func TestLsnode(t *testing.T) {
165+
// Happy path
166+
t.Run("run successful lsnode", func(t *testing.T) {
167+
body = `[{"name":"node1", "id":"1", "status":"online", "IO_group_name":"io_grp0"}]`
168+
_, err := c.Lsnode()
169+
if err != nil {
170+
t.Errorf("lsnode check should return without error")
171+
}
172+
})
173+
174+
// unhappy path
175+
t.Run("run failed lsnode", func(t *testing.T) {
176+
body = ``
177+
_, err := c.Lsnode()
178+
if err == nil {
179+
t.Errorf("lsnode check should return error")
180+
}
181+
})
182+
}
183+
184+
func TestLssystemstats(t *testing.T) {
185+
// Happy path
186+
t.Run("run successful Lssystemstats", func(t *testing.T) {
187+
body = `[{"stat_name": "vdisk_r_mb", "stat_current": "5", "stat_peak": "0" ,"stat_peak_time": "210604162102"}]`
188+
_, err := c.Lssystemstats()
189+
if err != nil {
190+
t.Errorf("Lssystemstats check should return without error")
191+
}
192+
})
193+
194+
// unhappy path
195+
t.Run("run failed Lssystemstats", func(t *testing.T) {
196+
body = ``
197+
_, err := c.Lssystemstats()
198+
if err == nil {
199+
t.Errorf("Lssystemstats check should return error")
200+
}
201+
})
202+
}
203+
204+
func TestLscurrentuser(t *testing.T) {
205+
// Happy path
206+
t.Run("run successful Lscurrentuser", func(t *testing.T) {
207+
body = `[{"name": "superuser", "role": "Administrator"}]`
208+
_, err := c.Lscurrentuser()
209+
if err != nil {
210+
t.Errorf("Lscurrentuser check should return without error")
211+
}
212+
})
213+
214+
// unhappy path
215+
t.Run("run failed Lscurrentuser", func(t *testing.T) {
216+
body = ``
217+
_, err := c.Lscurrentuser()
218+
if err == nil {
219+
t.Errorf("Lscurrentuser check should return error")
220+
}
221+
})
222+
}
223+
224+
func TestLsmdiskgrp(t *testing.T) {
225+
// Happy path
226+
t.Run("run successful Lsmdiskgrp", func(t *testing.T) {
227+
body = `[{"id": "0", "name": "Pool0", "status": "online"}]`
228+
_, err := c.Lsmdiskgrp()
229+
if err != nil {
230+
t.Errorf("Lsmdiskgrp check should return without error")
231+
}
232+
})
233+
234+
// unhappy path
235+
t.Run("run failed Lsmdiskgrp", func(t *testing.T) {
236+
body = ``
237+
_, err := c.Lsmdiskgrp()
238+
if err == nil {
239+
t.Errorf("Lsmdiskgrp check should return error")
240+
}
241+
})
242+
}
243+
244+
func TestNewFSRestClient(t *testing.T) {
245+
// unHappy path
246+
t.Run("run successful NewFSRestClient", func(t *testing.T) {
247+
_, err := c.NewFSRestClient(config1, &manager1)
248+
if err == nil {
249+
t.Errorf("NewFSRestClient check should return with error")
250+
}
251+
})
252+
}
253+
254+
func TestRetryDo(t *testing.T) {
255+
// Happy path
256+
t.Run("run successful retryDo", func(t *testing.T) {
257+
body = `{"id": "0000020420E0E8DC", "name": "fab3p-159-c", "location": "local"}`
258+
_, err := c.retryDo("https://my-url", "lssystem")
259+
if err != nil {
260+
t.Errorf("retryDo check should return without error")
261+
}
262+
})
263+
}

0 commit comments

Comments
 (0)