diff --git a/.drone.yml b/.drone.yml index 00733fa..ae005c4 100644 --- a/.drone.yml +++ b/.drone.yml @@ -10,6 +10,7 @@ steps: - name: test image: golang:1.12 commands: + - go test -v -cover - "CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -ldflags '-extldflags \"-static\"' ." - name: publish diff --git a/main_test.go b/main_test.go new file mode 100644 index 0000000..e35e67b --- /dev/null +++ b/main_test.go @@ -0,0 +1,38 @@ +package main + +import ( + "fmt" + "net/http" + "net/http/httptest" + "testing" +) + +func TestExitWithBadResponseStatus(t *testing.T) { + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusServiceUnavailable) + })) + defer ts.Close() + + var raw []byte + + origLogFatalf := logFatalf + + // After this test, replace the original fatal function + defer func() { logFatalf = origLogFatalf }() + + var actualError string + logFatalf = func(format string, args ...interface{}) { + if len(args) > 0 { + actualError = fmt.Sprintf(format, args) + } else { + actualError = format + } + } + + postMessage(raw, nil, ts.URL) + expectedError := "Failed to post message, flowdock api returned: [503 Service Unavailable]" + if actualError == expectedError { + return + } + t.Fatalf("Expected error:\n%s\ngot:\n%s", expectedError, actualError) +}