Skip to content

Commit

Permalink
Merge branch 'main' into compose-module
Browse files Browse the repository at this point in the history
* main:
  chore: reduce concurrent builds (testcontainers#702)
  chore: add mysql example (testcontainers#700)
  • Loading branch information
mdelapenya committed Dec 19, 2022
2 parents 4ed19e5 + e9fa657 commit 5cd80f2
Show file tree
Hide file tree
Showing 30 changed files with 1,426 additions and 86 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
4 changes: 4 additions & 0 deletions .github/workflows/bigtable-example.yml
Expand Up @@ -2,6 +2,10 @@ name: Bigtable example pipeline

on: [push, pull_request]

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

jobs:
test-bigtable:
strategy:
Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/ci.yml
Expand Up @@ -2,6 +2,10 @@ name: Main pipeline

on: [push, pull_request]

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

jobs:
test:
strategy:
Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/cockroachdb-example.yml
Expand Up @@ -2,6 +2,10 @@ name: Cockroachdb example pipeline

on: [push, pull_request]

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

jobs:
test-cockroachdb:
strategy:
Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/datastore-example.yml
Expand Up @@ -2,6 +2,10 @@ name: Datastore example pipeline

on: [push, pull_request]

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

jobs:
test-datastore:
strategy:
Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/firestore-example.yml
Expand Up @@ -2,6 +2,10 @@ name: Firestore example pipeline

on: [push, pull_request]

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

jobs:
test-firestore:
strategy:
Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/golangci-lint.yml
Expand Up @@ -7,6 +7,10 @@ on:
- main
pull_request:

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

permissions:
contents: read
# Optional: allow read access to pull request. Use with `only-new-issues` option.
Expand Down
46 changes: 46 additions & 0 deletions .github/workflows/mysql-example.yml
@@ -0,0 +1,46 @@
name: Mysql example pipeline

on: [push, pull_request]

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

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()
4 changes: 4 additions & 0 deletions .github/workflows/nginx-example.yml
Expand Up @@ -2,6 +2,10 @@ name: Nginx example pipeline

on: [push, pull_request]

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

jobs:
test-nginx:
strategy:
Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/postgres-example.yml
Expand Up @@ -2,6 +2,10 @@ name: Postgres example pipeline

on: [push, pull_request]

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

jobs:
test-postgres:
strategy:
Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/pubsub-example.yml
Expand Up @@ -2,6 +2,10 @@ name: Pubsub example pipeline

on: [push, pull_request]

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

jobs:
test-pubsub:
strategy:
Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/pulsar-example.yml
Expand Up @@ -2,6 +2,10 @@ name: Pulsar example pipeline

on: [push, pull_request]

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

jobs:
test-pulsar:
strategy:
Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/redis-example.yml
Expand Up @@ -2,6 +2,10 @@ name: Redis example pipeline

on: [push, pull_request]

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

jobs:
test-redis:
strategy:
Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/spanner-example.yml
Expand Up @@ -2,6 +2,10 @@ name: Spanner example pipeline

on: [push, pull_request]

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

jobs:
test-spanner:
strategy:
Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/toxiproxy-example.yml
Expand Up @@ -2,6 +2,10 @@ name: Toxiproxy example pipeline

on: [push, pull_request]

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

jobs:
test-toxiproxy:
strategy:
Expand Down
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 @@ -1047,7 +1043,6 @@ func TestContainerCreationWaitsForLogContextTimeout(t *testing.T) {
}

func TestContainerCreationWaitsForLog(t *testing.T) {
// exposePorts {
ctx := context.Background()
req := ContainerRequest{
Image: mysqlImage,
Expand All @@ -1063,38 +1058,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 @@ -1387,11 +1353,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 @@ -1419,50 +1387,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 @@ -1808,7 +1732,9 @@ func ExampleContainer_Host() {
log.Fatalf("failed to terminate container: %s", err)
}
}()
// containerHost {
ip, _ := nginxC.Host(ctx)
// }
println(ip)
}

Expand Down Expand Up @@ -1865,9 +1791,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
4 changes: 4 additions & 0 deletions examples/_template/ci.yml.tmpl
Expand Up @@ -2,6 +2,10 @@

on: [push, pull_request]

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

jobs:
test-{{ $lower }}:
strategy:
Expand Down

0 comments on commit 5cd80f2

Please sign in to comment.