Skip to content

Commit

Permalink
feat: add mariadb module (#1548)
Browse files Browse the repository at this point in the history
* chore: scaffolding for mariadb module

* feat: implement MariaDB module, using MySQL as reference

* fix: refine mysql tests and docs

* chore: mod tidy

* chore: use same config as in Java

* chore: support older versions of MariaDB

* chore: reuse code

* chore: apply all MARIADB_ env vars to MYSQL_
  • Loading branch information
mdelapenya authored Aug 30, 2023
1 parent 372c550 commit 7c11a19
Show file tree
Hide file tree
Showing 13 changed files with 827 additions and 4 deletions.
11 changes: 11 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,17 @@ updates:
all:
patterns:
- '*'
- package-ecosystem: gomod
directory: /modules/mariadb
schedule:
interval: monthly
day: sunday
open-pull-requests-limit: 3
rebase-strategy: disabled
groups:
all:
patterns:
- '*'
- package-ecosystem: gomod
directory: /modules/mongodb
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 @@ -90,7 +90,7 @@ jobs:
matrix:
go-version: [1.20.x, 1.x]
platform: [ubuntu-latest, macos-latest]
module: [artemis, clickhouse, compose, couchbase, k3s, localstack, mongodb, mysql, neo4j, postgres, pulsar, redis, redpanda, vault]
module: [artemis, clickhouse, compose, couchbase, k3s, localstack, mariadb, mongodb, mysql, neo4j, postgres, pulsar, redis, redpanda, vault]
uses: ./.github/workflows/ci-test-go.yml
with:
go-version: ${{ matrix.go-version }}
Expand Down
123 changes: 123 additions & 0 deletions docs/modules/mariadb.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
# MariaDB

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 MariaDB.

## Adding this module to your project dependencies

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

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

## Usage example

<!--codeinclude-->
[Creating a MariaDB container](../../modules/mariadb/mariadb_test.go) inside_block:createMariaDBContainer
<!--/codeinclude-->

## Module reference

The MariaDB module exposes one entrypoint function to create the MariaDB container, and this function receives two parameters:

```golang
func RunContainer(ctx context.Context, opts ...testcontainers.ContainerCustomizer) (*MariaDBContainer, error)
```

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

### Container Options

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

!!!tip

You can find all the available configuration and environment variables for the MariaDB Docker image on [Docker Hub](https://hub.docker.com/_/mariadb).

#### Image

If you need to set a different MariaDB Docker image, you can use `testcontainers.WithImage` with a valid Docker image
for MariaDB. E.g. `testcontainers.WithImage("mariadb:11.0.3")`.

<!--codeinclude-->
[Custom Image](../../modules/mariadb/mariadb_test.go) inside_block:withConfigFile
<!--/codeinclude-->

!!!info
From MariaDB [docs](https://github.com/docker-library/docs/tree/master/mariadb#environment-variables):

From tag 10.2.38, 10.3.29, 10.4.19, 10.5.10 onwards, and all 10.6 and later tags,
the `MARIADB_*` equivalent variables are provided. `MARIADB_*` variants will always be
used in preference to `MYSQL_*` variants.

The MariaDB module will take all the environment variables that start with `MARIADB_` and duplicate them with the `MYSQL_` prefix.

#### Wait Strategies

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

!!!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`.

#### Docker type modifiers

If you need an advanced configuration for MariaDB, 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.

#### Set username, password and database name

If you need to set a different database, and its credentials, you can use `WithUsername`, `WithPassword`, `WithDatabase`
options. By default, the username, the password and the database name is `test`.

<!--codeinclude-->
[Custom Database initialization](../../modules/mariadb/mariadb_test.go) inside_block:customInitialization
<!--/codeinclude-->

!!!info
The default values for the username is `root`, for password is `test` and for the default database name is `test`.

#### Init Scripts

If you would like to perform DDL or DML operations in the MariaDB container, add one or more `*.sql`, `*.sql.gz`, or `*.sh`
scripts to the container request. Those files will be copied under `/docker-entrypoint-initdb.d`.

<!--codeinclude-->
[Include init scripts](../../modules/mariadb/mariadb_test.go) inside_block:withScripts
<!--/codeinclude-->

#### Custom configuration

If you need to set a custom configuration, you can use `WithConfigFile` option.

<!--codeinclude-->
[Custom MariaDB config file](../../modules/mariadb/mariadb_test.go) inside_block:withConfigFile
<!--/codeinclude-->

### Container Methods

The MariaDB container exposes the following methods:

#### ConnectionString

This method returns the connection string to connect to the MariaDB container, using the default `3306` port.
It's possible to pass extra parameters to the connection string, e.g. `tls=false`, in a variadic way.

!!!info
By default, MariaDB transmits data between the server and clients without encrypting it.

<!--codeinclude-->
[Get connection string](../../modules/mariadb/mariadb_test.go) inside_block:connectionString
<!--/codeinclude-->
2 changes: 1 addition & 1 deletion docs/modules/mysql.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ If you need to set a custom configuration, you can use `WithConfigFile` option.
#### ConnectionString
This method returns the connection string to connect to the MySQL container, using the default `3306` port.
It's possible to pass extra parameters to the connection string, e.g. `sslmode=disable` or `application_name=myapp`, in a variadic way.
It's possible to pass extra parameters to the connection string, e.g. `tls=skip-verify` or `application_name=myapp`, in a variadic way.
<!--codeinclude-->
[Get connection string](../../modules/mysql/mysql_test.go) inside_block:connectionString
Expand Down
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ nav:
- modules/couchbase.md
- modules/k3s.md
- modules/localstack.md
- modules/mariadb.md
- modules/mongodb.md
- modules/mysql.md
- modules/neo4j.md
Expand Down
5 changes: 5 additions & 0 deletions modules/mariadb/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
include ../../commons-test.mk

.PHONY: test
test:
$(MAKE) test-mariadb
45 changes: 45 additions & 0 deletions modules/mariadb/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
module github.com/testcontainers/testcontainers-go/modules/mariadb

go 1.20

require (
github.com/go-sql-driver/mysql v1.7.1
github.com/testcontainers/testcontainers-go v0.23.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.1 // indirect
github.com/containerd/containerd v1.7.5 // indirect
github.com/cpuguy83/dockercfg v0.3.1 // indirect
github.com/docker/distribution v2.8.2+incompatible // indirect
github.com/docker/docker v24.0.5+incompatible // indirect
github.com/docker/go-connections v0.4.0 // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/uuid v1.3.1 // indirect
github.com/klauspost/compress v1.16.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/moby/patternmatcher v0.5.0 // indirect
github.com/moby/sys/sequential v0.5.0 // indirect
github.com/moby/term v0.5.0 // indirect
github.com/morikuni/aec v1.0.0 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
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/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.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
)

replace github.com/testcontainers/testcontainers-go => ../..
Loading

0 comments on commit 7c11a19

Please sign in to comment.