github.com/drycc/workflow-cli@v1.5.3-0.20240322092846-d4ee25983af9/cmd/gateways_test.go (about) 1 package cmd 2 3 import ( 4 "bytes" 5 "fmt" 6 "net/http" 7 "testing" 8 9 "github.com/drycc/controller-sdk-go/api" 10 "github.com/stretchr/testify/assert" 11 12 "github.com/drycc/workflow-cli/pkg/testutil" 13 ) 14 15 func TestGatewaysList(t *testing.T) { 16 t.Parallel() 17 cf, server, err := testutil.NewTestServerAndClient() 18 if err != nil { 19 t.Fatal(err) 20 } 21 defer server.Close() 22 var b bytes.Buffer 23 cmdr := DryccCmd{WOut: &b, ConfigFile: cf} 24 25 server.Mux.HandleFunc("/v2/apps/foo/gateways/", func(w http.ResponseWriter, _ *http.Request) { 26 testutil.SetHeaders(w) 27 fmt.Fprintf(w, `{ 28 "count": 1, 29 "next": null, 30 "previous": null, 31 "results": [ 32 { 33 "app": "foo", 34 "name": "foo", 35 "created": "2023-04-19T00:00:00UTC", 36 "owner": "test", 37 "updated": "2023-04-19T00:00:00UTC", 38 "listeners": [ 39 { 40 "name": "foo-80-http", 41 "port": 80, 42 "protocol": "HTTP", 43 "allowedRoutes": {"namespaces": {"from": "All"}} 44 }, 45 { 46 "name": "foo-443-https", 47 "port": 443, 48 "protocol": "HTTPS", 49 "allowedRoutes": {"namespaces": {"from": "All"}} 50 } 51 ], 52 "addresses": [ 53 { 54 "type": "IPAddress", 55 "value": "192.168.11.1" 56 } 57 ] 58 } 59 ] 60 }`) 61 }) 62 63 err = cmdr.GatewaysList("foo", -1) 64 assert.NoError(t, err) 65 66 assert.Equal(t, b.String(), `NAME LISENTER PORT PROTOCOL ADDRESSES 67 foo foo-80-http 80 HTTP 192.168.11.1 68 foo foo-443-https 443 HTTPS 192.168.11.1 69 `, "output") 70 } 71 72 func TestGatewaysAdd(t *testing.T) { 73 t.Parallel() 74 cf, server, err := testutil.NewTestServerAndClient() 75 if err != nil { 76 t.Fatal(err) 77 } 78 defer server.Close() 79 var b bytes.Buffer 80 cmdr := DryccCmd{WOut: &b, ConfigFile: cf} 81 82 server.Mux.HandleFunc("/v2/apps/foo/gateways/", func(w http.ResponseWriter, r *http.Request) { 83 testutil.AssertBody(t, api.GatewayCreateRequest{Name: "example-go", Port: 443, Protocol: "HTTPS"}, r) 84 testutil.SetHeaders(w) 85 w.WriteHeader(http.StatusCreated) 86 // Body isn't used by CLI, so it isn't set. 87 w.Write([]byte("{}")) 88 }) 89 90 err = cmdr.GatewaysAdd("foo", "example-go", 443, "HTTPS") 91 assert.NoError(t, err) 92 93 assert.Equal(t, testutil.StripProgress(b.String()), "Adding gateway example-go to foo... done\n", "output") 94 } 95 96 func TestGatewaysDelete(t *testing.T) { 97 t.Parallel() 98 cf, server, err := testutil.NewTestServerAndClient() 99 if err != nil { 100 t.Fatal(err) 101 } 102 defer server.Close() 103 var b bytes.Buffer 104 cmdr := DryccCmd{WOut: &b, ConfigFile: cf} 105 106 server.Mux.HandleFunc("/v2/apps/foo/gateways/", func(w http.ResponseWriter, _ *http.Request) { 107 testutil.SetHeaders(w) 108 w.WriteHeader(http.StatusNoContent) 109 }) 110 111 err = cmdr.GatewaysRemove("foo", "example-go", 443, "HTTPS") 112 assert.NoError(t, err) 113 114 assert.Equal(t, testutil.StripProgress(b.String()), "Removing gateway example-go to foo... done\n", "output") 115 }