|
| 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