Skip to content

Commit

Permalink
fix(localstack): refactor isLegacyMode and isVersion2
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanBaulch committed Jan 14, 2025
1 parent 09e009d commit 2f87127
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 37 deletions.
39 changes: 6 additions & 33 deletions modules/localstack/localstack.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,57 +21,30 @@ 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]

if pos := strings.LastIndexByte(version, '-'); pos >= 0 {
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
}

if !strings.HasPrefix(version, "v") {
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
Expand Down Expand Up @@ -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
}

Expand Down
8 changes: 4 additions & 4 deletions modules/localstack/localstack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
})
}
Expand Down

0 comments on commit 2f87127

Please sign in to comment.