Skip to content

Commit

Permalink
fix: switch to standard comparison
Browse files Browse the repository at this point in the history
  • Loading branch information
gene-redpanda committed Jan 30, 2024
1 parent 8ef74ae commit 9e5e83d
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 17 deletions.
7 changes: 3 additions & 4 deletions modules/redpanda/go.mod
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
module github.com/testcontainers/testcontainers-go/modules/redpanda

go 1.20
go 1.21

require (
github.com/Masterminds/semver/v3 v3.2.1
github.com/docker/go-connections v0.5.0
github.com/stretchr/testify v1.8.4
github.com/testcontainers/testcontainers-go v0.27.0
github.com/twmb/franz-go v1.15.4
github.com/twmb/franz-go/pkg/kadm v1.10.0
golang.org/x/mod v0.14.0
)

require (
Expand Down Expand Up @@ -59,9 +59,8 @@ require (
go.opentelemetry.io/otel/trace v1.19.0 // indirect
golang.org/x/crypto v0.17.0 // indirect
golang.org/x/exp v0.0.0-20230510235704-dd950f8aeaea // indirect
golang.org/x/mod v0.11.0 // indirect
golang.org/x/sys v0.16.0 // indirect
golang.org/x/tools v0.10.0 // indirect
golang.org/x/tools v0.13.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230711160842-782d3b101e98 // indirect
google.golang.org/grpc v1.58.3 // indirect
google.golang.org/protobuf v1.31.0 // indirect
Expand Down
4 changes: 4 additions & 0 deletions modules/redpanda/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.11.0 h1:bUO06HqtnRcc/7l71XBe4WcqTZ+3AH1J59zWDDwLKgU=
golang.org/x/mod v0.11.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/mod v0.14.0 h1:dGoOF9QVLYng8IHTm7BAyWqCqSheQ5pYWGhzW00YJr0=
golang.org/x/mod v0.14.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
Expand Down Expand Up @@ -170,6 +172,8 @@ golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roY
golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
golang.org/x/tools v0.10.0 h1:tvDr/iQoUqNdohiYm0LmmKcBk+q86lb9EprIUFhHHGg=
golang.org/x/tools v0.10.0/go.mod h1:UJwyiVBsOA2uwvK/e5OY3GTpDUJriEd+/YlqAwLPmyM=
golang.org/x/tools v0.13.0 h1:Iey4qkscZuv0VvIt8E0neZjtPVQFSc870HQ448QgEmQ=
golang.org/x/tools v0.13.0/go.mod h1:HvlwmtVNQAhOuCjW7xxvovg8wbNq7LwfXh/k7wXUl58=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down
36 changes: 23 additions & 13 deletions modules/redpanda/redpanda.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,15 @@ import (
"context"
_ "embed"
"fmt"
"golang.org/x/mod/semver"
"math"
"os"
"path/filepath"
"strings"
"text/template"
"time"

semver "github.com/Masterminds/semver/v3"

"github.com/docker/go-connections/nat"

"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/wait"
)
Expand Down Expand Up @@ -92,16 +90,8 @@ func RunContainer(ctx context.Context, opts ...testcontainers.ContainerCustomize
opt.Customize(&req)
}

ver, err := semver.NewVersion(strings.Split(req.ContainerRequest.Image, ":")[1])
if err != nil {
return nil, err
}

minVerForTransform, err := semver.NewVersion("v23.3")
if err != nil {
return nil, err
}
if ver.LessThan(minVerForTransform) {
// 2.1. If the image is not at least v23.3, disable wasm transform
if !isAtLeastVersion(req.ContainerRequest.Image, "23.3") {
settings.EnableWasmTransform = false
}

Expand Down Expand Up @@ -362,3 +352,23 @@ type listener struct {
Port int
AuthenticationMethod string
}

// isAtLeastVersion returns true if the base image (without tag) is in a version or above
func isAtLeastVersion(image, major string) bool {
parts := strings.Split(image, ":")
version := parts[len(parts)-1]

if version == "latest" {
return true
}

if !strings.HasPrefix(version, "v") {
version = fmt.Sprintf("v%s", version)
}

if semver.IsValid(version) {
return semver.Compare(version, fmt.Sprintf("v%s", major)) >= 0 // version >= v8.x
}

return false
}
49 changes: 49 additions & 0 deletions modules/redpanda/redpanda_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -563,3 +563,52 @@ D4ZNvyXf/6E27Ibu6v2p/vs=
-----END TESTING KEY-----`))

func testingKey(s string) string { return strings.ReplaceAll(s, "TESTING KEY", "PRIVATE KEY") }

func Test_isAtLeastVersion(t *testing.T) {
type args struct {
image string
major string
}
tests := []struct {
name string
args args
want bool
}{
{
name: "v21.5.6",
args: args{
image: "redpandadata/redpanda:v21.5.6",
major: "23.3",
},
want: false,
},
{
name: "v23.3.3",
args: args{
image: "redpandadata/redpanda:v23.3.3",
major: "23.3",
},
want: true,
},
{
name: "v23.3.3-rc1",
args: args{
image: "redpandadata/redpanda:v23.3.3-rc1",
major: "23.3",
},
want: true,
},
{
name: "v21.3.3-rc1",
args: args{
image: "redpandadata/redpanda:v21.3.3-rc1",
major: "23.3",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, isAtLeastVersion(tt.args.image, tt.args.major), "isAtLeastVersion(%v, %v)", tt.args.image, tt.args.major)
})
}
}

0 comments on commit 9e5e83d

Please sign in to comment.