Skip to content

Commit 4cb30e3

Browse files
committed
add unit tests for collector
1 parent 3b4dea9 commit 4cb30e3

15 files changed

+257
-179
lines changed

.github/workflows/build.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ jobs:
1616
- name: Test
1717
run: |
1818
go test ./... -coverprofile coverage.out
19+
make test test-collector
1920
- name: Report
2021
if: github.actor == 'linuxsuren'
2122
env:

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
bin/
22
.idea/
33
coverage.out
4+
collector-coverage.out
45
dist/
56
.vscode/launch.json
67
sample.yaml

Makefile

+3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ copy-restart: build
1717
test:
1818
go test ./... -cover -v -coverprofile=coverage.out
1919
go tool cover -func=coverage.out
20+
test-collector:
21+
go test github.com/linuxsuren/api-testing/extensions/collector/./... -cover -v -coverprofile=collector-coverage.out
22+
go tool cover -func=collector-coverage.out
2023
grpc:
2124
protoc --go_out=. --go_opt=paths=source_relative \
2225
--go-grpc_out=. --go-grpc_opt=paths=source_relative \

extensions/collector/Makefile

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
test:
2+
go test ./... -cover -v -coverprofile=coverage.out
3+
go tool cover -func=coverage.out

extensions/collector/cmd/collect.go

+7-69
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,15 @@ package cmd
33
import (
44
"context"
55
"fmt"
6-
"io"
76
"net/http"
87
"os"
98
"os/signal"
10-
"strings"
119
"syscall"
1210

1311
"github.com/elazarl/goproxy"
1412
"github.com/linuxsuren/api-testing/extensions/collector/pkg"
1513
"github.com/linuxsuren/api-testing/extensions/collector/pkg/filter"
16-
atestpkg "github.com/linuxsuren/api-testing/pkg/testing"
1714
"github.com/spf13/cobra"
18-
"gopkg.in/yaml.v2"
1915
)
2016

