|
| 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 | + "encoding/json" |
| 22 | + "io/ioutil" |
| 23 | + "net/url" |
| 24 | + "testing" |
| 25 | + |
| 26 | + "github.com/elastic/cloud-sdk-go/pkg/api" |
| 27 | + "github.com/elastic/cloud-sdk-go/pkg/api/mock" |
| 28 | + "github.com/elastic/cloud-sdk-go/pkg/models" |
| 29 | + |
| 30 | + "github.com/elastic/ecctl/cmd/util/testutils" |
| 31 | +) |
| 32 | + |
| 33 | +func Test_updateCmd(t *testing.T) { |
| 34 | + updatePayloadRawResp, err := ioutil.ReadFile("./testdata/update-payload.json") |
| 35 | + if err != nil { |
| 36 | + t.Fatal(err) |
| 37 | + } |
| 38 | + |
| 39 | + var succeedPayloadResp = new(models.TrafficFilterRulesetRequest) |
| 40 | + if err := succeedPayloadResp.UnmarshalBinary(updatePayloadRawResp); err != nil { |
| 41 | + t.Fatal(err) |
| 42 | + } |
| 43 | + |
| 44 | + updatePayloadJSONOutput, err := json.MarshalIndent(succeedPayloadResp, "", " ") |
| 45 | + if err != nil { |
| 46 | + t.Fatal(err) |
| 47 | + } |
| 48 | + |
| 49 | + updateRawResp, err := ioutil.ReadFile("./testdata/update.json") |
| 50 | + if err != nil { |
| 51 | + t.Fatal(err) |
| 52 | + } |
| 53 | + |
| 54 | + var succeedResp = new(models.TrafficFilterRulesetResponse) |
| 55 | + if err := succeedResp.UnmarshalBinary(updateRawResp); err != nil { |
| 56 | + t.Fatal(err) |
| 57 | + } |
| 58 | + |
| 59 | + updateJSONOutput, err := json.MarshalIndent(succeedResp, "", " ") |
| 60 | + if err != nil { |
| 61 | + t.Fatal(err) |
| 62 | + } |
| 63 | + |
| 64 | + tests := []struct { |
| 65 | + name string |
| 66 | + args testutils.Args |
| 67 | + want testutils.Assertion |
| 68 | + }{ |
| 69 | + { |
| 70 | + name: "fails due to empty argument", |
| 71 | + args: testutils.Args{ |
| 72 | + Cmd: updateCmd, |
| 73 | + Args: []string{ |
| 74 | + "update", |
| 75 | + }, |
| 76 | + Cfg: testutils.MockCfg{Responses: []mock.Response{ |
| 77 | + mock.SampleInternalError(), |
| 78 | + }}, |
| 79 | + }, |
| 80 | + want: testutils.Assertion{ |
| 81 | + Err: `requires at least 1 arg(s), only received 0`, |
| 82 | + }, |
| 83 | + }, |
| 84 | + { |
| 85 | + name: "fails due to missing flags", |
| 86 | + args: testutils.Args{ |
| 87 | + Cmd: updateCmd, |
| 88 | + Args: []string{ |
| 89 | + "update", "4e974d9476534d35b12fbdcfd0acee0a", |
| 90 | + }, |
| 91 | + Cfg: testutils.MockCfg{Responses: []mock.Response{ |
| 92 | + mock.SampleInternalError(), |
| 93 | + }}, |
| 94 | + }, |
| 95 | + want: testutils.Assertion{ |
| 96 | + Err: `one of --file or --generate-payload must be set`, |
| 97 | + }, |
| 98 | + }, |
| 99 | + { |
| 100 | + name: "fails due to mismatching flags", |
| 101 | + args: testutils.Args{ |
| 102 | + Cmd: updateCmd, |
| 103 | + Args: []string{ |
| 104 | + "update", "4e974d9476534d35b12fbdcfd0acee0a", "--generate-payload", "--file", "testdata/update.json", |
| 105 | + }, |
| 106 | + Cfg: testutils.MockCfg{Responses: []mock.Response{ |
| 107 | + mock.SampleInternalError(), |
| 108 | + }}, |
| 109 | + }, |
| 110 | + want: testutils.Assertion{ |
| 111 | + Err: "both --file and --generate-payload are set. Only one may be used", |
| 112 | + }, |
| 113 | + }, |
| 114 | + { |
| 115 | + name: "fails due to API error", |
| 116 | + args: testutils.Args{ |
| 117 | + Cmd: updateCmd, |
| 118 | + Args: []string{ |
| 119 | + "update", "4e974d9476534d35b12fbdcfd0acee0a", "--generate-payload", |
| 120 | + }, |
| 121 | + Cfg: testutils.MockCfg{Responses: []mock.Response{ |
| 122 | + mock.SampleInternalError(), |
| 123 | + }}, |
| 124 | + }, |
| 125 | + want: testutils.Assertion{ |
| 126 | + Err: mock.MultierrorInternalError.Error(), |
| 127 | + }, |
| 128 | + }, |
| 129 | + { |
| 130 | + name: "succeeds to generate payload", |
| 131 | + args: testutils.Args{ |
| 132 | + Cmd: updateCmd, |
| 133 | + Args: []string{ |
| 134 | + "update", "4e974d9476534d35b12fbdcfd0acee0a", "--generate-payload", |
| 135 | + }, |
| 136 | + Cfg: testutils.MockCfg{ |
| 137 | + OutputFormat: "json", |
| 138 | + Responses: []mock.Response{ |
| 139 | + mock.New200ResponseAssertion( |
| 140 | + &mock.RequestAssertion{ |
| 141 | + Header: api.DefaultReadMockHeaders, |
| 142 | + Method: "GET", |
| 143 | + Path: "/api/v1/deployments/traffic-filter/rulesets/4e974d9476534d35b12fbdcfd0acee0a", |
| 144 | + Host: api.DefaultMockHost, |
| 145 | + Query: url.Values{ |
| 146 | + "include_associations": []string{"false"}, |
| 147 | + }, |
| 148 | + }, |
| 149 | + mock.NewByteBody(updatePayloadRawResp), |
| 150 | + ), |
| 151 | + }, |
| 152 | + }, |
| 153 | + }, |
| 154 | + want: testutils.Assertion{ |
| 155 | + Stdout: string(updatePayloadJSONOutput) + "\n", |
| 156 | + }, |
| 157 | + }, |
| 158 | + { |
| 159 | + name: "succeeds to update", |
| 160 | + args: testutils.Args{ |
| 161 | + Cmd: updateCmd, |
| 162 | + Args: []string{ |
| 163 | + "update", "4e974d9476534d35b12fbdcfd0acee0a", "--file", "./testdata/update-payload.json", |
| 164 | + }, |
| 165 | + Cfg: testutils.MockCfg{ |
| 166 | + OutputFormat: "json", |
| 167 | + Responses: []mock.Response{ |
| 168 | + mock.New200ResponseAssertion( |
| 169 | + &mock.RequestAssertion{ |
| 170 | + Header: api.DefaultWriteMockHeaders, |
| 171 | + Method: "PUT", |
| 172 | + Path: "/api/v1/deployments/traffic-filter/rulesets/4e974d9476534d35b12fbdcfd0acee0a", |
| 173 | + Body: mock.NewStringBody( |
| 174 | + `{"include_by_default":false,"name":"1235","region":"azure-eastus2","rules":[{"id":"fad8132b0470479f95c3a41c5d67bd57","source":"0.0.0.0/0"}],"type":"ip"}` + |
| 175 | + "\n"), |
| 176 | + Host: api.DefaultMockHost, |
| 177 | + }, |
| 178 | + mock.NewByteBody(updateRawResp), |
| 179 | + ), |
| 180 | + }, |
| 181 | + }, |
| 182 | + }, |
| 183 | + want: testutils.Assertion{ |
| 184 | + Stdout: string(updateJSONOutput) + "\n", |
| 185 | + }, |
| 186 | + }, |
| 187 | + } |
| 188 | + for _, tt := range tests { |
| 189 | + t.Run(tt.name, func(t *testing.T) { |
| 190 | + testutils.RunCmdAssertion(t, tt.args, tt.want) |
| 191 | + tt.args.Cmd.ResetFlags() |
| 192 | + defer initUpdateFlags() |
| 193 | + }) |
| 194 | + } |
| 195 | +} |
0 commit comments