|
| 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 cmdutil |
| 19 | + |
| 20 | +import ( |
| 21 | + "io/ioutil" |
| 22 | + "reflect" |
| 23 | + "testing" |
| 24 | + "time" |
| 25 | + |
| 26 | + "github.com/elastic/cloud-sdk-go/pkg/api" |
| 27 | + "github.com/elastic/cloud-sdk-go/pkg/models" |
| 28 | + "github.com/elastic/cloud-sdk-go/pkg/output" |
| 29 | + "github.com/elastic/cloud-sdk-go/pkg/plan" |
| 30 | + "github.com/elastic/cloud-sdk-go/pkg/plan/planutil" |
| 31 | + |
| 32 | + "github.com/elastic/ecctl/pkg/ecctl" |
| 33 | + "github.com/elastic/ecctl/pkg/formatter" |
| 34 | + "github.com/elastic/ecctl/pkg/util" |
| 35 | +) |
| 36 | + |
| 37 | +func TestNewTrackParams(t *testing.T) { |
| 38 | + textFormatter := &formatter.Text{} |
| 39 | + some := models.APIKeyResponse{} |
| 40 | + someOutputDevice := output.NewDevice(ioutil.Discard) |
| 41 | + emptyMockAAPI := api.NewMock() |
| 42 | + ecctlInstance := ecctl.App{ |
| 43 | + API: emptyMockAAPI, |
| 44 | + Formatter: textFormatter, |
| 45 | + Config: ecctl.Config{ |
| 46 | + Output: "text", |
| 47 | + OutputDevice: someOutputDevice, |
| 48 | + }, |
| 49 | + } |
| 50 | + |
| 51 | + type args struct { |
| 52 | + params TrackParamsConfig |
| 53 | + } |
| 54 | + tests := []struct { |
| 55 | + name string |
| 56 | + args args |
| 57 | + want TrackParams |
| 58 | + }{ |
| 59 | + { |
| 60 | + name: "empty FrequencyConfig obtains the default one", |
| 61 | + args: args{params: TrackParamsConfig{ |
| 62 | + DeploymentID: util.ValidClusterID, |
| 63 | + Track: true, |
| 64 | + Template: "some/template", |
| 65 | + Response: some, |
| 66 | + App: &ecctlInstance, |
| 67 | + }}, |
| 68 | + want: TrackParams{ |
| 69 | + TrackChangeParams: planutil.TrackChangeParams{ |
| 70 | + TrackChangeParams: plan.TrackChangeParams{ |
| 71 | + DeploymentID: util.ValidClusterID, |
| 72 | + API: emptyMockAAPI, |
| 73 | + Config: DefaultTrackFrequencyConfig, |
| 74 | + }, |
| 75 | + Writer: someOutputDevice, |
| 76 | + Format: "text", |
| 77 | + }, |
| 78 | + Template: "some/template", |
| 79 | + Formatter: textFormatter, |
| 80 | + Track: true, |
| 81 | + Response: some, |
| 82 | + }, |
| 83 | + }, |
| 84 | + { |
| 85 | + name: "Specifies a TrackFrequencyConfig", |
| 86 | + args: args{params: TrackParamsConfig{ |
| 87 | + DeploymentID: util.ValidClusterID, |
| 88 | + Track: true, |
| 89 | + Template: "some/template", |
| 90 | + Response: some, |
| 91 | + App: &ecctlInstance, |
| 92 | + FrequencyConfig: &plan.TrackFrequencyConfig{ |
| 93 | + PollFrequency: time.Second * 10, |
| 94 | + MaxRetries: 5, |
| 95 | + }, |
| 96 | + }}, |
| 97 | + want: TrackParams{ |
| 98 | + TrackChangeParams: planutil.TrackChangeParams{ |
| 99 | + TrackChangeParams: plan.TrackChangeParams{ |
| 100 | + DeploymentID: util.ValidClusterID, |
| 101 | + API: emptyMockAAPI, |
| 102 | + Config: plan.TrackFrequencyConfig{ |
| 103 | + PollFrequency: time.Second * 10, |
| 104 | + MaxRetries: 5, |
| 105 | + }, |
| 106 | + }, |
| 107 | + Writer: someOutputDevice, |
| 108 | + Format: "text", |
| 109 | + }, |
| 110 | + Template: "some/template", |
| 111 | + Formatter: textFormatter, |
| 112 | + Track: true, |
| 113 | + Response: some, |
| 114 | + }, |
| 115 | + }, |
| 116 | + } |
| 117 | + for _, tt := range tests { |
| 118 | + t.Run(tt.name, func(t *testing.T) { |
| 119 | + got := NewTrackParams(tt.args.params) |
| 120 | + if !reflect.DeepEqual(got, tt.want) { |
| 121 | + t.Errorf("NewTrackParams() = %v, want %v", got, tt.want) |
| 122 | + } |
| 123 | + }) |
| 124 | + } |
| 125 | +} |
0 commit comments