2117
type option struct {
@@ -24,6 +20,7 @@ type option struct {
2420
output string
2521
}
2622

23+
// NewRootCmd creates the root command
2724
func NewRootCmd() (c *cobra.Command) {
2825
opt := &option{}
2926
c = &cobra.Command{
@@ -36,7 +33,7 @@ func NewRootCmd() (c *cobra.Command) {
3633
flags.StringVarP(&opt.filterPath, "filter-path", "", "", "The path prefix for filtering")
3734
flags.StringVarP(&opt.output, "output", "o", "sample.yaml", "The output file")
3835

39-
cobra.MarkFlagRequired(flags, "filter-path")
36+
_ = cobra.MarkFlagRequired(flags, "filter-path")
4037
return
4138
}
4239

@@ -54,12 +51,8 @@ func (o *option) runE(cmd *cobra.Command, args []string) (err error) {
5451
return r, nil
5552
})
5653

57-
exporter := &sampleExporter{
58-
testSuite: atestpkg.TestSuite{
59-
Name: "sample",
60-
},
61-
}
62-
collects.AddEvent(exporter.add)
54+
exporter := pkg.NewSampleExporter()
55+
collects.AddEvent(exporter.Add)
6356

6457
srv := &http.Server{
6558
Addr: fmt.Sprintf(":%d", o.port),
@@ -71,69 +64,14 @@ func (o *option) runE(cmd *cobra.Command, args []string) (err error) {
7164
go func() {
7265
<-sig
7366
collects.Stop()
74-
srv.Shutdown(context.Background())
67+
_ = srv.Shutdown(context.Background())
7568
}()
7669

7770
cmd.Println("Starting the proxy server with port", o.port)
78-
srv.ListenAndServe()
71+
_ = srv.ListenAndServe()
7972
var data string
80-
if data, err = exporter.export(); err == nil {
73+
if data, err = exporter.Export(); err == nil {
8174
err = os.WriteFile(o.output, []byte(data), 0644)
8275
}
8376
return
8477
}
85-
86-
type sampleExporter struct {
87-
testSuite atestpkg.TestSuite
88-
}
89-
90-
func (e *sampleExporter) add(r *http.Request) {
91-
body := r.Body
92-
data, _ := io.ReadAll(body)
93-
94-
fmt.Println("receive", r.URL.Path)
95-
req := atestpkg.Request{
96-
API: r.URL.String(),
97-
Method: r.Method,
98-
Header: map[string]string{},
99-
Body: string(data),
100-
}
101-
102-
testCase := atestpkg.TestCase{
103-
Request: req,
104-
Expect: atestpkg.Response{
105-
StatusCode: 200,
106-
},
107-
}
108-
109-
specs := strings.Split(r.URL.Path, "/")
110-
if len(specs) > 0 {
111-
testCase.Name = specs[len(specs)-1]
112-
}
113-
114-
if val := r.Header.Get("Content-Type"); val != "" {
115-
req.Header["Content-Type"] = val
116-
}
117-
118-
e.testSuite.Items = append(e.testSuite.Items, testCase)
119-
}
120-
121-
var prefix = `#!api-testing
122-
# yaml-language-server: $schema=https://gitee.com/linuxsuren/api-testing/raw/master/sample/api-testing-schema.json
123-
`
124-
125-
func (e *sampleExporter) export() (string, error) {
126-
marker := map[string]int{}
127-
128-
for i, item := range e.testSuite.Items {
129-
if _, ok := marker[item.Name]; ok {
130-
marker[item.Name]++
131-
e.testSuite.Items[i].Name = fmt.Sprintf("%s-%d", item.Name, marker[item.Name])
132-
} else {
133-
marker[item.Name] = 0
134-
}
135-
}
136-
137-
data, err := yaml.Marshal(e.testSuite)
138-
return prefix + string(data), err
139-
}
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package cmd
2+
3+
import (
4+
"github.com/stretchr/testify/assert"
5+
"testing"
6+
)
7+
8+
func TestNewRootCmd(t *testing.T) {
9+
c := NewRootCmd()
10+
assert.NotNil(t, c)
11+
assert.Equal(t, "atest-collector", c.Use)
12+
}

extensions/collector/go.mod

+13-19
Original file line numberDiff line numberDiff line change
@@ -2,43 +2,37 @@ module github.com/linuxsuren/api-testing/extensions/collector
22

33
go 1.19
44

5+
require (
6+
github.com/elazarl/goproxy v0.0.0-20221015165544-a0805db90819
7+
github.com/linuxsuren/api-testing v0.0.11
8+
github.com/spf13/cobra v1.7.0
9+
github.com/stretchr/testify v1.8.4
10+
gopkg.in/yaml.v2 v2.4.0
11+
)
12+
513
require (
614
github.com/Masterminds/goutils v1.1.1 // indirect
715
github.com/Masterminds/semver/v3 v3.2.0 // indirect
816
github.com/Masterminds/sprig/v3 v3.2.3 // indirect
9-
github.com/andreyvit/diff v0.0.0-20170406064948-c7f18ee00883 // indirect
10-
github.com/antonmedv/expr v1.12.1 // indirect
11-
github.com/elazarl/goproxy v0.0.0-20221015165544-a0805db90819 // indirect
17+
github.com/davecgh/go-spew v1.1.1 // indirect
1218
github.com/ghodss/yaml v1.0.0 // indirect
13-
github.com/golang/protobuf v1.5.2 // indirect
1419
github.com/google/uuid v1.3.0 // indirect
1520
github.com/huandu/xstrings v1.3.3 // indirect
16-
github.com/iancoleman/orderedmap v0.0.0-20190318233801-ac98e3ecb4b0 // indirect
1721
github.com/imdario/mergo v0.3.11 // indirect
1822
github.com/inconshreveable/mousetrap v1.1.0 // indirect
19-
github.com/invopop/jsonschema v0.7.0 // indirect
20-
github.com/linuxsuren/api-testing v0.0.11 // indirect
21-
github.com/linuxsuren/go-fake-runtime v0.0.0-20230413085645-15e77ab55dbd // indirect
22-
github.com/linuxsuren/unstructured v0.0.1 // indirect
23+
github.com/kr/pretty v0.1.0 // indirect
2324
github.com/mitchellh/copystructure v1.0.0 // indirect
2425
github.com/mitchellh/reflectwalk v1.0.0 // indirect
25-
github.com/sergi/go-diff v1.2.0 // indirect
26+
github.com/pmezard/go-difflib v1.0.0 // indirect
2627
github.com/shopspring/decimal v1.2.0 // indirect
2728
github.com/spf13/cast v1.3.1 // indirect
28-
github.com/spf13/cobra v1.7.0 // indirect
2929
github.com/spf13/pflag v1.0.5 // indirect
3030
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f // indirect
3131
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
3232
github.com/xeipuuv/gojsonschema v1.2.0 // indirect
3333
golang.org/x/crypto v0.3.0 // indirect
34-
golang.org/x/net v0.8.0 // indirect
35-
golang.org/x/sync v0.1.0 // indirect
36-
golang.org/x/sys v0.6.0 // indirect
37-
golang.org/x/text v0.8.0 // indirect
38-
google.golang.org/genproto v0.0.0-20230110181048-76db0878b65f // indirect
39-
google.golang.org/grpc v1.54.0 // indirect
40-
google.golang.org/protobuf v1.30.0 // indirect
41-
gopkg.in/yaml.v2 v2.3.0 // indirect
34+
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
35+
gopkg.in/yaml.v3 v3.0.1 // indirect
4236
)
4337

4438
replace github.com/linuxsuren/api-testing => ../../.

0 commit comments

Comments
 (0)