diff --git a/main.go b/main.go index 2004706..6e87ae3 100644 --- a/main.go +++ b/main.go @@ -3,7 +3,6 @@ package main import ( "flag" "fmt" - "log" "os" "time" ) @@ -29,7 +28,8 @@ func main() { smokeTests, err := parseVapefile(*vapeFile) if err != nil { - log.Fatal(err) + fmt.Printf("Error reading config: %s", err.Error()) + os.Exit(1) } testsLen := len(smokeTests) @@ -53,10 +53,9 @@ func main() { } if passedCount < testsLen { - fmt.Println("\n🔥 Some tests failed. you may have a bad deployment") + fmt.Println("\n🔥 Some tests failed. You may have a bad deployment") os.Exit(2) } fmt.Printf("\n✨ [%d/%d] tests passed in %s\n", passedCount, testsLen, time.Since(start)) - } diff --git a/utils.go b/utils.go index 1d6c1a3..6e94c7e 100644 --- a/utils.go +++ b/utils.go @@ -20,6 +20,14 @@ func parseVapefile(file string) (SmokeTests, error) { if err != nil { return nil, err } + + for _, test := range tests { + if test.URI == "" || test.ExpectedStatusCode == 0 { + return nil, fmt.Errorf("Each test should have at least a uri and expected_status_code:\n %v", string(raw)) + } + + } + return tests, nil } diff --git a/utils_test.go b/utils_test.go index e5a8098..ce2a2d7 100644 --- a/utils_test.go +++ b/utils_test.go @@ -3,6 +3,7 @@ package main import ( "io/ioutil" "os" + "strings" "testing" ) @@ -50,11 +51,11 @@ func TestReadVapefileSuccess(t *testing.T) { json := `[ { "uri": "/status/200", - "expectedStatusCode": 200 + "expected_status_code": 200 }, { "uri": "/status/500", - "expectedStatusCode": 500 + "expected_status_code": 500 } ]` tmpfile, cleanup, err := tmpFile(json) @@ -68,6 +69,54 @@ func TestReadVapefileSuccess(t *testing.T) { } } +func TestReadVapefileMissingFields(t *testing.T) { + t.Run("TestUriMissing", func(t *testing.T) { + json := `[ + { + "expected_status_code": 200 + } + ]` + tmpfile, cleanup, err := tmpFile(json) + if err != nil { + t.Fatal(err) + } + defer cleanup() + _, err = parseVapefile(tmpfile.Name()) + if err == nil { + t.Errorf("expected error: got: %v", err) + } + + expectedError := "Each test should have at least a uri and expected_status_code" + if strings.Contains(err.Error(), expectedError) == false { + t.Errorf("expected message: %s got %s", expectedError, err.Error()) + } + }) + + t.Run("TestExpectedStatusCodeMissing", func(t *testing.T) { + t.Run("TestUriMissing", func(t *testing.T) { + json := `[ + { + "uri": "/health" + } + ]` + tmpfile, cleanup, err := tmpFile(json) + if err != nil { + t.Fatal(err) + } + defer cleanup() + _, err = parseVapefile(tmpfile.Name()) + if err == nil { + t.Errorf("expected error: got: %v", err) + } + + expectedError := "Each test should have at least a uri and expected_status_code" + if strings.Contains(err.Error(), expectedError) == false { + t.Errorf("expected message: %s got %s", expectedError, err.Error()) + } + }) + }) +} + func TestParseBaseURL(t *testing.T) { t.Run("TestInvalidURL", func(t *testing.T) { url := ":"