Skip to content

Commit 8ecb2fe

Browse files
Merge pull request #487 from xiaoyu74/healthcheck_test_improve
Update to use Ginkgo BDD testing framework to align with the exist test style
2 parents f89e595 + 5125f75 commit 8ecb2fe

File tree

6 files changed

+317
-269
lines changed

6 files changed

+317
-269
lines changed

pkg/healthcheck/check_proxy.go

+11-5
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@ import (
1010

1111
// CheckProxyConnectivity checks the proxy connectivity
1212
func CheckProxyConnectivity(client HTTPClient) (string, error) {
13-
bpConfig, err := getConfigFunc()
13+
logger.Debug("Starting CheckProxyConnectivity")
14+
bpConfig, err := GetConfigFunc()
1415
if err != nil {
1516
logger.Errorf("Failed to get backplane configuration: %v", err)
1617
return "", fmt.Errorf("failed to get backplane configuration: %v", err)
1718
}
19+
logger.Debugf("Backplane configuration: %+v", bpConfig)
1820

1921
proxyURL := bpConfig.ProxyURL
2022
if proxyURL == nil || *proxyURL == "" {
@@ -25,25 +27,28 @@ func CheckProxyConnectivity(client HTTPClient) (string, error) {
2527

2628
logger.Infof("Getting the working proxy URL ['%s'] from local backplane configuration.", *proxyURL)
2729

28-
proxy, err := url.Parse(*proxyURL)
30+
parsedProxyURL, err := url.Parse(*proxyURL)
2931
if err != nil {
3032
logger.Errorf("Invalid proxy URL: %v", err)
3133
return "", fmt.Errorf("invalid proxy URL: %v", err)
3234
}
35+
logger.Debugf("Parsed proxy URL: %s", parsedProxyURL)
3336

34-
httpClientWithProxy := &DefaultHTTPClient{
37+
httpClientWithProxy := &DefaultHTTPClientImpl{
3538
Client: &http.Client{
3639
Transport: &http.Transport{
37-
Proxy: http.ProxyURL(proxy),
40+
Proxy: http.ProxyURL(parsedProxyURL),
3841
},
3942
},
4043
}
44+
logger.Debug("HTTP client with proxy configured")
4145

4246
proxyTestEndpoint, err := GetProxyTestEndpointFunc()
4347
if err != nil {
4448
logger.Errorf("Failed to get proxy test endpoint: %v", err)
4549
return "", err
4650
}
51+
logger.Debugf("Proxy test endpoint: %s", proxyTestEndpoint)
4752

4853
logger.Infof("Testing connectivity to the pre-defined test endpoint ['%s'] with the proxy.", proxyTestEndpoint)
4954
if err := testEndPointConnectivity(proxyTestEndpoint, httpClientWithProxy); err != nil {
@@ -52,11 +57,12 @@ func CheckProxyConnectivity(client HTTPClient) (string, error) {
5257
return "", fmt.Errorf(errMsg)
5358
}
5459

60+
logger.Debugf("Successfully connected to proxy test endpoint: %s", proxyTestEndpoint)
5561
return *proxyURL, nil
5662
}
5763

5864
func GetProxyTestEndpoint() (string, error) {
59-
bpConfig, err := getConfigFunc()
65+
bpConfig, err := GetConfigFunc()
6066
if err != nil {
6167
logger.Errorf("Failed to get backplane configuration: %v", err)
6268
return "", fmt.Errorf("failed to get backplane configuration: %v", err)

pkg/healthcheck/check_proxy_test.go

+107-117
Original file line numberDiff line numberDiff line change
@@ -1,143 +1,133 @@
1-
package healthcheck
1+
package healthcheck_test
22

33
import (
44
"errors"
55
"net/http"
66
"net/http/httptest"
7-
"testing"
87

98
"github.com/golang/mock/gomock"
9+
. "github.com/onsi/ginkgo"
10+
. "github.com/onsi/gomega"
1011
"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"
1214
)
1315

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) {
3557
return "", errors.New("proxy test endpoint not configured")
3658
}
37-
return tt.proxyEndpoint, nil
38-
}
39-
40-
mockCtrl := gomock.NewController(t)
41-
defer mockCtrl.Finish()
4259

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+
}
4563

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+
})
5367
})
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) {
7676
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
7982
}
80-
}))
81-
defer server.Close()
82-
tt.apiURL = server.URL
8383

84-
mockCtrl := gomock.NewController(t)
85-
defer mockCtrl.Finish()
84+
mockClient.EXPECT().Get(server.URL).Return(&http.Response{StatusCode: http.StatusOK}, nil).AnyTimes()
8685

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+
})
89103

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
93108
}
109+
110+
endpoint, err := healthcheck.GetProxyTestEndpoint()
111+
Expect(err).ToNot(HaveOccurred())
112+
Expect(endpoint).To(Equal(proxyEndpoint))
94113
})
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
135118
}
136119

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")
140127
}
128+
129+
_, err := healthcheck.GetProxyTestEndpoint()
130+
Expect(err).To(HaveOccurred())
141131
})
142-
}
143-
}
132+
})
133+
})

pkg/healthcheck/check_vpn.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func CheckVPNConnectivity(netInterfaces NetworkInterface, client HTTPClient) err
5151
}
5252

5353
func GetVPNCheckEndpoint() (string, error) {
54-
bpConfig, err := getConfigFunc()
54+
bpConfig, err := GetConfigFunc()
5555
if err != nil {
5656
logger.Errorf("Failed to get backplane configuration: %v", err)
5757
return "", fmt.Errorf("failed to get backplane configuration: %v", err)

0 commit comments

Comments
 (0)