From 171e3038f1c9c5ba5ed2a16ce072e58fe41d73ed Mon Sep 17 00:00:00 2001 From: Yuri Shkuro Date: Tue, 24 Oct 2017 13:30:00 -0400 Subject: [PATCH] Add tests Signed-off-by: Yuri Shkuro --- .../app/fixture/ui-config-malformed.json | 1 + cmd/query/app/fixture/ui-config.json | 1 + cmd/query/app/fixture/ui-config.toml | 4 ++ cmd/query/app/fixture/ui-config.yaml | 4 ++ cmd/query/app/fixture/ui-config.yml | 4 ++ cmd/query/app/static_handler.go | 14 +++-- cmd/query/app/static_handler_test.go | 52 +++++++++++++++++++ 7 files changed, 72 insertions(+), 8 deletions(-) create mode 100644 cmd/query/app/fixture/ui-config-malformed.json create mode 100644 cmd/query/app/fixture/ui-config.json create mode 100644 cmd/query/app/fixture/ui-config.toml create mode 100644 cmd/query/app/fixture/ui-config.yaml create mode 100644 cmd/query/app/fixture/ui-config.yml diff --git a/cmd/query/app/fixture/ui-config-malformed.json b/cmd/query/app/fixture/ui-config-malformed.json new file mode 100644 index 00000000000..88b6ab31830 --- /dev/null +++ b/cmd/query/app/fixture/ui-config-malformed.json @@ -0,0 +1 @@ +{"x" == "y"} diff --git a/cmd/query/app/fixture/ui-config.json b/cmd/query/app/fixture/ui-config.json new file mode 100644 index 00000000000..4dd3e7b7a63 --- /dev/null +++ b/cmd/query/app/fixture/ui-config.json @@ -0,0 +1 @@ +{"x": "y"} diff --git a/cmd/query/app/fixture/ui-config.toml b/cmd/query/app/fixture/ui-config.toml new file mode 100644 index 00000000000..cc011767a14 --- /dev/null +++ b/cmd/query/app/fixture/ui-config.toml @@ -0,0 +1,4 @@ +x: abcd +z: + - a + - b diff --git a/cmd/query/app/fixture/ui-config.yaml b/cmd/query/app/fixture/ui-config.yaml new file mode 100644 index 00000000000..cc011767a14 --- /dev/null +++ b/cmd/query/app/fixture/ui-config.yaml @@ -0,0 +1,4 @@ +x: abcd +z: + - a + - b diff --git a/cmd/query/app/fixture/ui-config.yml b/cmd/query/app/fixture/ui-config.yml new file mode 100644 index 00000000000..cc011767a14 --- /dev/null +++ b/cmd/query/app/fixture/ui-config.yml @@ -0,0 +1,4 @@ +x: abcd +z: + - a + - b diff --git a/cmd/query/app/static_handler.go b/cmd/query/app/static_handler.go index 9fdd8640d41..5db692c2d74 100644 --- a/cmd/query/app/static_handler.go +++ b/cmd/query/app/static_handler.go @@ -79,22 +79,20 @@ func loadUIConfig(uiConfig string) (map[string]interface{}, error) { } var c map[string]interface{} + var unmarshall func([]byte, interface{}) error switch strings.ToLower(ext) { case ".yaml", ".yml": - if err := yaml.Unmarshal(bytes, &c); err != nil { - return nil, errors.Wrapf(err, "Cannot parse UI config file %v as YAML", uiConfig) - } - + unmarshall = yaml.Unmarshal case ".json": - if err := json.Unmarshal(bytes, &c); err != nil { - return nil, errors.Wrapf(err, "Cannot parse UI config file %v as JSON", uiConfig) - } - + unmarshall = json.Unmarshal default: return nil, fmt.Errorf("Unrecognized UI config file format %v", uiConfig) } + if err := unmarshall(bytes, &c); err != nil { + return nil, errors.Wrapf(err, "Cannot parse UI config file %v", uiConfig) + } return c, nil } diff --git a/cmd/query/app/static_handler_test.go b/cmd/query/app/static_handler_test.go index d9aa4f3d813..feb3917ff6e 100644 --- a/cmd/query/app/static_handler_test.go +++ b/cmd/query/app/static_handler_test.go @@ -74,3 +74,55 @@ func TestRegisterRoutesHandler(t *testing.T) { assert.Equal(t, http.StatusOK, resp.StatusCode) assert.Equal(t, expectedRespString, respString) } + +func TestLoadUIConfig(t *testing.T) { + type testCase struct { + configFile string + expected map[string]interface{} + expectedError string + } + + run := func(description string, testCase testCase) { + t.Run(description, func(t *testing.T) { + config, err := loadUIConfig(testCase.configFile) + if testCase.expectedError != "" { + assert.EqualError(t, err, testCase.expectedError) + } else { + assert.NoError(t, err) + } + assert.EqualValues(t, testCase.expected, config) + }) + } + + run("no config", testCase{}) + run("invalid config", testCase{ + configFile: "invalid", + expectedError: "Cannot read UI config file invalid: open invalid: no such file or directory", + }) + run("unsupported type", testCase{ + configFile: "fixture/ui-config.toml", + expectedError: "Unrecognized UI config file format fixture/ui-config.toml", + }) + run("malformed", testCase{ + configFile: "fixture/ui-config-malformed.json", + expectedError: "Cannot parse UI config file fixture/ui-config-malformed.json: invalid character '=' after object key", + }) + run("yaml", testCase{ + configFile: "fixture/ui-config.yaml", + expected: map[string]interface{}{ + "x": "abcd", + "z": []interface{}{"a", "b"}, + }, + }) + run("yml", testCase{ + configFile: "fixture/ui-config.yml", + expected: map[string]interface{}{ + "x": "abcd", + "z": []interface{}{"a", "b"}, + }, + }) + run("json", testCase{ + configFile: "fixture/ui-config.json", + expected: map[string]interface{}{"x": "y"}, + }) +}