diff options
Diffstat (limited to 'client/go/cmd/test.go')
-rw-r--r-- | client/go/cmd/test.go | 67 |
1 files changed, 35 insertions, 32 deletions
diff --git a/client/go/cmd/test.go b/client/go/cmd/test.go index e9d5e571845..c2aaab68a0c 100644 --- a/client/go/cmd/test.go +++ b/client/go/cmd/test.go @@ -119,7 +119,7 @@ func runTest(testPath string, target vespa.Target, dryRun bool) string { testName = filepath.Base(testPath) } if !dryRun { - fmt.Fprintf(stdout, "Running %s:", color.Cyan(testName)) + fmt.Fprintf(stdout, "%s:", testName) } defaultParameters, err := getParameters(test.Defaults.ParametersRaw, path.Dir(testPath)) @@ -133,9 +133,9 @@ func runTest(testPath string, target vespa.Target, dryRun bool) string { fatalErrHint(fmt.Errorf("a test must have at least one step, but none were found in %s", testPath), "See https://cloud.vespa.ai/en/reference/testing") } for i, step := range test.Steps { - stepName := step.Name - if stepName == "" { - stepName = fmt.Sprintf("step %d", i+1) + stepName := fmt.Sprintf("Step %d", i+1) + if step.Name != "" { + stepName = fmt.Sprintf("Step: %s", step.Name) } failure, longFailure, err := verify(step, path.Dir(testPath), test.Defaults.Cluster, defaultParameters, target, dryRun) if err != nil { @@ -144,13 +144,13 @@ func runTest(testPath string, target vespa.Target, dryRun bool) string { } if !dryRun { if failure != "" { - fmt.Fprintf(stdout, " %s %s:\n%s\n", color.Red("Failed"), color.Cyan(stepName), longFailure) + fmt.Fprintf(stdout, " %s\n%s:\n%s\n", color.Red("failed"), stepName, longFailure) return fmt.Sprintf("%s: %s: %s", testName, stepName, failure) } if i == 0 { fmt.Fprintf(stdout, " ") } - fmt.Fprint(stdout, color.Green(".")) + fmt.Fprint(stdout, ".") } } if !dryRun { @@ -204,7 +204,7 @@ func verify(step step, testsPath string, defaultCluster string, defaultParameter } externalEndpoint := requestUrl.IsAbs() if !externalEndpoint { - baseURL := "http://dummy/" + baseURL := "" if service != nil { baseURL = service.BaseURL } @@ -264,8 +264,13 @@ func verify(step step, testsPath string, defaultCluster string, defaultParameter defer response.Body.Close() if statusCode != response.StatusCode { - failure := fmt.Sprintf("Unexpected %s: %s", "status code", color.Red(response.StatusCode)) - return failure, fmt.Sprintf("%s\nExpected: %s\nActual response:\n%s", failure, color.Cyan(statusCode), util.ReaderToJSON(response.Body)), nil + return fmt.Sprintf("Unexpected status code: %d", color.Red(response.StatusCode)), + fmt.Sprintf("Unexpected status code\nExpected: %d\nActual: %d\nRequested: %s at %s\nResponse:\n%s", + color.Cyan(statusCode), + color.Red(response.StatusCode), + color.Cyan(method), + color.Cyan(requestUrl), + util.ReaderToJSON(response.Body)), nil } if responseBodySpec == nil { @@ -282,20 +287,24 @@ func verify(step step, testsPath string, defaultCluster string, defaultParameter return "", "", fmt.Errorf("got non-JSON response; %w:\n%s", err, string(responseBodyBytes)) } - failure, expected, err := compare(responseBodySpec, responseBody, "") + failure, expected, actual, err := compare(responseBodySpec, responseBody, "") if failure != "" { responsePretty, _ := json.MarshalIndent(responseBody, "", " ") longFailure := failure if expected != "" { - longFailure += "\n" + expected + longFailure += "\nExpected: " + expected } - longFailure += "\nActual response:\n" + string(responsePretty) + if actual != "" { + failure += ": " + actual + longFailure += "\nActual: " + actual + } + longFailure += fmt.Sprintf("\nRequested: %s at %s\nResponse:\n%s", color.Cyan(method), color.Cyan(requestUrl), string(responsePretty)) return failure, longFailure, err } return "", "", err } -func compare(expected interface{}, actual interface{}, path string) (string, string, error) { +func compare(expected interface{}, actual interface{}, path string) (string, string, string, error) { typeMatch := false valueMatch := false switch u := expected.(type) { @@ -320,18 +329,15 @@ func compare(expected interface{}, actual interface{}, path string) (string, str if ok { if len(u) == len(v) { for i, e := range u { - failure, expected, err := compare(e, v[i], fmt.Sprintf("%s/%d", path, i)) - if failure != "" || err != nil { - return failure, expected, err + if failure, expected, actual, err := compare(e, v[i], fmt.Sprintf("%s/%d", path, i)); failure != "" || err != nil { + return failure, expected, actual, err } } valueMatch = true } else { - return fmt.Sprintf("Unexpected %s at %s: %d", - "number of elements", - color.Cyan(path), - color.Red(len(v))), - fmt.Sprintf("Expected: %d", color.Cyan(len(u))), + return fmt.Sprintf("Unexpected number of elements at %s", color.Cyan(path)), + fmt.Sprintf("%d", color.Cyan(len(u))), + fmt.Sprintf("%d", color.Red(len(v))), nil } } @@ -343,17 +349,16 @@ func compare(expected interface{}, actual interface{}, path string) (string, str childPath := fmt.Sprintf("%s/%s", path, strings.ReplaceAll(strings.ReplaceAll(n, "~", "~0"), "/", "~1")) f, ok := v[n] if !ok { - return fmt.Sprintf("Missing expected field at %s", color.Red(childPath)), "", nil + return fmt.Sprintf("Missing expected field at %s", color.Red(childPath)), "", "", nil } - failure, expected, err := compare(e, f, childPath) - if failure != "" || err != nil { - return failure, expected, err + if failure, expected, actual, err := compare(e, f, childPath); failure != "" || err != nil { + return failure, expected, actual, err } } valueMatch = true } default: - return "", "", fmt.Errorf("unexpected JSON type for value '%v'", expected) + return "", "", "", fmt.Errorf("unexpected JSON type for value '%v'", expected) } if !valueMatch { @@ -366,14 +371,12 @@ func compare(expected interface{}, actual interface{}, path string) (string, str } expectedJson, _ := json.Marshal(expected) actualJson, _ := json.Marshal(actual) - return fmt.Sprintf("Unexpected %s at %s: %s", - mismatched, - color.Cyan(path), - color.Red(actualJson)), - fmt.Sprintf("Expected: %s", color.Cyan(expectedJson)), + return fmt.Sprintf("Unexpected %s at %s", mismatched, color.Cyan(path)), + fmt.Sprintf("%s", color.Cyan(expectedJson)), + fmt.Sprintf("%s", color.Red(actualJson)), nil } - return "", "", nil + return "", "", "", nil } func getParameters(parametersRaw []byte, testsPath string) (map[string]string, error) { |