Skip to content

Commit

Permalink
chore: improve docs, use assert for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
abemedia committed Aug 13, 2023
1 parent eaa2720 commit 06d9965
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 89 deletions.
60 changes: 58 additions & 2 deletions docs/modules/artemis.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ go get github.com/testcontainers/testcontainers-go/modules/artemis
## Usage example

<!--codeinclude-->
[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
<!--/codeinclude-->

## Module reference

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.
Expand All @@ -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:
Expand All @@ -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)
```
34 changes: 16 additions & 18 deletions modules/artemis/artemis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -73,29 +73,27 @@ func WithExtraArgs(args string) testcontainers.CustomizeRequestOption {

// RunContainer creates an instance of the Artemis container type.
func RunContainer(ctx context.Context, opts ...testcontainers.ContainerCustomizer) (*Container, error) {
req := testcontainers.ContainerRequest{
Image: "docker.io/apache/activemq-artemis:2.30.0-alpine",
Env: map[string]string{
"ARTEMIS_USER": "artemis",
"ARTEMIS_PASSWORD": "artemis",
req := testcontainers.GenericContainerRequest{
ContainerRequest: testcontainers.ContainerRequest{
Image: "docker.io/apache/activemq-artemis:2.30.0-alpine",
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"),
),
},
ExposedPorts: []string{defaultBrokerPort, defaultHTTPPort},
WaitingFor: wait.ForAll(
wait.ForLog("Server is now live"),
wait.ForLog("REST API available"),
),
}

genericContainerReq := testcontainers.GenericContainerRequest{
ContainerRequest: req,
Started: true,
Started: true,
}

for _, opt := range opts {
opt.Customize(&genericContainerReq)
opt.Customize(&req)
}

container, err := testcontainers.GenericContainer(ctx, genericContainerReq)
container, err := testcontainers.GenericContainer(ctx, req)
if err != nil {
return nil, err
}
Expand Down
78 changes: 23 additions & 55 deletions modules/artemis/artemis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -56,73 +58,50 @@ 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 != "" {
opt = append(opt, stomp.ConnOpt.Login(test.user, test.pass))
}

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 {
Expand All @@ -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")
}
26 changes: 18 additions & 8 deletions modules/artemis/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}()
}
8 changes: 6 additions & 2 deletions modules/artemis/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,18 @@ 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
)

require (
dario.cat/mergo v1.0.0 // indirect
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/cenkalti/backoff/v4 v4.2.0 // indirect
github.com/cenkalti/backoff/v4 v4.2.1 // 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
Expand All @@ -31,15 +33,17 @@ 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
golang.org/x/net v0.9.0 // indirect
golang.org/x/sys v0.8.0 // indirect
golang.org/x/sys v0.11.0 // indirect
golang.org/x/tools v0.7.0 // indirect
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 => ../..
9 changes: 5 additions & 4 deletions modules/artemis/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03
github.com/Microsoft/go-winio v0.6.1 h1:9/kr64B9VUZrLm5YYwbGtUJnMgqWVOdUAXu6Migciow=
github.com/Microsoft/go-winio v0.6.1/go.mod h1:LRdKpFKfdobln8UmuiYcKPot9D2v6svN5+sAH+4kjUM=
github.com/Microsoft/hcsshim v0.10.0-rc.8 h1:YSZVvlIIDD1UxQpJp0h+dnpLUw+TrY0cx8obKsp3bek=
github.com/cenkalti/backoff/v4 v4.2.0 h1:HN5dHm3WBOgndBH6E8V0q2jIYIR3s9yglV8k/+MN3u4=
github.com/cenkalti/backoff/v4 v4.2.0/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE=
github.com/cenkalti/backoff/v4 v4.2.1 h1:y4OZtCnogmCPw98Zjyt5a6+QwPLGkiQsYW5oUqylYbM=
github.com/cenkalti/backoff/v4 v4.2.1/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE=
github.com/checkpoint-restore/go-criu/v5 v5.3.0/go.mod h1:E/eQpaFtUKGOOSEBZgmKAcn+zUUwWxqcaKZlF54wK8E=
github.com/cilium/ebpf v0.7.0/go.mod h1:/oI2+1shJiTGAMgl6/RgJr36Eo1jzrRcAWbcXO2usCA=
github.com/containerd/console v1.0.3/go.mod h1:7LqA/THxQ86k76b8c/EMSiaJ3h1eZkMkXar0TQ1gf3U=
Expand Down Expand Up @@ -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=
Expand Down Expand Up @@ -139,8 +140,8 @@ golang.org/x/sys v0.0.0-20210906170528-6f6e22806c34/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20211025201205-69cdffdb9359/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211116061358-0a5406a5449c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.8.0 h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM=
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
Expand Down

0 comments on commit 06d9965

Please sign in to comment.