Skip to content

Commit 39bc084

Browse files
authored
Merge pull request #16 from NETWAYS/feature/add-tests
Add basic integration tests
2 parents 8affac7 + 09c12e3 commit 39bc084

File tree

4 files changed

+204
-0
lines changed

4 files changed

+204
-0
lines changed

.golangci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
run:
22
deadline: 1m
3+
tests: false
34

45
linters:
56
disable-all: false

Makefile

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
.PHONY: test coverage lint vet
2+
3+
build:
4+
go build
5+
lint:
6+
go fmt $(go list ./... | grep -v /vendor/)
7+
vet:
8+
go vet $(go list ./... | grep -v /vendor/)
9+
test:
10+
go test -v -cover ./...
11+
coverage:
12+
go test -v -cover -coverprofile=coverage.out ./... &&\
13+
go tool cover -html=coverage.out -o coverage.html

cmd/health_test.go

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package cmd
2+
3+
import (
4+
"net/http"
5+
"net/http/httptest"
6+
"net/url"
7+
"os/exec"
8+
"strings"
9+
"testing"
10+
)
11+
12+
func TestHealth_ConnectionRefused(t *testing.T) {
13+
14+
cmd := exec.Command("go", "run", "../main.go", "health", "--port", "9999")
15+
out, _ := cmd.CombinedOutput()
16+
17+
actual := string(out)
18+
expected := "UNKNOWN - could not fetch cluster info: dial"
19+
20+
if !strings.Contains(actual, expected) {
21+
t.Error("\nActual: ", actual, "\nExpected: ", expected)
22+
}
23+
}
24+
25+
type HealthTest struct {
26+
name string
27+
server *httptest.Server
28+
args []string
29+
expected string
30+
}
31+
32+
func TestHealthCmd(t *testing.T) {
33+
tests := []HealthTest{
34+
{
35+
name: "health-ok",
36+
server: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
37+
w.Header().Set("X-Elastic-Product", "Elasticsearch")
38+
w.WriteHeader(http.StatusOK)
39+
w.Write([]byte(`{"cluster_name":"test","status":"green","timed_out":false,"number_of_nodes":1,"number_of_data_nodes":1,"active_primary_shards":3,"active_shards":3,"relocating_shards":0,"initializing_shards":0,"unassigned_shards":0,"delayed_unassigned_shards":0,"number_of_pending_tasks":0,"number_of_in_flight_fetch":0,"task_max_waiting_in_queue_millis":0,"active_shards_percent_as_number":100.0}`))
40+
})),
41+
args: []string{"run", "../main.go", "health"},
42+
expected: "OK - Cluster test is green | status=0 nodes=1 data_nodes=1 active_primary_shards=3 active_shards=3\n",
43+
},
44+
{
45+
name: "health-yellow",
46+
server: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
47+
w.Header().Set("X-Elastic-Product", "Elasticsearch")
48+
w.WriteHeader(http.StatusOK)
49+
w.Write([]byte(`{"cluster_name":"test","status":"yellow","timed_out":false,"number_of_nodes":1,"number_of_data_nodes":1,"active_primary_shards":3,"active_shards":3,"relocating_shards":0,"initializing_shards":0,"unassigned_shards":0,"delayed_unassigned_shards":0,"number_of_pending_tasks":0,"number_of_in_flight_fetch":0,"task_max_waiting_in_queue_millis":0,"active_shards_percent_as_number":100.0}`))
50+
})),
51+
args: []string{"run", "../main.go", "health"},
52+
expected: "WARNING - Cluster test is yellow | status=1 nodes=1 data_nodes=1 active_primary_shards=3 active_shards=3\nexit status 1\n",
53+
},
54+
{
55+
name: "health-red",
56+
server: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
57+
w.Header().Set("X-Elastic-Product", "Elasticsearch")
58+
w.WriteHeader(http.StatusOK)
59+
w.Write([]byte(`{"cluster_name":"test","status":"red","timed_out":false,"number_of_nodes":1,"number_of_data_nodes":1,"active_primary_shards":3,"active_shards":3,"relocating_shards":0,"initializing_shards":0,"unassigned_shards":0,"delayed_unassigned_shards":0,"number_of_pending_tasks":0,"number_of_in_flight_fetch":0,"task_max_waiting_in_queue_millis":0,"active_shards_percent_as_number":100.0}`))
60+
})),
61+
args: []string{"run", "../main.go", "health"},
62+
expected: "CRITICAL - Cluster test is red | status=2 nodes=1 data_nodes=1 active_primary_shards=3 active_shards=3\nexit status 2\n",
63+
},
64+
}
65+
66+
for _, test := range tests {
67+
t.Run(test.name, func(t *testing.T) {
68+
defer test.server.Close()
69+
70+
// We need the random Port extracted
71+
u, _ := url.Parse(test.server.URL)
72+
cmd := exec.Command("go", append(test.args, "--port", u.Port())...)
73+
out, _ := cmd.CombinedOutput()
74+
75+
actual := string(out)
76+
77+
if actual != test.expected {
78+
t.Error("\nActual: ", actual, "\nExpected: ", test.expected)
79+
}
80+
81+
})
82+
}
83+
}

