Skip to content

Commit cea44b0

Browse files
adding tests
1 parent 6b0156e commit cea44b0

File tree

6 files changed

+348
-2
lines changed

6 files changed

+348
-2
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package collectors
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"net/http"
7+
"net/http/httptest"
8+
"os"
9+
"regexp"
10+
"strconv"
11+
"strings"
12+
"testing"
13+
14+
client "purestorage/fa-openmetrics-exporter/internal/rest-client"
15+
)
16+
17+
func TestNetworkInterfacesCollectorTest(t *testing.T) {
18+
19+
rhw, _ := os.ReadFile("../../test/data/network_interfaces.json")
20+
vers, _ := os.ReadFile("../../test/data/versions.json")
21+
var hwl client.NetworkInterfacesList
22+
json.Unmarshal(rhw, &hwl)
23+
server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
24+
url := regexp.MustCompile(`^/api/([0-9]+.[0-9]+)?/network-interfaces$`)
25+
if r.URL.Path == "/api/api_version" {
26+
w.Header().Set("Content-Type", "application/json")
27+
w.WriteHeader(http.StatusOK)
28+
w.Write([]byte(vers))
29+
} else if url.MatchString(r.URL.Path) {
30+
w.Header().Set("x-auth-token", "faketoken")
31+
w.Header().Set("Content-Type", "application/json")
32+
w.WriteHeader(http.StatusOK)
33+
w.Write([]byte(rhw))
34+
}
35+
}))
36+
endp := strings.Split(server.URL, "/")
37+
e := endp[len(endp)-1]
38+
want := make(map[string]bool)
39+
for _, h := range hwl.Items {
40+
want[fmt.Sprintf("label:{name:\"enabled\" value:\"%s\"} label:{name:\"ethsubtype\" value:\"%s\"} label:{name:\"name\" value:\"%s\"} label:{name:\"services\" value:\"%s\"} label:{name:\"type\" value:\"%s\"} gauge:{value:%g}", strconv.FormatBool(h.Enabled), h.Eth.Subtype, h.Name, strings.Join(h.Services, ", "), h.InterfaceType, float64(h.Speed))] = true
41+
}
42+
c := client.NewRestClient(e, "fake-api-token", "latest", false)
43+
nc := NewNetworkInterfacesCollector(c)
44+
metricsCheck(t, nc, want)
45+
server.Close()
46+
}

internal/openmetrics-exporter/network_interfaces_collector.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func (c *NetworkInterfacesCollector) Collect(ch chan<- prometheus.Metric) {
2727
c.NetworkInterfaceInfoDesc,
2828
prometheus.GaugeValue,
2929
float64(h.Speed),
30-
h.Name, strconv.FormatBool(h.Enabled), strings.Join(h.Services, ", "), h.InterfaceType, h.Eth.Subtype,
30+
strconv.FormatBool(h.Enabled), h.Eth.Subtype, h.Name, strings.Join(h.Services, ", "), h.InterfaceType,
3131
)
3232
}
3333
}
@@ -37,7 +37,7 @@ func NewNetworkInterfacesCollector(fa *client.FAClient) *NetworkInterfacesCollec
3737
NetworkInterfaceInfoDesc: prometheus.NewDesc(
3838
"purefa_network_interface_speed_bandwidth_bytes",
3939
"FlashArray network interface speed in bytes per second",
40-
[]string{"name", "enabled", "services", "type", "ethsubtype"},
40+
[]string{"enabled", "ethsubtype", "name", "services", "type"},
4141
prometheus.Labels{},
4242
),
4343
Client: fa,
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package collectors
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"net/http"
7+
"net/http/httptest"
8+
"os"
9+
"regexp"
10+
"strings"
11+
"testing"
12+
13+
client "purestorage/fa-openmetrics-exporter/internal/rest-client"
14+
)
15+
16+
func TestPortsCollector(t *testing.T) {
17+
18+
rhw, _ := os.ReadFile("../../test/data/ports.json")
19+
vers, _ := os.ReadFile("../../test/data/versions.json")
20+
var hwl client.PortsList
21+
json.Unmarshal(rhw, &hwl)
22+
server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
23+
url := regexp.MustCompile(`^/api/([0-9]+.[0-9]+)?/ports$`)
24+
if r.URL.Path == "/api/api_version" {
25+
w.Header().Set("Content-Type", "application/json")
26+
w.WriteHeader(http.StatusOK)
27+
w.Write([]byte(vers))
28+
} else if url.MatchString(r.URL.Path) {
29+
w.Header().Set("x-auth-token", "faketoken")
30+
w.Header().Set("Content-Type", "application/json")
31+
w.WriteHeader(http.StatusOK)
32+
w.Write([]byte(rhw))
33+
}
34+
}))
35+
endp := strings.Split(server.URL, "/")
36+
e := endp[len(endp)-1]
37+
want := make(map[string]bool)
38+
for _, h := range hwl.Items {
39+
want[fmt.Sprintf("label:{name:\"iqn\" value:\"%s\"} label:{name:\"name\" value:\"%s\"} label:{name:\"nqn\" value:\"%s\"} label:{name:\"portal\" value:\"%s\"} label:{name:\"wwn\" value:\"%s\"} gauge:{value:1}", h.Iqn, h.Name, h.Nqn, h.Portal, h.Wwn)] = true
40+
}
41+
c := client.NewRestClient(e, "fake-api-token", "latest", false)
42+
pc := NewPortsCollector(c)
43+
metricsCheck(t, pc, want)
44+
server.Close()
45+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package client
2+
3+
import (
4+
"encoding/json"
5+
"net/http"
6+
"net/http/httptest"
7+
"os"
8+
"regexp"
9+
"strings"
10+
"testing"
11+
12+
"github.com/google/go-cmp/cmp"
13+
)
14+
15+
func TestNetworkInterfaces(t *testing.T) {
16+
17+
res, _ := os.ReadFile("../../test/data/network_interfaces.json")
18+
vers, _ := os.ReadFile("../../test/data/versions.json")
19+
var nw NetworkInterfacesList
20+
json.Unmarshal(res, &nw)
21+
server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
22+
valid := regexp.MustCompile(`^/api/([0-9]+.[0-9]+)?/network-interfaces$`)
23+
if r.URL.Path == "/api/api_version" {
24+
w.Header().Set("Content-Type", "application/json")
25+
w.WriteHeader(http.StatusOK)
26+
w.Write([]byte(vers))
27+
} else if valid.MatchString(r.URL.Path) {
28+
w.Header().Set("x-auth-token", "faketoken")
29+
w.Header().Set("Content-Type", "application/json")
30+
w.WriteHeader(http.StatusOK)
31+
w.Write([]byte(res))
32+
}
33+
}))
34+
endp := strings.Split(server.URL, "/")
35+
e := endp[len(endp)-1]
36+
t.Run("network_interfaces_1", func(t *testing.T) {
37+
defer server.Close()
38+
c := NewRestClient(e, "fake-api-token", "latest", false)
39+
nl := c.GetNetworkInterfaces()
40+
if diff := cmp.Diff(nl.Items, nw.Items); diff != "" {
41+
t.Errorf("Mismatch (-want +got):\n%s", diff)
42+
}
43+
})
44+
}

