|
1 | 1 | package collectors |
2 | 2 |
|
3 | | - |
4 | 3 | import ( |
| 4 | + "encoding/json" |
5 | 5 | "fmt" |
6 | | - "testing" |
7 | | - "regexp" |
8 | | - "strings" |
9 | 6 | "net/http" |
10 | 7 | "net/http/httptest" |
11 | | - "encoding/json" |
12 | | - "os" |
| 8 | + "os" |
| 9 | + "regexp" |
| 10 | + "strings" |
| 11 | + "testing" |
13 | 12 |
|
14 | | - "purestorage/fa-openmetrics-exporter/internal/rest-client" |
| 13 | + client "purestorage/fa-openmetrics-exporter/internal/rest-client" |
15 | 14 | ) |
16 | 15 |
|
17 | 16 | func TestArraysCollector(t *testing.T) { |
18 | 17 |
|
19 | 18 | res, _ := os.ReadFile("../../test/data/arrays.json") |
| 19 | + ressub, _ := os.ReadFile("../../test/data/subscriptions.json") |
20 | 20 | vers, _ := os.ReadFile("../../test/data/versions.json") |
21 | | - var arrs client.ArraysList |
22 | | - json.Unmarshal(res, &arrs) |
| 21 | + var arrs client.ArraysList |
| 22 | + var subs client.SubscriptionsList |
| 23 | + json.Unmarshal(res, &arrs) |
| 24 | + json.Unmarshal(ressub, &subs) |
23 | 25 | server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
24 | | - valid := regexp.MustCompile(`^/api/([0-9]+.[0-9]+)?/arrays$`) |
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 valid.MatchString(r.URL.Path) { |
| 26 | + urlarr := regexp.MustCompile(`^/api/([0-9]+.[0-9]+)?/arrays$`) |
| 27 | + urlsubs := regexp.MustCompile(`^/api/([0-9]+.[0-9]+)?/subscriptions$`) |
| 28 | + if r.URL.Path == "/api/api_version" { |
| 29 | + w.Header().Set("Content-Type", "application/json") |
| 30 | + w.WriteHeader(http.StatusOK) |
| 31 | + w.Write([]byte(vers)) |
| 32 | + } else if urlarr.MatchString(r.URL.Path) { |
30 | 33 | w.Header().Set("x-auth-token", "faketoken") |
31 | 34 | w.Header().Set("Content-Type", "application/json") |
32 | 35 | w.WriteHeader(http.StatusOK) |
33 | 36 | w.Write([]byte(res)) |
| 37 | + } else if urlsubs.MatchString(r.URL.Path) { |
| 38 | + w.Header().Set("x-auth-token", "faketoken") |
| 39 | + w.Header().Set("Content-Type", "application/json") |
| 40 | + w.WriteHeader(http.StatusOK) |
| 41 | + w.Write([]byte(ressub)) |
34 | 42 | } |
35 | | - })) |
36 | | - endp := strings.Split(server.URL, "/") |
37 | | - e := endp[len(endp)-1] |
| 43 | + })) |
| 44 | + endp := strings.Split(server.URL, "/") |
| 45 | + e := endp[len(endp)-1] |
| 46 | + a := arrs.Items[0] |
| 47 | + s := subs.Items[0] |
38 | 48 | want := make(map[string]bool) |
39 | | - for _, a := range arrs.Items { |
40 | | - want[fmt.Sprintf("label:{name:\"array_name\" value:\"%s\"} label:{name:\"os\" value:\"%s\"} label:{name:\"system_id\" value:\"%s\"} label:{name:\"version\" value:\"%s\"} gauge:{value:1}", a.Name, a.Os, a.Id, a.Version)] = true |
41 | | - } |
42 | | - defer server.Close() |
| 49 | + |
| 50 | + want[fmt.Sprintf("label:{name:\"array_name\" value:\"%s\"} label:{name:\"os\" value:\"%s\"} label:{name:\"subscription_type\" value:\"%s\"} label:{name:\"system_id\" value:\"%s\"} label:{name:\"version\" value:\"%s\"} gauge:{value:1}", a.Name, a.Os, s.Service, a.Id, a.Version)] = true |
| 51 | + |
43 | 52 | c := client.NewRestClient(e, "fake-api-token", "latest", "test-user-agent-string", false) |
44 | | - ac := NewArraysCollector(c) |
| 53 | + ac := NewArraysCollector(c) |
45 | 54 | metricsCheck(t, ac, want) |
| 55 | + server.Close() |
46 | 56 | } |
0 commit comments