@@ -17,6 +17,7 @@ package cmd
1717
1818import (
1919 "bytes"
20+ "context"
2021 _ "embed"
2122 "errors"
2223 "fmt"
@@ -27,6 +28,7 @@ import (
2728 "path/filepath"
2829 "strings"
2930 "testing"
31+ "time"
3032
3133 "github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
3234 "github.com/h2non/gock"
@@ -214,7 +216,7 @@ func TestFrontEndHandlerWithLocation(t *testing.T) {
214216
215217func TestProxy (t * testing.T ) {
216218 t .Run ("normal" , func (t * testing.T ) {
217- gock .Off ()
219+ defer gock .Off ()
218220
219221 gock .New ("http://localhost:8080" ).Post ("/api/v1/echo" ).Reply (http .StatusOK )
220222 gock .New ("http://localhost:9090" ).Post ("/api/v1/echo" ).Reply (http .StatusOK )
@@ -228,7 +230,7 @@ func TestProxy(t *testing.T) {
228230 })
229231
230232 t .Run ("no proxy" , func (t * testing.T ) {
231- gock .Off ()
233+ defer gock .Off ()
232234
233235 gock .New ("http://localhost:8080" ).Post ("/api/v1/echo" ).Reply (http .StatusOK )
234236
@@ -286,18 +288,55 @@ func TestStartPlugins(t *testing.T) {
286288 err = os .WriteFile (filepath .Join (dir , "stores.yaml" ), []byte (sampleStores ), 0644 )
287289 assert .NoError (t , err )
288290
289- rootCmd := & cobra.Command {
290- Use : "atest" ,
291- }
292- rootCmd .SetOut (io .Discard )
293- rootCmd .AddCommand (createServerCmd (
294- fakeruntime.FakeExecer {ExpectOS : "linux" , ExpectLookPathError : errors .New ("not-found" )},
295- server .NewFakeHTTPServer (),
296- ))
297-
298- rootCmd .SetArgs ([]string {"server" , "--config-dir" , dir , "--dry-run" , "--port=0" , "--http-port=0" })
299- err = rootCmd .Execute ()
300- assert .NoError (t , err )
291+ t .Run ("dry-run" , func (t * testing.T ) {
292+ rootCmd := & cobra.Command {
293+ Use : "atest" ,
294+ }
295+ rootCmd .SetOut (io .Discard )
296+ rootCmd .AddCommand (createServerCmd (
297+ fakeruntime.FakeExecer {ExpectOS : "linux" , ExpectLookPathError : errors .New ("not-found" )},
298+ server .NewFakeHTTPServer (),
299+ ))
300+
301+ rootCmd .SetArgs ([]string {"server" , "--config-dir" , dir , "--dry-run" , "--port=0" , "--http-port=0" })
302+ err = rootCmd .Execute ()
303+ assert .NoError (t , err )
304+ })
305+
306+ t .Run ("normal" , func (t * testing.T ) {
307+ httpServer := server .NewDefaultHTTPServer ()
308+ rootCmd := & cobra.Command {
309+ Use : "atest" ,
310+ }
311+ rootCmd .SetOut (io .Discard )
312+ rootCmd .AddCommand (createServerCmd (
313+ fakeruntime.FakeExecer {ExpectOS : "linux" , ExpectLookPathError : errors .New ("not-found" )},
314+ httpServer ,
315+ ))
316+
317+ rootCmd .SetArgs ([]string {"server" , "--config-dir" , dir , "--port=0" , "--http-port=0" })
318+ go func () {
319+ err = rootCmd .Execute ()
320+ assert .NoError (t , err )
321+ }()
322+
323+ for httpServer .GetPort () == "" {
324+ time .Sleep (time .Second )
325+ }
326+
327+ defer func () {
328+ httpServer .Shutdown (context .Background ())
329+ }()
330+ resp , err := http .Post (fmt .Sprintf ("http://localhost:%s/server.Runner/GetSuites" , httpServer .GetPort ()), util .JSON , nil )
331+ if assert .NoError (t , err ) {
332+ assert .Equal (t , http .StatusOK , resp .StatusCode )
333+ }
334+
335+ resp , err = http .Get (fmt .Sprintf ("http://localhost:%s/metrics" , httpServer .GetPort ()))
336+ if assert .NoError (t , err ) {
337+ assert .Equal (t , http .StatusOK , resp .StatusCode )
338+ }
339+ })
301340}
302341
303342type fakeResponseWriter struct {
0 commit comments