Skip to content

Commit 5481c3f

Browse files
committed
fix: the code smells from sonarqube
1 parent f6d271b commit 5481c3f

File tree

12 files changed

+149
-137
lines changed

12 files changed

+149
-137
lines changed

.vscode/extensions.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"recommendations": [
3+
"linuxsuren.api-testing"
4+
]
5+
}

Dockerfile

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
1-
FROM golang:1.18 as builder
1+
FROM golang:1.18 AS builder
22

33
WORKDIR /workspace
4-
COPY . .
4+
COPY cmd/ cmd/
5+
COPY pkg/ pkg/
6+
COPY sample/ sample/
7+
COPY go.mod go.mod
8+
COPY go.sum go.sum
9+
COPY main.go main.go
10+
COPY README.md README.md
11+
COPY LICENSE LICENSE
12+
513
RUN go mod download
614
RUN CGO_ENABLE=0 go build -ldflags "-w -s" -o atest .
715

@@ -19,5 +27,7 @@ LABEL "maintainer"="Rick <[email protected]>"
1927
LABEL "Name"="API testing"
2028

2129
COPY --from=builder /workspace/atest /usr/local/bin/atest
30+
COPY --from=builder /workspace/LICENSE /LICENSE
31+
COPY --from=builder /workspace/README.md /README.md
2232

2333
CMD ["atest", "server"]

cmd/root_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
exec "github.com/linuxsuren/go-fake-runtime"
1010
)
1111

