-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathversion_resolution_test.go
More file actions
118 lines (101 loc) · 4.24 KB
/
version_resolution_test.go
File metadata and controls
118 lines (101 loc) · 4.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package integration_test
import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/localstack/lstk/test/integration/env"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// returns a mock server for catalog and license endpoints.
// Empty catalogVersion → 503. The returned *string captures the product version from license requests.
func createVersionResolutionMockServer(t *testing.T, catalogVersion string, licenseSuccess bool) (*httptest.Server, *string) {
t.Helper()
capturedVersion := new(string)
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch {
case r.Method == http.MethodGet && strings.HasPrefix(r.URL.Path, "/v1/license/catalog/"):
if catalogVersion == "" {
w.WriteHeader(http.StatusServiceUnavailable)
return
}
parts := strings.Split(strings.Trim(r.URL.Path, "/"), "/")
emulatorType := parts[len(parts)-2]
w.Header().Set("Content-Type", "application/json")
_, _ = fmt.Fprintf(w, `{"emulator_type":%q,"version":%q}`, emulatorType, catalogVersion)
case r.Method == http.MethodPost && r.URL.Path == "/v1/license/request":
body, _ := io.ReadAll(r.Body)
var req struct {
Product struct {
Version string `json:"version"`
} `json:"product"`
}
_ = json.Unmarshal(body, &req)
*capturedVersion = req.Product.Version
if licenseSuccess {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte(`{"license_type":"pro"}`))
} else {
w.WriteHeader(http.StatusForbidden)
}
default:
w.WriteHeader(http.StatusNotFound)
}
}))
t.Cleanup(srv.Close)
return srv, capturedVersion
}
// Verifies that when the catalog API returns a version, the license request uses
// that version (not "latest"), allowing license validation to happen before pulling the image.
func TestVersionResolvedViaCatalog(t *testing.T) {
requireDocker(t)
_ = env.Require(t, env.AuthToken)
cleanup()
t.Cleanup(cleanup)
mockServer, capturedVersion := createVersionResolutionMockServer(t, "4.14.0", true)
ctx := testContext(t)
stdout, stderr, err := runLstk(t, ctx, "", env.With(env.APIEndpoint, mockServer.URL), "start")
require.NoError(t, err, "lstk start failed:\nstdout: %s\nstderr: %s", stdout, stderr)
assert.Equal(t, "4.14.0", *capturedVersion,
"license request should carry the version returned by the catalog API")
assert.NotEqual(t, "latest", *capturedVersion,
"license request should not use the unresolved 'latest' tag")
assert.Contains(t, stdout, "Checking license")
assert.NotContains(t, stdout, "(4.14.0)")
}
// Verifies that when the catalog endpoint is unavailable, the version is resolved
// by inspecting the pulled image and used for licensing.
func TestVersionFallsBackToImageInspectionWhenCatalogFails(t *testing.T) {
requireDocker(t)
_ = env.Require(t, env.AuthToken)
cleanup()
t.Cleanup(cleanup)
// Catalog returns 503; license accepts all requests
mockServer, capturedVersion := createVersionResolutionMockServer(t, "", true)
ctx := testContext(t)
stdout, stderr, err := runLstk(t, ctx, "", env.With(env.APIEndpoint, mockServer.URL), "start")
require.NoError(t, err, "lstk start should succeed via image inspection fallback:\nstdout: %s\nstderr: %s", stdout, stderr)
assert.NotEmpty(t, *capturedVersion, "license request should carry a version resolved from image inspection")
assert.NotEqual(t, "latest", *capturedVersion, "resolved version should not be the unresolved 'latest' tag")
}
// Verifies that when both the catalog endpoint is unavailable and the license
// validation fails in the image inspection fallback path, the command exits with a clear error.
func TestCommandFailsNicelyWhenCatalogAndLicenseBothFail(t *testing.T) {
requireDocker(t)
_ = env.Require(t, env.AuthToken)
cleanup()
t.Cleanup(cleanup)
// Catalog unavailable; license rejects all requests
mockServer, _ := createVersionResolutionMockServer(t, "", false)
ctx := testContext(t)
stdout, stderr, err := runLstk(t, ctx, "",
env.With(env.APIEndpoint, mockServer.URL), "start")
require.Error(t, err, "expected lstk start to fail when catalog is down and license check fails")
assert.Contains(t, stderr, "license validation failed",
"stdout: %s", stdout)
}