diff --git a/modules/localstack/localstack.go b/modules/localstack/localstack.go index 23660a98b6..10ff7a8573 100644 --- a/modules/localstack/localstack.go +++ b/modules/localstack/localstack.go @@ -21,14 +21,14 @@ const ( localstackHostEnvVar = "LOCALSTACK_HOST" ) -var nonLegacyTags = []string{ +var recentVersionTags = []string{ "latest", "s3", "s3-latest", "stable", } -func isLegacyMode(image string) bool { +func isMinimumVersion(image string, minVersion string) bool { parts := strings.Split(image, ":") version := parts[len(parts)-1] @@ -36,30 +36,7 @@ func isLegacyMode(image string) bool { version = version[0:pos] } - if slices.Contains(nonLegacyTags, version) { - return false - } - - if !strings.HasPrefix(version, "v") { - version = "v" + version - } - - if semver.IsValid(version) { - return semver.Compare(version, "v0.11") < 0 // version < v0.11 - } - - return true -} - -func isVersion2(image string) bool { - parts := strings.Split(image, ":") - version := parts[len(parts)-1] - - if pos := strings.LastIndexByte(version, '-'); pos >= 0 { - version = version[0:pos] - } - - if slices.Contains(nonLegacyTags, version) { + if slices.Contains(recentVersionTags, version) { return true } @@ -67,11 +44,7 @@ func isVersion2(image string) bool { version = "v" + version } - if semver.IsValid(version) { - return semver.Compare(version, "v2.0") >= 0 // version >= v2.0 - } - - return false + return semver.IsValid(version) && semver.Compare(version, minVersion) >= 0 } // WithNetwork creates a network with the given name and attaches the container to it, setting the network alias @@ -116,12 +89,12 @@ func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustom } } - if isLegacyMode(localStackReq.Image) { + if !isMinimumVersion(localStackReq.Image, "v0.11") { return nil, fmt.Errorf("version=%s. Testcontainers for Go does not support running LocalStack in legacy mode. Please use a version >= 0.11.0", localStackReq.Image) } envVar := hostnameExternalEnvVar - if isVersion2(localStackReq.Image) { + if isMinimumVersion(localStackReq.Image, "v2") { envVar = localstackHostEnvVar } diff --git a/modules/localstack/localstack_test.go b/modules/localstack/localstack_test.go index ac862b9677..0a336d85b4 100644 --- a/modules/localstack/localstack_test.go +++ b/modules/localstack/localstack_test.go @@ -82,7 +82,7 @@ func TestConfigureDockerHost(t *testing.T) { } } -func TestIsLegacyMode(t *testing.T) { +func TestIsLegacyVersion(t *testing.T) { tests := []struct { version string want bool @@ -112,13 +112,13 @@ func TestIsLegacyMode(t *testing.T) { for _, tt := range tests { t.Run(tt.version, func(t *testing.T) { - got := isLegacyMode("localstack/localstack:" + tt.version) + got := !isMinimumVersion("localstack/localstack:"+tt.version, "v0.11") require.Equal(t, tt.want, got, "runInLegacyMode() = %v, want %v", got, tt.want) }) } } -func TestIsVersion2(t *testing.T) { +func TestIsMinimumVersion2(t *testing.T) { tests := []struct { version string want bool @@ -152,7 +152,7 @@ func TestIsVersion2(t *testing.T) { for _, tt := range tests { t.Run(tt.version, func(t *testing.T) { - got := isVersion2("localstack/localstack:" + tt.version) + got := isMinimumVersion("localstack/localstack:"+tt.version, "v2") require.Equal(t, tt.want, got, "runInLegacyMode() = %v, want %v", got, tt.want) }) }