Skip to content

Commit

Permalink
Merge branch 'main' into remove-redis-dependency
Browse files Browse the repository at this point in the history
* main:
  chore: add mysql example (testcontainers#700)
  • Loading branch information
mdelapenya committed Dec 17, 2022
2 parents 8821b2f + bb03057 commit bd7c703
Show file tree
Hide file tree
Showing 15 changed files with 1,362 additions and 81 deletions.
6 changes: 6 additions & 0 deletions .github/dependabot.yml
Expand Up @@ -36,6 +36,12 @@ updates:
interval: daily
open-pull-requests-limit: 3
rebase-strategy: disabled
- package-ecosystem: gomod
directory: /examples/mysql
schedule:
interval: daily
open-pull-requests-limit: 3
rebase-strategy: disabled
- package-ecosystem: gomod
directory: /examples/nginx
schedule:
Expand Down
42 changes: 42 additions & 0 deletions .github/workflows/mysql-example.yml
@@ -0,0 +1,42 @@
name: Mysql example pipeline

on: [push, pull_request]

jobs:
test-mysql:
strategy:
matrix:
go-version: [1.18.x, 1.x]
runs-on: "ubuntu-latest"
steps:

- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: ${{ matrix.go-version }}
id: go

- name: Check out code into the Go module directory
uses: actions/checkout@v3

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

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

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

- name: Run checker
run: |
./scripts/check_environment.sh
- name: Test Summary
uses: test-summary/action@4ee9ece4bca777a38f05c8fc578ac2007fe266f7
with:
paths: "**/TEST-mysql*.xml"
if: always()
2 changes: 2 additions & 0 deletions container_test.go
Expand Up @@ -391,7 +391,9 @@ func createTestContainer(t *testing.T, ctx context.Context) int {
if err != nil {
t.Fatalf("could not start container: %v", err)
}
// mappedPort {
port, err := container.MappedPort(ctx, nginxDefaultPort)
// }
if err != nil {
t.Fatalf("could not get mapped port: %v", err)
}
Expand Down
84 changes: 6 additions & 78 deletions docker_test.go
Expand Up @@ -2,12 +2,10 @@ package testcontainers

import (
"context"
"database/sql"
"errors"
"fmt"
"log"

// Import mysql into the scope of this package (required)
"io"
"math/rand"
"net/http"
Expand All @@ -18,8 +16,6 @@ import (
"testing"
"time"

_ "github.com/go-sql-driver/mysql"

"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/strslice"
"github.com/docker/go-units"
Expand Down Expand Up @@ -1046,7 +1042,6 @@ func TestContainerCreationWaitsForLogContextTimeout(t *testing.T) {
}

func TestContainerCreationWaitsForLog(t *testing.T) {
// exposePorts {
ctx := context.Background()
req := ContainerRequest{
Image: mysqlImage,
Expand All @@ -1062,38 +1057,9 @@ func TestContainerCreationWaitsForLog(t *testing.T) {
ContainerRequest: req,
Started: true,
})
// }

require.NoError(t, err)
terminateContainerOnEnd(t, ctx, mysqlC)

// containerHost {
host, _ := mysqlC.Host(ctx)
// }
// mappedPort {
p, _ := mysqlC.MappedPort(ctx, "3306/tcp")
port := p.Int()
// }
connectionString := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?tls=skip-verify",
"root", "password", host, port, "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)
}
_, err = db.Exec("CREATE TABLE IF NOT EXISTS a_table ( \n" +
" `col_1` VARCHAR(128) NOT NULL, \n" +
" `col_2` VARCHAR(128) NOT NULL, \n" +
" PRIMARY KEY (`col_1`, `col_2`) \n" +
")")
if err != nil {
t.Errorf("error creating table: %+v\n", err)
}
}

func Test_BuildContainerFromDockerfile(t *testing.T) {
Expand Down Expand Up @@ -1355,11 +1321,13 @@ func TestContainerCreationWaitsForLogAndPortContextTimeout(t *testing.T) {

func TestContainerCreationWaitingForHostPort(t *testing.T) {
ctx := context.Background()
// exposePorts {
req := ContainerRequest{
Image: nginxAlpineImage,
ExposedPorts: []string{nginxDefaultPort},
WaitingFor: wait.ForListeningPort(nginxDefaultPort),
}
// }
nginx, err := GenericContainer(ctx, GenericContainerRequest{
ProviderType: providerType,
ContainerRequest: req,
Expand Down Expand Up @@ -1387,50 +1355,6 @@ func TestContainerCreationWaitingForHostPortWithoutBashThrowsAnError(t *testing.
terminateContainerOnEnd(t, ctx, nginx)
}

func TestContainerCreationWaitsForLogAndPort(t *testing.T) {
ctx := context.Background()
req := ContainerRequest{
Image: mysqlImage,
ExposedPorts: []string{"3306/tcp", "33060/tcp"},
Env: map[string]string{
"MYSQL_ROOT_PASSWORD": "password",
"MYSQL_DATABASE": "database",
},
WaitingFor: wait.ForAll(
wait.ForLog("port: 3306 MySQL Community Server - GPL"),
wait.ForListeningPort("3306/tcp"),
),
}

mysqlC, err := GenericContainer(ctx, GenericContainerRequest{
ProviderType: providerType,
ContainerRequest: req,
Started: true,
})

require.NoError(t, err)
terminateContainerOnEnd(t, ctx, mysqlC)

// buildingAddresses {
host, _ := mysqlC.Host(ctx)
p, _ := mysqlC.MappedPort(ctx, "3306/tcp")
port := p.Int()
connectionString := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?tls=skip-verify",
"root", "password", host, port, "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)
}
}

func TestCMD(t *testing.T) {
/*
echo a unique statement to ensure that we
Expand Down Expand Up @@ -1776,7 +1700,9 @@ func ExampleContainer_Host() {
log.Fatalf("failed to terminate container: %s", err)
}
}()
// containerHost {
ip, _ := nginxC.Host(ctx)
// }
println(ip)
}

Expand Down Expand Up @@ -1833,9 +1759,11 @@ func ExampleContainer_MappedPort() {
log.Fatalf("failed to terminate container: %s", err)
}
}()
// buildingAddresses {
ip, _ := nginxC.Host(ctx)
port, _ := nginxC.MappedPort(ctx, "80")
_, _ = http.Get(fmt.Sprintf("http://%s:%s", ip, port.Port()))
// }
}

func TestContainerCreationWithBindAndVolume(t *testing.T) {
Expand Down
9 changes: 9 additions & 0 deletions docs/examples/mysql.md
@@ -0,0 +1,9 @@
# Mysql

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

<!--codeinclude-->
[Test for a Mysql container](../../examples/mysql/mysql_test.go)
<!--/codeinclude-->
2 changes: 1 addition & 1 deletion docs/features/networking.md
Expand Up @@ -20,7 +20,7 @@ Because there is this layer of indirection, it is necessary to ask Testcontainer
This can be done using the `MappedPort` function, which takes the original (container) port as an argument:

<!--codeinclude-->
[Retrieving actual ports at runtime](../../docker_test.go) inside_block:mappedPort
[Retrieving actual ports at runtime](../../container_test.go) inside_block:mappedPort
<!--/codeinclude-->

!!! warning
Expand Down
5 changes: 5 additions & 0 deletions examples/mysql/Makefile
@@ -0,0 +1,5 @@
include ../../commons-test.mk

.PHONY: test
test:
$(MAKE) test-mysql

0 comments on commit bd7c703

Please sign in to comment.