|
1 |
| -package healthcheck |
| 1 | +package healthcheck_test |
2 | 2 |
|
3 | 3 | import (
|
4 | 4 | "errors"
|
5 | 5 | "net/http"
|
6 | 6 | "net/http/httptest"
|
7 |
| - "testing" |
8 | 7 |
|
9 | 8 | "github.com/golang/mock/gomock"
|
| 9 | + . "github.com/onsi/ginkgo" |
| 10 | + . "github.com/onsi/gomega" |
10 | 11 | "github.com/openshift/backplane-cli/pkg/cli/config"
|
11 |
| - "github.com/openshift/backplane-cli/pkg/healthcheck/mocks" |
| 12 | + "github.com/openshift/backplane-cli/pkg/healthcheck" |
| 13 | + healthcheckMock "github.com/openshift/backplane-cli/pkg/healthcheck/mocks" |
12 | 14 | )
|
13 | 15 |
|
14 |
| -func TestCheckProxyConnectivity(t *testing.T) { |
15 |
| - tests := []struct { |
16 |
| - name string |
17 |
| - proxyURL string |
18 |
| - proxyEndpoint string |
19 |
| - expectErr bool |
20 |
| - }{ |
21 |
| - { |
22 |
| - name: "Proxy not configured", |
23 |
| - proxyURL: "", |
24 |
| - expectErr: true, |
25 |
| - }, |
26 |
| - } |
27 |
| - |
28 |
| - originalGetProxyTestEndpointFunc := GetProxyTestEndpointFunc |
29 |
| - defer func() { GetProxyTestEndpointFunc = originalGetProxyTestEndpointFunc }() |
30 |
| - |
31 |
| - for _, tt := range tests { |
32 |
| - t.Run(tt.name, func(t *testing.T) { |
33 |
| - GetProxyTestEndpointFunc = func() (string, error) { |
34 |
| - if tt.proxyEndpoint == "" { |
| 16 | +var _ = Describe("Proxy Connectivity", func() { |
| 17 | + var ( |
| 18 | + mockCtrl *gomock.Controller |
| 19 | + mockClient *healthcheckMock.MockHTTPClient |
| 20 | + mockProxy *httptest.Server |
| 21 | + ) |
| 22 | + |
| 23 | + BeforeEach(func() { |
| 24 | + mockCtrl = gomock.NewController(GinkgoT()) |
| 25 | + mockClient = healthcheckMock.NewMockHTTPClient(mockCtrl) |
| 26 | + healthcheck.HTTPClients = mockClient |
| 27 | + |
| 28 | + // Set up a mock proxy server |
| 29 | + mockProxy = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 30 | + w.WriteHeader(http.StatusOK) |
| 31 | + })) |
| 32 | + healthcheck.GetConfigFunc = func() (config.BackplaneConfiguration, error) { |
| 33 | + proxyURL := mockProxy.URL |
| 34 | + return config.BackplaneConfiguration{ProxyURL: &proxyURL}, nil |
| 35 | + } |
| 36 | + }) |
| 37 | + |
| 38 | + AfterEach(func() { |
| 39 | + mockProxy.Close() |
| 40 | + mockCtrl.Finish() |
| 41 | + }) |
| 42 | + |
| 43 | + Describe("CheckProxyConnectivity", func() { |
| 44 | + var originalGetProxyTestEndpointFunc func() (string, error) |
| 45 | + |
| 46 | + BeforeEach(func() { |
| 47 | + originalGetProxyTestEndpointFunc = healthcheck.GetProxyTestEndpointFunc |
| 48 | + }) |
| 49 | + |
| 50 | + AfterEach(func() { |
| 51 | + healthcheck.GetProxyTestEndpointFunc = originalGetProxyTestEndpointFunc |
| 52 | + }) |
| 53 | + |
| 54 | + Context("When proxy is not configured", func() { |
| 55 | + It("should return an error", func() { |
| 56 | + healthcheck.GetProxyTestEndpointFunc = func() (string, error) { |
35 | 57 | return "", errors.New("proxy test endpoint not configured")
|
36 | 58 | }
|
37 |
| - return tt.proxyEndpoint, nil |
38 |
| - } |
39 |
| - |
40 |
| - mockCtrl := gomock.NewController(t) |
41 |
| - defer mockCtrl.Finish() |
42 | 59 |
|
43 |
| - client := mocks.NewMockHTTPClient(mockCtrl) |
44 |
| - client.EXPECT().Get(gomock.Any()).Return(&http.Response{StatusCode: http.StatusOK}, nil).AnyTimes() |
| 60 | + healthcheck.GetConfigFunc = func() (config.BackplaneConfiguration, error) { |
| 61 | + return config.BackplaneConfiguration{ProxyURL: nil}, nil |
| 62 | + } |
45 | 63 |
|
46 |
| - url, err := CheckProxyConnectivity(client) |
47 |
| - if (err != nil) != tt.expectErr { |
48 |
| - t.Errorf("CheckProxyConnectivity() error = %v, expectErr %v", err, tt.expectErr) |
49 |
| - } |
50 |
| - if err == nil && url != tt.proxyURL { |
51 |
| - t.Errorf("Expected proxy URL = %v, got %v", tt.proxyURL, url) |
52 |
| - } |
| 64 | + _, err := healthcheck.CheckProxyConnectivity(mockClient) |
| 65 | + Expect(err).To(HaveOccurred()) |
| 66 | + }) |
53 | 67 | })
|
54 |
| - } |
55 |
| -} |
56 |
| - |
57 |
| -func TestCheckBackplaneAPIConnectivity(t *testing.T) { |
58 |
| - tests := []struct { |
59 |
| - name string |
60 |
| - proxyURL string |
61 |
| - apiURL string |
62 |
| - expectErr bool |
63 |
| - }{ |
64 |
| - { |
65 |
| - name: "API not accessible through proxy", |
66 |
| - proxyURL: "http://proxy:8080", |
67 |
| - apiURL: "http://bad-api-endpoint", |
68 |
| - expectErr: true, |
69 |
| - }, |
70 |
| - } |
71 |
| - |
72 |
| - for _, tt := range tests { |
73 |
| - t.Run(tt.name, func(t *testing.T) { |
74 |
| - server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
75 |
| - if r.URL.String() == tt.apiURL { |
| 68 | + |
| 69 | + Context("When proxy is configured", func() { |
| 70 | + It("should pass if proxy connectivity is good", func() { |
| 71 | + healthcheck.GetProxyTestEndpointFunc = func() (string, error) { |
| 72 | + return "http://proxy-test-endpoint", nil |
| 73 | + } |
| 74 | + |
| 75 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
76 | 76 | w.WriteHeader(http.StatusOK)
|
77 |
| - } else { |
78 |
| - w.WriteHeader(http.StatusInternalServerError) |
| 77 | + })) |
| 78 | + defer server.Close() |
| 79 | + |
| 80 | + healthcheck.GetProxyTestEndpointFunc = func() (string, error) { |
| 81 | + return server.URL, nil |
79 | 82 | }
|
80 |
| - })) |
81 |
| - defer server.Close() |
82 |
| - tt.apiURL = server.URL |
83 | 83 |
|
84 |
| - mockCtrl := gomock.NewController(t) |
85 |
| - defer mockCtrl.Finish() |
| 84 | + mockClient.EXPECT().Get(server.URL).Return(&http.Response{StatusCode: http.StatusOK}, nil).AnyTimes() |
86 | 85 |
|
87 |
| - client := mocks.NewMockHTTPClient(mockCtrl) |
88 |
| - client.EXPECT().Get(gomock.Any()).Return(&http.Response{StatusCode: http.StatusOK}, nil).AnyTimes() |
| 86 | + url, err := healthcheck.CheckProxyConnectivity(mockClient) |
| 87 | + Expect(err).ToNot(HaveOccurred()) |
| 88 | + Expect(url).To(Equal(mockProxy.URL)) |
| 89 | + }) |
| 90 | + }) |
| 91 | + }) |
| 92 | + |
| 93 | + Describe("GetProxyTestEndpoint", func() { |
| 94 | + var originalGetConfigFunc func() (config.BackplaneConfiguration, error) |
| 95 | + |
| 96 | + BeforeEach(func() { |
| 97 | + originalGetConfigFunc = healthcheck.GetConfigFunc |
| 98 | + }) |
| 99 | + |
| 100 | + AfterEach(func() { |
| 101 | + healthcheck.GetConfigFunc = originalGetConfigFunc |
| 102 | + }) |
89 | 103 |
|
90 |
| - err := CheckBackplaneAPIConnectivity(client, tt.proxyURL) |
91 |
| - if (err != nil) != tt.expectErr { |
92 |
| - t.Errorf("CheckBackplaneAPIConnectivity() error = %v, expectErr %v", err, tt.expectErr) |
| 104 | + It("should return the configured proxy endpoint", func() { |
| 105 | + proxyEndpoint := "http://proxy-endpoint" |
| 106 | + healthcheck.GetConfigFunc = func() (config.BackplaneConfiguration, error) { |
| 107 | + return config.BackplaneConfiguration{ProxyCheckEndpoint: proxyEndpoint}, nil |
93 | 108 | }
|
| 109 | + |
| 110 | + endpoint, err := healthcheck.GetProxyTestEndpoint() |
| 111 | + Expect(err).ToNot(HaveOccurred()) |
| 112 | + Expect(endpoint).To(Equal(proxyEndpoint)) |
94 | 113 | })
|
95 |
| - } |
96 |
| -} |
97 |
| - |
98 |
| -func TestGetProxyTestEndpoint(t *testing.T) { |
99 |
| - originalGetConfigFunc := getConfigFunc |
100 |
| - defer func() { getConfigFunc = originalGetConfigFunc }() |
101 |
| - |
102 |
| - tests := []struct { |
103 |
| - name string |
104 |
| - config config.BackplaneConfiguration |
105 |
| - expectErr bool |
106 |
| - }{ |
107 |
| - { |
108 |
| - name: "Configured proxy endpoint", |
109 |
| - config: config.BackplaneConfiguration{ |
110 |
| - ProxyCheckEndpoint: "http://proxy-endpoint", |
111 |
| - }, |
112 |
| - expectErr: false, |
113 |
| - }, |
114 |
| - { |
115 |
| - name: "No proxy endpoint configured", |
116 |
| - config: config.BackplaneConfiguration{ |
117 |
| - ProxyCheckEndpoint: "", |
118 |
| - }, |
119 |
| - expectErr: true, |
120 |
| - }, |
121 |
| - { |
122 |
| - name: "Failed to get backplane configuration", |
123 |
| - config: config.BackplaneConfiguration{}, |
124 |
| - expectErr: true, |
125 |
| - }, |
126 |
| - } |
127 |
| - |
128 |
| - for _, tt := range tests { |
129 |
| - t.Run(tt.name, func(t *testing.T) { |
130 |
| - getConfigFunc = func() (config.BackplaneConfiguration, error) { |
131 |
| - if tt.name == "Failed to get backplane configuration" { |
132 |
| - return config.BackplaneConfiguration{}, errors.New("failed to get backplane configuration") |
133 |
| - } |
134 |
| - return tt.config, nil |
| 114 | + |
| 115 | + It("should return an error if proxy endpoint is not configured", func() { |
| 116 | + healthcheck.GetConfigFunc = func() (config.BackplaneConfiguration, error) { |
| 117 | + return config.BackplaneConfiguration{ProxyCheckEndpoint: ""}, nil |
135 | 118 | }
|
136 | 119 |
|
137 |
| - _, err := GetProxyTestEndpoint() |
138 |
| - if (err != nil) != tt.expectErr { |
139 |
| - t.Errorf("GetProxyTestEndpoint() error = %v, expectErr %v", err, tt.expectErr) |
| 120 | + _, err := healthcheck.GetProxyTestEndpoint() |
| 121 | + Expect(err).To(HaveOccurred()) |
| 122 | + }) |
| 123 | + |
| 124 | + It("should return an error if failed to get backplane configuration", func() { |
| 125 | + healthcheck.GetConfigFunc = func() (config.BackplaneConfiguration, error) { |
| 126 | + return config.BackplaneConfiguration{}, errors.New("failed to get backplane configuration") |
140 | 127 | }
|
| 128 | + |
| 129 | + _, err := healthcheck.GetProxyTestEndpoint() |
| 130 | + Expect(err).To(HaveOccurred()) |
141 | 131 | })
|
142 |
| - } |
143 |
| -} |
| 132 | + }) |
| 133 | +}) |
0 commit comments