Skip to content

Commit 4f9041a

Browse files
committed
added all unit tests with final code refactoring
Signed-off-by: deep-poharkar <[email protected]>
1 parent 1ed265f commit 4f9041a

16 files changed

+696
-129
lines changed

go.mod

+1-4
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,7 @@ require (
2020
sigs.k8s.io/yaml v1.3.0
2121
)
2222

23-
require (
24-
github.com/evanphx/json-patch v5.6.0+incompatible // indirect
25-
github.com/pmezard/go-difflib v1.0.0 // indirect
26-
)
23+
require github.com/pmezard/go-difflib v1.0.0 // indirect
2724

2825
require (
2926
cloud.google.com/go/compute v1.3.0 // indirect

go.sum

-2
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,6 @@ github.com/envoyproxy/go-control-plane v0.10.1/go.mod h1:AY7fTTXNdv/aJ2O5jwpxAPO
141141
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
142142
github.com/envoyproxy/protoc-gen-validate v0.6.2/go.mod h1:2t7qjJNvHPx8IjnBOzl9E9/baC+qXE/TeeyBRzgJDws=
143143
github.com/evanphx/json-patch v4.9.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk=
144-
github.com/evanphx/json-patch v5.6.0+incompatible h1:jBYDEEiFBPxA0v50tFdvOzQQTCvpL6mnFh5mB2/l16U=
145-
github.com/evanphx/json-patch v5.6.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk=
146144
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
147145
github.com/fatih/color v1.9.0/go.mod h1:eQcE1qtQxscV5RaZvpXrrb8Drkc3/DdQ+uUYCNjL+zU=
148146
github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w=

pkg/tests/auth_test.go pkg/apis/auth_test.go

+8-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package tests
1+
package apis
22

33
import (
44
"bytes"
@@ -7,11 +7,10 @@ import (
77
"net/http"
88
"testing"
99

10-
"github.com/litmuschaos/litmusctl/pkg/apis"
1110
"github.com/litmuschaos/litmusctl/pkg/types"
1211
)
1312

14-
var originalClient = apis.Client
13+
var originalClient = Client
1514

1615
type MockHTTPClientAuth struct {
1716
mockResponse types.AuthResponse
@@ -32,14 +31,14 @@ func (c *MockHTTPClientAuth) Do(req *http.Request) (*http.Response, error) {
3231
func TestAuthSuccess(t *testing.T) {
3332
// Store the original HTTP client and restore it after the test.
3433
defer func() {
35-
apis.Client = originalClient
34+
Client = originalClient
3635
}()
3736

3837
// Create an instance of the MockHTTPClient.
3938
mockClient := &MockHTTPClientAuth{}
4039

4140
// Replace the global HTTP client with the mock client for this test.
42-
apis.Client = mockClient
41+
Client = mockClient
4342

4443
input := types.AuthInput{
4544
Username: "testuser",
@@ -48,7 +47,7 @@ func TestAuthSuccess(t *testing.T) {
4847
}
4948

5049
// Call the Auth function with the test input.
51-
authResponse, err := apis.Auth(input, mockClient)
50+
authResponse, err := Auth(input, mockClient)
5251

5352
if err != nil {
5453
t.Fatalf("Expected no error, but got %v", err)
@@ -62,21 +61,21 @@ func TestAuthSuccess(t *testing.T) {
6261
func TestAuthFailed(t *testing.T) {
6362

6463
defer func() {
65-
apis.Client = originalClient
64+
Client = originalClient
6665
}()
6766
mockClient := &MockHTTPClientAuth{
6867
mockError: fmt.Errorf("mocked error"),
6968
}
7069

71-
apis.Client = mockClient
70+
Client = mockClient
7271

7372
input := types.AuthInput{
7473
Username: "testuser",
7574
Password: "testpassword",
7675
Endpoint: "https://example.com",
7776
}
7877

79-
_, err := apis.Auth(input, mockClient)
78+
_, err := Auth(input, mockClient)
8079
// fmt.Println("Response:", res)
8180
if err == nil {
8281
t.Fatal("Expected an error, but got nil")

pkg/tests/environment_test.go pkg/apis/environment/environment_test.go

+15-16
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package tests
1+
package environment
22

33
import (
44
"bytes"
@@ -9,14 +9,13 @@ import (
99
"testing"
1010

1111
models "github.com/litmuschaos/litmus/chaoscenter/graphql/server/graph/model"
12-
"github.com/litmuschaos/litmusctl/pkg/apis/environment"
1312
"github.com/litmuschaos/litmusctl/pkg/types"
1413
"github.com/stretchr/testify/assert"
1514
)
1615

1716
type MockHTTPClientCreate struct {
1817
mockError error
19-
mockResponse environment.CreateEnvironmentResponse
18+
mockResponse CreateEnvironmentResponse
2019
}
2120

2221
func (c *MockHTTPClientCreate) Do(req *http.Request) (*http.Response, error) {
@@ -35,7 +34,7 @@ func (c *MockHTTPClientCreate) Do(req *http.Request) (*http.Response, error) {
3534
}
3635

3736
type MockHTTPClientGet struct {
38-
mockResponse environment.ListEnvironmentData
37+
mockResponse ListEnvironmentData
3938
mockError error
4039
}
4140

@@ -70,12 +69,12 @@ func TestCreateEnvironmentSuccess(t *testing.T) {
7069

7170
//define the mock data response
7271

73-
mockData := environment.CreateEnvironmentResponse{
72+
mockData := CreateEnvironmentResponse{
7473
Errors: []struct {
7574
Message string `json:"message"`
7675
Path []string `json:"path"`
7776
}(nil),
78-
Data: environment.CreateEnvironmentData{
77+
Data: CreateEnvironmentData{
7978
EnvironmentDetails: models.Environment{
8079
ProjectID: "project123",
8180
EnvironmentID: "env123",
@@ -106,7 +105,7 @@ func TestCreateEnvironmentSuccess(t *testing.T) {
106105
mockError: nil, // Set this to nil if you don't want to return an error.
107106
}
108107

109-
result, err := environment.CreateEnvironment("testpid", mockRequest, input, mockClient)
108+
result, err := CreateEnvironment("testpid", mockRequest, input, mockClient)
110109

111110
if err != nil {
112111
fmt.Println(err)
@@ -131,7 +130,7 @@ func TestCreateEnvironmentFailed(t *testing.T) {
131130

132131
//define the mock data response
133132

134-
mockData := environment.CreateEnvironmentResponse{
133+
mockData := CreateEnvironmentResponse{
135134
Errors: []struct {
136135
Message string `json:"message"`
137136
Path []string `json:"path"`
@@ -141,7 +140,7 @@ func TestCreateEnvironmentFailed(t *testing.T) {
141140
Path: []string{"path1", "path2"},
142141
},
143142
},
144-
Data: environment.CreateEnvironmentData{
143+
Data: CreateEnvironmentData{
145144
EnvironmentDetails: models.Environment{
146145
ProjectID: "project123",
147146
EnvironmentID: "env123",
@@ -172,21 +171,21 @@ func TestCreateEnvironmentFailed(t *testing.T) {
172171
mockError: nil,
173172
}
174173

175-
result, _ := environment.CreateEnvironment("testpid", mockRequest, input, mockClient)
174+
result, _ := CreateEnvironment("testpid", mockRequest, input, mockClient)
176175

177176
assert.NotEqual(t, mockData, result)
178177
}
179178

180179
func TestGetEnvironmentListSuccess(t *testing.T) {
181180

182-
mockData := environment.ListEnvironmentData{
181+
mockData := ListEnvironmentData{
183182
Errors: []struct {
184183
Message string `json:"message"`
185184
Path []string `json:"path"`
186185
}{
187186
//keeping the error empty for asserting
188187
},
189-
Data: environment.EnvironmentsList{
188+
Data: EnvironmentsList{
190189
ListEnvironmentDetails: models.ListEnvironmentResponse{
191190
TotalNoOfEnvironments: 2,
192191
Environments: []*models.Environment{
@@ -246,7 +245,7 @@ func TestGetEnvironmentListSuccess(t *testing.T) {
246245
Endpoint: "https://example.com",
247246
}
248247

249-
result, err := environment.GetEnvironmentList("testpid", input, mockClient)
248+
result, err := GetEnvironmentList("testpid", input, mockClient)
250249

251250
if err != nil {
252251
fmt.Println(err)
@@ -256,7 +255,7 @@ func TestGetEnvironmentListSuccess(t *testing.T) {
256255

257256
func TestGetEnvironmentListFailed(t *testing.T) {
258257

259-
mockData := environment.ListEnvironmentData{
258+
mockData := ListEnvironmentData{
260259
Errors: []struct {
261260
Message string `json:"message"`
262261
Path []string `json:"path"`
@@ -266,7 +265,7 @@ func TestGetEnvironmentListFailed(t *testing.T) {
266265
Path: []string{"path1", "path2"},
267266
},
268267
},
269-
Data: environment.EnvironmentsList{
268+
Data: EnvironmentsList{
270269
ListEnvironmentDetails: models.ListEnvironmentResponse{
271270
TotalNoOfEnvironments: 2,
272271
Environments: []*models.Environment{
@@ -326,7 +325,7 @@ func TestGetEnvironmentListFailed(t *testing.T) {
326325
Endpoint: "https://example.com",
327326
}
328327

329-
result, _ := environment.GetEnvironmentList("testpid", input, mockClient)
328+
result, _ := GetEnvironmentList("testpid", input, mockClient)
330329

331330
assert.NotEqual(t, mockData, result)
332331
}

pkg/apis/experiment/experiment.go

+18-16
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
/*
22
Copyright © 2021 The LitmusChaos Authors
3-
43
Licensed under the Apache License, Version 2.0 (the "License");
54
you may not use this file except in compliance with the License.
65
You may obtain a copy of the License at
@@ -28,7 +27,7 @@ import (
2827
)
2928

3029
// CreateExperiment sends GraphQL API request for creating a Experiment
31-
func CreateExperiment(pid string, requestData model.SaveChaosExperimentRequest, cred types.Credentials) (RunExperimentResponse, error) {
30+
func CreateExperiment(pid string, requestData model.SaveChaosExperimentRequest, cred types.Credentials, httpClient apis.HTTPClientInterface) (RunExperimentResponse, error) {
3231

3332
var gqlReq SaveChaosExperimentGraphQLRequest
3433

@@ -45,7 +44,8 @@ func CreateExperiment(pid string, requestData model.SaveChaosExperimentRequest,
4544
apis.SendRequestParams{
4645
Endpoint: cred.Endpoint + utils.GQLAPIPath,
4746
Token: cred.Token,
48-
}, apis.Client,
47+
},
48+
httpClient,
4949
query,
5050
string(types.Post),
5151
)
@@ -80,7 +80,7 @@ func CreateExperiment(pid string, requestData model.SaveChaosExperimentRequest,
8080

8181
// Query to Run the Chaos Experiment
8282
runQuery := `{"query":"mutation{ \n runChaosExperiment(experimentID: \"` + requestData.ID + `\", projectID: \"` + pid + `\"){\n notifyID \n}}"}`
83-
resp, err = apis.SendRequest(apis.SendRequestParams{Endpoint: cred.Endpoint + utils.GQLAPIPath, Token: cred.Token}, apis.Client, []byte(runQuery), string(types.Post))
83+
resp, err = apis.SendRequest(apis.SendRequestParams{Endpoint: cred.Endpoint + utils.GQLAPIPath, Token: cred.Token}, httpClient, []byte(runQuery), string(types.Post))
8484

8585
if err != nil {
8686
return RunExperimentResponse{}, errors.New("Error in Running Chaos Experiment: " + err.Error())
@@ -108,7 +108,7 @@ func CreateExperiment(pid string, requestData model.SaveChaosExperimentRequest,
108108
}
109109
}
110110

111-
func SaveExperiment(pid string, requestData model.SaveChaosExperimentRequest, cred types.Credentials) (SaveExperimentData, error) {
111+
func SaveExperiment(pid string, requestData model.SaveChaosExperimentRequest, cred types.Credentials, httpClient apis.HTTPClientInterface) (SaveExperimentData, error) {
112112

113113
// Query to Save the Experiment
114114
var gqlReq SaveChaosExperimentGraphQLRequest
@@ -126,7 +126,8 @@ func SaveExperiment(pid string, requestData model.SaveChaosExperimentRequest, cr
126126
apis.SendRequestParams{
127127
Endpoint: cred.Endpoint + utils.GQLAPIPath,
128128
Token: cred.Token,
129-
}, apis.Client,
129+
},
130+
httpClient,
130131
query,
131132
string(types.Post),
132133
)
@@ -162,11 +163,11 @@ func SaveExperiment(pid string, requestData model.SaveChaosExperimentRequest, cr
162163

163164
}
164165

165-
func RunExperiment(pid string, eid string, cred types.Credentials) (RunExperimentResponse, error) {
166+
func RunExperiment(pid string, eid string, cred types.Credentials, httpClient apis.HTTPClientInterface) (RunExperimentResponse, error) {
166167
var err error
167168
runQuery := `{"query":"mutation{ \n runChaosExperiment(experimentID: \"` + eid + `\", projectID: \"` + pid + `\"){\n notifyID \n}}"}`
168169

169-
resp, err := apis.SendRequest(apis.SendRequestParams{Endpoint: cred.Endpoint + utils.GQLAPIPath, Token: cred.Token}, apis.Client, []byte(runQuery), string(types.Post))
170+
resp, err := apis.SendRequest(apis.SendRequestParams{Endpoint: cred.Endpoint + utils.GQLAPIPath, Token: cred.Token}, httpClient, []byte(runQuery), string(types.Post))
170171

171172
if err != nil {
172173
return RunExperimentResponse{}, errors.New("Error in Running Chaos Experiment: " + err.Error())
@@ -195,7 +196,7 @@ func RunExperiment(pid string, eid string, cred types.Credentials) (RunExperimen
195196
}
196197

197198
// GetExperimentList sends GraphQL API request for fetching a list of experiments.
198-
func GetExperimentList(pid string, in model.ListExperimentRequest, cred types.Credentials) (ExperimentListData, error) {
199+
func GetExperimentList(pid string, in model.ListExperimentRequest, cred types.Credentials, httpClient apis.HTTPClientInterface) (ExperimentListData, error) {
199200

200201
var gqlReq GetChaosExperimentsGraphQLRequest
201202
var err error
@@ -213,7 +214,7 @@ func GetExperimentList(pid string, in model.ListExperimentRequest, cred types.Cr
213214
apis.SendRequestParams{
214215
Endpoint: cred.Endpoint + utils.GQLAPIPath,
215216
Token: cred.Token,
216-
}, apis.Client,
217+
}, httpClient,
217218
query,
218219
string(types.Post),
219220
)
@@ -245,7 +246,7 @@ func GetExperimentList(pid string, in model.ListExperimentRequest, cred types.Cr
245246
}
246247

247248
// GetExperimentRunsList sends GraphQL API request for fetching a list of experiment runs.
248-
func GetExperimentRunsList(pid string, in model.ListExperimentRunRequest, cred types.Credentials) (ExperimentRunListData, error) {
249+
func GetExperimentRunsList(pid string, in model.ListExperimentRunRequest, cred types.Credentials, httpClient apis.HTTPClientInterface) (ExperimentRunListData, error) {
249250

250251
var gqlReq GetChaosExperimentRunGraphQLRequest
251252
var err error
@@ -263,7 +264,7 @@ func GetExperimentRunsList(pid string, in model.ListExperimentRunRequest, cred t
263264
apis.SendRequestParams{
264265
Endpoint: cred.Endpoint + utils.GQLAPIPath,
265266
Token: cred.Token,
266-
}, apis.Client,
267+
}, httpClient,
267268
query,
268269
string(types.Post),
269270
)
@@ -295,7 +296,7 @@ func GetExperimentRunsList(pid string, in model.ListExperimentRunRequest, cred t
295296
}
296297

297298
// DeleteChaosExperiment sends GraphQL API request for deleting a given Chaos Experiment.
298-
func DeleteChaosExperiment(projectID string, experimentID *string, cred types.Credentials) (DeleteChaosExperimentData, error) {
299+
func DeleteChaosExperiment(projectID string, experimentID *string, cred types.Credentials, httpClient apis.HTTPClientInterface) (DeleteChaosExperimentData, error) {
299300

300301
var gqlReq DeleteChaosExperimentGraphQLRequest
301302
var err error
@@ -315,7 +316,8 @@ func DeleteChaosExperiment(projectID string, experimentID *string, cred types.Cr
315316
apis.SendRequestParams{
316317
Endpoint: cred.Endpoint + utils.GQLAPIPath,
317318
Token: cred.Token,
318-
}, apis.Client,
319+
},
320+
httpClient,
319321
query,
320322
string(types.Post),
321323
)
@@ -347,12 +349,12 @@ func DeleteChaosExperiment(projectID string, experimentID *string, cred types.Cr
347349
}
348350

349351
// GetServerVersion fetches the GQL server version
350-
func GetServerVersion(endpoint string) (ServerVersionResponse, error) {
352+
func GetServerVersion(endpoint string, httpClient apis.HTTPClientInterface) (ServerVersionResponse, error) {
351353
query := `{"query":"query{\n getServerVersion{\n key value\n }\n}"}`
352354
resp, err := apis.SendRequest(
353355
apis.SendRequestParams{
354356
Endpoint: endpoint + utils.GQLAPIPath,
355-
}, apis.Client,
357+
}, httpClient,
356358
[]byte(query),
357359
string(types.Post),
358360
)

0 commit comments

Comments
 (0)