Skip to content

Commit 9e4259f

Browse files
authored
support to run test case with filter (#560)
* feat: support to get go mod version * support to run test case with filter * fix unit tests * fix dockerfile * add missing console/atest-ui/package.json * fix the e2e --------- Co-authored-by: rick <[email protected]>
1 parent 19871a5 commit 9e4259f

File tree

23 files changed

+326
-8
lines changed

23 files changed

+326
-8
lines changed

Dockerfile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ FROM docker.io/golang:1.22.4 AS builder
1010
ARG VERSION
1111
ARG GOPROXY
1212
WORKDIR /workspace
13+
RUN mkdir -p console/atest-ui
14+
1315
COPY cmd/ cmd/
1416
COPY pkg/ pkg/
1517
COPY operator/ operator/
@@ -21,6 +23,8 @@ COPY go.sum go.sum
2123
COPY go.work go.work
2224
COPY go.work.sum go.work.sum
2325
COPY main.go main.go
26+
COPY console/atest-ui/ui.go console/atest-ui/ui.go
27+
COPY console/atest-ui/package.json console/atest-ui/package.json
2428
COPY README.md README.md
2529
COPY LICENSE LICENSE
2630

cmd/run.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ type runOption struct {
5151
duration time.Duration
5252
requestTimeout time.Duration
5353
requestIgnoreError bool
54+
caseFilter string
5455
thread int64
5556
context context.Context
5657
qps int32
@@ -116,6 +117,7 @@ See also https://github.com/LinuxSuRen/api-testing/tree/master/sample`,
116117
flags.DurationVarP(&opt.duration, "duration", "", 0, "Running duration")
117118
flags.DurationVarP(&opt.requestTimeout, "request-timeout", "", time.Minute, "Timeout for per request")
118119
flags.BoolVarP(&opt.requestIgnoreError, "request-ignore-error", "", false, "Indicate if ignore the request error")
120+
flags.StringVarP(&opt.caseFilter, "case-filter", "", "", "The filter of the test case")
119121
flags.StringVarP(&opt.report, "report", "", "", "The type of target report. Supported: markdown, md, html, json, discard, std, prometheus, http, grpc")
120122
flags.StringVarP(&opt.reportFile, "report-file", "", "", "The file path of the report")
121123
flags.BoolVarP(&opt.reportIgnore, "report-ignore", "", false, "Indicate if ignore the report output")
@@ -130,8 +132,14 @@ See also https://github.com/LinuxSuRen/api-testing/tree/master/sample`,
130132
return
131133
}
132134

135+
const caseFilter = "case-filter"
136+
133137
func (o *runOption) preRunE(cmd *cobra.Command, args []string) (err error) {
134-
o.context = cmd.Context()
138+
ctx := cmd.Context()
139+
if ctx == nil {
140+
ctx = context.Background()
141+
}
142+
o.context = context.WithValue(ctx, caseFilter, o.caseFilter)
135143
writer := cmd.OutOrStdout()
136144

137145
if o.reportFile != "" && !strings.HasPrefix(o.reportFile, "http://") && !strings.HasPrefix(o.reportFile, "https://") {
@@ -345,8 +353,15 @@ func (o *runOption) runSuite(loader testing.Loader, dataContext map[string]inter
345353
suiteRunner.WithOutputWriter(os.Stdout)
346354
suiteRunner.WithWriteLevel(o.level)
347355
suiteRunner.WithSuite(testSuite)
348-
runLogger.Info("run test suite", "name", testSuite.Name)
356+
var caseFilterObj interface{}
357+
if o.context != nil {
358+
caseFilterObj = o.context.Value(caseFilter)
359+
}
360+
runLogger.Info("run test suite", "name", testSuite.Name, "filter", caseFilter)
349361
for _, testCase := range testSuite.Items {
362+
if caseFilterObj != nil && !strings.Contains(testCase.Name, caseFilterObj.(string)) {
363+
continue
364+
}
350365
if !testCase.InScope(o.caseItems) {
351366
continue
352367
}

cmd/server.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,7 @@ func (o *serverOption) runE(cmd *cobra.Command, args []string) (err error) {
337337
mux.HandlePath(http.MethodGet, "/swagger.json", frontEndHandlerWithLocation(o.consolePath))
338338
mux.HandlePath(http.MethodGet, "/get", o.getAtestBinary)
339339
mux.HandlePath(http.MethodPost, "/runner/{suite}/{case}", service.WebRunnerHandler)
340+
mux.HandlePath(http.MethodGet, "/api/v1/sbom", service.SBomHandler)
340341

341342
postRequestProxyFunc := postRequestProxy(o.skyWalking)
342343
mux.HandlePath(http.MethodPost, "/browser/{app}", postRequestProxyFunc)

console/atest-ui/src/views/TemplateFunctions.vue

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,8 @@ Magic.Keys(() => {
5656
</el-table-column>
5757
</el-table>
5858
</span>
59+
<div>
60+
Powered by <a href="https://masterminds.github.io/sprig/" target="_blank">Sprig</a> and <a href="https://pkg.go.dev/text/template" target="_blank">built-in templates</a>.
61+
</div>
5962
</el-dialog>
6063
</template>

console/atest-ui/src/views/WelcomePage.vue

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1+
<script setup lang="ts">
2+
import { ref } from 'vue'
3+
import { API } from '../views/net'
4+
5+
interface SBOM {
6+
go: {}
7+
js: {
8+
dependencies: {}
9+
devDependencies: {}
10+
}
11+
}
12+
const sbomItems = ref({} as SBOM)
13+
API.SBOM((d) => {
14+
sbomItems.value = d
15+
})
16+
</script>
17+
118
<template>
219
<div>Welcome to use atest to improve your code quality!</div>
320
<div>Please read the following guide if this is your first time to use atest.</div>
@@ -8,4 +25,31 @@
825
<div>
926
Please get more details from the <a href="https://linuxsuren.github.io/api-testing/" target="_blank" rel="noopener">official document</a>.
1027
</div>
28+
29+
<el-divider/>
30+
31+
<div>
32+
Golang dependencies:
33+
<div>
34+
<el-scrollbar height="200px" always>
35+
<li v-for="k, v in sbomItems.go">
36+
{{ v }}@{{ k }}
37+
</li>
38+
</el-scrollbar>
39+
</div>
40+
</div>
41+
42+
<div>
43+
JavaScript dependencies:
44+
<div>
45+
<el-scrollbar height="200px" always>
46+
<li v-for="k, v in sbomItems.js.dependencies">
47+
{{ v }}@{{ k }}
48+
</li>
49+
<li v-for="k, v in sbomItems.js.devDependencies">
50+
{{ v }}@{{ k }}
51+
</li>
52+
</el-scrollbar>
53+
</div>
54+
</div>
1155
</template>

console/atest-ui/src/views/net.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -767,6 +767,11 @@ function DownloadResponseFile(testcase,
767767
.then(callback).catch(errHandle)
768768
}
769769

770+
var SBOM = (callback: (d: any) => void) => {
771+
fetch(`/api/sbom`, {})
772+
.then(DefaultResponseProcess)
773+
.then(callback)
774+
}
770775

771776
export const API = {
772777
DefaultResponseProcess,
@@ -780,6 +785,6 @@ export const API = {
780785
FunctionsQuery,
781786
GetSecrets, DeleteSecret, CreateOrUpdateSecret,
782787
GetSuggestedAPIs,
783-
ReloadMockServer, GetMockConfig,
788+
ReloadMockServer, GetMockConfig, SBOM,
784789
getToken
785790
}

console/atest-ui/ui.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package ui
2+
3+
import (
4+
_ "embed"
5+
"encoding/json"
6+
)
7+
8+
//go:embed package.json
9+
var packageJSON []byte
10+
11+
type JSON struct {
12+
Dependencies map[string]string `json:"dependencies"`
13+
DevDependencies map[string]string `json:"devDependencies"`
14+
}
15+
16+
func GetPackageJSON() (data JSON) {
17+
data = JSON{}
18+
_ = json.Unmarshal(packageJSON, &data)
19+
return
20+
}

docs/site/content/zh/latest/tasks/quickstart.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,10 @@ description: 只需几个简单的步骤即可开始使用 API Testing。
66

77
本指南将帮助您通过几个简单的步骤开始使用 API Testing。
88

9-
// TBD
9+
## 执行部分测试用例
10+
11+
下面的命令会执行名称中包含 `sbom` 的所有测试用例:
12+
13+
```shell
14+
atest run -p test-suite.yaml --case-filter sbom
15+
```

docs/site/content/zh/latest/tasks/verify.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,30 @@ title = "测试用例验证"
1616
verify:
1717
- len(data.data) == 6
1818
```
19+
20+
## 数组值检查
21+
22+
```yaml
23+
- name: popularHeaders
24+
request:
25+
api: /popularHeaders
26+
expect:
27+
verify:
28+
- any(data.data, {.key == "Content-Type"})
29+
```
30+
31+
[更多用法](https://expr-lang.org/docs/language-definition#any).
32+
33+
## 字符串判断
34+
35+
```yaml
36+
- name: metrics
37+
request:
38+
api: |
39+
{{.param.server}}/metrics
40+
expect:
41+
verify:
42+
- indexOf(data, "atest_execution_count") != -1
43+
```
44+
45+
[更多用法](https://expr-lang.org/docs/language-definition#indexOf).

e2e/test-suite-common.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,3 +376,12 @@ items:
376376
- indexOf(data, "atest_execution_success") != -1
377377
- indexOf(data, "atest_runners_count") != -1
378378
- indexOf(data, "http_requests_total") != -1
379+
380+
- name: sbom
381+
request:
382+
api: /sbom
383+
expect:
384+
verify:
385+
- len(data.go) > 0
386+
- len(data.js.dependencies) > 0
387+
- len(data.js.devDependencies) > 0

0 commit comments

Comments
 (0)