Skip to content

Commit 16291cf

Browse files
committed
refactor: updated test implementations
Signed-off-by: David J. Allen <davidallendj@gmail.com>
1 parent 26e32a4 commit 16291cf

6 files changed

Lines changed: 253 additions & 125 deletions

File tree

pkg/collect_test.go

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"net/http/httptest"
66
"testing"
77

8+
"github.com/OpenCHAMI/magellan/internal/format"
89
"github.com/stretchr/testify/assert"
910
)
1011

@@ -32,22 +33,41 @@ func TestCollect(t *testing.T) {
3233
{
3334
name: "basic",
3435
assets: []RemoteAsset{
35-
RemoteAsset{
36-
Host: "",
37-
Port: 443,
36+
{
37+
Host: "",
38+
Port: 443,
39+
State: true,
40+
Protocol: "tcp",
41+
ServiceType: BMC,
3842
},
3943
},
4044
params: &CollectParams{
4145
Concurrency: 1,
4246
Timeout: timeout,
47+
Insecure: true,
48+
Format: format.FORMAT_JSON,
49+
SecretStore: nil,
4350
},
4451
want: 0,
4552
},
4653
{
4754
name: "",
55+
assets: []RemoteAsset{
56+
{
57+
Host: "",
58+
Port: 443,
59+
State: true,
60+
Protocol: "tcp",
61+
ServiceType: BMC,
62+
},
63+
},
64+
params: &CollectParams{},
4865
},
4966
{
5067
name: "",
68+
assets: []RemoteAsset{
69+
{},
70+
},
5171
},
5272
}
5373

pkg/crawl_test.go

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,32 @@
11
package magellan
22

33
import (
4+
"net/http/httptest"
45
"testing"
56

67
"github.com/OpenCHAMI/magellan/pkg/crawler"
8+
"github.com/OpenCHAMI/magellan/pkg/secrets"
9+
"github.com/OpenCHAMI/magellan/pkg/test"
10+
"github.com/go-chi/chi/v5"
711
)
812

913
type CrawlTestClient struct {
1014
config *crawler.CrawlerConfig
1115
}
1216

1317
func NewCrawlTestClient() *CrawlTestClient {
14-
return &CrawlTestClient{}
18+
store := secrets.NewStaticStore("test", "test")
19+
return &CrawlTestClient{
20+
config: &crawler.CrawlerConfig{
21+
URI: "http://localhost:5000",
22+
Insecure: true,
23+
CredentialStore: store,
24+
},
25+
}
1526
}
1627

1728
func TestCrawl(t *testing.T) {
1829
t.Parallel()
19-
// TODO: initialize secret store for test case
2030

2131
cases := []struct {
2232
name string
@@ -25,34 +35,33 @@ func TestCrawl(t *testing.T) {
2535
{
2636
name: "basic",
2737
config: &crawler.CrawlerConfig{
28-
URI: "",
29-
Insecure: true,
30-
CredentialStore: nil,
31-
UseDefault: true,
32-
},
33-
},
34-
{
35-
name: "secrets",
36-
config: &crawler.CrawlerConfig{
37-
URI: "",
38-
Insecure: true,
39-
CredentialStore: nil,
40-
UseDefault: true,
38+
UseDefault: true,
4139
},
4240
},
4341
{
4442
name: "no_default",
4543
config: &crawler.CrawlerConfig{
46-
URI: "",
47-
Insecure: true,
48-
CredentialStore: nil,
49-
UseDefault: false,
44+
UseDefault: false,
5045
},
5146
},
5247
}
5348

5449
for _, tc := range cases {
55-
c := NewCrawlTestClient()
56-
c.config = tc.config
50+
t.Run(tc.name, func(t *testing.T) {
51+
mux := chi.NewMux()
52+
mux.HandleFunc("/redfish/v1", test.Make(test.RESPONSE_ServiceRoot))
53+
mux.HandleFunc("/redfish/v1/Systems", test.Make(test.RESPONSE_Systems))
54+
mux.HandleFunc("/redfish/v1/Node0/EthernetInterfaces", test.Make(test.RESPONSE_EthernetInterface))
55+
56+
// create a mock server to simulator a Redfish service
57+
mockServer := httptest.NewServer(mux)
58+
defer mockServer.Close()
59+
60+
c := NewCrawlTestClient()
61+
c.config = tc.config
62+
63+
crawler.CrawlBMCForSystems(*c.config)
64+
crawler.CrawlBMCForManagers(*c.config)
65+
})
5766
}
5867
}

pkg/scan.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,20 @@ type RemoteAsset struct {
2222
Protocol string `json:"protocol"`
2323
State bool `json:"state"`
2424
Timestamp time.Time `json:"timestamp"`
25-
ServiceType string `json:"service_type,omitempty"`
25+
ServiceType Scanner `json:"service_type,omitempty"`
2626
}
2727

28-
type ScanType int
28+
type Scanner string
2929

3030
const (
31-
BMC ScanType = iota
32-
PDU
31+
BMC Scanner = "bmcs"
32+
PDU Scanner = "pdus"
3333
)
3434

35+
func (st Scanner) String() string {
36+
return string(st)
37+
}
38+
3539
func (ra *RemoteAsset) String() string {
3640
return fmt.Sprintf("%v %s %s %s",
3741
ra.Timestamp,
@@ -140,7 +144,7 @@ func ScanForAssets(params *ScanParams) []RemoteAsset {
140144
Str("url", probeURL).
141145
Msg("could not close response resource")
142146
}
143-
foundAsset.ServiceType = probe.Type
147+
foundAsset.ServiceType = Scanner(probe.Type)
144148
assetsToAdd = append(assetsToAdd, foundAsset)
145149
log.Debug().
146150
Str("host", foundAsset.Host).

pkg/scan_test.go

Lines changed: 41 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -9,61 +9,14 @@ import (
99
"strconv"
1010
"testing"
1111

12+
"github.com/OpenCHAMI/magellan/pkg/test"
1213
"github.com/stretchr/testify/assert"
1314
)
1415

1516
const (
16-
defaultBaseURI = "https://bmc.openchami.cluster"
17-
scheme = "https"
18-
protocol = "tcp"
19-
timeout = 10
20-
serviceRootResponse = `{
21-
"@odata.etag": "W/\"1646860561\"",
22-
"@odata.id": "/redfish/v1/",
23-
"@odata.type": "#ServiceRoot.v1_2_0.ServiceRoot",
24-
"AccountService": {
25-
"@odata.id": "/redfish/v1/AccountService"
26-
},
27-
"CertificateService": {
28-
"@odata.id": "/redfish/v1/CertificateService"
29-
},
30-
"Chassis": {
31-
"@odata.id": "/redfish/v1/Chassis"
32-
},
33-
"Description": "The service root for all Redfish requests on this host",
34-
"EventService": {
35-
"@odata.id": "/redfish/v1/EventService"
36-
},
37-
"Id": "RootService",
38-
"JsonSchemas": {
39-
"@odata.id": "/redfish/v1/JsonSchemas"
40-
},
41-
"Links": {
42-
"Sessions": {
43-
"@odata.id": "/redfish/v1/SessionService/Sessions"
44-
}
45-
},
46-
"Managers": {
47-
"@odata.id": "/redfish/v1/Managers"
48-
},
49-
"Name": "Root Service",
50-
"RedfishVersion": "1.2.0",
51-
"Registries": {
52-
"@odata.id": "/redfish/v1/Registries"
53-
},
54-
"SessionService": {
55-
"@odata.id": "/redfish/v1/SessionService"
56-
},
57-
"Systems": {
58-
"@odata.id": "/redfish/v1/Systems"
59-
},
60-
"Tasks": {
61-
"@odata.id": "/redfish/v1/TaskService"
62-
},
63-
"UpdateService": {
64-
"@odata.id": "/redfish/v1/UpdateService"
65-
}
66-
}`
17+
scheme = "https"
18+
protocol = "tcp"
19+
timeout = 10
6720
)
6821

6922
type ScanTestClient struct {
@@ -153,6 +106,7 @@ func TestScan(t *testing.T) {
153106

154107
for _, tc := range cases {
155108
t.Run(tc.name, func(t *testing.T) {
109+
156110
// Create a mock server
157111
var servers []*httptest.Server
158112
for range tc.wantFound {
@@ -164,7 +118,7 @@ func TestScan(t *testing.T) {
164118
t.Fatalf("Expected GET request, got: %s", r.Method)
165119
}
166120
w.WriteHeader(http.StatusOK)
167-
w.Write([]byte(serviceRootResponse))
121+
w.Write([]byte(test.RESPONSE_ServiceRoot))
168122
}))
169123
defer mockServer.Close() // Close the server when the test finishes
170124
servers = append(servers, mockServer)
@@ -185,9 +139,7 @@ func TestScan(t *testing.T) {
185139

186140
assert.Len(t, found, tc.wantFound)
187141
})
188-
189142
}
190-
191143
}
192144

193145
func TestGenerateHostsFromSubnet(t *testing.T) {
@@ -217,60 +169,53 @@ func TestGenerateHostsFromSubnet(t *testing.T) {
217169

218170
cases := []TestCase{
219171
{
220-
name: "basic",
221-
subnet: "172.21.0.0",
222-
subnetMask: &defaultSubnetMask,
223-
ports: defaultPorts,
224-
scheme: scheme,
225-
wantTotalHosts: 0,
172+
name: "basic",
173+
subnet: "172.21.0.0",
174+
subnetMask: &defaultSubnetMask,
175+
ports: defaultPorts,
176+
scheme: scheme,
226177
},
227178
{
228-
name: "none",
229-
subnet: "10.0.0.0",
230-
subnetMask: &defaultSubnetMask,
231-
ports: defaultPorts,
232-
scheme: scheme,
233-
wantTotalHosts: 0,
179+
name: "none",
180+
subnet: "10.0.0.0",
181+
subnetMask: &defaultSubnetMask,
182+
ports: defaultPorts,
183+
scheme: scheme,
234184
},
235185
{
236-
name: "invalid subnet",
237-
subnet: "invalid",
238-
subnetMask: &defaultSubnetMask,
239-
ports: defaultPorts,
240-
scheme: scheme,
241-
wantTotalHosts: 0,
186+
name: "invalid subnet",
187+
subnet: "invalid",
188+
subnetMask: &defaultSubnetMask,
189+
ports: defaultPorts,
190+
scheme: scheme,
242191
},
243192
{
244-
name: "no subnet",
245-
subnet: "",
246-
subnetMask: &defaultSubnetMask,
247-
ports: defaultPorts,
248-
scheme: scheme,
249-
wantTotalHosts: 0,
193+
name: "no subnet",
194+
subnet: "",
195+
subnetMask: &defaultSubnetMask,
196+
ports: defaultPorts,
197+
scheme: scheme,
250198
},
251199
{
252-
name: "cidr",
253-
subnet: "172.21.0.0/24",
254-
subnetMask: &defaultSubnetMask,
255-
ports: defaultPorts,
256-
scheme: scheme,
257-
wantTotalHosts: 0,
200+
name: "cidr",
201+
subnet: "172.21.0.0/24",
202+
subnetMask: &defaultSubnetMask,
203+
ports: defaultPorts,
204+
scheme: scheme,
258205
},
259206
{
260-
name: "additional ports",
261-
subnet: "172.21.0.0/24",
262-
subnetMask: &defaultSubnetMask,
263-
ports: []int{443, 5000},
264-
scheme: scheme,
265-
wantTotalHosts: 0,
207+
name: "additional ports",
208+
subnet: "172.21.0.0/24",
209+
subnetMask: &defaultSubnetMask,
210+
ports: []int{443, 5000},
211+
scheme: scheme,
266212
},
267213
{
268-
name: "different scheme",
269-
subnet: "172.21.0.0/24",
270-
subnetMask: &defaultSubnetMask,
271-
ports: defaultPorts,
272-
scheme: "http",
273-
wantTotalHosts: 0,
214+
name: "different scheme",
215+
subnet: "172.21.0.0/24",
216+
subnetMask: &defaultSubnetMask,
217+
ports: defaultPorts,
218+
scheme: "http",
274219
},
275220
}
276221

0 commit comments

Comments
 (0)