Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 44 additions & 25 deletions modules/artemis/artemis.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package artemis
import (
"context"
"fmt"
"strings"

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

Expand All @@ -13,6 +14,8 @@ import (
const (
defaultBrokerPort = "61616/tcp"
defaultHTTPPort = "8161/tcp"
defaultUser = "artemis"
defaultPassword = "artemis"
)

// Container represents the Artemis container type used in the module.
Expand Down Expand Up @@ -86,39 +89,55 @@ func RunContainer(ctx context.Context, opts ...testcontainers.ContainerCustomize

// Run creates an instance of the Artemis container type with a given image
func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustomizer) (*Container, error) {
req := testcontainers.GenericContainerRequest{
ContainerRequest: testcontainers.ContainerRequest{
Image: img,
Env: map[string]string{
"ARTEMIS_USER": "artemis",
"ARTEMIS_PASSWORD": "artemis",
},
ExposedPorts: []string{defaultBrokerPort, defaultHTTPPort},
WaitingFor: wait.ForAll(
wait.ForLog("Server is now live"),
wait.ForLog("REST API available"),
),
},
Started: true,
moduleOpts := []testcontainers.ContainerCustomizer{
testcontainers.WithExposedPorts(defaultBrokerPort, defaultHTTPPort),
testcontainers.WithEnv(map[string]string{
"ARTEMIS_USER": defaultUser,
"ARTEMIS_PASSWORD": defaultPassword,
}),
testcontainers.WithWaitStrategy(wait.ForAll(
wait.ForListeningPort(defaultBrokerPort),
wait.ForListeningPort(defaultHTTPPort),
wait.ForLog("Server is now live"),
wait.ForLog("REST API available"),
)),
}

for _, opt := range opts {
if err := opt.Customize(&req); err != nil {
return nil, err
}
}
moduleOpts = append(moduleOpts, opts...)

container, err := testcontainers.GenericContainer(ctx, req)
ctr, err := testcontainers.Run(ctx, img, moduleOpts...)
var c *Container
if container != nil {
c = &Container{Container: container}
if ctr != nil {
c = &Container{Container: ctr}
}
if err != nil {
return c, fmt.Errorf("generic container: %w", err)
return c, fmt.Errorf("run artemis: %w", err)
}

// initialize the credentials
c.user = defaultUser
c.password = defaultPassword

inspect, err := ctr.Inspect(ctx)
if err != nil {
return c, fmt.Errorf("inspect artemis: %w", err)
}

c.user = req.Env["ARTEMIS_USER"]
c.password = req.Env["ARTEMIS_PASSWORD"]
// refresh the credentials from the environment variables
foundUser, foundPass := false, false
for _, env := range inspect.Config.Env {
if v, ok := strings.CutPrefix(env, "ARTEMIS_USER="); ok {
c.user, foundUser = v, true
continue
} else if v, ok := strings.CutPrefix(env, "ARTEMIS_PASSWORD="); ok {
c.password, foundPass = v, true
continue
}

if foundUser && foundPass {
break
}
}

return c, nil
}
10 changes: 5 additions & 5 deletions modules/artemis/artemis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"time"

"github.com/go-stomp/stomp/v3"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/testcontainers/testcontainers-go"
Expand Down Expand Up @@ -54,8 +53,8 @@ func TestArtemis(t *testing.T) {
artemis.WithExtraArgs("--http-host 0.0.0.0 --relax-jolokia --queues ArgsTestQueue"),
// }
},
user: "artemis",
pass: "artemis",
user: "artemis", // default user
pass: "artemis", // default password
hook: func(t *testing.T, container *artemis.Container) {
t.Helper()
expectQueue(t, container, "ArgsTestQueue")
Expand All @@ -80,11 +79,11 @@ func TestArtemis(t *testing.T) {
require.Equal(t, http.StatusOK, res.StatusCode, "failed to access console")

if test.user != "" {
assert.Equal(t, test.user, ctr.User(), "unexpected user")
require.Equal(t, test.user, ctr.User(), "unexpected user")
}

if test.pass != "" {
assert.Equal(t, test.pass, ctr.Password(), "unexpected password")
require.Equal(t, test.pass, ctr.Password(), "unexpected password")
}

// brokerEndpoint {
Expand All @@ -109,6 +108,7 @@ func TestArtemis(t *testing.T) {
require.NoError(t, err, "failed to send")

ticker := time.NewTicker(10 * time.Second)
defer ticker.Stop()
select {
case <-ticker.C:
t.Fatal("timed out waiting for message")
Expand Down
Loading