diff --git a/go.mod b/go.mod index b3667470f0..3d883a9ad0 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,9 @@ module github.com/hashicorp/watchtower go 1.13 require ( + github.com/alecthomas/assert v0.0.0-20170929043011-405dbfeb8e38 + github.com/alecthomas/colour v0.1.0 // indirect + github.com/alecthomas/repr v0.0.0-20200325044227-4184120f674c // indirect github.com/armon/go-metrics v0.3.3 github.com/bufbuild/buf v0.16.0 github.com/fatih/color v1.9.0 diff --git a/go.sum b/go.sum index b9c24756ff..200c97f2d7 100644 --- a/go.sum +++ b/go.sum @@ -120,6 +120,12 @@ github.com/StackExchange/wmi v0.0.0-20180116203802-5d049714c4a6 h1:fLjPD/aNc3UIO github.com/StackExchange/wmi v0.0.0-20180116203802-5d049714c4a6/go.mod h1:3eOhrUMpNV+6aFIbp5/iudMxNCF27Vw2OZgy4xEx0Fg= github.com/abdullin/seq v0.0.0-20160510034733-d5467c17e7af/go.mod h1:5Jv4cbFiHJMsVxt52+i0Ha45fjshj6wxYr1r19tB9bw= github.com/agnivade/levenshtein v1.0.1/go.mod h1:CURSv5d9Uaml+FovSIICkLbAUZ9S4RqaHDIsdSBg7lM= +github.com/alecthomas/assert v0.0.0-20170929043011-405dbfeb8e38 h1:smF2tmSOzy2Mm+0dGI2AIUHY+w0BUc+4tn40djz7+6U= +github.com/alecthomas/assert v0.0.0-20170929043011-405dbfeb8e38/go.mod h1:r7bzyVFMNntcxPZXK3/+KdruV1H5KSlyVY0gc+NgInI= +github.com/alecthomas/colour v0.1.0 h1:nOE9rJm6dsZ66RGWYSFrXw461ZIt9A6+nHgL7FRrDUk= +github.com/alecthomas/colour v0.1.0/go.mod h1:QO9JBoKquHd+jz9nshCh40fOfO+JzsoXy8qTHF68zU0= +github.com/alecthomas/repr v0.0.0-20200325044227-4184120f674c h1:MVVbswUlqicyj8P/JljoocA7AyCo62gzD0O7jfvrhtE= +github.com/alecthomas/repr v0.0.0-20200325044227-4184120f674c/go.mod h1:xTS7Pm1pD1mvyM075QCDSRqH6qRLXylzS24ZTpRiSzQ= github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= @@ -1151,6 +1157,7 @@ github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdh github.com/sean-/conswriter v0.0.0-20180208195008-f5ae3917a627/go.mod h1:7zjs06qF79/FKAJpBvFx3P8Ww4UTIMAe+lpNXDHziac= github.com/sean-/pager v0.0.0-20180208200047-666be9bf53b5/go.mod h1:BeybITEsBEg6qbIiqJ6/Bqeq25bCLbL7YFmpaFfJDuM= github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc= +github.com/sergi/go-diff v1.0.0 h1:Kpca3qRNrduNnOQeazBd0ysaKrUJiIuISHxogkT9RPQ= github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo= github.com/shirou/gopsutil v2.19.9+incompatible h1:IrPVlK4nfwW10DF7pW+7YJKws9NkgNzWozwwWv9FsgY= github.com/shirou/gopsutil v2.19.9+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA= diff --git a/internal/servers/controller/handler_test.go b/internal/servers/controller/handler_test.go index 8af449456a..db6ceb372b 100644 --- a/internal/servers/controller/handler_test.go +++ b/internal/servers/controller/handler_test.go @@ -1,14 +1,10 @@ package controller import ( - "bytes" "context" "fmt" - "io/ioutil" "net/http" "net/http/httptest" - "os" - "path/filepath" "testing" "github.com/grpc-ecosystem/grpc-gateway/runtime" @@ -157,121 +153,3 @@ func TestHandleGrpcGateway(t *testing.T) { }) } } - -func TestHandleDevPassthrough(t *testing.T) { - // Create a temporary directory - tempDir, err := ioutil.TempDir("", "watchtower-test-") - require.NoError(t, err) - defer func() { - assert.NoError(t, os.RemoveAll(tempDir)) - }() - - nameContentsMap := map[string]string{ - "index.html": `index`, - "favicon.png": `favicon`, - "/assets/styles.css": `css`, - "index.htm": `badindex`, - } - - for k, v := range nameContentsMap { - dir := filepath.Dir(k) - if dir != "/" { - require.NoError(t, os.MkdirAll(filepath.Join(tempDir, dir), 0755)) - } - require.NoError(t, ioutil.WriteFile(filepath.Join(tempDir, k), []byte(v), 0644)) - } - - c := NewTestController(t, &TestControllerOpts{DisableAutoStart: true}) - - c.c.conf.RawConfig.PassthroughDirectory = tempDir - require.NoError(t, c.c.Start()) - defer c.Shutdown() - - cases := []struct { - name string - path string - contentsKey string - code int - mimeType string - }{ - { - "direct index", - "index.html", - "index.html", - http.StatusOK, - "text/html; charset=utf-8", - }, - { - "base slash", - "", - "index.html", - http.StatusOK, - "text/html; charset=utf-8", - }, - { - "no extension", - "orgs", - "index.html", - http.StatusOK, - "text/html; charset=utf-8", - }, - { - "favicon", - "favicon.png", - "favicon.png", - http.StatusOK, - "image/png", - }, - { - "bad index", - "index.htm", - "index.htm", - http.StatusOK, - "text/html; charset=utf-8", - }, - { - "bad path", - "index.ht", - "index.ht", - http.StatusNotFound, - "text/plain; charset=utf-8", - }, - { - "css", - "assets/styles.css", - "assets/styles.css", - http.StatusOK, - "text/css; charset=utf-8", - }, - { - "invalid extension", - "foo.bāb", - "index.html", - http.StatusOK, - "text/html; charset=utf-8", - }, - } - for _, tc := range cases { - t.Run(tc.name, func(t *testing.T) { - assert := assert.New(t) - - url := fmt.Sprintf("%s/%s", c.ApiAddrs()[0], tc.path) - resp, err := http.Post(url, "", nil) - assert.NoError(err) - assert.Equal(http.StatusMethodNotAllowed, resp.StatusCode) - - resp, err = http.Get(url) - assert.NoError(err) - assert.Equal(tc.code, resp.StatusCode) - assert.Equal(tc.mimeType, resp.Header.Get("content-type")) - - contents, ok := nameContentsMap[tc.contentsKey] - if ok { - reader := new(bytes.Buffer) - _, err = reader.ReadFrom(resp.Body) - assert.NoError(err) - assert.Equal(contents, reader.String()) - } - }) - } -} diff --git a/internal/servers/controller/handler_ui_test.go b/internal/servers/controller/handler_ui_test.go new file mode 100644 index 0000000000..fb02186563 --- /dev/null +++ b/internal/servers/controller/handler_ui_test.go @@ -0,0 +1,134 @@ +// +build ui + +package controller + +import ( + "bytes" + "fmt" + "io/ioutil" + "net/http" + "os" + "path/filepath" + "testing" + + "github.com/alecthomas/assert" + "github.com/stretchr/testify/require" +) + +func TestUiRouting(t *testing.T) { + // Create a temporary directory + tempDir, err := ioutil.TempDir("", "watchtower-test-") + require.NoError(t, err) + defer func() { + assert.NoError(t, os.RemoveAll(tempDir)) + }() + + nameContentsMap := map[string]string{ + "index.html": `index`, + "favicon.png": `favicon`, + "/assets/styles.css": `css`, + "index.htm": `badindex`, + } + + for k, v := range nameContentsMap { + dir := filepath.Dir(k) + if dir != "/" { + require.NoError(t, os.MkdirAll(filepath.Join(tempDir, dir), 0755)) + } + require.NoError(t, ioutil.WriteFile(filepath.Join(tempDir, k), []byte(v), 0644)) + } + + c := NewTestController(t, &TestControllerOpts{DisableAutoStart: true}) + + c.c.conf.RawConfig.PassthroughDirectory = tempDir + require.NoError(t, c.c.Start()) + defer c.Shutdown() + + cases := []struct { + name string + path string + contentsKey string + code int + mimeType string + }{ + { + "direct index", + "index.html", + "index.html", + http.StatusOK, + "text/html; charset=utf-8", + }, + { + "base slash", + "", + "index.html", + http.StatusOK, + "text/html; charset=utf-8", + }, + { + "no extension", + "orgs", + "index.html", + http.StatusOK, + "text/html; charset=utf-8", + }, + { + "favicon", + "favicon.png", + "favicon.png", + http.StatusOK, + "image/png", + }, + { + "bad index", + "index.htm", + "index.htm", + http.StatusOK, + "text/html; charset=utf-8", + }, + { + "bad path", + "index.ht", + "index.ht", + http.StatusNotFound, + "text/plain; charset=utf-8", + }, + { + "css", + "assets/styles.css", + "assets/styles.css", + http.StatusOK, + "text/css; charset=utf-8", + }, + { + "invalid extension", + "foo.bāb", + "index.html", + http.StatusOK, + "text/html; charset=utf-8", + }, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + assert := assert.New(t) + + url := fmt.Sprintf("%s/%s", c.ApiAddrs()[0], tc.path) + resp, err := http.Post(url, "", nil) + assert.NoError(err) + assert.Equal(http.StatusMethodNotAllowed, resp.StatusCode) + + resp, err = http.Get(url) + assert.NoError(err) + assert.Equal(tc.code, resp.StatusCode) + assert.Equal(tc.mimeType, resp.Header.Get("content-type")) + + contents, ok := nameContentsMap[tc.contentsKey] + if ok { + reader := new(bytes.Buffer) + _, err = reader.ReadFrom(resp.Body) + assert.NoError(err) + assert.Equal(contents, reader.String()) + } + }) + } +}