12-
func Test_setRelativeDir(t *testing.T) {
12+
func TestSetRelativeDir(t *testing.T) {
1313
type args struct {
1414
configFile string
1515
testcase *atesting.TestCase

cmd/run_test.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,19 @@ func TestRunSuite(t *testing.T) {
2121
hasError bool
2222
}{{
2323
name: "simple",
24-
suiteFile: "testdata/simple-suite.yaml",
24+
suiteFile: simpleSuite,
2525
prepare: func() {
26-
gock.New("http://foo").
26+
gock.New(urlFoo).
2727
Get("/bar").
2828
Reply(http.StatusOK).
2929
JSON("{}")
3030
},
3131
hasError: false,
3232
}, {
3333
name: "response is not JSON",
34-
suiteFile: "testdata/simple-suite.yaml",
34+
suiteFile: simpleSuite,
3535
prepare: func() {
36-
gock.New("http://foo").
36+
gock.New(urlFoo).
3737
Get("/bar").
3838
Reply(http.StatusOK)
3939
},
@@ -69,9 +69,9 @@ func TestRunCommand(t *testing.T) {
6969
hasErr bool
7070
}{{
7171
name: "status code is not match",
72-
args: []string{"-p", "testdata/simple-suite.yaml"},
72+
args: []string{"-p", simpleSuite},
7373
prepare: func() {
74-
gock.New("http://foo").Get("/bar")
74+
gock.New(urlFoo).Get("/bar")
7575
},
7676
hasErr: true,
7777
}, {
@@ -81,9 +81,9 @@ func TestRunCommand(t *testing.T) {
8181
hasErr: false,
8282
}, {
8383
name: "normal case",
84-
args: []string{"-p", "testdata/simple-suite.yaml"},
84+
args: []string{"-p", simpleSuite},
8585
prepare: func() {
86-
gock.New("http://foo").Get("/bar").Reply(http.StatusOK).JSON("{}")
86+
gock.New(urlFoo).Get("/bar").Reply(http.StatusOK).JSON("{}")
8787
},
8888
hasErr: false,
8989
}}
@@ -177,3 +177,6 @@ func TestPreRunE(t *testing.T) {
177177
})
178178
}
179179
}
180+
181+
const urlFoo = "http://foo"
182+
const simpleSuite = "testdata/simple-suite.yaml"

pkg/runner/kubernetes/verify_test.go

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
)
1313

1414
func TestKubernetesValidatorFunc(t *testing.T) {
15-
os.Setenv("KUBERNETES_SERVER", "http://foo")
15+
os.Setenv("KUBERNETES_SERVER", urlFoo)
1616
os.Setenv("KUBERNETES_TOKEN", "token")
1717
gock.InterceptClient(kubernetes.GetClient())
1818
defer gock.RestoreClient(http.DefaultClient)
@@ -92,44 +92,49 @@ func TestKubernetesValidatorFunc(t *testing.T) {
9292
}
9393
}
9494

95-
func emptyPrepare() {}
95+
func emptyPrepare() {
96+
// only for testing
97+
}
9698

9799
func preparePod() {
98-
gock.New("http://foo").
100+
gock.New(urlFoo).
99101
Get("/api/v1/namespaces/ns/pods/foo").
100-
MatchHeader("Authorization", "Bearer token").
102+
MatchHeader("Authorization", defaultToken).
101103
Reply(http.StatusOK).
102104
JSON(`{"kind":"pod"}`)
103105
}
104106

105107
func prepareDeploy() {
106-
gock.New("http://foo").
108+
gock.New(urlFoo).
107109
Get("/apis/apps/v1/namespaces/ns/deployments/foo").
108-
MatchHeader("Authorization", "Bearer token").
110+
MatchHeader("Authorization", defaultToken).
109111
Reply(http.StatusOK).
110112
JSON(`{"kind":"deploy"}`)
111113
}
112114

113115
func prepareStatefulset() {
114-
gock.New("http://foo").
116+
gock.New(urlFoo).
115117
Get("/apis/apps/v1/namespaces/ns/statefulsets/foo").
116-
MatchHeader("Authorization", "Bearer token").
118+
MatchHeader("Authorization", defaultToken).
117119
Reply(http.StatusOK).
118120
JSON(`{"kind":"statefulset"}`)
119121
}
120122

121123
func prepareDaemonset() {
122-
gock.New("http://foo").
124+
gock.New(urlFoo).
123125
Get("/apis/apps/v1/namespaces/ns/daemonsets/foo").
124-
MatchHeader("Authorization", "Bearer token").
126+
MatchHeader("Authorization", defaultToken).
125127
Reply(http.StatusOK).
126128
JSON(`{"kind":"daemonset"}`)
127129
}
128130

129131
func prepareCRDVM() {
130-
gock.New("http://foo").
132+
gock.New(urlFoo).
131133
Get("/apis/bar/v2/namespaces/ns/vms/foo").
132-
MatchHeader("Authorization", "Bearer token").
134+
MatchHeader("Authorization", defaultToken).
133135
Reply(http.StatusOK).
134136
JSON(`{"kind":"vm"}`)
135137
}
138+
139+
const urlFoo = "http://foo"
140+
const defaultToken = "Bearer token"

pkg/runner/reporter_discard.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ func NewDiscardTestReporter() TestReporter {
99
}
1010

1111
// PutRecord does nothing
12-
func (r *discardTestReporter) PutRecord(*ReportRecord) {}
12+
func (r *discardTestReporter) PutRecord(*ReportRecord) {
13+
// Do nothing which is the design purpose
14+
}
1315

1416
// GetAllRecords does nothing
1517
func (r *discardTestReporter) GetAllRecords() []*ReportRecord {

pkg/runner/reporter_memory.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,17 @@ func (r *memoryTestReporter) GetAllRecords() []*ReportRecord {
3434
return r.records
3535
}
3636

37+
func getMaxAndMin(max, min, duration time.Duration) (time.Duration, time.Duration) {
38+
if max < duration {
39+
max = duration
40+
}
41+
42+
if min > duration {
43+
min = duration
44+
}
45+
return max, min
46+
}
47+
3748
// ExportAllReportResults exports all the report results
3849
func (r *memoryTestReporter) ExportAllReportResults() (result ReportResultSlice, err error) {
3950
resultWithTotal := map[string]*ReportResultWithTotal{}
@@ -42,13 +53,7 @@ func (r *memoryTestReporter) ExportAllReportResults() (result ReportResultSlice,
4253
duration := record.Duration()
4354

4455
if item, ok := resultWithTotal[api]; ok {
45-
if item.Max < duration {
46-
item.Max = duration
47-
}
48-
49-
if item.Min > duration {
50-
item.Min = duration
51-
}
56+
item.Max, item.Min = getMaxAndMin(item.Max, item.Min, duration)
5257
item.Error += record.ErrorCount()
5358
item.Total += duration
5459
item.Count += 1

pkg/runner/reporter_memory_test.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ import (
1010
"github.com/stretchr/testify/assert"
1111
)
1212

13+
const urlFoo = "http://foo"
14+
const urlBar = "http://bar"
15+
const urlFake = "http://fake"
16+
1317
func TestExportAllReportResults(t *testing.T) {
1418
now := time.Now()
1519

@@ -24,28 +28,28 @@ func TestExportAllReportResults(t *testing.T) {
2428
}, {
2529
name: "normal",
2630
records: []*runner.ReportRecord{{
27-
API: "http://foo",
31+
API: urlFoo,
2832
Method: http.MethodGet,
2933
BeginTime: now,
3034
EndTime: now.Add(time.Second * 3),
3135
}, {
32-
API: "http://foo",
36+
API: urlFoo,
3337
Method: http.MethodGet,
3438
BeginTime: now,
3539
EndTime: now.Add(time.Second * 4),
3640
Error: errors.New("fake"),
3741
}, {
38-
API: "http://foo",
42+
API: urlFoo,
3943
Method: http.MethodGet,
4044
BeginTime: now,
4145
EndTime: now.Add(time.Second * 2),
4246
}, {
43-
API: "http://bar",
47+
API: urlBar,
4448
Method: http.MethodGet,
4549
BeginTime: now,
4650
EndTime: now.Add(time.Second),
4751
}, {
48-
API: "http://fake",
52+
API: urlFake,
4953
Method: http.MethodGet,
5054
BeginTime: now,
5155
EndTime: now.Add(time.Second * 5),

pkg/runner/simple.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ func (r *simpleTestCaseRunner) RunTestCase(testcase *testing.TestCase, dataConte
217217
}
218218

219219
if len(testcase.Request.Form) > 0 {
220-
if testcase.Request.Header["Content-Type"] == "multipart/form-data" {
220+
if testcase.Request.Header[contentType] == "multipart/form-data" {
221221
multiBody := &bytes.Buffer{}
222222
writer := multipart.NewWriter(multiBody)
223223
for key, val := range testcase.Request.Form {
@@ -226,8 +226,8 @@ func (r *simpleTestCaseRunner) RunTestCase(testcase *testing.TestCase, dataConte
226226

227227
_ = writer.Close()
228228
requestBody = multiBody
229-
testcase.Request.Header["Content-Type"] = writer.FormDataContentType()
230-
} else if testcase.Request.Header["Content-Type"] == "application/x-www-form-urlencoded" {
229+
testcase.Request.Header[contentType] = writer.FormDataContentType()
230+
} else if testcase.Request.Header[contentType] == "application/x-www-form-urlencoded" {
231231
data := url.Values{}
232232
for key, val := range testcase.Request.Form {
233233
data.Set(key, val)
@@ -436,3 +436,5 @@ func jsonSchemaValidation(schema string, body []byte) (err error) {
436436
}
437437
return
438438
}
439+
440+
const contentType = "Content-Type"

0 commit comments

Comments
 (0)