Skip to content

Commit

Permalink
feat(modules): add artemis container (#1440)
Browse files Browse the repository at this point in the history
* feat(modules): add artemis container

* chore: improve docs, use assert for tests

* chore: bump Go to 1.20

See #1497

---------

Co-authored-by: Manuel de la Peña <social.mdelapenya@gmail.com>
  • Loading branch information
abemedia and mdelapenya authored Aug 14, 2023
1 parent c4236fb commit d213b73
Show file tree
Hide file tree
Showing 10 changed files with 646 additions and 1 deletion.
11 changes: 11 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,17 @@ updates:
all:
patterns:
- '*'
- package-ecosystem: gomod
directory: /modules/artemis
schedule:
interval: monthly
day: sunday
open-pull-requests-limit: 3
rebase-strategy: disabled
groups:
all:
patterns:
- '*'
- package-ecosystem: gomod
directory: /modules/clickhouse
schedule:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ jobs:
matrix:
go-version: [1.19.x, 1.x]
platform: [ubuntu-latest, macos-latest]
module: [clickhouse, compose, couchbase, k3s, localstack, mongodb, mysql, neo4j, postgres, pulsar, redis, redpanda, vault]
module: [artemis, clickhouse, compose, couchbase, k3s, localstack, mongodb, mysql, neo4j, postgres, pulsar, redis, redpanda, vault]
exclude:
- module: compose
go-version: 1.19.x
Expand Down
124 changes: 124 additions & 0 deletions docs/modules/artemis.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
# Apache ActiveMQ Artemis

Not available until the next release of testcontainers-go <a href="https://github.com/testcontainers/testcontainers-go"><span class="tc-version">:material-tag: main</span></a>

## Introduction

The Testcontainers module for Artemis.

## Adding this module to your project dependencies

Please run the following command to add the Artemis module to your Go dependencies:

```
go get github.com/testcontainers/testcontainers-go/modules/artemis
```

## Usage example

<!--codeinclude-->
[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) (*Container, error)
```

- `context.Context`, the Go context.
- `testcontainers.ContainerCustomizer`, a variadic argument for passing options.

### Container Options

When starting the Artemis container, you can pass options in a variadic way to configure it.

#### Image

If you need to set a different Artemis Docker image, you can use `testcontainers.WithImage` with a valid Docker image
for Artemis. E.g. `testcontainers.WithImage("docker.io/apache/activemq-artemis:2.30.0")`.

#### Wait Strategies

If you need to set a different wait strategy for Artemis, you can use `testcontainers.WithWaitStrategy` with a valid wait strategy
for Artemis.

!!!info
The default deadline for the wait strategy is 60 seconds.

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:

- `testcontainers.WithConfigModifier`
- `testcontainers.WithHostConfigModifier`
- `testcontainers.WithEndpointSettingsModifier`

Please read the [Create containers: Advanced Settings](../features/creating_container.md#advanced-settings) documentation for more information.

### Container Methods

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)
```
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ nav:
- SQL: features/wait/sql.md
- Modules:
- modules/index.md
- modules/artemis.md
- modules/clickhouse.md
- modules/couchbase.md
- modules/k3s.md
Expand Down
5 changes: 5 additions & 0 deletions modules/artemis/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
include ../../commons-test.mk

.PHONY: test
test:
$(MAKE) test-artemis
105 changes: 105 additions & 0 deletions modules/artemis/artemis.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package artemis

import (
"context"
"fmt"

"github.com/docker/go-connections/nat"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/wait"
)

const (
defaultBrokerPort = "61616/tcp"
defaultHTTPPort = "8161/tcp"
)

// Container represents the Artemis container type used in the module.
type Container struct {
testcontainers.Container
user string
password string
}

// User returns the administrator username.
func (c *Container) User() string {
return c.user
}

// Password returns the administrator password.
func (c *Container) Password() string {
return c.password
}

// BrokerEndpoint returns the host:port for the combined protocols endpoint.
// The endpoint accepts CORE, MQTT, AMQP, STOMP, HORNETQ and OPENWIRE protocols.
func (c *Container) BrokerEndpoint(ctx context.Context) (string, error) {
return c.PortEndpoint(ctx, nat.Port(defaultBrokerPort), "")
}

// ConsoleURL returns the URL for the management console.
func (c *Container) ConsoleURL(ctx context.Context) (string, error) {
host, err := c.PortEndpoint(ctx, nat.Port(defaultHTTPPort), "")
if err != nil {
return "", err
}
return fmt.Sprintf("http://%s:%s@%s/console", c.user, c.password, host), nil
}

// WithCredentials sets the administrator credentials. The default is artemis:artemis.
func WithCredentials(user, password string) testcontainers.CustomizeRequestOption {
return func(req *testcontainers.GenericContainerRequest) {
req.Env["ARTEMIS_USER"] = user
req.Env["ARTEMIS_PASSWORD"] = password
}
}

// WithAnonymousLogin enables anonymous logins.
func WithAnonymousLogin() testcontainers.CustomizeRequestOption {
return func(req *testcontainers.GenericContainerRequest) {
req.Env["ANONYMOUS_LOGIN"] = "true"
}
}

// Additional arguments sent to the `artemis create“ command.
// 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.
func WithExtraArgs(args string) testcontainers.CustomizeRequestOption {
return func(req *testcontainers.GenericContainerRequest) {
req.Env["EXTRA_ARGS"] = args
}
}

// RunContainer creates an instance of the Artemis container type.
func RunContainer(ctx context.Context, opts ...testcontainers.ContainerCustomizer) (*Container, error) {
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"),
),
},
Started: true,
}

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

container, err := testcontainers.GenericContainer(ctx, req)
if err != nil {
return nil, err
}

user := req.Env["ARTEMIS_USER"]
password := req.Env["ARTEMIS_PASSWORD"]

return &Container{Container: container, user: user, password: password}, nil
}
Loading

0 comments on commit d213b73

Please sign in to comment.