diff --git a/docs/modules/artemis.md b/docs/modules/artemis.md index eaf5c52a1e2..4a87a254ca8 100644 --- a/docs/modules/artemis.md +++ b/docs/modules/artemis.md @@ -17,7 +17,7 @@ go get github.com/testcontainers/testcontainers-go/modules/artemis ## Usage example -[Creating an Artemis container](../../modules/artemis/example_test.go) inside_block:runContainer +[Creating and connecting to an Artemis container](../../modules/artemis/example_test.go) inside_block:ExampleRunContainer ## Module reference @@ -25,7 +25,7 @@ go get github.com/testcontainers/testcontainers-go/modules/artemis The Artemis module exposes one entrypoint function to create the Artemis container, and this function receives two parameters: ```golang -func RunContainer(ctx context.Context, opts ...testcontainers.ContainerCustomizer) (*ArtemisContainer, error) +func RunContainer(ctx context.Context, opts ...testcontainers.ContainerCustomizer) (*Container, error) ``` - `context.Context`, the Go context. @@ -50,6 +50,33 @@ for Artemis. At the same time, it's possible to set a wait strategy and a custom deadline with `testcontainers.WithWaitStrategyAndDeadline`. +#### Credentials + +If you need to change the default admin credentials (i.e. `artemis:artemis`) use `WithCredentials`. + +```go +container, err := artemis.RunContainer(ctx, artemis.WithCredentials("user", "password")) +``` + +#### Anonymous Login + +If you need to enable anonymous logins (which are disabled by default) use `WithAnonymousLogin`. + +```go +container, err := artemis.RunContainer(ctx, artemis.WithAnonymousLogin()) +``` + +#### Custom Arguments + +If you need to pass custom arguments to the `artemis create` command, use `WithExtraArgs`. +The default is `--http-host 0.0.0.0 --relax-jolokia`. +Setting this value will override the default. +See the documentation on `artemis create` for available options. + +```go +container, err := artemis.RunContainer(ctx, artemis.WithExtraArgs("--http-host 0.0.0.0 --relax-jolokia --queues ArgsTestQueue")) +``` + #### Docker type modifiers If you need an advanced configuration for Artemis, you can leverage the following Docker type modifiers: @@ -64,5 +91,34 @@ Please read the [Create containers: Advanced Settings](../features/creating_cont The Artemis container exposes the following methods: +#### User +User returns the administrator username. +```go +user := container.User() +``` + +#### Password + +Password returns the administrator password. + +```go +password := container.Password() +``` + +#### BrokerEndpoint + +BrokerEndpoint returns the host:port for the combined protocols endpoint. + +```go +host, err := container.BrokerEndpoint(ctx) +``` + +#### ConsoleURL + +ConsoleURL returns the URL for the management console. + +```go +url, err := container.ConsoleURL(ctx) +``` diff --git a/modules/artemis/artemis.go b/modules/artemis/artemis.go index 7e3cff1ae66..716d1404fac 100644 --- a/modules/artemis/artemis.go +++ b/modules/artemis/artemis.go @@ -21,7 +21,7 @@ type Container struct { password string } -// Username returns the administrator username. +// User returns the administrator username. func (c *Container) User() string { return c.user } diff --git a/modules/artemis/artemis_test.go b/modules/artemis/artemis_test.go index 7074d234d0b..200d0a84399 100644 --- a/modules/artemis/artemis_test.go +++ b/modules/artemis/artemis_test.go @@ -8,6 +8,8 @@ import ( "time" "github.com/go-stomp/stomp/v3" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" "github.com/testcontainers/testcontainers-go" "github.com/testcontainers/testcontainers-go/modules/artemis" ) @@ -56,42 +58,27 @@ func TestArtemis(t *testing.T) { for _, test := range tests { t.Run(test.name, func(t *testing.T) { container, err := artemis.RunContainer(ctx, test.opts...) - if err != nil { - t.Fatal(err) - } - t.Cleanup(func() { - if err := container.Terminate(ctx); err != nil { - t.Fatalf("failed to terminate container: %s", err) - } - }) + require.NoError(t, err) + t.Cleanup(func() { require.NoError(t, container.Terminate(ctx), "failed to terminate container") }) u, err := container.ConsoleURL(ctx) - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) res, err := http.Get(u) - if err != nil { - t.Fatal(err) - } + require.NoError(t, err, "failed to access console") res.Body.Close() + assert.Equal(t, http.StatusOK, res.StatusCode, "failed to access console") - if res.StatusCode != http.StatusOK { - t.Error("failed to access console") + if test.user != "" { + assert.Equal(t, test.user, container.User(), "unexpected user") } - if test.user != "" && container.User() != test.user { - t.Fatal("unexpected user") - } - - if test.pass != "" && container.Password() != test.pass { - t.Fatal("unexpected password") + if test.pass != "" { + assert.Equal(t, test.pass, container.Password(), "unexpected password") } host, err := container.BrokerEndpoint(ctx) - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) var opt []func(*stomp.Conn) error if test.user != "" || test.pass != "" { @@ -99,30 +86,22 @@ func TestArtemis(t *testing.T) { } conn, err := stomp.Dial("tcp", host, opt...) - if err != nil { - t.Fatal(err) - } - t.Cleanup(func() { conn.Disconnect() }) + require.NoError(t, err, "failed to connect") + t.Cleanup(func() { require.NoError(t, conn.Disconnect()) }) sub, err := conn.Subscribe("test", stomp.AckAuto) - if err != nil { - t.Fatal(err) - } - t.Cleanup(func() { sub.Unsubscribe() }) + require.NoError(t, err, "failed to subscribe") + t.Cleanup(func() { require.NoError(t, sub.Unsubscribe()) }) err = conn.Send("test", "", []byte("test")) - if err != nil { - t.Fatal(err) - } + require.NoError(t, err, "failed to send") ticker := time.NewTicker(10 * time.Second) select { case <-ticker.C: t.Fatal("timed out waiting for message") case msg := <-sub.C: - if string(msg.Body) != "test" { - t.Fatal("received unexpected message bytes") - } + require.Equal(t, "test", string(msg.Body), "received unexpected message") } if test.hook != nil { @@ -136,26 +115,15 @@ func expectQueue(t *testing.T, container *artemis.Container, queueName string) { t.Helper() u, err := container.ConsoleURL(context.Background()) - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) r, err := http.Get(u + `/jolokia/read/org.apache.activemq.artemis:broker="0.0.0.0"/QueueNames`) - if err != nil { - t.Fatal(err) - } + require.NoError(t, err, "failed to request QueueNames") defer r.Body.Close() var res struct{ Value []string } - if err = json.NewDecoder(r.Body).Decode(&res); err != nil { - t.Fatal(err) - } - - for _, v := range res.Value { - if v == queueName { - return - } - } + err = json.NewDecoder(r.Body).Decode(&res) + require.NoError(t, err, "failed to decode QueueNames response") - t.Fatalf("should contain queue %q", queueName) + require.Containsf(t, res.Value, queueName, "should contain queue") } diff --git a/modules/artemis/example_test.go b/modules/artemis/example_test.go index f6338c18a5e..5ff7679cac8 100644 --- a/modules/artemis/example_test.go +++ b/modules/artemis/example_test.go @@ -10,25 +10,35 @@ import ( func ExampleRunContainer() { ctx := context.Background() - user := "username" - pass := "password" - - // runContainer { - container, err := artemis.RunContainer(ctx, artemis.WithCredentials(user, pass)) + // Run container. + container, err := artemis.RunContainer(ctx) if err != nil { panic(err) } - // } + defer func() { + if err := container.Terminate(ctx); err != nil { + panic(err) + } + }() + // Get broker endpoint. host, err := container.BrokerEndpoint(ctx) if err != nil { panic(err) } + // Get credentials. + user := container.User() + pass := container.Password() + + // Connect to Artemis via STOMP. conn, err := stomp.Dial("tcp", host, stomp.ConnOpt.Login(user, pass)) if err != nil { panic(err) } - - _ = conn + defer func() { + if err := conn.Disconnect(); err != nil { + panic(err) + } + }() } diff --git a/modules/artemis/go.mod b/modules/artemis/go.mod index 2e37f5cd46d..207a9aadef3 100644 --- a/modules/artemis/go.mod +++ b/modules/artemis/go.mod @@ -5,6 +5,7 @@ go 1.19 require ( github.com/docker/go-connections v0.4.0 github.com/go-stomp/stomp/v3 v3.0.5 + github.com/stretchr/testify v1.8.4 github.com/testcontainers/testcontainers-go v0.22.0 ) @@ -15,6 +16,7 @@ require ( github.com/cenkalti/backoff/v4 v4.2.0 // indirect github.com/containerd/containerd v1.7.3 // indirect github.com/cpuguy83/dockercfg v0.3.1 // indirect + github.com/davecgh/go-spew v1.1.1 // indirect github.com/docker/distribution v2.8.2+incompatible // indirect github.com/docker/docker v24.0.5+incompatible // indirect github.com/docker/go-units v0.5.0 // indirect @@ -31,6 +33,7 @@ require ( github.com/opencontainers/image-spec v1.1.0-rc4 // indirect github.com/opencontainers/runc v1.1.5 // indirect github.com/pkg/errors v0.9.1 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect github.com/sirupsen/logrus v1.9.0 // indirect golang.org/x/exp v0.0.0-20230510235704-dd950f8aeaea // indirect golang.org/x/mod v0.9.0 // indirect @@ -40,6 +43,7 @@ require ( google.golang.org/genproto/googleapis/rpc v0.0.0-20230525234030-28d5490b6b19 // indirect google.golang.org/grpc v1.57.0 // indirect google.golang.org/protobuf v1.30.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect ) replace github.com/testcontainers/testcontainers-go => ../.. diff --git a/modules/artemis/go.sum b/modules/artemis/go.sum index 33127514013..e1411acca32 100644 --- a/modules/artemis/go.sum +++ b/modules/artemis/go.sum @@ -94,6 +94,7 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= github.com/syndtr/gocapability v0.0.0-20200815063812-42c35b437635/go.mod h1:hkRG7XYTFWNJGYcbNJQlaLq0fg1yr4J4t/NcTQtrfww= github.com/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0= github.com/vishvananda/netlink v1.1.0/go.mod h1:cTgwzPIzzgDAYoQrMm0EdrjRUBkTqKYppBueQtXaqoE=