-
-
Notifications
You must be signed in to change notification settings - Fork 503
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(modules): add artemis container (#1440)
* 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
1 parent
c4236fb
commit d213b73
Showing
10 changed files
with
646 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
include ../../commons-test.mk | ||
|
||
.PHONY: test | ||
test: | ||
$(MAKE) test-artemis |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.