|
| 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 deployment |
| 19 | + |
| 20 | +import ( |
| 21 | + "errors" |
| 22 | + "reflect" |
| 23 | + "testing" |
| 24 | + |
| 25 | + "github.com/elastic/cloud-sdk-go/pkg/api" |
| 26 | + "github.com/elastic/cloud-sdk-go/pkg/api/mock" |
| 27 | + "github.com/elastic/cloud-sdk-go/pkg/models" |
| 28 | + "github.com/elastic/cloud-sdk-go/pkg/util/ec" |
| 29 | + |
| 30 | + "github.com/elastic/ecctl/pkg/util" |
| 31 | +) |
| 32 | + |
| 33 | +var deploymentList = `{ |
| 34 | + "deployments": [ |
| 35 | + { |
| 36 | + "id": "709551ecd21143adbb7a37bbf36c4321", |
| 37 | + "name": "admin-console-elasticsearch", |
| 38 | + "resources": [ |
| 39 | + { |
| 40 | + "cloud_id": "admin-console-elasticsearch:MTkyLjE2OC40NC4xMC5pcC5lcy5pbzo5MjQzJDcwOTU1MWVjZDIxMTQzYWRiYjdhMzdiYmYzNmM0MzIxJA==", |
| 41 | + "id": "709551ecd21143adbb7a37bbf36c4321", |
| 42 | + "kind": "elasticsearch", |
| 43 | + "ref_id": "elasticsearch", |
| 44 | + "region": "ece-region" |
| 45 | + } |
| 46 | + ] |
| 47 | + }, |
| 48 | + { |
| 49 | + "id": "83c2027d92c44aeba34378f44cf08137", |
| 50 | + "name": "logging-and-metrics", |
| 51 | + "resources": [ |
| 52 | + { |
| 53 | + "cloud_id": "logging-and-metrics:MTkyLjE2OC40NC4xMC5pcC5lcy5pbzo5MjQzJDgzYzIwMjdkOTJjNDRhZWJhMzQzNzhmNDRjZjA4MTM3JGQxNDBiNmUzNWIwNDQ1YTlhZmQ0NDIwZjZjMWIzYjIx", |
| 54 | + "id": "83c2027d92c44aeba34378f44cf08137", |
| 55 | + "kind": "elasticsearch", |
| 56 | + "ref_id": "elasticsearch", |
| 57 | + "region": "ece-region" |
| 58 | + }, |
| 59 | + { |
| 60 | + "elasticsearch_cluster_ref_id": "elasticsearch", |
| 61 | + "id": "d140b6e35b0445a9afd4420f6c1b3b21", |
| 62 | + "kind": "kibana", |
| 63 | + "ref_id": "kibana", |
| 64 | + "region": "ece-region" |
| 65 | + } |
| 66 | + ] |
| 67 | + }, |
| 68 | + { |
| 69 | + "id": "89a54cee4a2847c291f520ac00760886", |
| 70 | + "name": "security-cluster", |
| 71 | + "resources": [ |
| 72 | + { |
| 73 | + "cloud_id": "security-cluster:MTkyLjE2OC40NC4xMC5pcC5lcy5pbzo5MjQzJDg5YTU0Y2VlNGEyODQ3YzI5MWY1MjBhYzAwNzYwODg2JA==", |
| 74 | + "id": "89a54cee4a2847c291f520ac00760886", |
| 75 | + "kind": "elasticsearch", |
| 76 | + "ref_id": "elasticsearch", |
| 77 | + "region": "ece-region" |
| 78 | + } |
| 79 | + ] |
| 80 | + } |
| 81 | + ] |
| 82 | +} |
| 83 | +` |
| 84 | + |
| 85 | +func TestList(t *testing.T) { |
| 86 | + type args struct { |
| 87 | + params ListParams |
| 88 | + } |
| 89 | + tests := []struct { |
| 90 | + name string |
| 91 | + args args |
| 92 | + want *models.DeploymentsListResponse |
| 93 | + err error |
| 94 | + }{ |
| 95 | + { |
| 96 | + name: "fails on parameter validation", |
| 97 | + err: util.ErrAPIReq, |
| 98 | + }, |
| 99 | + { |
| 100 | + name: "fails on API error", |
| 101 | + args: args{params: ListParams{ |
| 102 | + API: api.NewMock(mock.New500Response(mock.NewStringBody("error"))), |
| 103 | + }}, |
| 104 | + err: errors.New("unknown error (status 500)"), |
| 105 | + }, |
| 106 | + { |
| 107 | + name: "Succeeds", |
| 108 | + args: args{params: ListParams{ |
| 109 | + API: api.NewMock(mock.New200Response(mock.NewStringBody(deploymentList))), |
| 110 | + }}, |
| 111 | + want: &models.DeploymentsListResponse{Deployments: []*models.DeploymentsListingData{ |
| 112 | + { |
| 113 | + ID: ec.String("709551ecd21143adbb7a37bbf36c4321"), |
| 114 | + Name: ec.String("admin-console-elasticsearch"), |
| 115 | + Resources: []*models.DeploymentResource{ |
| 116 | + { |
| 117 | + CloudID: "admin-console-elasticsearch:MTkyLjE2OC40NC4xMC5pcC5lcy5pbzo5MjQzJDcwOTU1MWVjZDIxMTQzYWRiYjdhMzdiYmYzNmM0MzIxJA==", |
| 118 | + ID: ec.String("709551ecd21143adbb7a37bbf36c4321"), |
| 119 | + Kind: ec.String("elasticsearch"), |
| 120 | + RefID: ec.String("elasticsearch"), |
| 121 | + Region: ec.String("ece-region"), |
| 122 | + }, |
| 123 | + }, |
| 124 | + }, |
| 125 | + { |
| 126 | + ID: ec.String("83c2027d92c44aeba34378f44cf08137"), |
| 127 | + Name: ec.String("logging-and-metrics"), |
| 128 | + Resources: []*models.DeploymentResource{ |
| 129 | + { |
| 130 | + CloudID: "logging-and-metrics:MTkyLjE2OC40NC4xMC5pcC5lcy5pbzo5MjQzJDgzYzIwMjdkOTJjNDRhZWJhMzQzNzhmNDRjZjA4MTM3JGQxNDBiNmUzNWIwNDQ1YTlhZmQ0NDIwZjZjMWIzYjIx", |
| 131 | + ID: ec.String("83c2027d92c44aeba34378f44cf08137"), |
| 132 | + Kind: ec.String("elasticsearch"), |
| 133 | + RefID: ec.String("elasticsearch"), |
| 134 | + Region: ec.String("ece-region"), |
| 135 | + }, |
| 136 | + { |
| 137 | + ElasticsearchClusterRefID: "elasticsearch", |
| 138 | + ID: ec.String("d140b6e35b0445a9afd4420f6c1b3b21"), |
| 139 | + Kind: ec.String("kibana"), |
| 140 | + RefID: ec.String("kibana"), |
| 141 | + Region: ec.String("ece-region"), |
| 142 | + }, |
| 143 | + }, |
| 144 | + }, |
| 145 | + { |
| 146 | + ID: ec.String("89a54cee4a2847c291f520ac00760886"), |
| 147 | + Name: ec.String("security-cluster"), |
| 148 | + Resources: []*models.DeploymentResource{ |
| 149 | + { |
| 150 | + CloudID: "security-cluster:MTkyLjE2OC40NC4xMC5pcC5lcy5pbzo5MjQzJDg5YTU0Y2VlNGEyODQ3YzI5MWY1MjBhYzAwNzYwODg2JA==", |
| 151 | + ID: ec.String("89a54cee4a2847c291f520ac00760886"), |
| 152 | + Kind: ec.String("elasticsearch"), |
| 153 | + RefID: ec.String("elasticsearch"), |
| 154 | + Region: ec.String("ece-region"), |
| 155 | + }, |
| 156 | + }, |
| 157 | + }, |
| 158 | + }}, |
| 159 | + }, |
| 160 | + } |
| 161 | + for _, tt := range tests { |
| 162 | + t.Run(tt.name, func(t *testing.T) { |
| 163 | + got, err := List(tt.args.params) |
| 164 | + if !reflect.DeepEqual(err, tt.err) { |
| 165 | + t.Errorf("List() error = %v, wantErr %v", err, tt.err) |
| 166 | + return |
| 167 | + } |
| 168 | + if !reflect.DeepEqual(got, tt.want) { |
| 169 | + t.Errorf("List() = %+v, want %+v", got, tt.want) |
| 170 | + } |
| 171 | + }) |
| 172 | + } |
| 173 | +} |
0 commit comments