Skip to content

Commit 04da9ad

Browse files
authored
Add integration test for api infra setup (#2331)
* Add integration test for api infra setup
1 parent 7f5fd80 commit 04da9ad

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+1608
-746
lines changed

.github/workflows/ci.yaml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,29 @@ jobs:
8686
name: Integration tests
8787
runs-on: ubuntu-latest
8888
needs:
89+
- int-tests-api
8990
- int-tests-kind
9091
steps:
9192
- name: Succeed if all tests passed
9293
run: echo "Integration tests succeeded"
9394

95+
int-tests-api:
96+
name: Integration tests (api)
97+
runs-on: ubuntu-latest
98+
steps:
99+
- name: Checkout
100+
uses: actions/checkout@v4
101+
with:
102+
fetch-depth: 0
103+
- name: Setup go
104+
uses: actions/setup-go@v5
105+
with:
106+
go-version-file: go.mod
107+
cache-dependency-path: "**/*.sum"
108+
- name: Run tests
109+
run: |
110+
make test-integration
111+
94112
int-tests-kind:
95113
name: Integration tests (kind)
96114
runs-on: ubuntu-latest

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,10 @@ unit-tests: envtest
277277
$(MAKE) -C operator test
278278
$(MAKE) -C utils unit-tests
279279

280+
.PHONY: test-integration
281+
test-integration: static
282+
$(MAKE) -C api test-integration
283+
280284
.PHONY: vet
281285
vet:
282286
go vet -tags $(GO_BUILD_TAGS) ./...

api/Makefile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,8 @@ swag:
1313

1414
.PHONY: unit-tests
1515
unit-tests:
16-
go test -race -tags $(GO_BUILD_TAGS) -v ./...
16+
go test -race -tags $(GO_BUILD_TAGS) -v $(shell go list ./... | grep -v '/integration')
17+
18+
.PHONY: test-integration
19+
test-integration:
20+
go test -race -tags $(GO_BUILD_TAGS) -v ./integration

api/client/client.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ import (
1111

1212
type Client interface {
1313
Authenticate(password string) error
14-
GetInstallationConfig() (*types.InstallationConfig, error)
15-
GetInstallationStatus() (*types.Status, error)
16-
ConfigureInstallation(config *types.InstallationConfig) (*types.Status, error)
17-
SetupInfra() (*types.Infra, error)
18-
GetInfraStatus() (*types.Infra, error)
19-
SetInstallStatus(status *types.Status) (*types.Status, error)
14+
GetInstallationConfig() (types.InstallationConfig, error)
15+
GetInstallationStatus() (types.Status, error)
16+
ConfigureInstallation(config types.InstallationConfig) (types.Status, error)
17+
SetupInfra() (types.Infra, error)
18+
GetInfraStatus() (types.Infra, error)
19+
SetInstallStatus(status types.Status) (types.Status, error)
2020
}
2121

2222
type client struct {

api/client/client_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ func TestGetInstallationConfig(t *testing.T) {
138138
c = New(errorServer.URL, WithToken("test-token"))
139139
config, err = c.GetInstallationConfig()
140140
assert.Error(t, err)
141-
assert.Nil(t, config)
141+
assert.Equal(t, types.InstallationConfig{}, config)
142142

143143
apiErr, ok := err.(*types.APIError)
144144
require.True(t, ok, "Expected err to be of type *types.APIError")
@@ -177,7 +177,7 @@ func TestConfigureInstallation(t *testing.T) {
177177
GlobalCIDR: "20.0.0.0/24",
178178
LocalArtifactMirrorPort: 9081,
179179
}
180-
status, err := c.ConfigureInstallation(&config)
180+
status, err := c.ConfigureInstallation(config)
181181
assert.NoError(t, err)
182182
assert.NotNil(t, status)
183183
assert.Equal(t, types.StateRunning, status.State)
@@ -194,9 +194,9 @@ func TestConfigureInstallation(t *testing.T) {
194194
defer errorServer.Close()
195195

196196
c = New(errorServer.URL, WithToken("test-token"))
197-
status, err = c.ConfigureInstallation(&config)
197+
status, err = c.ConfigureInstallation(config)
198198
assert.Error(t, err)
199-
assert.Nil(t, status)
199+
assert.Equal(t, types.Status{}, status)
200200

201201
apiErr, ok := err.(*types.APIError)
202202
require.True(t, ok, "Expected err to be of type *types.APIError")
@@ -216,7 +216,7 @@ func TestSetupInfra(t *testing.T) {
216216
// Return successful response
217217
w.WriteHeader(http.StatusOK)
218218
json.NewEncoder(w).Encode(types.Infra{
219-
Status: &types.Status{
219+
Status: types.Status{
220220
State: types.StateRunning,
221221
Description: "Installing infra",
222222
},
@@ -246,7 +246,7 @@ func TestSetupInfra(t *testing.T) {
246246
c = New(errorServer.URL, WithToken("test-token"))
247247
infra, err = c.SetupInfra()
248248
assert.Error(t, err)
249-
assert.Nil(t, infra)
249+
assert.Equal(t, types.Infra{}, infra)
250250

251251
apiErr, ok := err.(*types.APIError)
252252
require.True(t, ok, "Expected err to be of type *types.APIError")
@@ -266,7 +266,7 @@ func TestGetInfraStatus(t *testing.T) {
266266
// Return successful response
267267
w.WriteHeader(http.StatusOK)
268268
json.NewEncoder(w).Encode(types.Infra{
269-
Status: &types.Status{
269+
Status: types.Status{
270270
State: types.StateSucceeded,
271271
Description: "Installation successful",
272272
},
@@ -295,7 +295,7 @@ func TestGetInfraStatus(t *testing.T) {
295295
c = New(errorServer.URL, WithToken("test-token"))
296296
infra, err = c.GetInfraStatus()
297297
assert.Error(t, err)
298-
assert.Nil(t, infra)
298+
assert.Equal(t, types.Infra{}, infra)
299299

300300
apiErr, ok := err.(*types.APIError)
301301
require.True(t, ok, "Expected err to be of type *types.APIError")
@@ -325,7 +325,7 @@ func TestSetInstallStatus(t *testing.T) {
325325

326326
// Test successful set
327327
c := New(server.URL, WithToken("test-token"))
328-
status := &types.Status{
328+
status := types.Status{
329329
State: types.StateSucceeded,
330330
Description: "Installation successful",
331331
}
@@ -347,7 +347,7 @@ func TestSetInstallStatus(t *testing.T) {
347347
c = New(errorServer.URL, WithToken("test-token"))
348348
newStatus, err = c.SetInstallStatus(status)
349349
assert.Error(t, err)
350-
assert.Nil(t, newStatus)
350+
assert.Equal(t, types.Status{}, newStatus)
351351

352352
apiErr, ok := err.(*types.APIError)
353353
require.True(t, ok, "Expected err to be of type *types.APIError")

api/client/install.go

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -8,174 +8,174 @@ import (
88
"github.com/replicatedhq/embedded-cluster/api/types"
99
)
1010

11-
func (c *client) GetInstallationConfig() (*types.InstallationConfig, error) {
11+
func (c *client) GetInstallationConfig() (types.InstallationConfig, error) {
1212
req, err := http.NewRequest("GET", c.apiURL+"/api/install/installation/config", nil)
1313
if err != nil {
14-
return nil, err
14+
return types.InstallationConfig{}, err
1515
}
1616
req.Header.Set("Content-Type", "application/json")
1717
setAuthorizationHeader(req, c.token)
1818

1919
resp, err := c.httpClient.Do(req)
2020
if err != nil {
21-
return nil, err
21+
return types.InstallationConfig{}, err
2222
}
2323
defer resp.Body.Close()
2424

2525
if resp.StatusCode != http.StatusOK {
26-
return nil, errorFromResponse(resp)
26+
return types.InstallationConfig{}, errorFromResponse(resp)
2727
}
2828

2929
var config types.InstallationConfig
3030
err = json.NewDecoder(resp.Body).Decode(&config)
3131
if err != nil {
32-
return nil, err
32+
return types.InstallationConfig{}, err
3333
}
3434

35-
return &config, nil
35+
return config, nil
3636
}
3737

38-
func (c *client) ConfigureInstallation(config *types.InstallationConfig) (*types.Status, error) {
38+
func (c *client) ConfigureInstallation(config types.InstallationConfig) (types.Status, error) {
3939
b, err := json.Marshal(config)
4040
if err != nil {
41-
return nil, err
41+
return types.Status{}, err
4242
}
4343

4444
req, err := http.NewRequest("POST", c.apiURL+"/api/install/installation/configure", bytes.NewBuffer(b))
4545
if err != nil {
46-
return nil, err
46+
return types.Status{}, err
4747
}
4848
req.Header.Set("Content-Type", "application/json")
4949
setAuthorizationHeader(req, c.token)
5050

5151
resp, err := c.httpClient.Do(req)
5252
if err != nil {
53-
return nil, err
53+
return types.Status{}, err
5454
}
5555
defer resp.Body.Close()
5656

5757
if resp.StatusCode != http.StatusOK {
58-
return nil, errorFromResponse(resp)
58+
return types.Status{}, errorFromResponse(resp)
5959
}
6060

6161
var status types.Status
6262
err = json.NewDecoder(resp.Body).Decode(&status)
6363
if err != nil {
64-
return nil, err
64+
return types.Status{}, err
6565
}
6666

67-
return &status, nil
67+
return status, nil
6868
}
6969

70-
func (c *client) GetInstallationStatus() (*types.Status, error) {
70+
func (c *client) GetInstallationStatus() (types.Status, error) {
7171
req, err := http.NewRequest("GET", c.apiURL+"/api/install/installation/status", nil)
7272
if err != nil {
73-
return nil, err
73+
return types.Status{}, err
7474
}
7575
req.Header.Set("Content-Type", "application/json")
7676
setAuthorizationHeader(req, c.token)
7777

7878
resp, err := c.httpClient.Do(req)
7979
if err != nil {
80-
return nil, err
80+
return types.Status{}, err
8181
}
8282
defer resp.Body.Close()
8383

8484
if resp.StatusCode != http.StatusOK {
85-
return nil, errorFromResponse(resp)
85+
return types.Status{}, errorFromResponse(resp)
8686
}
8787

8888
var status types.Status
8989
err = json.NewDecoder(resp.Body).Decode(&status)
9090
if err != nil {
91-
return nil, err
91+
return types.Status{}, err
9292
}
9393

94-
return &status, nil
94+
return status, nil
9595
}
9696

97-
func (c *client) SetupInfra() (*types.Infra, error) {
97+
func (c *client) SetupInfra() (types.Infra, error) {
9898
req, err := http.NewRequest("POST", c.apiURL+"/api/install/infra/setup", nil)
9999
if err != nil {
100-
return nil, err
100+
return types.Infra{}, err
101101
}
102102
req.Header.Set("Content-Type", "application/json")
103103
setAuthorizationHeader(req, c.token)
104104

105105
resp, err := c.httpClient.Do(req)
106106
if err != nil {
107-
return nil, err
107+
return types.Infra{}, err
108108
}
109109
defer resp.Body.Close()
110110

111111
if resp.StatusCode != http.StatusOK {
112-
return nil, errorFromResponse(resp)
112+
return types.Infra{}, errorFromResponse(resp)
113113
}
114114

115115
var infra types.Infra
116116
err = json.NewDecoder(resp.Body).Decode(&infra)
117117
if err != nil {
118-
return nil, err
118+
return types.Infra{}, err
119119
}
120120

121-
return &infra, nil
121+
return infra, nil
122122
}
123123

124-
func (c *client) GetInfraStatus() (*types.Infra, error) {
124+
func (c *client) GetInfraStatus() (types.Infra, error) {
125125
req, err := http.NewRequest("GET", c.apiURL+"/api/install/infra/status", nil)
126126
if err != nil {
127-
return nil, err
127+
return types.Infra{}, err
128128
}
129129
req.Header.Set("Content-Type", "application/json")
130130
setAuthorizationHeader(req, c.token)
131131

132132
resp, err := c.httpClient.Do(req)
133133
if err != nil {
134-
return nil, err
134+
return types.Infra{}, err
135135
}
136136
defer resp.Body.Close()
137137

138138
if resp.StatusCode != http.StatusOK {
139-
return nil, errorFromResponse(resp)
139+
return types.Infra{}, errorFromResponse(resp)
140140
}
141141

142142
var infra types.Infra
143143
err = json.NewDecoder(resp.Body).Decode(&infra)
144144
if err != nil {
145-
return nil, err
145+
return types.Infra{}, err
146146
}
147147

148-
return &infra, nil
148+
return infra, nil
149149
}
150150

151-
func (c *client) SetInstallStatus(s *types.Status) (*types.Status, error) {
151+
func (c *client) SetInstallStatus(s types.Status) (types.Status, error) {
152152
b, err := json.Marshal(s)
153153
if err != nil {
154-
return nil, err
154+
return types.Status{}, err
155155
}
156156

157157
req, err := http.NewRequest("POST", c.apiURL+"/api/install/status", bytes.NewBuffer(b))
158158
if err != nil {
159-
return nil, err
159+
return types.Status{}, err
160160
}
161161
req.Header.Set("Content-Type", "application/json")
162162
setAuthorizationHeader(req, c.token)
163163

164164
resp, err := c.httpClient.Do(req)
165165
if err != nil {
166-
return nil, err
166+
return types.Status{}, err
167167
}
168168
defer resp.Body.Close()
169169

170170
if resp.StatusCode != http.StatusOK {
171-
return nil, errorFromResponse(resp)
171+
return types.Status{}, errorFromResponse(resp)
172172
}
173173

174174
var status types.Status
175175
err = json.NewDecoder(resp.Body).Decode(&status)
176176
if err != nil {
177-
return nil, err
177+
return types.Status{}, err
178178
}
179179

180-
return &status, nil
180+
return status, nil
181181
}

0 commit comments

Comments
 (0)