internal/rest-client/ports_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package client
2+
3+
import (
4+
"encoding/json"
5+
"net/http"
6+
"net/http/httptest"
7+
"os"
8+
"regexp"
9+
"strings"
10+
"testing"
11+
12+
"github.com/google/go-cmp/cmp"
13+
)
14+
15+
func TestPorts(t *testing.T) {
16+
17+
res, _ := os.ReadFile("../../test/data/ports.json")
18+
vers, _ := os.ReadFile("../../test/data/versions.json")
19+
var ports PortsList
20+
json.Unmarshal(res, &ports)
21+
server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
22+
valid := regexp.MustCompile(`^/api/([0-9]+.[0-9]+)?/ports$`)
23+
if r.URL.Path == "/api/api_version" {
24+
w.Header().Set("Content-Type", "application/json")
25+
w.WriteHeader(http.StatusOK)
26+
w.Write([]byte(vers))
27+
} else if valid.MatchString(r.URL.Path) {
28+
w.Header().Set("x-auth-token", "faketoken")
29+
w.Header().Set("Content-Type", "application/json")
30+
w.WriteHeader(http.StatusOK)
31+
w.Write([]byte(res))
32+
}
33+
}))
34+
endp := strings.Split(server.URL, "/")
35+
e := endp[len(endp)-1]
36+
t.Run("ports_1", func(t *testing.T) {
37+
defer server.Close()
38+
c := NewRestClient(e, "fake-api-token", "latest", false)
39+
pl := c.GetPorts()
40+
if diff := cmp.Diff(pl.Items, ports.Items); diff != "" {
41+
t.Errorf("Mismatch (-want +got):\n%s", diff)
42+
}
43+
})
44+
}

