|
| 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 cmdcomment |
| 19 | + |
| 20 | +import ( |
| 21 | + "testing" |
| 22 | + |
| 23 | + "github.com/elastic/cloud-sdk-go/pkg/api" |
| 24 | + "github.com/elastic/cloud-sdk-go/pkg/api/mock" |
| 25 | + |
| 26 | + "github.com/elastic/ecctl/cmd/util/testutils" |
| 27 | +) |
| 28 | + |
| 29 | +func Test_updateCmd(t *testing.T) { |
| 30 | + tests := []struct { |
| 31 | + name string |
| 32 | + args testutils.Args |
| 33 | + want testutils.Assertion |
| 34 | + }{ |
| 35 | + { |
| 36 | + name: "fails due to missing arguments", |
| 37 | + args: testutils.Args{ |
| 38 | + Cmd: updateCmd, |
| 39 | + Args: []string{"update"}, |
| 40 | + Cfg: testutils.MockCfg{Responses: []mock.Response{ |
| 41 | + mock.SampleInternalError(), |
| 42 | + }}, |
| 43 | + }, |
| 44 | + want: testutils.Assertion{ |
| 45 | + Err: `accepts 2 arg(s), received 0`, |
| 46 | + }, |
| 47 | + }, |
| 48 | + { |
| 49 | + name: "fails due to missing resource-id", |
| 50 | + args: testutils.Args{ |
| 51 | + Cmd: updateCmd, |
| 52 | + Args: []string{"update", "comment-id", "some-message", "--resource-type", "allocator"}, |
| 53 | + Cfg: testutils.MockCfg{Responses: []mock.Response{ |
| 54 | + mock.SampleInternalError(), |
| 55 | + }}, |
| 56 | + }, |
| 57 | + want: testutils.Assertion{ |
| 58 | + Err: `required flag(s) "resource-id" not set`, |
| 59 | + }, |
| 60 | + }, |
| 61 | + { |
| 62 | + name: "fails due to missing resource-type", |
| 63 | + args: testutils.Args{ |
| 64 | + Cmd: updateCmd, |
| 65 | + Args: []string{"update", "comment-id", "some-message", "--resource-id", "i-i123"}, |
| 66 | + Cfg: testutils.MockCfg{Responses: []mock.Response{ |
| 67 | + mock.SampleInternalError(), |
| 68 | + }}, |
| 69 | + }, |
| 70 | + want: testutils.Assertion{ |
| 71 | + Err: `required flag(s) "resource-type" not set`, |
| 72 | + }, |
| 73 | + }, |
| 74 | + { |
| 75 | + name: "fails due to API error", |
| 76 | + args: testutils.Args{ |
| 77 | + Cmd: updateCmd, |
| 78 | + Args: []string{ |
| 79 | + "update", "comment-id", "some-message", "--resource-type", "allocator", "--resource-id", "i-123", |
| 80 | + }, |
| 81 | + Cfg: testutils.MockCfg{Responses: []mock.Response{ |
| 82 | + mock.SampleInternalError(), |
| 83 | + }}, |
| 84 | + }, |
| 85 | + want: testutils.Assertion{ |
| 86 | + Err: mock.MultierrorInternalError.Error(), |
| 87 | + }, |
| 88 | + }, |
| 89 | + { |
| 90 | + name: "succeeds", |
| 91 | + args: testutils.Args{ |
| 92 | + Cmd: updateCmd, |
| 93 | + Args: []string{ |
| 94 | + "update", "comment-id", "some-message", "--resource-type", "allocator", "--resource-id", "i-123", |
| 95 | + }, |
| 96 | + Cfg: testutils.MockCfg{ |
| 97 | + Responses: []mock.Response{ |
| 98 | + mock.New200ResponseAssertion( |
| 99 | + &mock.RequestAssertion{ |
| 100 | + Header: api.DefaultWriteMockHeaders, |
| 101 | + Method: "PUT", |
| 102 | + Path: "/api/v1/regions/ece-region/comments/allocator/i-123/comment-id", |
| 103 | + Host: api.DefaultMockHost, |
| 104 | + Body: mock.NewStringBody(`{"message":"some-message"}` + "\n"), |
| 105 | + }, |
| 106 | + mock.NewStringBody(`{"id":"random-generated-id"}`), |
| 107 | + ), |
| 108 | + }, |
| 109 | + }, |
| 110 | + }, |
| 111 | + want: testutils.Assertion{ |
| 112 | + Stdout: `"random-generated-id"` + "\n", |
| 113 | + }, |
| 114 | + }, |
| 115 | + { |
| 116 | + name: "succeeds with version", |
| 117 | + args: testutils.Args{ |
| 118 | + Cmd: updateCmd, |
| 119 | + Args: []string{ |
| 120 | + "update", "comment-id", "some-message", "--resource-type", "allocator", "--resource-id", "i-123", "--version", "v1", |
| 121 | + }, |
| 122 | + Cfg: testutils.MockCfg{ |
| 123 | + Responses: []mock.Response{ |
| 124 | + mock.New200ResponseAssertion( |
| 125 | + &mock.RequestAssertion{ |
| 126 | + Header: api.DefaultWriteMockHeaders, |
| 127 | + Method: "PUT", |
| 128 | + Path: "/api/v1/regions/ece-region/comments/allocator/i-123/comment-id", |
| 129 | + Host: api.DefaultMockHost, |
| 130 | + Body: mock.NewStringBody(`{"message":"some-message"}` + "\n"), |
| 131 | + Query: map[string][]string{"version": {"v1"}}, |
| 132 | + }, |
| 133 | + mock.NewStringBody(`{"id":"random-generated-id"}`), |
| 134 | + ), |
| 135 | + }, |
| 136 | + }, |
| 137 | + }, |
| 138 | + want: testutils.Assertion{ |
| 139 | + Stdout: `"random-generated-id"` + "\n", |
| 140 | + }, |
| 141 | + }, |
| 142 | + } |
| 143 | + for _, tt := range tests { |
| 144 | + t.Run(tt.name, func(t *testing.T) { |
| 145 | + testutils.RunCmdAssertion(t, tt.args, tt.want) |
| 146 | + tt.args.Cmd.ResetFlags() |
| 147 | + defer initUpdateFlags() |
| 148 | + }) |
| 149 | + } |
| 150 | +} |
0 commit comments