Skip to content

Commit f96f217

Browse files
committed
Add basic integration tests
1 parent edac0d0 commit f96f217

File tree

2 files changed

+69
-1
lines changed

2 files changed

+69
-1
lines changed

.golangci.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
run:
2-
deadline: 1m
2+
timeout: 5m
3+
skip-files:
4+
- '(.+)_test\.go'
35

46
linters:
57
disable-all: false

check_test.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package main
2+
3+
import (
4+
"net/http"
5+
"net/http/httptest"
6+
"os/exec"
7+
"strings"
8+
"testing"
9+
)
10+
11+
func TestMain_ConnectionRefused(t *testing.T) {
12+
cmd := exec.Command("go", "run", "./...")
13+
out, _ := cmd.CombinedOutput()
14+
15+
actual := string(out)
16+
expected := "UNKNOWN - url and token are required"
17+
18+
if !strings.Contains(actual, expected) {
19+
t.Error("\nActual: ", actual, "\nExpected: ", expected)
20+
}
21+
}
22+
23+
type IntegrationTest struct {
24+
name string
25+
server *httptest.Server
26+
args []string
27+
expected string
28+
}
29+
30+
func TestMainCmd(t *testing.T) {
31+
tests := []IntegrationTest{
32+
{
33+
name: "invalid-json",
34+
server: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
35+
w.WriteHeader(http.StatusOK)
36+
w.Write([]byte(`{"foo}`))
37+
})),
38+
args: []string{"run", "./...", "-T", "test", "--url"},
39+
expected: "UNKNOWN - could not decode JSON from body",
40+
},
41+
{
42+
name: "empty-response",
43+
server: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
44+
w.WriteHeader(http.StatusOK)
45+
w.Write([]byte(`{"data" : [ {"id": "c86dc437", "name": "test1"}], "pagination": { "nextCursor": null, "totalItems": 2}}`))
46+
})),
47+
args: []string{"run", "./...", "-T", "test", "--url"},
48+
expected: "WARNING -",
49+
},
50+
}
51+
52+
for _, test := range tests {
53+
t.Run(test.name, func(t *testing.T) {
54+
defer test.server.Close()
55+
56+
cmd := exec.Command("go", append(test.args, test.server.URL)...)
57+
out, _ := cmd.CombinedOutput()
58+
59+
actual := string(out)
60+
61+
if !strings.Contains(actual, test.expected) {
62+
t.Error("\nActual: ", actual, "\nExpected: ", test.expected)
63+
}
64+
})
65+
}
66+
}

0 commit comments

Comments
 (0)