Skip to content

Commit

Permalink
Add test for config and scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
eddumelendez committed Mar 21, 2023
1 parent 7b68713 commit f50dcf8
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 51 deletions.
27 changes: 21 additions & 6 deletions modules/mysql/mysql.go
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/wait"
"path/filepath"
"strings"
)

Expand Down Expand Up @@ -49,14 +50,28 @@ func StartContainer(ctx context.Context, image string, opts ...Option) (*MySQLCo
Image: image,
ExposedPorts: []string{"3306/tcp", "33060/tcp"},
Env: mysqlEnv,
WaitingFor: wait.ForLog("port: 3306 MySQL Community Server - GPL"),
WaitingFor: wait.ForLog("port: 3306 MySQL Community Server"),
}

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

var initScripts []testcontainers.ContainerFile
for _, script := range config.scripts {
cf := testcontainers.ContainerFile{
HostFilePath: script,
ContainerFilePath: "/docker-entrypoint-initdb.d/" + filepath.Base(script),
FileMode: 0755,
}
initScripts = append(initScripts, cf)
}
req.Files = append(req.Files, initScripts...)

container, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
ContainerRequest: req,
Expand Down
145 changes: 100 additions & 45 deletions modules/mysql/mysql_test.go
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"database/sql"
"fmt"
"path/filepath"
"testing"

// Import mysql into the scope of this package (required)
Expand Down Expand Up @@ -107,48 +108,102 @@ func TestMySQLWithRootUserAndEmptyPassword(t *testing.T) {
}
}

//func TestMySQLWithConfigFile(t *testing.T) {
// ctx := context.Background()
//
// container, err := StartContainer(ctx, "mysql:5.6.51", WithConfigFile("./conf.d/my.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
//
// host, _ := container.Host(ctx)
//
// p, _ := container.MappedPort(ctx, "3306/tcp")
// port := p.Int()
//
// connectionString := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?tls=skip-verify",
// container.Username(), container.Password(), host, port, container.Database())
//
// 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, _ := db.Prepare("SELECT @@GLOBAL.innodb_file_format")
// row := stmt.QueryRow()
// var 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 TestMySQLWithConfigFile(t *testing.T) {
ctx := context.Background()

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() {
if err := container.Terminate(ctx); err != nil {
t.Fatalf("failed to terminate container: %s", err)
}
})

// perform assertions

host, _ := container.Host(ctx)

p, _ := container.MappedPort(ctx, "3306/tcp")
port := p.Int()

connectionString := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s",
container.Username(), container.Password(), host, port, container.Database())

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, _ := db.Prepare("SELECT @@GLOBAL.innodb_file_format")
if err != nil {
t.Fatal(err)
}
defer stmt.Close()
row := stmt.QueryRow()
var 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 := 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() {
if err := container.Terminate(ctx); err != nil {
t.Fatalf("failed to terminate container: %s", err)
}
})

// perform assertions

host, _ := container.Host(ctx)

p, _ := container.MappedPort(ctx, "3306/tcp")
port := p.Int()

connectionString := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?tls=skip-verify",
container.Username(), container.Password(), host, port, container.Database())

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 name from profile")
if err != nil {
t.Fatal(err)
}
defer stmt.Close()
row := stmt.QueryRow()
var name string
err = row.Scan(&name)
if err != nil {
t.Errorf("error fetching data")
}
if name != "profile 1" {
t.Fatal("The expected record was not found in the database.")
}
}
7 changes: 7 additions & 0 deletions modules/mysql/options.go
Expand Up @@ -7,6 +7,7 @@ type Config struct {
password string
database string
configFile string
scripts []string
}

func WithUsername(username string) Option {
Expand All @@ -32,3 +33,9 @@ func WithConfigFile(configFile string) Option {
config.configFile = configFile
}
}

func WithScripts(scripts ...string) Option {
return func(config *Config) {
config.scripts = scripts
}
}
File renamed without changes.
7 changes: 7 additions & 0 deletions modules/mysql/testresources/schema.sql
@@ -0,0 +1,7 @@
CREATE TABLE IF NOT EXISTS profile (
id MEDIUMINT NOT NULL AUTO_INCREMENT,
name VARCHAR(30) NOT NULL,
PRIMARY KEY (id)
);

INSERT INTO profile (name) values ('profile 1');

0 comments on commit f50dcf8

Please sign in to comment.