|
| 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 util |
| 17 | + |
| 18 | +import ( |
| 19 | + "context" |
| 20 | + "net/http" |
| 21 | + "net/http/httptest" |
| 22 | + "strconv" |
| 23 | + "testing" |
| 24 | + "time" |
| 25 | + |
| 26 | + "github.com/mia-platform/miactl/internal/client" |
| 27 | + "github.com/stretchr/testify/assert" |
| 28 | + "github.com/stretchr/testify/require" |
| 29 | +) |
| 30 | + |
| 31 | +func TestCompareVersion(t *testing.T) { |
| 32 | + t.Parallel() |
| 33 | + |
| 34 | + defaultMajorInt, err := strconv.Atoi(defaultMajor) |
| 35 | + require.NoError(t, err) |
| 36 | + defaultMinorInt, err := strconv.Atoi(defaultMinor) |
| 37 | + require.NoError(t, err) |
| 38 | + |
| 39 | + testCases := map[string]struct { |
| 40 | + major int |
| 41 | + minor int |
| 42 | + check bool |
| 43 | + testServer *httptest.Server |
| 44 | + expectedError string |
| 45 | + }{ |
| 46 | + "missing api use default version - check true minor": { |
| 47 | + major: defaultMajorInt, |
| 48 | + minor: defaultMinorInt, |
| 49 | + check: true, |
| 50 | + testServer: versionTestServer(t, func(w http.ResponseWriter) { |
| 51 | + w.WriteHeader(http.StatusNotFound) |
| 52 | + }), |
| 53 | + }, |
| 54 | + "missing api use default version - check true major": { |
| 55 | + major: defaultMajorInt - 1, |
| 56 | + minor: defaultMinorInt + 1, |
| 57 | + check: true, |
| 58 | + testServer: versionTestServer(t, func(w http.ResponseWriter) { |
| 59 | + w.WriteHeader(http.StatusNotFound) |
| 60 | + }), |
| 61 | + }, |
| 62 | + "missing api use default version - check false major": { |
| 63 | + major: defaultMajorInt + 1, |
| 64 | + minor: defaultMinorInt - 1, |
| 65 | + check: false, |
| 66 | + testServer: versionTestServer(t, func(w http.ResponseWriter) { |
| 67 | + w.WriteHeader(http.StatusNotFound) |
| 68 | + }), |
| 69 | + }, |
| 70 | + "missing api use default version - check false minor": { |
| 71 | + major: defaultMajorInt, |
| 72 | + minor: defaultMinorInt + 1, |
| 73 | + check: false, |
| 74 | + testServer: versionTestServer(t, func(w http.ResponseWriter) { |
| 75 | + w.WriteHeader(http.StatusNotFound) |
| 76 | + }), |
| 77 | + }, |
| 78 | + "other error return error": { |
| 79 | + testServer: versionTestServer(t, func(w http.ResponseWriter) { |
| 80 | + w.WriteHeader(http.StatusInternalServerError) |
| 81 | + w.Write([]byte(`{"statusCode": 500, "message": "error from server"}`)) |
| 82 | + }), |
| 83 | + expectedError: "error from server", |
| 84 | + }, |
| 85 | + "successful get return a valid version - check true minor": { |
| 86 | + testServer: versionTestServer(t, func(w http.ResponseWriter) { |
| 87 | + w.Write([]byte(`{"major": "5", "minor":"11"}`)) |
| 88 | + }), |
| 89 | + major: 5, |
| 90 | + minor: 10, |
| 91 | + check: true, |
| 92 | + }, |
| 93 | + "successful get return a valid version - check true major": { |
| 94 | + testServer: versionTestServer(t, func(w http.ResponseWriter) { |
| 95 | + w.Write([]byte(`{"major": "5", "minor":"11"}`)) |
| 96 | + }), |
| 97 | + major: 4, |
| 98 | + minor: 12, |
| 99 | + check: true, |
| 100 | + }, |
| 101 | + "successful get return a valid version - check false major": { |
| 102 | + testServer: versionTestServer(t, func(w http.ResponseWriter) { |
| 103 | + w.Write([]byte(`{"major": "5", "minor":"10"}`)) |
| 104 | + }), |
| 105 | + major: 6, |
| 106 | + minor: 10, |
| 107 | + check: false, |
| 108 | + }, |
| 109 | + "successful get return a valid version - check false minor": { |
| 110 | + testServer: versionTestServer(t, func(w http.ResponseWriter) { |
| 111 | + w.Write([]byte(`{"major": "5", "minor":"10"}`)) |
| 112 | + }), |
| 113 | + major: 5, |
| 114 | + minor: 11, |
| 115 | + check: false, |
| 116 | + }, |
| 117 | + } |
| 118 | + |
| 119 | + for name, test := range testCases { |
| 120 | + t.Run(name, func(t *testing.T) { |
| 121 | + server := test.testServer |
| 122 | + defer server.Close() |
| 123 | + |
| 124 | + client, err := client.APIClientForConfig(&client.Config{ |
| 125 | + Host: server.URL, |
| 126 | + }) |
| 127 | + require.NoError(t, err) |
| 128 | + ctx, cancel := context.WithTimeout(t.Context(), 1*time.Second) |
| 129 | + defer cancel() |
| 130 | + |
| 131 | + check, err := VersionCheck(ctx, client, test.major, test.minor) |
| 132 | + if len(test.expectedError) > 0 { |
| 133 | + assert.Error(t, err) |
| 134 | + assert.False(t, check) |
| 135 | + return |
| 136 | + } |
| 137 | + |
| 138 | + assert.NoError(t, err) |
| 139 | + assert.Equal(t, test.check, check) |
| 140 | + }) |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +func versionTestServer(t *testing.T, response func(w http.ResponseWriter)) *httptest.Server { |
| 145 | + t.Helper() |
| 146 | + |
| 147 | + return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 148 | + switch { |
| 149 | + default: |
| 150 | + w.WriteHeader(http.StatusNotFound) |
| 151 | + assert.Fail(t, "request not expexted") |
| 152 | + case r.URL.Path == "/api/version" && r.Method == http.MethodGet: |
| 153 | + response(w) |
| 154 | + } |
| 155 | + })) |
| 156 | +} |
0 commit comments