|
| 1 | +// Copyright Mia srl |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | + |
| 16 | +package project |
| 17 | + |
| 18 | +import ( |
| 19 | + "bytes" |
| 20 | + "context" |
| 21 | + "encoding/json" |
| 22 | + "net/http" |
| 23 | + "net/http/httptest" |
| 24 | + "testing" |
| 25 | + "time" |
| 26 | + |
| 27 | + "github.com/mia-platform/miactl/internal/client" |
| 28 | + "github.com/mia-platform/miactl/internal/clioptions" |
| 29 | + "github.com/mia-platform/miactl/internal/encoding" |
| 30 | + "github.com/stretchr/testify/assert" |
| 31 | + "github.com/stretchr/testify/require" |
| 32 | + "sigs.k8s.io/kustomize/kyaml/yaml" |
| 33 | +) |
| 34 | + |
| 35 | +func TestCreateDescribeCmd(t *testing.T) { |
| 36 | + t.Run("test command creation", func(t *testing.T) { |
| 37 | + opts := clioptions.NewCLIOptions() |
| 38 | + cmd := DescribeCmd(opts) |
| 39 | + require.NotNil(t, cmd) |
| 40 | + }) |
| 41 | +} |
| 42 | + |
| 43 | +func TestDescribeProjectCmd(t *testing.T) { |
| 44 | + testCases := map[string]struct { |
| 45 | + options describeProjectOptions |
| 46 | + revisionName string |
| 47 | + versionName string |
| 48 | + expectError bool |
| 49 | + expectedErrorMsg string |
| 50 | + testServer *httptest.Server |
| 51 | + outputTextJSON string |
| 52 | + }{ |
| 53 | + "error missing project id": { |
| 54 | + options: describeProjectOptions{}, |
| 55 | + expectError: true, |
| 56 | + expectedErrorMsg: "missing project name, please provide a project name as argument", |
| 57 | + testServer: describeTestServer(t, func(_ http.ResponseWriter, _ *http.Request) bool { |
| 58 | + return false |
| 59 | + }), |
| 60 | + }, |
| 61 | + "error missing revision/version": { |
| 62 | + options: describeProjectOptions{ |
| 63 | + ProjectID: "test-project", |
| 64 | + }, |
| 65 | + expectError: true, |
| 66 | + expectedErrorMsg: "missing revision/version name, please provide one as argument", |
| 67 | + testServer: describeTestServer(t, func(_ http.ResponseWriter, _ *http.Request) bool { |
| 68 | + return false |
| 69 | + }), |
| 70 | + }, |
| 71 | + "error both revision/version specified": { |
| 72 | + options: describeProjectOptions{ |
| 73 | + ProjectID: "test-project", |
| 74 | + RevisionName: "test-revision", |
| 75 | + VersionName: "test-version", |
| 76 | + }, |
| 77 | + expectError: true, |
| 78 | + expectedErrorMsg: "both revision and version specified, please provide only one", |
| 79 | + testServer: describeTestServer(t, func(_ http.ResponseWriter, _ *http.Request) bool { |
| 80 | + return false |
| 81 | + }), |
| 82 | + }, |
| 83 | + "valid project with revision": { |
| 84 | + options: describeProjectOptions{ |
| 85 | + ProjectID: "test-project", |
| 86 | + RevisionName: "test-json-revision", |
| 87 | + OutputFormat: "json", |
| 88 | + }, |
| 89 | + revisionName: "test-revision", |
| 90 | + testServer: describeTestServer(t, func(w http.ResponseWriter, r *http.Request) bool { |
| 91 | + if r.URL.Path == "/api/backend/projects/test-project/revisions/test-json-revision/configuration" && r.Method == http.MethodGet { |
| 92 | + w.WriteHeader(http.StatusOK) |
| 93 | + _, _ = w.Write([]byte(`{"name": "test-project", "revision": "test-json-revision"}`)) |
| 94 | + return true |
| 95 | + } |
| 96 | + return false |
| 97 | + }), |
| 98 | + outputTextJSON: `{"name": "test-project", "revision": "test-json-revision"}`, |
| 99 | + }, |
| 100 | + "valid project with version": { |
| 101 | + options: describeProjectOptions{ |
| 102 | + ProjectID: "test-project", |
| 103 | + VersionName: "test-version", |
| 104 | + OutputFormat: "json", |
| 105 | + }, |
| 106 | + testServer: describeTestServer(t, func(w http.ResponseWriter, r *http.Request) bool { |
| 107 | + if r.URL.Path == "/api/backend/projects/test-project/versions/test-version/configuration" && r.Method == http.MethodGet { |
| 108 | + w.WriteHeader(http.StatusOK) |
| 109 | + _, _ = w.Write([]byte(`{"name": "test-project", "revision": "test-version"}`)) |
| 110 | + return true |
| 111 | + } |
| 112 | + return false |
| 113 | + }), |
| 114 | + outputTextJSON: `{"name": "test-project", "revision": "test-version"}`, |
| 115 | + }, |
| 116 | + "valid project with yaml output format": { |
| 117 | + options: describeProjectOptions{ |
| 118 | + ProjectID: "test-project", |
| 119 | + RevisionName: "test-yaml-revision", |
| 120 | + OutputFormat: "yaml", |
| 121 | + }, |
| 122 | + testServer: describeTestServer(t, func(w http.ResponseWriter, r *http.Request) bool { |
| 123 | + if r.URL.Path == "/api/backend/projects/test-project/revisions/test-yaml-revision/configuration" && r.Method == http.MethodGet { |
| 124 | + w.WriteHeader(http.StatusOK) |
| 125 | + _, _ = w.Write([]byte(`{"name": "test-project", "revision": "test-yaml-revision"}`)) |
| 126 | + return true |
| 127 | + } |
| 128 | + return false |
| 129 | + }), |
| 130 | + outputTextJSON: `{"name": "test-project", "revision": "test-yaml-revision"}`, |
| 131 | + }, |
| 132 | + "revision with slash": { |
| 133 | + options: describeProjectOptions{ |
| 134 | + ProjectID: "test-project", |
| 135 | + RevisionName: "some/revision", |
| 136 | + OutputFormat: "yaml", |
| 137 | + }, |
| 138 | + testServer: describeTestServer(t, func(w http.ResponseWriter, r *http.Request) bool { |
| 139 | + if r.URL.Path == "/api/backend/projects/test-project/revisions/some%2Frevision/configuration" && r.Method == http.MethodGet { |
| 140 | + w.WriteHeader(http.StatusOK) |
| 141 | + _, _ = w.Write([]byte(`{"name": "test-project", "revision": "test-yaml-revision"}`)) |
| 142 | + return true |
| 143 | + } |
| 144 | + return false |
| 145 | + }), |
| 146 | + outputTextJSON: `{"name": "test-project", "revision": "test-yaml-revision"}`, |
| 147 | + }, |
| 148 | + "version with slash": { |
| 149 | + options: describeProjectOptions{ |
| 150 | + ProjectID: "test-project", |
| 151 | + VersionName: "version/1.2.3", |
| 152 | + OutputFormat: "yaml", |
| 153 | + }, |
| 154 | + testServer: describeTestServer(t, func(w http.ResponseWriter, r *http.Request) bool { |
| 155 | + if r.URL.Path == "/api/backend/projects/test-project/versions/version%2F1.2.3/configuration" && r.Method == http.MethodGet { |
| 156 | + w.WriteHeader(http.StatusOK) |
| 157 | + _, _ = w.Write([]byte(`{"name": "test-project", "revision": "test-yaml-revision"}`)) |
| 158 | + return true |
| 159 | + } |
| 160 | + return false |
| 161 | + }), |
| 162 | + outputTextJSON: `{"name": "test-project", "revision": "test-yaml-revision"}`, |
| 163 | + }, |
| 164 | + } |
| 165 | + |
| 166 | + for name, testCase := range testCases { |
| 167 | + t.Run(name, func(t *testing.T) { |
| 168 | + server := testCase.testServer |
| 169 | + defer server.Close() |
| 170 | + |
| 171 | + ctx, cancel := context.WithTimeout(t.Context(), 5*time.Second) |
| 172 | + defer cancel() |
| 173 | + |
| 174 | + client, err := client.APIClientForConfig(&client.Config{ |
| 175 | + Host: server.URL, |
| 176 | + }) |
| 177 | + require.NoError(t, err) |
| 178 | + |
| 179 | + outputBuffer := bytes.NewBuffer([]byte{}) |
| 180 | + |
| 181 | + err = describeProject(ctx, client, testCase.options, outputBuffer) |
| 182 | + |
| 183 | + if testCase.expectError { |
| 184 | + require.Error(t, err) |
| 185 | + require.EqualError(t, err, testCase.expectedErrorMsg) |
| 186 | + } else { |
| 187 | + require.NoError(t, err) |
| 188 | + |
| 189 | + if testCase.options.OutputFormat == encoding.JSON { |
| 190 | + require.JSONEq(t, testCase.outputTextJSON, outputBuffer.String(), "output should match expected JSON") |
| 191 | + } else { |
| 192 | + foundMap := map[string]interface{}{} |
| 193 | + err := yaml.Unmarshal(outputBuffer.Bytes(), &foundMap) |
| 194 | + require.NoError(t, err) |
| 195 | + |
| 196 | + expectedMap := map[string]interface{}{} |
| 197 | + err = json.Unmarshal([]byte(testCase.outputTextJSON), &expectedMap) |
| 198 | + require.NoError(t, err) |
| 199 | + |
| 200 | + require.Equal(t, expectedMap, foundMap) |
| 201 | + } |
| 202 | + } |
| 203 | + }) |
| 204 | + } |
| 205 | +} |
| 206 | + |
| 207 | +func describeTestServer(t *testing.T, handler func(w http.ResponseWriter, r *http.Request) bool) *httptest.Server { |
| 208 | + t.Helper() |
| 209 | + return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 210 | + if handler(w, r) { |
| 211 | + return |
| 212 | + } |
| 213 | + |
| 214 | + t.Logf("unexpected request: %#v\n%#v", r.URL, r) |
| 215 | + w.WriteHeader(http.StatusNotFound) |
| 216 | + assert.Fail(t, "unexpected request") |
| 217 | + })) |
| 218 | +} |
0 commit comments