cmd/query_test.go

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package cmd
2+
3+
import (
4+
"net/http"
5+
"net/http/httptest"
6+
"net/url"
7+
"os/exec"
8+
"strings"
9+
"testing"
10+
)
11+
12+
func TestQuery_ConnectionRefused(t *testing.T) {
13+
14+
cmd := exec.Command("go", "run", "../main.go", "query", "--port", "9999")
15+
out, _ := cmd.CombinedOutput()
16+
17+
actual := string(out)
18+
expected := "UNKNOWN - could not fetch cluster info: dial"
19+
20+
if !strings.Contains(actual, expected) {
21+
t.Error("\nActual: ", actual, "\nExpected: ", expected)
22+
}
23+
}
24+
25+
type QueryTest struct {
26+
name string
27+
server *httptest.Server
28+
args []string
29+
expected string
30+
}
31+
32+
func TestQueryCmd(t *testing.T) {
33+
tests := []QueryTest{
34+
{
35+
name: "query-default",
36+
server: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
37+
w.Header().Set("X-Elastic-Product", "Elasticsearch")
38+
w.WriteHeader(http.StatusOK)
39+
w.Write([]byte(`{"name":"test","cluster_name":"cluster","cluster_uuid":"6nZDLRvSQ1iDxZUTf0Hrmg","version":{"number":"7.17.7","build_flavor":"default","build_type":"docker","build_hash":"78dcaaa8cee33438b91eca7f5c7f56a70fec9e80","build_date":"2022-10-17T15:29:54.167373105Z","build_snapshot":false,"lucene_version":"8.11.1","minimum_wire_compatibility_version":"6.8.0","minimum_index_compatibility_version":"6.0.0-beta1"},"tagline":"YouKnow,forSearch"}`))
40+
})),
41+
args: []string{"run", "../main.go", "query"},
42+
expected: "OK - Total hits: 0 | total=0;20;50\n",
43+
},
44+
{
45+
name: "query-ok",
46+
server: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
47+
w.Header().Set("X-Elastic-Product", "Elasticsearch")
48+
w.WriteHeader(http.StatusOK)
49+
w.Write([]byte(`{"took":3,"timed_out":false,"_shards":{"total":1,"successful":1,"skipped":0,"failed":0},"hits":{"total":{"value":2,"relation":"eq"},"max_score":1.0,"hits":[{"_index":"my_index","_type":"_doc","_id":"yUi6voQB87C1kW3InC4l","_score":1.0,"_source":{"title":"One","tags":["ruby"]}},{"_index":"my_index","_type":"_doc","_id":"y0i9voQB87C1kW3I9y74","_score":1.0,"_source":{"title":"One","tags":["ruby"]}}]}}`))
50+
})),
51+
args: []string{"run", "../main.go", "query", "-I", "my_index", "-q", "*", "--msgkey", "title", "-w", "3"},
52+
expected: `OK - Total hits: 2
53+
One
54+
One
55+
| total=2;3;50
56+
`,
57+
},
58+
{
59+
name: "query-critical",
60+
server: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
61+
w.Header().Set("X-Elastic-Product", "Elasticsearch")
62+
w.WriteHeader(http.StatusOK)
63+
w.Write([]byte(`{"took":3,"timed_out":false,"_shards":{"total":1,"successful":1,"skipped":0,"failed":0},"hits":{"total":{"value":2,"relation":"eq"},"max_score":1.0,"hits":[{"_index":"my_index","_type":"_doc","_id":"yUi6voQB87C1kW3InC4l","_score":1.0,"_source":{"title":"One","tags":["ruby"]}},{"_index":"my_index","_type":"_doc","_id":"y0i9voQB87C1kW3I9y74","_score":1.0,"_source":{"title":"One","tags":["ruby"]}}]}}`))
64+
})),
65+
args: []string{"run", "../main.go", "query", "-I", "my_index", "-q", "*", "--msgkey", "title", "-c", "1"},
66+
expected: `CRITICAL - Total hits: 2
67+
One
68+
One
69+
| total=2;20;1
70+
exit status 2
71+
`,
72+
},
73+
{
74+
name: "query-warning",
75+
server: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
76+
w.Header().Set("X-Elastic-Product", "Elasticsearch")
77+
w.WriteHeader(http.StatusOK)
78+
w.Write([]byte(`{"took":3,"timed_out":false,"_shards":{"total":1,"successful":1,"skipped":0,"failed":0},"hits":{"total":{"value":2,"relation":"eq"},"max_score":1.0,"hits":[{"_index":"my_index","_type":"_doc","_id":"yUi6voQB87C1kW3InC4l","_score":1.0,"_source":{"title":"One","tags":["ruby"]}},{"_index":"my_index","_type":"_doc","_id":"y0i9voQB87C1kW3I9y74","_score":1.0,"_source":{"title":"One","tags":["ruby"]}}]}}`))
79+
})),
80+
args: []string{"run", "../main.go", "query", "-I", "my_index", "-q", "*", "--msgkey", "title", "-w", "1"},
81+
expected: `WARNING - Total hits: 2
82+
One
83+
One
84+
| total=2;1;50
85+
exit status 1
86+
`,
87+
},
88+
}
89+
90+
for _, test := range tests {
91+
t.Run(test.name, func(t *testing.T) {
92+
defer test.server.Close()
93+
94+
// We need the random Port extracted
95+
u, _ := url.Parse(test.server.URL)
96+
cmd := exec.Command("go", append(test.args, "--port", u.Port())...)
97+
out, _ := cmd.CombinedOutput()
98+
99+
actual := string(out)
100+
101+
if actual != test.expected {
102+
t.Error("\nActual: ", actual, "\nExpected: ", test.expected)
103+
}
104+
105+
})
106+
}
107+
}

0 commit comments

Comments
 (0)