|
| 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 cmddeploymenttrafficfilter |
| 19 | + |
| 20 | +import ( |
| 21 | + "net/url" |
| 22 | + "testing" |
| 23 | + |
| 24 | + "github.com/elastic/cloud-sdk-go/pkg/api" |
| 25 | + "github.com/elastic/cloud-sdk-go/pkg/api/mock" |
| 26 | + |
| 27 | + "github.com/elastic/ecctl/cmd/util/testutils" |
| 28 | +) |
| 29 | + |
| 30 | +func Test_deleteCmd(t *testing.T) { |
| 31 | + tests := []struct { |
| 32 | + name string |
| 33 | + args testutils.Args |
| 34 | + want testutils.Assertion |
| 35 | + }{ |
| 36 | + { |
| 37 | + name: "fails due to empty argument", |
| 38 | + args: testutils.Args{ |
| 39 | + Cmd: showCmd, |
| 40 | + Args: []string{"delete"}, |
| 41 | + Cfg: testutils.MockCfg{Responses: []mock.Response{ |
| 42 | + mock.SampleInternalError(), |
| 43 | + }}, |
| 44 | + }, |
| 45 | + want: testutils.Assertion{ |
| 46 | + Err: `requires at least 1 arg(s), only received 0`, |
| 47 | + }, |
| 48 | + }, |
| 49 | + { |
| 50 | + name: "fails due to API error", |
| 51 | + args: testutils.Args{ |
| 52 | + Cmd: showCmd, |
| 53 | + Args: []string{ |
| 54 | + "delete", "11111111111111111111111111111111", |
| 55 | + }, |
| 56 | + Cfg: testutils.MockCfg{Responses: []mock.Response{ |
| 57 | + mock.SampleInternalError(), |
| 58 | + }}, |
| 59 | + }, |
| 60 | + want: testutils.Assertion{ |
| 61 | + Err: mock.MultierrorInternalError.Error(), |
| 62 | + }, |
| 63 | + }, |
| 64 | + { |
| 65 | + name: "succeeds", |
| 66 | + args: testutils.Args{ |
| 67 | + Cmd: showCmd, |
| 68 | + Args: []string{ |
| 69 | + "delete", "4e974d9476534d35b12fbdcfd0acee0a", |
| 70 | + }, |
| 71 | + Cfg: testutils.MockCfg{ |
| 72 | + OutputFormat: "json", |
| 73 | + Responses: []mock.Response{ |
| 74 | + mock.New200ResponseAssertion( |
| 75 | + &mock.RequestAssertion{ |
| 76 | + Header: api.DefaultWriteMockHeaders, |
| 77 | + Method: "DELETE", |
| 78 | + Path: "/api/v1/deployments/traffic-filter/rulesets/4e974d9476534d35b12fbdcfd0acee0a", |
| 79 | + Host: api.DefaultMockHost, |
| 80 | + Query: url.Values{ |
| 81 | + "ignore_associations": []string{"false"}, |
| 82 | + }, |
| 83 | + }, |
| 84 | + mock.NewByteBody(nil), |
| 85 | + ), |
| 86 | + }, |
| 87 | + }, |
| 88 | + }, |
| 89 | + }, |
| 90 | + { |
| 91 | + name: "succeeds with ignore associations", |
| 92 | + args: testutils.Args{ |
| 93 | + Cmd: showCmd, |
| 94 | + Args: []string{ |
| 95 | + "delete", "4e974d9476534d35b12fbdcfd0acee0a", "--ignore-associations", |
| 96 | + }, |
| 97 | + Cfg: testutils.MockCfg{ |
| 98 | + OutputFormat: "json", |
| 99 | + Responses: []mock.Response{ |
| 100 | + mock.New200ResponseAssertion( |
| 101 | + &mock.RequestAssertion{ |
| 102 | + Header: api.DefaultWriteMockHeaders, |
| 103 | + Method: "DELETE", |
| 104 | + Path: "/api/v1/deployments/traffic-filter/rulesets/4e974d9476534d35b12fbdcfd0acee0a", |
| 105 | + Host: api.DefaultMockHost, |
| 106 | + Query: url.Values{ |
| 107 | + "ignore_associations": []string{"true"}, |
| 108 | + }, |
| 109 | + }, |
| 110 | + mock.NewByteBody(nil), |
| 111 | + ), |
| 112 | + }, |
| 113 | + }, |
| 114 | + }, |
| 115 | + }, |
| 116 | + } |
| 117 | + for _, tt := range tests { |
| 118 | + t.Run(tt.name, func(t *testing.T) { |
| 119 | + testutils.RunCmdAssertion(t, tt.args, tt.want) |
| 120 | + tt.args.Cmd.ResetFlags() |
| 121 | + }) |
| 122 | + } |
| 123 | +} |
0 commit comments