Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add MySQL module #943

Merged
merged 14 commits into from
Mar 23, 2023
12 changes: 6 additions & 6 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,6 @@ updates:
interval: monthly
open-pull-requests-limit: 3
rebase-strategy: disabled
- package-ecosystem: gomod
directory: /examples/mysql
schedule:
interval: monthly
open-pull-requests-limit: 3
rebase-strategy: disabled
- package-ecosystem: gomod
directory: /examples/nginx
schedule:
Expand Down Expand Up @@ -108,6 +102,12 @@ updates:
interval: monthly
open-pull-requests-limit: 3
rebase-strategy: disabled
- package-ecosystem: gomod
eddumelendez marked this conversation as resolved.
Show resolved Hide resolved
directory: /modules/mysql
schedule:
interval: monthly
open-pull-requests-limit: 3
rebase-strategy: disabled
- package-ecosystem: gomod
directory: /modules/pulsar
schedule:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
name: Mysql example pipeline
name: MySQL module pipeline

on: [push, pull_request]

concurrency:
group: "${{ github.workflow }}-${{ github.head_ref || github.sha }}"
group: ${{ github.workflow }}-${{ github.head_ref || github.sha }}
cancel-in-progress: true

jobs:
Expand All @@ -24,15 +24,15 @@ jobs:
uses: actions/checkout@v3

- name: modVerify
working-directory: ./examples/mysql
working-directory: ./modules/mysql
run: go mod verify

- name: modTidy
working-directory: ./examples/mysql
working-directory: ./modules/mysql
run: make tools-tidy

- name: gotestsum
working-directory: ./examples/mysql
working-directory: ./modules/mysql
run: make test-unit

- name: Run checker
Expand Down
9 changes: 0 additions & 9 deletions docs/examples/mysql.md

This file was deleted.

9 changes: 9 additions & 0 deletions docs/modules/mysql.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# MySQL

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

<!--codeinclude-->
[Test for a MySQL container](../../modules/mysql/mysql_test.go)
<!--/codeinclude-->
38 changes: 0 additions & 38 deletions examples/mysql/mysql.go

This file was deleted.

55 changes: 0 additions & 55 deletions examples/mysql/mysql_test.go

This file was deleted.

2 changes: 1 addition & 1 deletion mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ nav:
- modules/index.md
- modules/couchbase.md
- modules/localstack.md
- modules/mysql.md
- modules/pulsar.md
- Examples:
- examples/index.md
Expand All @@ -61,7 +62,6 @@ nav:
- examples/datastore.md
- examples/firestore.md
- examples/mongodb.md
- examples/mysql.md
- examples/nginx.md
- examples/postgres.md
- examples/pubsub.md
Expand Down
File renamed without changes.
54 changes: 54 additions & 0 deletions modules/mysql/conf.d/my.cnf
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
[mysqld]
user = mysql
port = 3306
#socket = /tmp/mysql.sock
skip-external-locking
key_buffer_size = 16K
max_allowed_packet = 1M
table_open_cache = 4
sort_buffer_size = 64K
read_buffer_size = 256K
read_rnd_buffer_size = 256K
net_buffer_length = 2K
thread_stack = 128K


innodb_file_format=Barracuda





# Don't listen on a TCP/IP port at all. This can be a security enhancement,
# if all processes that need to connect to mysqld run on the same host.
# All interaction with mysqld must be made via Unix sockets or named pipes.
# Note that using this option without enabling named pipes on Windows
# (using the "enable-named-pipe" option) will render mysqld useless!
#
#skip-networking
#server-id = 1

# Uncomment the following if you want to log updates
#log-bin=mysql-bin

# binary logging format - mixed recommended
#binlog_format=mixed

# Causes updates to non-transactional engines using statement format to be
# written directly to binary log. Before using this option make sure that
# there are no dependencies between transactional and non-transactional
# tables such as in the statement INSERT INTO t_myisam SELECT * FROM
# t_innodb; otherwise, slaves may diverge from the master.
#binlog_direct_non_transactional_updates=TRUE

# Uncomment the following if you are using InnoDB tables
innodb_data_file_path = ibdata1:10M:autoextend
# You can set .._buffer_pool_size up to 50 - 80 %
# of RAM but beware of setting memory usage too high
innodb_buffer_pool_size = 16M
#innodb_additional_mem_pool_size = 2M
# Set .._log_file_size to 25 % of buffer pool size
innodb_log_file_size = 5M
innodb_log_buffer_size = 8M
innodb_flush_log_at_trx_commit = 1
innodb_lock_wait_timeout = 50
2 changes: 1 addition & 1 deletion examples/mysql/go.mod → modules/mysql/go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/testcontainers/testcontainers-go/examples/mysql
module github.com/testcontainers/testcontainers-go/modules/mysql

go 1.19

Expand Down
File renamed without changes.
82 changes: 82 additions & 0 deletions modules/mysql/mysql.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package mysql

import (
"context"
"fmt"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/wait"
"strings"
)

const rootUser = "root"
const defaultUser = "test"
const defaultPassword = "test"
const defaultDatabaseName = "test"

// MySQLContainer represents the MySQL container type used in the module
type MySQLContainer struct {
testcontainers.Container
config *Config
}

// StartContainer creates an instance of the MySQL container type
func StartContainer(ctx context.Context, image string, opts ...Option) (*MySQLContainer, error) {
eddumelendez marked this conversation as resolved.
Show resolved Hide resolved
config := &Config{
username: defaultUser,
password: defaultPassword,
database: defaultDatabaseName,
}

for _, opt := range opts {
opt(config)
}

mysqlEnv := map[string]string{}
mysqlEnv["MYSQL_DATABASE"] = config.database
if !strings.EqualFold(rootUser, config.username) {
mysqlEnv["MYSQL_USER"] = config.username
}
if len(config.password) != 0 && config.password != "" {
mysqlEnv["MYSQL_PASSWORD"] = config.password
mysqlEnv["MYSQL_ROOT_PASSWORD"] = config.password
} else if strings.EqualFold(rootUser, config.username) {
mysqlEnv["MYSQL_ALLOW_EMPTY_PASSWORD"] = "yes"
} else {
return nil, fmt.Errorf("empty password can be used only with the root user")
mdelapenya marked this conversation as resolved.
Show resolved Hide resolved
}

req := testcontainers.ContainerRequest{
Image: image,
ExposedPorts: []string{"3306/tcp", "33060/tcp"},
Env: mysqlEnv,
WaitingFor: wait.ForLog("port: 3306 MySQL Community Server - GPL"),
}

//if config.configFile != "" {
// req.Files = []testcontainers.ContainerFile{
// {HostFilePath: config.configFile, ContainerFilePath: "/etc/mysql/conf.d/my.cnf", FileMode: 700},
// }
//}

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

return &MySQLContainer{container, config}, nil
}

func (c *MySQLContainer) Username() string {
return c.config.username
}

func (c *MySQLContainer) Password() string {
return c.config.password
}

func (c *MySQLContainer) Database() string {
return c.config.database
}