|
| 1 | +package arangodb |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "net/http" |
| 7 | + |
| 8 | + "github.com/testcontainers/testcontainers-go" |
| 9 | + "github.com/testcontainers/testcontainers-go/wait" |
| 10 | +) |
| 11 | + |
| 12 | +const ( |
| 13 | + defaultPort = "8529/tcp" |
| 14 | + |
| 15 | + // DefaultUser is the default username for the ArangoDB container. |
| 16 | + // This is the username to be used when connecting to the ArangoDB instance. |
| 17 | + DefaultUser = "root" |
| 18 | + |
| 19 | + defaultPassword = "root" |
| 20 | +) |
| 21 | + |
| 22 | +// Container represents the ArangoDB container type used in the module |
| 23 | +type Container struct { |
| 24 | + testcontainers.Container |
| 25 | + password string |
| 26 | +} |
| 27 | + |
| 28 | +// Credentials returns the credentials for the ArangoDB container: |
| 29 | +// first return value is the username, second is the password. |
| 30 | +func (c *Container) Credentials() (string, string) { |
| 31 | + return DefaultUser, c.password |
| 32 | +} |
| 33 | + |
| 34 | +// HTTPEndpoint returns the HTTP endpoint of the ArangoDB container, using the following format: `http://$host:$port`. |
| 35 | +func (c *Container) HTTPEndpoint(ctx context.Context) (string, error) { |
| 36 | + hostPort, err := c.PortEndpoint(ctx, defaultPort, "http") |
| 37 | + if err != nil { |
| 38 | + return "", fmt.Errorf("port endpoint: %w", err) |
| 39 | + } |
| 40 | + |
| 41 | + return hostPort, nil |
| 42 | +} |
| 43 | + |
| 44 | +// Run creates an instance of the ArangoDB container type |
| 45 | +func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustomizer) (*Container, error) { |
| 46 | + req := testcontainers.ContainerRequest{ |
| 47 | + Image: img, |
| 48 | + ExposedPorts: []string{defaultPort}, |
| 49 | + Env: map[string]string{ |
| 50 | + "ARANGO_ROOT_PASSWORD": defaultPassword, |
| 51 | + }, |
| 52 | + } |
| 53 | + |
| 54 | + genericContainerReq := testcontainers.GenericContainerRequest{ |
| 55 | + ContainerRequest: req, |
| 56 | + Started: true, |
| 57 | + } |
| 58 | + |
| 59 | + for _, opt := range opts { |
| 60 | + if err := opt.Customize(&genericContainerReq); err != nil { |
| 61 | + return nil, fmt.Errorf("customize: %w", err) |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + // Wait for the container to be ready once we know the credentials |
| 66 | + genericContainerReq.WaitingFor = wait.ForAll( |
| 67 | + wait.ForListeningPort(defaultPort), |
| 68 | + wait.ForHTTP("/_admin/status"). |
| 69 | + WithPort(defaultPort). |
| 70 | + WithBasicAuth(DefaultUser, req.Env["ARANGO_ROOT_PASSWORD"]). |
| 71 | + WithHeaders(map[string]string{ |
| 72 | + "Accept": "application/json", |
| 73 | + }). |
| 74 | + WithStatusCodeMatcher(func(status int) bool { |
| 75 | + return status == http.StatusOK |
| 76 | + }), |
| 77 | + ) |
| 78 | + |
| 79 | + container, err := testcontainers.GenericContainer(ctx, genericContainerReq) |
| 80 | + var c *Container |
| 81 | + if container != nil { |
| 82 | + c = &Container{Container: container, password: req.Env["ARANGO_ROOT_PASSWORD"]} |
| 83 | + } |
| 84 | + |
| 85 | + if err != nil { |
| 86 | + return c, fmt.Errorf("generic container: %w", err) |
| 87 | + } |
| 88 | + |
| 89 | + return c, nil |
| 90 | +} |
0 commit comments