Fix ui dir where there is no index tests and lint issue.

This commit is contained in:
Paul Banks 2020-10-01 12:26:19 +01:00
parent a6c748ec1b
commit 72fcc8ec02
No known key found for this signature in database
GPG Key ID: C25A851A849B8221
4 changed files with 56 additions and 14 deletions

View File

@ -28,18 +28,20 @@ func TestUiIndex(t *testing.T) {
// Make the server // Make the server
a := NewTestAgent(t, ` a := NewTestAgent(t, `
ui_dir = "`+uiDir+`" ui_config {
dir = "`+uiDir+`"
}
`) `)
defer a.Shutdown() defer a.Shutdown()
testrpc.WaitForLeader(t, a.RPC, "dc1") testrpc.WaitForLeader(t, a.RPC, "dc1")
// Create file // Create file
path := filepath.Join(a.Config.UIConfig.Dir, "my-file") path := filepath.Join(a.Config.UIConfig.Dir, "my-file")
if err := ioutil.WriteFile(path, []byte("test"), 0777); err != nil { if err := ioutil.WriteFile(path, []byte("test"), 0644); err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
// Register node // Request the custom file
req, _ := http.NewRequest("GET", "/ui/my-file", nil) req, _ := http.NewRequest("GET", "/ui/my-file", nil)
req.URL.Scheme = "http" req.URL.Scheme = "http"
req.URL.Host = a.HTTPAddr() req.URL.Host = a.HTTPAddr()

View File

@ -35,6 +35,9 @@ func uiTemplateDataFromConfig(cfg *config.RuntimeConfig) (map[string]interface{}
} }
err := uiTemplateDataFromConfigEnterprise(cfg, d, uiCfg) err := uiTemplateDataFromConfigEnterprise(cfg, d, uiCfg)
if err != nil {
return nil, err
}
// Render uiCfg down to JSON ready to inject into the template // Render uiCfg down to JSON ready to inject into the template
bs, err := json.Marshal(uiCfg) bs, err := json.Marshal(uiCfg)

View File

@ -88,10 +88,22 @@ func (h *Handler) ReloadConfig(newCfg *config.RuntimeConfig) error {
// Render a new index.html with the new config values ready to serve. // Render a new index.html with the new config values ready to serve.
buf, info, err := renderIndex(newCfg, fs) buf, info, err := renderIndex(newCfg, fs)
if err != nil { if _, ok := err.(*os.PathError); ok && newCfg.UIConfig.Dir != "" {
// A Path error indicates that there is no index.html. This could happen if
// the user configured their own UI dir and is serving something that is not
// our usual UI. This won't work perfectly because our uiserver will still
// redirect everything to the UI but we shouldn't fail the entire UI server
// with a 500 in this case. Partly that's just bad UX and partly it's a
// breaking change although quite an edge case. Instead, continue but just
// return a 404 response for the index.html and log a warning.
h.logger.Warn("ui_config.dir does not contain an index.html. Index templating and redirects to index.html are disabled.")
} else if err != nil {
return err return err
} }
// buf can be nil in the PathError case above. We should skip this part but
// still serve the rest of the files in that case.
if buf != nil {
// Create a new fs that serves the rendered index file or falls back to the // Create a new fs that serves the rendered index file or falls back to the
// underlying FS. // underlying FS.
fs = &bufIndexFS{ fs = &bufIndexFS{
@ -104,6 +116,8 @@ func (h *Handler) ReloadConfig(newCfg *config.RuntimeConfig) error {
// redirected requests for /index.html get served the rendered version not the // redirected requests for /index.html get served the rendered version not the
// original. // original.
fs = &redirectFS{fs: fs} fs = &redirectFS{fs: fs}
}
newState.srv = http.FileServer(fs) newState.srv = http.FileServer(fs)
// Store the new state // Store the new state

View File

@ -2,9 +2,12 @@ package uiserver
import ( import (
"encoding/json" "encoding/json"
"io/ioutil"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"net/url" "net/url"
"os"
"path/filepath"
"regexp" "regexp"
"testing" "testing"
@ -228,3 +231,23 @@ func TestReload(t *testing.T) {
require.Contains(t, rec.Body.String(), "exotic-metrics-provider-name") require.Contains(t, rec.Body.String(), "exotic-metrics-provider-name")
} }
} }
func TestCustomDir(t *testing.T) {
uiDir := testutil.TempDir(t, "consul-uiserver")
defer os.RemoveAll(uiDir)
path := filepath.Join(uiDir, "test-file")
require.NoError(t, ioutil.WriteFile(path, []byte("test"), 0644))
cfg := basicUIEnabledConfig()
cfg.UIConfig.Dir = uiDir
h := NewHandler(cfg, testutil.Logger(t))
req := httptest.NewRequest("GET", "/test-file", nil)
rec := httptest.NewRecorder()
h.ServeHTTP(rec, req)
require.Equal(t, http.StatusOK, rec.Code)
require.Contains(t, rec.Body.String(), "test")
}