@@ -3,19 +3,18 @@ package cmd
33import (
44 "net/http"
55 "net/http/httptest"
6- "net/url"
76 "os/exec"
87 "strings"
98 "testing"
109)
1110
1211func TestHealth_ConnectionRefused (t * testing.T ) {
1312
14- cmd := exec .Command ("go" , "run" , "../main.go" , "health" , "--port " , "9999" )
13+ cmd := exec .Command ("go" , "run" , "../main.go" , "health" , "--hostname " , "http://localhost: 9999" )
1514 out , _ := cmd .CombinedOutput ()
1615
1716 actual := string (out )
18- expected := "[UNKNOWN] - could not fetch cluster health: Get \" http://localhost:9999/_cluster/health \" : dial "
17+ expected := "[UNKNOWN] - could not fetch cluster health: no node reachable (*errors.errorString) "
1918
2019 if ! strings .Contains (actual , expected ) {
2120 t .Error ("\n Actual: " , actual , "\n Expected: " , expected )
@@ -48,7 +47,24 @@ func TestHealthCmd(t *testing.T) {
4847 w .Write ([]byte (`The Authorization header wasn't set` ))
4948 })),
5049 args : []string {"run" , "../main.go" , "health" , "--username" , "username" , "--password" , "password" },
51- expected : "[OK] - Cluster test is green | status=0 nodes=1 data_nodes=1 active_primary_shards=3 active_shards=3\n " ,
50+ expected : "[OK] - Cluster test is green | nodes=1 data_nodes=1 active_primary_shards=3 active_shards=3\n " ,
51+ },
52+ {
53+ name : "health-bearer-ok" ,
54+ server : httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
55+ token := r .Header .Get ("Authorization" )
56+ if token == "Bearer secret" {
57+ // Just for testing, this is now how to handle tokens properly
58+ w .Header ().Set ("X-Elastic-Product" , "Elasticsearch" )
59+ w .WriteHeader (http .StatusOK )
60+ 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}` ))
61+ return
62+ }
63+ w .WriteHeader (http .StatusUnauthorized )
64+ w .Write ([]byte (`The Authorization header wasn't set` ))
65+ })),
66+ args : []string {"run" , "../main.go" , "--bearer" , "secret" , "health" },
67+ expected : "[OK] - Cluster test is green | nodes=1 data_nodes=1 active_primary_shards=3 active_shards=3\n " ,
5268 },
5369 {
5470 name : "health-invalid" ,
@@ -58,7 +74,7 @@ func TestHealthCmd(t *testing.T) {
5874 w .Write ([]byte (`{}` ))
5975 })),
6076 args : []string {"run" , "../main.go" , "health" },
61- expected : "[UNKNOWN] - Cluster status unknown | status=3 nodes=0 data_nodes=0 active_primary_shards=0 active_shards=0\n exit status 3\n " ,
77+ expected : "[UNKNOWN] - Cluster status unknown | nodes=0 data_nodes=0 active_primary_shards=0 active_shards=0\n exit status 3\n " ,
6278 },
6379 {
6480 name : "health-404" ,
@@ -78,7 +94,7 @@ func TestHealthCmd(t *testing.T) {
7894 w .Write ([]byte (`{"cluster_name":"test","status":"foobar","timed_out":false,"number_of_nodes":1,"number_of_data_nodes":1,"active_primary_shards":3}` ))
7995 })),
8096 args : []string {"run" , "../main.go" , "health" },
81- expected : "[UNKNOWN] - Cluster test is foobar | status=3 nodes=1 data_nodes=1 active_primary_shards=3 active_shards=0\n exit status 3\n " ,
97+ expected : "[UNKNOWN] - Cluster test is foobar | nodes=1 data_nodes=1 active_primary_shards=3 active_shards=0\n exit status 3\n " ,
8298 },
8399 {
84100 name : "health-ok" ,
@@ -88,7 +104,7 @@ func TestHealthCmd(t *testing.T) {
88104 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}` ))
89105 })),
90106 args : []string {"run" , "../main.go" , "health" },
91- expected : "[OK] - Cluster test is green | status=0 nodes=1 data_nodes=1 active_primary_shards=3 active_shards=3\n " ,
107+ expected : "[OK] - Cluster test is green | nodes=1 data_nodes=1 active_primary_shards=3 active_shards=3\n " ,
92108 },
93109 {
94110 name : "health-yellow" ,
@@ -98,7 +114,7 @@ func TestHealthCmd(t *testing.T) {
98114 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}` ))
99115 })),
100116 args : []string {"run" , "../main.go" , "health" },
101- expected : "[WARNING] - Cluster test is yellow | status=1 nodes=1 data_nodes=1 active_primary_shards=3 active_shards=3\n exit status 1\n " ,
117+ expected : "[WARNING] - Cluster test is yellow | nodes=1 data_nodes=1 active_primary_shards=3 active_shards=3\n exit status 1\n " ,
102118 },
103119 {
104120 name : "health-red" ,
@@ -108,17 +124,15 @@ func TestHealthCmd(t *testing.T) {
108124 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}` ))
109125 })),
110126 args : []string {"run" , "../main.go" , "health" },
111- expected : "[CRITICAL] - Cluster test is red | status=2 nodes=1 data_nodes=1 active_primary_shards=3 active_shards=3\n exit status 2\n " ,
127+ expected : "[CRITICAL] - Cluster test is red | nodes=1 data_nodes=1 active_primary_shards=3 active_shards=3\n exit status 2\n " ,
112128 },
113129 }
114130
115131 for _ , test := range tests {
116132 t .Run (test .name , func (t * testing.T ) {
117133 defer test .server .Close ()
118134
119- // We need the random Port extracted
120- u , _ := url .Parse (test .server .URL )
121- cmd := exec .Command ("go" , append (test .args , "--port" , u .Port ())... )
135+ cmd := exec .Command ("go" , append (test .args , "--hostname" , test .server .URL )... )
122136 out , _ := cmd .CombinedOutput ()
123137
124138 actual := string (out )
0 commit comments