|
| 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 TestSnapshot_ConnectionRefused(t *testing.T) { |
| 13 | + |
| 14 | + cmd := exec.Command("go", "run", "../main.go", "snapshot", "--port", "9999") |
| 15 | + out, _ := cmd.CombinedOutput() |
| 16 | + |
| 17 | + actual := string(out) |
| 18 | + expected := "[UNKNOWN] - could not fetch snapshots: Get \"http://localhost:9999/_snapshot/*/*?order=desc\": dial" |
| 19 | + |
| 20 | + if !strings.Contains(actual, expected) { |
| 21 | + t.Error("\nActual: ", actual, "\nExpected: ", expected) |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +func TestSnapshot_WithWrongFlags(t *testing.T) { |
| 26 | + |
| 27 | + cmd := exec.Command("go", "run", "../main.go", "snapshot", "--all", "--number", "9999") |
| 28 | + out, _ := cmd.CombinedOutput() |
| 29 | + |
| 30 | + actual := string(out) |
| 31 | + expected := "[UNKNOWN] - if any flags in the group" |
| 32 | + |
| 33 | + if !strings.Contains(actual, expected) { |
| 34 | + t.Error("\nActual: ", actual, "\nExpected: ", expected) |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +type SnapshotTest struct { |
| 39 | + name string |
| 40 | + server *httptest.Server |
| 41 | + args []string |
| 42 | + expected string |
| 43 | +} |
| 44 | + |
| 45 | +func TestSnapshotCmd(t *testing.T) { |
| 46 | + tests := []SnapshotTest{ |
| 47 | + { |
| 48 | + name: "no-snapshot", |
| 49 | + server: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 50 | + w.Header().Set("X-Elastic-Product", "Elasticsearch") |
| 51 | + w.WriteHeader(http.StatusOK) |
| 52 | + w.Write([]byte(`Hey dude where my snapshot`)) |
| 53 | + })), |
| 54 | + args: []string{"run", "../main.go", "snapshot"}, |
| 55 | + expected: "[UNKNOWN] - could not decode snapshot response", |
| 56 | + }, |
| 57 | + { |
| 58 | + name: "snapshot-ok", |
| 59 | + server: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 60 | + w.Header().Set("X-Elastic-Product", "Elasticsearch") |
| 61 | + w.WriteHeader(http.StatusOK) |
| 62 | + w.Write([]byte(`{"snapshots":[{"snapshot":"snapshot_1","uuid":"dKb54xw67gvdRctLCxSket","repository":"my_repository","version_id":1.1,"version":1,"indices":[],"data_streams":[],"feature_states":[],"include_global_state":true,"state":"SUCCESS","start_time":"2020-07-06T21:55:18.129Z","start_time_in_millis":1593093628850,"end_time":"2020-07-06T21:55:18.129Z","end_time_in_millis":1593094752018,"duration_in_millis":0,"failures":[],"shards":{"total":0,"failed":0,"successful":0}},{"snapshot":"snapshot_2","uuid":"vdRctLCxSketdKb54xw67g","repository":"my_repository","version_id":2,"version":2,"indices":[],"data_streams":[],"feature_states":[],"include_global_state":true,"state":"SUCCESS","start_time":"2020-07-06T21:55:18.130Z","start_time_in_millis":1593093628851,"end_time":"2020-07-06T21:55:18.130Z","end_time_in_millis":1593094752019,"duration_in_millis":1,"failures":[],"shards":{"total":0,"failed":0,"successful":0}}],"next":"c25hcHNob3RfMixteV9yZXBvc2l0b3J5LHNuYXBzaG90XzI=","total":3,"remaining":1} |
| 63 | +`)) |
| 64 | + })), |
| 65 | + args: []string{"run", "../main.go", "snapshot"}, |
| 66 | + expected: "[OK] - All evaluated snapshots are in state SUCCESS", |
| 67 | + }, |
| 68 | + { |
| 69 | + name: "snapshot-inprogress", |
| 70 | + server: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 71 | + w.Header().Set("X-Elastic-Product", "Elasticsearch") |
| 72 | + w.WriteHeader(http.StatusOK) |
| 73 | + w.Write([]byte(`{"snapshots":[{"snapshot":"snapshot_1","uuid":"dKb54xw67gvdRctLCxSket","repository":"my_repository","version_id":1,"version":1,"indices":[],"data_streams":[],"feature_states":[],"include_global_state":true,"state":"IN_PROGRESS","start_time":"2020-07-06T21:55:18.129Z","start_time_in_millis":1593093628850,"end_time":"2020-07-06T21:55:18.129Z","end_time_in_millis":1593094752018,"duration_in_millis":0,"failures":[],"shards":{"total":0,"failed":0,"successful":0}},{"snapshot":"snapshot_2","uuid":"vdRctLCxSketdKb54xw67g","repository":"my_repository","version_id":2,"version":2,"indices":[],"data_streams":[],"feature_states":[],"include_global_state":true,"state":"SUCCESS","start_time":"2020-07-06T21:55:18.130Z","start_time_in_millis":1593093628851,"end_time":"2020-07-06T21:55:18.130Z","end_time_in_millis":1593094752019,"duration_in_millis":1,"failures":[],"shards":{"total":0,"failed":0,"successful":0}}],"next":"c25hcHNob3RfMixteV9yZXBvc2l0b3J5LHNuYXBzaG90XzI=","total":3,"remaining":1} |
| 74 | +`)) |
| 75 | + })), |
| 76 | + args: []string{"run", "../main.go", "snapshot"}, |
| 77 | + expected: "[UNKNOWN] - At least one evaluated snapshot is in state IN_PROGRESS", |
| 78 | + }, |
| 79 | + { |
| 80 | + name: "snapshot-failed-with-all", |
| 81 | + server: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 82 | + w.Header().Set("X-Elastic-Product", "Elasticsearch") |
| 83 | + w.WriteHeader(http.StatusOK) |
| 84 | + w.Write([]byte(`{"snapshots":[{"snapshot":"snapshot_1","uuid":"dKb54xw67gvdRctLCxSket","repository":"my_repository","version_id":1,"version":1,"indices":[],"data_streams":[],"feature_states":[],"include_global_state":true,"state":"IN_PROGRESS","start_time":"2020-07-06T21:55:18.129Z","start_time_in_millis":1593093628850,"end_time":"2020-07-06T21:55:18.129Z","end_time_in_millis":1593094752018,"duration_in_millis":0,"failures":[],"shards":{"total":0,"failed":0,"successful":0}},{"snapshot":"snapshot_2","uuid":"vdRctLCxSketdKb54xw67g","repository":"my_repository","version_id":2,"version":2,"indices":[],"data_streams":[],"feature_states":[],"include_global_state":true,"state":"FAILED","start_time":"2020-07-06T21:55:18.130Z","start_time_in_millis":1593093628851,"end_time":"2020-07-06T21:55:18.130Z","end_time_in_millis":1593094752019,"duration_in_millis":1,"failures":[],"shards":{"total":0,"failed":0,"successful":0}}],"next":"c25hcHNob3RfMixteV9yZXBvc2l0b3J5LHNuYXBzaG90XzI=","total":3,"remaining":1} |
| 85 | +`)) |
| 86 | + })), |
| 87 | + args: []string{"run", "../main.go", "snapshot", "--all"}, |
| 88 | + expected: "[CRITICAL] - At least one evaluated snapshot is in state FAILED", |
| 89 | + }, |
| 90 | + { |
| 91 | + name: "snapshot-partial-with-number", |
| 92 | + server: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 93 | + w.Header().Set("X-Elastic-Product", "Elasticsearch") |
| 94 | + w.WriteHeader(http.StatusOK) |
| 95 | + w.Write([]byte(`{"snapshots":[{"snapshot":"snapshot_1","uuid":"dKb54xw67gvdRctLCxSket","repository":"my_repository","version_id":1,"version":1,"indices":[],"data_streams":[],"feature_states":[],"include_global_state":true,"state":"SUCCESS","start_time":"2020-07-06T21:55:18.129Z","start_time_in_millis":1593093628850,"end_time":"2020-07-06T21:55:18.129Z","end_time_in_millis":1593094752018,"duration_in_millis":0,"failures":[],"shards":{"total":0,"failed":0,"successful":0}},{"snapshot":"snapshot_2","uuid":"vdRctLCxSketdKb54xw67g","repository":"my_repository","version_id":2,"version":2,"indices":[],"data_streams":[],"feature_states":[],"include_global_state":true,"state":"PARTIAL","start_time":"2020-07-06T21:55:18.130Z","start_time_in_millis":1593093628851,"end_time":"2020-07-06T21:55:18.130Z","end_time_in_millis":1593094752019,"duration_in_millis":1,"failures":[],"shards":{"total":0,"failed":0,"successful":0}}],"next":"c25hcHNob3RfMixteV9yZXBvc2l0b3J5LHNuYXBzaG90XzI=","total":3,"remaining":1} |
| 96 | +`)) |
| 97 | + })), |
| 98 | + args: []string{"run", "../main.go", "snapshot", "--number", "4"}, |
| 99 | + expected: "[WARNING] - At least one evaluated snapshot is in state PARTIAL", |
| 100 | + }, |
| 101 | + } |
| 102 | + |
| 103 | + for _, test := range tests { |
| 104 | + t.Run(test.name, func(t *testing.T) { |
| 105 | + defer test.server.Close() |
| 106 | + |
| 107 | + // We need the random Port extracted |
| 108 | + u, _ := url.Parse(test.server.URL) |
| 109 | + cmd := exec.Command("go", append(test.args, "--port", u.Port())...) |
| 110 | + out, _ := cmd.CombinedOutput() |
| 111 | + |
| 112 | + actual := string(out) |
| 113 | + |
| 114 | + if !strings.Contains(actual, test.expected) { |
| 115 | + t.Error("\nActual: ", actual, "\nExpected: ", test.expected) |
| 116 | + } |
| 117 | + |
| 118 | + }) |
| 119 | + } |
| 120 | +} |
0 commit comments