|
| 1 | +// discover_test.go - Unit tests for cluster auto-detection. |
| 2 | +// |
| 3 | +// OSIRIS JSON Producer for HPE Aruba Networking Central introduction: |
| 4 | +// [OSIRIS-JSON-HPE-ARUBA-NETWORKING]: https://docs.osirisjson.org/osiris-producers/network/hpe-aruba-networking |
| 5 | + |
| 6 | +package arubacentral |
| 7 | + |
| 8 | +import ( |
| 9 | + "net/http" |
| 10 | + "net/http/httptest" |
| 11 | + "testing" |
| 12 | + "time" |
| 13 | +) |
| 14 | + |
| 15 | +func fakeClusterServer(t *testing.T, acceptToken string) *httptest.Server { |
| 16 | + t.Helper() |
| 17 | + return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 18 | + if r.Header.Get("Authorization") != "Bearer "+acceptToken { |
| 19 | + w.WriteHeader(http.StatusUnauthorized) |
| 20 | + w.Write([]byte(`{"message":"Unauthorized"}`)) |
| 21 | + return |
| 22 | + } |
| 23 | + w.WriteHeader(http.StatusOK) |
| 24 | + w.Write([]byte(`{"items":[]}`)) |
| 25 | + })) |
| 26 | +} |
| 27 | + |
| 28 | +func TestDetectClusterAmong_FindsAcceptingCluster(t *testing.T) { |
| 29 | + wrong1 := fakeClusterServer(t, "some-other-token") |
| 30 | + defer wrong1.Close() |
| 31 | + wrong2 := fakeClusterServer(t, "yet-another-token") |
| 32 | + defer wrong2.Close() |
| 33 | + correct := fakeClusterServer(t, "the-real-token") |
| 34 | + defer correct.Close() |
| 35 | + |
| 36 | + candidates := []clusterCandidate{ |
| 37 | + {cluster: "eu", baseURL: wrong1.URL}, |
| 38 | + {cluster: "prod", baseURL: correct.URL}, |
| 39 | + {cluster: "apac", baseURL: wrong2.URL}, |
| 40 | + } |
| 41 | + |
| 42 | + cluster, baseURL, err := detectClusterAmong(candidates, "the-real-token", 2*time.Second) |
| 43 | + if err != nil { |
| 44 | + t.Fatalf("detectClusterAmong failed: %v", err) |
| 45 | + } |
| 46 | + if cluster != "prod" { |
| 47 | + t.Errorf("expected cluster %q, got %q", "prod", cluster) |
| 48 | + } |
| 49 | + if baseURL != correct.URL { |
| 50 | + t.Errorf("expected baseURL %q, got %q", correct.URL, baseURL) |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +func TestDetectClusterAmong_NoneAccept(t *testing.T) { |
| 55 | + wrong1 := fakeClusterServer(t, "some-other-token") |
| 56 | + defer wrong1.Close() |
| 57 | + wrong2 := fakeClusterServer(t, "yet-another-token") |
| 58 | + defer wrong2.Close() |
| 59 | + |
| 60 | + candidates := []clusterCandidate{ |
| 61 | + {cluster: "eu", baseURL: wrong1.URL}, |
| 62 | + {cluster: "apac", baseURL: wrong2.URL}, |
| 63 | + } |
| 64 | + |
| 65 | + _, _, err := detectClusterAmong(candidates, "the-real-token", 2*time.Second) |
| 66 | + if err == nil { |
| 67 | + t.Fatal("expected an error when no cluster accepts the token") |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +func TestDetectClusterAmong_NoCandidates(t *testing.T) { |
| 72 | + _, _, err := detectClusterAmong(nil, "token", time.Second) |
| 73 | + if err == nil { |
| 74 | + t.Fatal("expected an error for an empty candidate list") |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +func TestCandidateClusters_ExcludesInternal(t *testing.T) { |
| 79 | + for _, c := range candidateClusters() { |
| 80 | + if c.cluster == "internal" { |
| 81 | + t.Fatal("expected \"internal\" to be excluded from auto-detection candidates") |
| 82 | + } |
| 83 | + } |
| 84 | + if len(candidateClusters()) != len(clusterBaseURLs)-1 { |
| 85 | + t.Errorf("expected exactly one cluster excluded, got %d candidates from %d total", len(candidateClusters()), len(clusterBaseURLs)) |
| 86 | + } |
| 87 | +} |
0 commit comments