diff --git a/check/test.go b/check/test.go index b15cb8b..c391ae3 100644 --- a/check/test.go +++ b/check/test.go @@ -56,8 +56,8 @@ type compare struct { } type testOutput struct { - testResult bool - actualResult string + testResult bool + actualResult string ExpectedResult string } @@ -76,36 +76,22 @@ func (t *testItem) execute(s string) *testOutput { } else { // Path != "" - we don't know whether it's YAML or JSON but // we can just try one then the other - buf := new(bytes.Buffer) var jsonInterface interface{} if t.Path != "" { - err := json.Unmarshal([]byte(s), &jsonInterface) + err := unmarshal(s, &jsonInterface) if err != nil { - err := yaml.Unmarshal([]byte(s), &jsonInterface) - if err != nil { - fmt.Fprintf(os.Stderr, "failed to load YAML or JSON from provided input \"%s\": %v\n", s, err) - return failTestItem("failed to load YAML or JSON") - } + fmt.Fprintf(os.Stderr, "failed to load YAML or JSON from provided input \"%s\": %v\n", s, err) + return failTestItem("failed to load YAML or JSON") } - } - // Parse the jsonpath/yamlpath expression... - j := jsonpath.New("jsonpath") - j.AllowMissingKeys(true) - err := j.Parse(t.Path) - if err != nil { - fmt.Fprintf(os.Stderr, "unable to parse path expression \"%s\": %v\n", t.Path, err) - return failTestItem("unable to parse path expression") } - err = j.Execute(buf, jsonInterface) + jsonpathResult, err := executeJSONPath(t.Path, &jsonInterface) if err != nil { - fmt.Fprintf(os.Stderr, "error executing path expression \"%s\": %v\n", t.Path, err) + fmt.Fprintf(os.Stderr, "unable to parse path expression \"%s\": %v\n", t.Path, err) return failTestItem("error executing path expression") } - - jsonpathResult := fmt.Sprintf("%s", buf) match = (jsonpathResult != "") flagVal = jsonpathResult } @@ -205,6 +191,35 @@ func (t *testItem) execute(s string) *testOutput { return result } +func unmarshal(s string, jsonInterface *interface{}) error { + data := []byte(s) + err := json.Unmarshal(data, jsonInterface) + if err != nil { + err := yaml.Unmarshal(data, jsonInterface) + if err != nil { + return err + } + } + return nil +} + +func executeJSONPath(path string, jsonInterface interface{}) (string, error) { + j := jsonpath.New("jsonpath") + j.AllowMissingKeys(true) + err := j.Parse(path) + if err != nil { + return "", err + } + + buf := new(bytes.Buffer) + err = j.Execute(buf, jsonInterface) + if err != nil { + return "", err + } + jsonpathResult := fmt.Sprintf("%s", buf) + return jsonpathResult, nil +} + type tests struct { TestItems []*testItem `yaml:"test_items"` BinOp binOp `yaml:"bin_op"` diff --git a/check/test_test.go b/check/test_test.go index a74679c..430863d 100644 --- a/check/test_test.go +++ b/check/test_test.go @@ -201,3 +201,124 @@ func TestTestExecuteExceptions(t *testing.T) { } } } + +func TestTestUnmarshal(t *testing.T) { + type kubeletConfig struct { + Kind string + ApiVersion string + Address string + } + cases := []struct { + content string + jsonInterface interface{} + expectedToFail bool + }{ + { + `{ + "kind": "KubeletConfiguration", + "apiVersion": "kubelet.config.k8s.io/v1beta1", + "address": "0.0.0.0" + } + `, + kubeletConfig{}, + false, + }, { + ` +kind: KubeletConfiguration +address: 0.0.0.0 +apiVersion: kubelet.config.k8s.io/v1beta1 +authentication: + anonymous: + enabled: false + webhook: + cacheTTL: 2m0s + enabled: true + x509: + clientCAFile: /etc/kubernetes/pki/ca.crt +tlsCipherSuites: + - TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 + - TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 +`, + kubeletConfig{}, + false, + }, + { + ` +kind: ddress: 0.0.0.0 +apiVersion: kubelet.config.k8s.io/v1beta +`, + kubeletConfig{}, + true, + }, + } + + for _, c := range cases { + err := unmarshal(c.content, &c.jsonInterface) + if err != nil { + if !c.expectedToFail { + t.Errorf("%s, expectedToFail:%v, got:%v\n", c.content, c.expectedToFail, err) + } + } else { + if c.expectedToFail { + t.Errorf("%s, expectedToFail:%v, got:Did not fail\n", c.content, c.expectedToFail) + } + } + } +} + +func TestExecuteJSONPath(t *testing.T) { + type kubeletConfig struct { + Kind string + ApiVersion string + Address string + } + cases := []struct { + jsonPath string + jsonInterface kubeletConfig + expectedResult string + expectedToFail bool + }{ + { + // JSONPath parse works, results don't match + "{.Kind}", + kubeletConfig{ + Kind: "KubeletConfiguration", + ApiVersion: "kubelet.config.k8s.io/v1beta1", + Address: "127.0.0.0", + }, + "blah", + true, + }, + { + // JSONPath parse works, results match + "{.Kind}", + kubeletConfig{ + Kind: "KubeletConfiguration", + ApiVersion: "kubelet.config.k8s.io/v1beta1", + Address: "127.0.0.0", + }, + "KubeletConfiguration", + false, + }, + { + // JSONPath parse fails + "{.ApiVersion", + kubeletConfig{ + Kind: "KubeletConfiguration", + ApiVersion: "kubelet.config.k8s.io/v1beta1", + Address: "127.0.0.0", + }, + "", + true, + }, + } + for _, c := range cases { + result, err := executeJSONPath(c.jsonPath, c.jsonInterface) + if err != nil && !c.expectedToFail { + t.Fatalf("jsonPath:%q, expectedResult:%q got:%v\n", c.jsonPath, c.expectedResult, err) + } + if c.expectedResult != result && !c.expectedToFail { + t.Errorf("jsonPath:%q, expectedResult:%q got:%q\n", c.jsonPath, c.expectedResult, result) + } + } +}