|
| 1 | +// Licensed to Elasticsearch B.V. under one or more contributor |
| 2 | +// license agreements. See the NOTICE file distributed with |
| 3 | +// this work for additional information regarding copyright |
| 4 | +// ownership. Elasticsearch B.V. licenses this file to you under |
| 5 | +// the Apache License, Version 2.0 (the "License"); you may |
| 6 | +// not use this file except in compliance with the License. |
| 7 | +// You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +package cmddeployment |
| 19 | + |
| 20 | +import ( |
| 21 | +"github.com/elastic/cloud-sdk-go/pkg/api" |
| 22 | +"github.com/elastic/cloud-sdk-go/pkg/api/mock" |
| 23 | +"github.com/elastic/cloud-sdk-go/pkg/models" |
| 24 | +"github.com/elastic/cloud-sdk-go/pkg/util/ec" |
| 25 | +"github.com/elastic/ecctl/cmd/util/testutils" |
| 26 | +"testing" |
| 27 | +) |
| 28 | + |
| 29 | +func Test_searchCmd(t *testing.T) { |
| 30 | +tests := []struct { |
| 31 | +name string |
| 32 | +args testutils.Args |
| 33 | +want testutils.Assertion |
| 34 | +}{ |
| 35 | +{ |
| 36 | +name: "all-matches collects deployments using multiple requests", |
| 37 | +args: testutils.Args{ |
| 38 | +Cmd: searchCmd, |
| 39 | +Args: []string{ |
| 40 | +"search", |
| 41 | +"-f", |
| 42 | +"testdata/search_query.json", |
| 43 | +"--all-matches", |
| 44 | +"--size", |
| 45 | +"250", |
| 46 | +}, |
| 47 | +Cfg: testutils.MockCfg{ |
| 48 | +OutputFormat: "json", |
| 49 | +Responses: []mock.Response{ |
| 50 | +mock.New200ResponseAssertion( |
| 51 | +&mock.RequestAssertion{ |
| 52 | +Header: api.DefaultWriteMockHeaders, |
| 53 | +Method: "POST", |
| 54 | +Path: "/api/v1/deployments/_search", |
| 55 | +Host: api.DefaultMockHost, |
| 56 | +Body: mock.NewStructBody(models.SearchRequest{ |
| 57 | +Query: &models.QueryContainer{ |
| 58 | +MatchAll: struct{}{}, |
| 59 | +}, |
| 60 | +Size: 250, |
| 61 | +Sort: []interface{}{"id"}, |
| 62 | +}), |
| 63 | +}, |
| 64 | +mock.NewStructBody(models.DeploymentsSearchResponse{ |
| 65 | +Cursor: "cursor1", |
| 66 | +Deployments: []*models.DeploymentSearchResponse{ |
| 67 | +{ID: ec.String("d1")}, |
| 68 | +{ID: ec.String("d2")}, |
| 69 | +}, |
| 70 | +MatchCount: 3, |
| 71 | +MinimalMetadata: nil, |
| 72 | +ReturnCount: ec.Int32(2), |
| 73 | +}), |
| 74 | +), |
| 75 | +mock.New200ResponseAssertion( |
| 76 | +&mock.RequestAssertion{ |
| 77 | +Header: api.DefaultWriteMockHeaders, |
| 78 | +Method: "POST", |
| 79 | +Path: "/api/v1/deployments/_search", |
| 80 | +Host: api.DefaultMockHost, |
| 81 | +Body: mock.NewStructBody(models.SearchRequest{ |
| 82 | +Cursor: "cursor1", |
| 83 | +Query: &models.QueryContainer{ |
| 84 | +MatchAll: struct{}{}, |
| 85 | +}, |
| 86 | +Size: 250, |
| 87 | +Sort: []interface{}{"id"}, |
| 88 | +}), |
| 89 | +}, |
| 90 | +mock.NewStructBody(models.DeploymentsSearchResponse{ |
| 91 | +Cursor: "cursor2", |
| 92 | +Deployments: []*models.DeploymentSearchResponse{ |
| 93 | +{ID: ec.String("d3")}, |
| 94 | +}, |
| 95 | +MatchCount: 3, |
| 96 | +MinimalMetadata: nil, |
| 97 | +ReturnCount: ec.Int32(1), |
| 98 | +}), |
| 99 | +), |
| 100 | +mock.New200ResponseAssertion( |
| 101 | +&mock.RequestAssertion{ |
| 102 | +Header: api.DefaultWriteMockHeaders, |
| 103 | +Method: "POST", |
| 104 | +Path: "/api/v1/deployments/_search", |
| 105 | +Host: api.DefaultMockHost, |
| 106 | +Body: mock.NewStructBody(models.SearchRequest{ |
| 107 | +Cursor: "cursor2", |
| 108 | +Query: &models.QueryContainer{ |
| 109 | +MatchAll: struct{}{}, |
| 110 | +}, |
| 111 | +Size: 250, |
| 112 | +Sort: []interface{}{"id"}, |
| 113 | +}), |
| 114 | +}, |
| 115 | +mock.NewStructBody(models.DeploymentsSearchResponse{ |
| 116 | +Cursor: "cursor3", |
| 117 | +Deployments: []*models.DeploymentSearchResponse{}, |
| 118 | +MatchCount: 3, |
| 119 | +MinimalMetadata: nil, |
| 120 | +ReturnCount: ec.Int32(0), |
| 121 | +}), |
| 122 | +), |
| 123 | +}, |
| 124 | +}, |
| 125 | +}, |
| 126 | +want: testutils.Assertion{ |
| 127 | +Stdout: string(expectedOutput) + "\n", |
| 128 | +}, |
| 129 | +}, |
| 130 | +{ |
| 131 | +name: "all-matches requires a query with a sort", |
| 132 | +args: testutils.Args{ |
| 133 | +Cmd: searchCmd, |
| 134 | +Args: []string{ |
| 135 | +"search", |
| 136 | +"-f", |
| 137 | +"testdata/search_query_no_sort.json", |
| 138 | +"--all-matches", |
| 139 | +}, |
| 140 | +Cfg: testutils.MockCfg{ |
| 141 | +OutputFormat: "json", |
| 142 | +Responses: []mock.Response{}, |
| 143 | +}, |
| 144 | +}, |
| 145 | +want: testutils.Assertion{ |
| 146 | +Err: "The query must include a sort-field when using --all-matches. Example: \"sort\": [\"id\"]", |
| 147 | +}, |
| 148 | +}, |
| 149 | +} |
| 150 | +for _, tt := range tests { |
| 151 | +t.Run(tt.name, func(t *testing.T) { |
| 152 | +testutils.RunCmdAssertion(t, tt.args, tt.want) |
| 153 | +}) |
| 154 | +} |
| 155 | +} |
| 156 | + |
| 157 | +var expectedOutput = `{ |
| 158 | + "deployments": [ |
| 159 | + { |
| 160 | + "healthy": null, |
| 161 | + "id": "d1", |
| 162 | + "name": null, |
| 163 | + "resources": null |
| 164 | + }, |
| 165 | + { |
| 166 | + "healthy": null, |
| 167 | + "id": "d2", |
| 168 | + "name": null, |
| 169 | + "resources": null |
| 170 | + }, |
| 171 | + { |
| 172 | + "healthy": null, |
| 173 | + "id": "d3", |
| 174 | + "name": null, |
| 175 | + "resources": null |
| 176 | + } |
| 177 | + ], |
| 178 | + "match_count": 3, |
| 179 | + "minimal_metadata": null, |
| 180 | + "return_count": 3 |
| 181 | +}` |
0 commit comments