|
| 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 cmddeploymentextension |
| 19 | + |
| 20 | +import ( |
| 21 | + "encoding/json" |
| 22 | + "io/ioutil" |
| 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 | + |
| 29 | + "github.com/elastic/ecctl/cmd/util/testutils" |
| 30 | +) |
| 31 | + |
| 32 | +func Test_createCmd(t *testing.T) { |
| 33 | + createRawResp, err := ioutil.ReadFile("./testdata/create.json") |
| 34 | + if err != nil { |
| 35 | + t.Fatal(err) |
| 36 | + } |
| 37 | + |
| 38 | + var succeedResp = new(models.Extension) |
| 39 | + if err := succeedResp.UnmarshalBinary(createRawResp); err != nil { |
| 40 | + t.Fatal(err) |
| 41 | + } |
| 42 | + |
| 43 | + createJSONOutput, err := json.MarshalIndent(succeedResp, "", " ") |
| 44 | + if err != nil { |
| 45 | + t.Fatal(err) |
| 46 | + } |
| 47 | + |
| 48 | + tests := []struct { |
| 49 | + name string |
| 50 | + args testutils.Args |
| 51 | + want testutils.Assertion |
| 52 | + }{ |
| 53 | + { |
| 54 | + name: "fails due to empty argument", |
| 55 | + args: testutils.Args{ |
| 56 | + Cmd: createCmd, |
| 57 | + Args: []string{ |
| 58 | + "create", |
| 59 | + }, |
| 60 | + Cfg: testutils.MockCfg{Responses: []mock.Response{ |
| 61 | + mock.SampleInternalError(), |
| 62 | + }}, |
| 63 | + }, |
| 64 | + want: testutils.Assertion{ |
| 65 | + Err: `accepts 1 arg(s), received 0`, |
| 66 | + }, |
| 67 | + }, |
| 68 | + { |
| 69 | + name: "fails due to missing flags", |
| 70 | + args: testutils.Args{ |
| 71 | + Cmd: createCmd, |
| 72 | + Args: []string{ |
| 73 | + "create", "mybundle", |
| 74 | + }, |
| 75 | + Cfg: testutils.MockCfg{Responses: []mock.Response{ |
| 76 | + mock.SampleInternalError(), |
| 77 | + }}, |
| 78 | + }, |
| 79 | + want: testutils.Assertion{ |
| 80 | + Err: `required flag(s) "type", "version" not set`, |
| 81 | + }, |
| 82 | + }, |
| 83 | + { |
| 84 | + name: "fails due to mismatching flags", |
| 85 | + args: testutils.Args{ |
| 86 | + Cmd: createCmd, |
| 87 | + Args: []string{ |
| 88 | + "create", "mybundle", "--version", "*", "--type", "bundle", |
| 89 | + "--download-url", "example.com", "--file", "hi.zip", |
| 90 | + }, |
| 91 | + Cfg: testutils.MockCfg{Responses: []mock.Response{ |
| 92 | + mock.SampleInternalError(), |
| 93 | + }}, |
| 94 | + }, |
| 95 | + want: testutils.Assertion{ |
| 96 | + Err: "both --file and --download-url are set. Only one may be used", |
| 97 | + }, |
| 98 | + }, |
| 99 | + { |
| 100 | + name: "fails due to API error", |
| 101 | + args: testutils.Args{ |
| 102 | + Cmd: createCmd, |
| 103 | + Args: []string{ |
| 104 | + "create", "mybundle", "--version", "*", "--type", "bundle", |
| 105 | + }, |
| 106 | + Cfg: testutils.MockCfg{Responses: []mock.Response{ |
| 107 | + mock.SampleInternalError(), |
| 108 | + }}, |
| 109 | + }, |
| 110 | + want: testutils.Assertion{ |
| 111 | + Err: mock.MultierrorInternalError.Error(), |
| 112 | + }, |
| 113 | + }, |
| 114 | + { |
| 115 | + name: "succeeds", |
| 116 | + args: testutils.Args{ |
| 117 | + Cmd: createCmd, |
| 118 | + Args: []string{ |
| 119 | + "create", "mybundle", "--version", "*", "--type", "bundle", "--description", "Why hello there", |
| 120 | + }, |
| 121 | + Cfg: testutils.MockCfg{ |
| 122 | + OutputFormat: "json", |
| 123 | + Responses: []mock.Response{ |
| 124 | + mock.New200ResponseAssertion( |
| 125 | + &mock.RequestAssertion{ |
| 126 | + Header: api.DefaultWriteMockHeaders, |
| 127 | + Method: "POST", |
| 128 | + Path: "/api/v1/deployments/extensions", |
| 129 | + Host: api.DefaultMockHost, |
| 130 | + Body: mock.NewStringBody(`{"description":"Why hello there","extension_type":"bundle","name":"mybundle","version":"*"}` + "\n"), |
| 131 | + }, |
| 132 | + mock.NewByteBody(createRawResp), |
| 133 | + ), |
| 134 | + }, |
| 135 | + }, |
| 136 | + }, |
| 137 | + want: testutils.Assertion{ |
| 138 | + Err: string(createJSONOutput) + "\n", |
| 139 | + }, |
| 140 | + }, |
| 141 | + { |
| 142 | + name: "succeeds with upload", |
| 143 | + args: testutils.Args{ |
| 144 | + Cmd: createCmd, |
| 145 | + Args: []string{ |
| 146 | + "create", "mybundle", "--version", "*", "--type", "bundle", "--file", |
| 147 | + "./testdata/extension.zip", "--description", "Why hello there", |
| 148 | + }, |
| 149 | + Cfg: testutils.MockCfg{ |
| 150 | + OutputFormat: "json", |
| 151 | + Responses: []mock.Response{ |
| 152 | + mock.New200Response( |
| 153 | + mock.NewByteBody(createRawResp), |
| 154 | + ), |
| 155 | + mock.New200Response( |
| 156 | + mock.NewByteBody(createRawResp), |
| 157 | + ), |
| 158 | + }, |
| 159 | + }, |
| 160 | + }, |
| 161 | + want: testutils.Assertion{ |
| 162 | + Err: string(createJSONOutput) + "\n", |
| 163 | + }, |
| 164 | + }, |
| 165 | + } |
| 166 | + for _, tt := range tests { |
| 167 | + t.Run(tt.name, func(t *testing.T) { |
| 168 | + testutils.RunCmdAssertion(t, tt.args, tt.want) |
| 169 | + tt.args.Cmd.ResetFlags() |
| 170 | + defer initCreateFlags() |
| 171 | + }) |
| 172 | + } |
| 173 | +} |
0 commit comments