|
| 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 | + "github.com/hashicorp/go-multierror" |
| 30 | + |
| 31 | + "github.com/elastic/ecctl/pkg/util" |
| 32 | +) |
| 33 | + |
| 34 | +func TestShutdown(t *testing.T) { |
| 35 | + type args struct { |
| 36 | + params ShutdownParams |
| 37 | + } |
| 38 | + tests := []struct { |
| 39 | + name string |
| 40 | + args args |
| 41 | + want *models.DeploymentShutdownResponse |
| 42 | + err error |
| 43 | + }{ |
| 44 | + { |
| 45 | + name: "fails on parameter validation", |
| 46 | + err: &multierror.Error{Errors: []error{ |
| 47 | + util.ErrAPIReq, |
| 48 | + util.ErrDeploymentID, |
| 49 | + }}, |
| 50 | + }, |
| 51 | + { |
| 52 | + name: "fails on API error", |
| 53 | + args: args{params: ShutdownParams{ |
| 54 | + API: api.NewMock(mock.New500Response(mock.NewStringBody("error"))), |
| 55 | + DeploymentID: util.ValidClusterID, |
| 56 | + }}, |
| 57 | + err: errors.New("unknown error (status 500)"), |
| 58 | + }, |
| 59 | + { |
| 60 | + name: "Succeeds", |
| 61 | + args: args{params: ShutdownParams{ |
| 62 | + API: api.NewMock(mock.New200Response(mock.NewStructBody(models.DeploymentShutdownResponse{ |
| 63 | + ID: ec.String(util.ValidClusterID), |
| 64 | + }))), |
| 65 | + DeploymentID: util.ValidClusterID, |
| 66 | + }}, |
| 67 | + want: &models.DeploymentShutdownResponse{ |
| 68 | + ID: ec.String(util.ValidClusterID), |
| 69 | + }, |
| 70 | + }, |
| 71 | + } |
| 72 | + for _, tt := range tests { |
| 73 | + t.Run(tt.name, func(t *testing.T) { |
| 74 | + got, err := Shutdown(tt.args.params) |
| 75 | + if !reflect.DeepEqual(err, tt.err) { |
| 76 | + t.Errorf("Shutdown() error = %v, wantErr %v", err, tt.err) |
| 77 | + return |
| 78 | + } |
| 79 | + if !reflect.DeepEqual(got, tt.want) { |
| 80 | + t.Errorf("Shutdown() = %v, want %v", got, tt.want) |
| 81 | + } |
| 82 | + }) |
| 83 | + } |
| 84 | +} |
0 commit comments