Skip to content

Commit

Permalink
fix: skip-host-cache option removed in latest MySQL 8.3.0 version (#2130
Browse files Browse the repository at this point in the history
)

* fix: skip-host-cache option removed in latest MySQL 8.3.0 version

* chore: use mysql:8.0.36

* chore: use test in package name

* chore: simplify example
  • Loading branch information
mdelapenya committed Jan 22, 2024
1 parent 05c56df commit b5b7c86
Show file tree
Hide file tree
Showing 13 changed files with 77 additions and 124 deletions.
4 changes: 2 additions & 2 deletions docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
)

const (
mysqlImage = "docker.io/mysql:8.0.30"
mysqlImage = "docker.io/mysql:8.0.36"
nginxDelayedImage = "docker.io/menedev/delayed-nginx:1.15.2"
nginxImage = "docker.io/nginx"
nginxAlpineImage = "docker.io/nginx:alpine"
Expand Down Expand Up @@ -1274,7 +1274,7 @@ func TestContainerCustomPlatformImage(t *testing.T) {
c, err := GenericContainer(ctx, GenericContainerRequest{
ProviderType: providerType,
ContainerRequest: ContainerRequest{
Image: "docker.io/mysql:5.7",
Image: "docker.io/mysql:8.0.36",
ImagePlatform: "linux/amd64",
},
Started: false,
Expand Down
2 changes: 1 addition & 1 deletion docs/features/image_name_substitution.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Consider this if:

* Developers and CI machines need to use different image names. For example, developers are able to pull images from Docker Hub, but CI machines need to pull from a private registry.
* Your private registry has copies of images from Docker Hub where the names are predictable, and just adding a prefix is enough.
For example, `registry.mycompany.com/mirror/mysql:8.0.24` can be derived from the original Docker Hub image name (`mysql:8.0.24`) with a consistent prefix string: `registry.mycompany.com/mirror`
For example, `registry.mycompany.com/mirror/mysql:8.0.36` can be derived from the original Docker Hub image name (`mysql:8.0.36`) with a consistent prefix string: `registry.mycompany.com/mirror`

In this case, image name references in code are **unchanged**.
i.e. you would leave as-is:
Expand Down
4 changes: 2 additions & 2 deletions docs/features/wait/log.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ The Log wait strategy will check if a string occurs in the container logs for a

```golang
req := ContainerRequest{
Image: "docker.io/mysql:8.0.30",
Image: "docker.io/mysql:8.0.36",
ExposedPorts: []string{"3306/tcp", "33060/tcp"},
Env: map[string]string{
"MYSQL_ROOT_PASSWORD": "password",
Expand All @@ -24,7 +24,7 @@ Using a regular expression:

```golang
req := ContainerRequest{
Image: "docker.io/mysql:8.0.30",
Image: "docker.io/mysql:8.0.36",
ExposedPorts: []string{"3306/tcp", "33060/tcp"},
Env: map[string]string{
"MYSQL_ROOT_PASSWORD": "password",
Expand Down
2 changes: 1 addition & 1 deletion docs/features/wait/multi.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Available Options:

```golang
req := ContainerRequest{
Image: "docker.io/mysql:8.0.30",
Image: "docker.io/mysql:8.0.36",
ExposedPorts: []string{"3306/tcp", "33060/tcp"},
Env: map[string]string{
"MYSQL_ROOT_PASSWORD": "password",
Expand Down
2 changes: 1 addition & 1 deletion docs/modules/mysql.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ When starting the MySQL container, you can pass options in a variadic way to con
#### Image
If you need to set a different MySQL Docker image, you can use `testcontainers.WithImage` with a valid Docker image
for MySQL. E.g. `testcontainers.WithImage("mysql:5.6")`.
for MySQL. E.g. `testcontainers.WithImage("mysql:8.0.36")`.
By default, the container will use the following Docker image:
Expand Down
2 changes: 1 addition & 1 deletion modules/compose/testdata/docker-compose-complex.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ services:
ports:
- "9080:80"
mysql:
image: docker.io/mysql:8
image: docker.io/mysql:8.0.36
environment:
- MYSQL_DATABASE=db
- MYSQL_ROOT_PASSWORD=my-secret-pw
Expand Down
2 changes: 1 addition & 1 deletion modules/compose/testdata/docker-compose-override.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ services:
ports:
- "9080:80"
mysql:
image: docker.io/mysql:8
image: docker.io/mysql:8.0.36
environment:
MYSQL_RANDOM_ROOT_PASSWORD: Y
ports:
Expand Down
2 changes: 1 addition & 1 deletion modules/mariadb/testdata/my.cnf
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ read_buffer_size = 256K
read_rnd_buffer_size = 256K
net_buffer_length = 2K
thread_stack = 512K
skip-host-cache
host_cache_size=0
skip-name-resolve

# Don't listen on a TCP/IP port at all. This can be a security enhancement,
Expand Down
53 changes: 52 additions & 1 deletion modules/mysql/examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package mysql_test

import (
"context"
"database/sql"
"fmt"
"path/filepath"

Expand All @@ -14,7 +15,7 @@ func ExampleRunContainer() {
ctx := context.Background()

mysqlContainer, err := mysql.RunContainer(ctx,
testcontainers.WithImage("mysql:8"),
testcontainers.WithImage("mysql:8.0.36"),
mysql.WithConfigFile(filepath.Join("testdata", "my_8.cnf")),
mysql.WithDatabase("foo"),
mysql.WithUsername("root"),
Expand Down Expand Up @@ -43,3 +44,53 @@ func ExampleRunContainer() {
// Output:
// true
}

func ExampleRunContainer_connect() {
ctx := context.Background()

mysqlContainer, err := mysql.RunContainer(ctx,
testcontainers.WithImage("mysql:8.0.36"),
mysql.WithConfigFile(filepath.Join("testdata", "my_8.cnf")),
mysql.WithDatabase("foo"),
mysql.WithUsername("root"),
mysql.WithPassword("password"),
mysql.WithScripts(filepath.Join("testdata", "schema.sql")),
)
if err != nil {
panic(err)
}

defer func() {
if err := mysqlContainer.Terminate(ctx); err != nil {
panic(err)
}
}()

connectionString, _ := mysqlContainer.ConnectionString(ctx)

db, err := sql.Open("mysql", connectionString)
if err != nil {
panic(err)
}
defer db.Close()

if err = db.Ping(); err != nil {
panic(err)
}
stmt, err := db.Prepare("SELECT @@GLOBAL.tmpdir")
if err != nil {
panic(err)
}
defer stmt.Close()
row := stmt.QueryRow()
tmpDir := ""
err = row.Scan(&tmpDir)
if err != nil {
panic(err)
}

fmt.Println(tmpDir)

// Output:
// /tmp
}
2 changes: 1 addition & 1 deletion modules/mysql/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const (
)

// defaultImage {
const defaultImage = "mysql:8"
const defaultImage = "mysql:8.0.36"

// }

Expand Down
70 changes: 13 additions & 57 deletions modules/mysql/mysql_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package mysql
package mysql_test

import (
"context"
Expand All @@ -9,13 +9,13 @@ import (
// Import mysql into the scope of this package (required)
_ "github.com/go-sql-driver/mysql"

"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/modules/mysql"
)

func TestMySQL(t *testing.T) {
ctx := context.Background()

container, err := RunContainer(ctx)
container, err := mysql.RunContainer(ctx)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -57,10 +57,10 @@ func TestMySQL(t *testing.T) {
func TestMySQLWithNonRootUserAndEmptyPassword(t *testing.T) {
ctx := context.Background()

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

container, err := RunContainer(ctx,
WithDatabase("foo"),
WithUsername("root"),
WithPassword(""))
container, err := mysql.RunContainer(ctx,
mysql.WithDatabase("foo"),
mysql.WithUsername("root"),
mysql.WithPassword(""))
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -106,55 +106,11 @@ func TestMySQLWithRootUserAndEmptyPassword(t *testing.T) {
}
}

func TestMySQLWithConfigFile(t *testing.T) {
ctx := context.Background()

container, err := RunContainer(ctx, testcontainers.WithImage("mysql:5.6"),
WithConfigFile(filepath.Join("testdata", "my_5.6.cnf")))
if err != nil {
t.Fatal(err)
}

// Clean up the container after the test is complete
t.Cleanup(func() {
if err := container.Terminate(ctx); err != nil {
t.Fatalf("failed to terminate container: %s", err)
}
})

// perform assertions
connectionString, _ := container.ConnectionString(ctx)

db, err := sql.Open("mysql", connectionString)
if err != nil {
t.Fatal(err)
}
defer db.Close()

if err = db.Ping(); err != nil {
t.Errorf("error pinging db: %+v\n", err)
}
stmt, err := db.Prepare("SELECT @@GLOBAL.innodb_file_format")
if err != nil {
t.Fatal(err)
}
defer stmt.Close()
row := stmt.QueryRow()
innodbFileFormat := ""
err = row.Scan(&innodbFileFormat)
if err != nil {
t.Errorf("error fetching innodb_file_format value")
}
if innodbFileFormat != "Barracuda" {
t.Fatal("The InnoDB file format has been set by the ini file content")
}
}

func TestMySQLWithScripts(t *testing.T) {
ctx := context.Background()

container, err := RunContainer(ctx,
WithScripts(filepath.Join("testdata", "schema.sql")))
container, err := mysql.RunContainer(ctx,
mysql.WithScripts(filepath.Join("testdata", "schema.sql")))
if err != nil {
t.Fatal(err)
}
Expand Down
54 changes: 0 additions & 54 deletions modules/mysql/testdata/my_5.6.cnf

This file was deleted.

2 changes: 1 addition & 1 deletion modules/mysql/testdata/my_8.cnf
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
# this will increase compatibility with older clients. For background, see:
# https://dev.mysql.com/doc/refman/8.1/en/server-system-variables.html#sysvar_default_authentication_plugin
# default-authentication-plugin=mysql_native_password
skip-host-cache
host_cache_size=0
skip-name-resolve
datadir=/var/lib/mysql
socket=/var/run/mysqld/mysqld.sock
Expand Down

0 comments on commit b5b7c86

Please sign in to comment.