Skip to content

Commit

Permalink
Add docs
Browse files Browse the repository at this point in the history
  • Loading branch information
eddumelendez committed Mar 21, 2023
1 parent f50dcf8 commit cde72cc
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 5 deletions.
54 changes: 53 additions & 1 deletion docs/modules/mysql.md
@@ -1,9 +1,61 @@
# MySQL

## Adding this module to your project dependencies

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

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

## Usage example

<!--codeinclude-->
[Creating a MySQL container](../../modules/mysql/mysql.go)
<!--/codeinclude-->

## Module Reference

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

```golang
func StartContainer(ctx context.Context, image string, opts ...Option) (*MySQLContainer, error) {
```
- `context.Context`, the Go context.
- `image`, the image to use for the container.
- `Option`, a variad argument for passing options.
## Container Options
When starting the MySQL 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 MySQL Docker image on [Docker Hub](https://hub.docker.com/_/mysql).
### 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/mysql/mysql_test.go) inside_block:customInitialization
<!--/codeinclude-->
### Init Scripts
If you would like to perform DDL or DML operations in the MySQL 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/mysql/mysql_test.go) inside_block:withScripts
<!--/codeinclude-->
### Custom configuration
If you need to set a custom configuration, you can use `WithConfigFile` option.
<!--codeinclude-->
[Test for a MySQL container](../../modules/mysql/mysql_test.go)
[Custom MySQL config file](../../modules/mysql/mysql_test.go) inside_block:withConfigFile
<!--/codeinclude-->
22 changes: 18 additions & 4 deletions modules/mysql/mysql_test.go
Expand Up @@ -58,7 +58,10 @@ func TestMySQL(t *testing.T) {
func TestMySQLWithNonRootUserAndEmptyPassword(t *testing.T) {
ctx := context.Background()

_, err := StartContainer(ctx, "mysql:8", WithDatabase("foo"), WithUsername("test"), WithPassword(""))
_, err := StartContainer(ctx, "mysql:8",
WithDatabase("foo"),
WithUsername("test"),
WithPassword(""))
if err.Error() != "empty password can be used only with the root user" {
t.Fatal(err)
}
Expand All @@ -67,10 +70,15 @@ func TestMySQLWithNonRootUserAndEmptyPassword(t *testing.T) {
func TestMySQLWithRootUserAndEmptyPassword(t *testing.T) {
ctx := context.Background()

container, err := StartContainer(ctx, "mysql:8", WithDatabase("foo"), WithUsername("root"), WithPassword(""))
// customInitialization {
container, err := StartContainer(ctx, "mysql:8",
WithDatabase("foo"),
WithUsername("root"),
WithPassword(""))
if err != nil {
t.Fatal(err)
}
// }

// Clean up the container after the test is complete
t.Cleanup(func() {
Expand Down Expand Up @@ -111,10 +119,13 @@ func TestMySQLWithRootUserAndEmptyPassword(t *testing.T) {
func TestMySQLWithConfigFile(t *testing.T) {
ctx := context.Background()

container, err := StartContainer(ctx, "mysql:5.6.51", WithConfigFile("./testresources/my.cnf"))
// withConfigFile {
container, err := StartContainer(ctx, "mysql:5.6.51",
WithConfigFile("./testresources/my.cnf"))
if err != nil {
t.Fatal(err)
}
// }

// Clean up the container after the test is complete
t.Cleanup(func() {
Expand Down Expand Up @@ -161,10 +172,13 @@ func TestMySQLWithConfigFile(t *testing.T) {
func TestMySQLWithScripts(t *testing.T) {
ctx := context.Background()

container, err := StartContainer(ctx, "mysql:8", WithScripts(filepath.Join("testresources", "schema.sql")))
// withScripts {
container, err := StartContainer(ctx, "mysql:8",
WithScripts(filepath.Join("testresources", "schema.sql")))
if err != nil {
t.Fatal(err)
}
// }

// Clean up the container after the test is complete
t.Cleanup(func() {
Expand Down

0 comments on commit cde72cc

Please sign in to comment.