Skip to content

Commit 56a3877

Browse files
authored
feat: support to download atest self (#180)
1 parent 98a8d5d commit 56a3877

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed

cmd/server.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ func (o *serverOption) runE(cmd *cobra.Command, args []string) (err error) {
125125
mux.HandlePath(http.MethodGet, "/", frontEndHandlerWithLocation(o.consolePath))
126126
mux.HandlePath(http.MethodGet, "/assets/{asset}", frontEndHandlerWithLocation(o.consolePath))
127127
mux.HandlePath(http.MethodGet, "/healthz", frontEndHandlerWithLocation(o.consolePath))
128+
mux.HandlePath(http.MethodGet, "/get", o.getAtestBinary)
128129
debugHandler(mux)
129130
o.httpServer.WithHandler(mux)
130131
log.Printf("HTTP server listening at %v", httplis.Addr())
@@ -186,6 +187,25 @@ func debugHandler(mux *runtime.ServeMux) {
186187
})
187188
}
188189

190+
func (o *serverOption) getAtestBinary(w http.ResponseWriter, r *http.Request, pathParams map[string]string) {
191+
w.Header().Set("Content-Disposition", "attachment; filename=atest")
192+
w.Header().Set("Content-Type", "application/octet-stream")
193+
w.Header().Set("Content-Transfer-Encoding", "binary")
194+
w.Header().Set("Expires", "0")
195+
w.Header().Set("Pragma", "no-cache")
196+
w.Header().Set("Cache-Control", "no-cache")
197+
198+
var data []byte
199+
if atestPath, err := o.execer.LookPath("atest"); err == nil {
200+
if data, err = os.ReadFile(atestPath); err != nil {
201+
data = []byte(fmt.Sprintf("failed to read atest: %v", err))
202+
}
203+
} else {
204+
data = []byte("not found atest")
205+
}
206+
w.Write(data)
207+
}
208+
189209
type gRPCServer interface {
190210
Serve(lis net.Listener) error
191211
grpc.ServiceRegistrar

cmd/server_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cmd
22

33
import (
44
"bytes"
5+
"errors"
56
"fmt"
67
"net"
78
"net/http"
@@ -137,6 +138,39 @@ func TestFrontEndHandlerWithLocation(t *testing.T) {
137138
assert.Equal(t, http.StatusOK, resp.StatusCode)
138139
}
139140
})
141+
142+
t.Run("download atest", func(t *testing.T) {
143+
opt := &serverOption{
144+
execer: fakeruntime.FakeExecer{
145+
ExpectOS: "linux",
146+
ExpectLookPathError: errors.New("fake"),
147+
},
148+
}
149+
150+
req, err := http.NewRequest(http.MethodGet, "/get", nil)
151+
assert.NoError(t, err)
152+
153+
resp := newFakeResponseWriter()
154+
155+
opt.getAtestBinary(resp, req, map[string]string{})
156+
assert.Equal(t, "not found atest", resp.GetBody().String())
157+
})
158+
159+
t.Run("download atest, failed to read", func(t *testing.T) {
160+
opt := &serverOption{
161+
execer: fakeruntime.FakeExecer{
162+
ExpectOS: "linux",
163+
},
164+
}
165+
166+
req, err := http.NewRequest(http.MethodGet, "/get", nil)
167+
assert.NoError(t, err)
168+
169+
resp := newFakeResponseWriter()
170+
171+
opt.getAtestBinary(resp, req, map[string]string{})
172+
assert.Equal(t, "failed to read atest: open : no such file or directory", resp.GetBody().String())
173+
})
140174
}
141175

142176
type fakeResponseWriter struct {

docs/README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,28 @@ jmeter -n -t bin/gitee.jmx
7474

7575
Please feel free to bring more test tool converters.
7676

77+
## Run in Jenkins
78+
You can run the API testings in Jenkins, see also the following example:
79+
80+
```Jenkinsfile
81+
pipeline {
82+
agent any
83+
84+
stages() {
85+
stage('test') {
86+
steps {
87+
sh '''
88+
curl http://localhost:9090/get -o atest
89+
chmod u+x atest
90+
91+
./atest run -p http://localhost:9090/server.Runner/ConvertTestSuite?suite=api-testing
92+
'''
93+
}
94+
}
95+
}
96+
}
97+
```
98+
7799
## Storage
78100
There are multiple storage backends supported. See the status from the list:
79101

0 commit comments

Comments
 (0)