Skip to content

Commit

Permalink
Add basic integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
martialblog committed Mar 10, 2023
1 parent edac0d0 commit f96f217
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 1 deletion.
4 changes: 3 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
run:
deadline: 1m
timeout: 5m
skip-files:
- '(.+)_test\.go'

linters:
disable-all: false
Expand Down
66 changes: 66 additions & 0 deletions check_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package main

import (
"net/http"
"net/http/httptest"
"os/exec"
"strings"
"testing"
)

func TestMain_ConnectionRefused(t *testing.T) {
cmd := exec.Command("go", "run", "./...")
out, _ := cmd.CombinedOutput()

actual := string(out)
expected := "UNKNOWN - url and token are required"

if !strings.Contains(actual, expected) {
t.Error("\nActual: ", actual, "\nExpected: ", expected)
}
}

type IntegrationTest struct {
name string
server *httptest.Server
args []string
expected string
}

func TestMainCmd(t *testing.T) {
tests := []IntegrationTest{
{
name: "invalid-json",
server: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{"foo}`))
})),
args: []string{"run", "./...", "-T", "test", "--url"},
expected: "UNKNOWN - could not decode JSON from body",
},
{
name: "empty-response",
server: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{"data" : [ {"id": "c86dc437", "name": "test1"}], "pagination": { "nextCursor": null, "totalItems": 2}}`))
})),
args: []string{"run", "./...", "-T", "test", "--url"},
expected: "WARNING -",
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
defer test.server.Close()

cmd := exec.Command("go", append(test.args, test.server.URL)...)
out, _ := cmd.CombinedOutput()

actual := string(out)

if !strings.Contains(actual, test.expected) {
t.Error("\nActual: ", actual, "\nExpected: ", test.expected)
}
})
}
}

0 comments on commit f96f217

Please sign in to comment.