test/data/ports.json

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
{
2+
"continuation_token": null,
3+
"items": [
4+
{
5+
"wwn": null,
6+
"name": "CT0.ETH14",
7+
"nqn": "nqn.2010-06.com.purestorage:flasharray.7b1b42b9580af8aa",
8+
"iqn": null,
9+
"portal": "192.168.27.140:4420",
10+
"failover": null
11+
},
12+
{
13+
"wwn": null,
14+
"name": "CT0.ETH15",
15+
"nqn": "nqn.2010-06.com.purestorage:flasharray.7b1b42b9580af8aa",
16+
"iqn": null,
17+
"portal": "192.168.27.141:4420",
18+
"failover": null
19+
},
20+
{
21+
"wwn": null,
22+
"name": "CT0.ETH4",
23+
"nqn": null,
24+
"iqn": "iqn.2010-06.com.purestorage:flasharray.7b1b42b9580af8aa",
25+
"portal": "192.168.29.133:3260",
26+
"failover": null
27+
},
28+
{
29+
"wwn": null,
30+
"name": "CT0.ETH5",
31+
"nqn": null,
32+
"iqn": "iqn.2010-06.com.purestorage:flasharray.7b1b42b9580af8aa",
33+
"portal": "192.168.29.134:3260",
34+
"failover": null
35+
},
36+
{
37+
"wwn": "52:4A:93:71:AF:4E:82:00",
38+
"name": "CT0.FC0",
39+
"nqn": null,
40+
"iqn": null,
41+
"portal": null,
42+
"failover": null
43+
},
44+
{
45+
"wwn": "52:4A:93:71:AF:4E:82:01",
46+
"name": "CT0.FC1",
47+
"nqn": null,
48+
"iqn": null,
49+
"portal": null,
50+
"failover": null
51+
},
52+
{
53+
"wwn": "52:4A:93:71:AF:4E:82:02",
54+
"name": "CT0.FC2",
55+
"nqn": null,
56+
"iqn": null,
57+
"portal": null,
58+
"failover": null
59+
},
60+
{
61+
"wwn": "52:4A:93:71:AF:4E:82:03",
62+
"name": "CT0.FC3",
63+
"nqn": null,
64+
"iqn": null,
65+
"portal": null,
66+
"failover": null
67+
},
68+
{
69+
"wwn": "52:4A:93:71:AF:4E:82:08",
70+
"name": "CT0.FC8",
71+
"nqn": null,
72+
"iqn": null,
73+
"portal": null,
74+
"failover": null
75+
},
76+
{
77+
"wwn": "52:4A:93:71:AF:4E:82:09",
78+
"name": "CT0.FC9",
79+
"nqn": null,
80+
"iqn": null,
81+
"portal": null,
82+
"failover": null
83+
},
84+
{
85+
"wwn": null,
86+
"name": "CT1.ETH14",
87+
"nqn": "nqn.2010-06.com.purestorage:flasharray.7b1b42b9580af8aa",
88+
"iqn": null,
89+
"portal": "192.168.27.142:4420",
90+
"failover": null
91+
},
92+
{
93+
"wwn": null,
94+
"name": "CT1.ETH15",
95+
"nqn": "nqn.2010-06.com.purestorage:flasharray.7b1b42b9580af8aa",
96+
"iqn": null,
97+
"portal": "192.168.27.143:4420",
98+
"failover": null
99+
},
100+
{
101+
"wwn": null,
102+
"name": "CT1.ETH4",
103+
"nqn": null,
104+
"iqn": "iqn.2010-06.com.purestorage:flasharray.7b1b42b9580af8aa",
105+
"portal": "192.168.29.135:3260",
106+
"failover": null
107+
},
108+
{
109+
"wwn": null,
110+
"name": "CT1.ETH5",
111+
"nqn": null,
112+
"iqn": "iqn.2010-06.com.purestorage:flasharray.7b1b42b9580af8aa",
113+
"portal": "192.168.29.136:3260",
114+
"failover": null
115+
},
116+
{
117+
"wwn": "52:4A:93:71:AF:4E:82:10",
118+
"name": "CT1.FC0",
119+
"nqn": null,
120+
"iqn": null,
121+
"portal": null,
122+
"failover": null
123+
},
124+
{
125+
"wwn": "52:4A:93:71:AF:4E:82:11",
126+
"name": "CT1.FC1",
127+
"nqn": null,
128+
"iqn": null,
129+
"portal": null,
130+
"failover": null
131+
},
132+
{
133+
"wwn": "52:4A:93:71:AF:4E:82:12",
134+
"name": "CT1.FC2",
135+
"nqn": null,
136+
"iqn": null,
137+
"portal": null,
138+
"failover": null
139+
},
140+
{
141+
"wwn": "52:4A:93:71:AF:4E:82:13",
142+
"name": "CT1.FC3",
143+
"nqn": null,
144+
"iqn": null,
145+
"portal": null,
146+
"failover": null
147+
},
148+
{
149+
"wwn": "52:4A:93:71:AF:4E:82:18",
150+
"name": "CT1.FC8",
151+
"nqn": null,
152+
"iqn": null,
153+
"portal": null,
154+
"failover": null
155+
},
156+
{
157+
"wwn": "52:4A:93:71:AF:4E:82:19",
158+
"name": "CT1.FC9",
159+
"nqn": null,
160+
"iqn": null,
161+
"portal": null,
162+
"failover": null
163+
}
164+
],
165+
"more_items_remaining": false,
166+
"total_item_count": null
167+
}

0 commit comments

Comments
 (0)