Skip to content
This repository has been archived by the owner on Apr 16, 2024. It is now read-only.

Commit

Permalink
Use dktest to run docker tests
Browse files Browse the repository at this point in the history
    - Leaving migrate/testing in case there are unknown consumers
    - Add migrate/dktesting package
    - Update tests to use migrate/dktesting instead of migrate/testing
  • Loading branch information
dhui committed Jan 8, 2019
1 parent fd47ba9 commit 809c7f0
Show file tree
Hide file tree
Showing 12 changed files with 1,019 additions and 697 deletions.
57 changes: 53 additions & 4 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,7 @@
[prune]
go-tests = true
unused-packages = true

[[constraint]]
name = "github.com/dhui/dktest"
version = "0.2.2"
63 changes: 38 additions & 25 deletions database/cassandra/cassandra_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,39 @@ import (
"fmt"
"strconv"
"testing"
)

import (
"github.com/dhui/dktest"
"github.com/gocql/gocql"
)

import (
dt "github.com/golang-migrate/migrate/v4/database/testing"
mt "github.com/golang-migrate/migrate/v4/testing"
"github.com/golang-migrate/migrate/v4/dktesting"
)

var versions = []mt.Version{
{Image: "cassandra:3.0.10"},
{Image: "cassandra:3.0"},
}
var (
opts = dktest.Options{PortRequired: true, ReadyFunc: isReady}
specs = []dktesting.ContainerSpec{
{ImageName: "cassandra:3.0.10", Options: opts},
{ImageName: "cassandra:3.0", Options: opts},
}
)

func isReady(i mt.Instance) bool {
func isReady(c dktest.ContainerInfo) bool {
// Cassandra exposes 5 ports (7000, 7001, 7199, 9042 & 9160)
// We only need the port bound to 9042, but we can only access to the first one
// through 'i.Port()' (which calls DockerContainer.firstPortMapping())
// So we need to get port mapping to retrieve correct port number bound to 9042
portMap := i.NetworkSettings().Ports
port, _ := strconv.Atoi(portMap["9042/tcp"][0].HostPort)
// We only need the port bound to 9042
ip, portStr, err := c.Port(9042)
if err != nil {
return false
}
port, err := strconv.Atoi(portStr)
if err != nil {
return false
}

cluster := gocql.NewCluster(i.Host())
cluster := gocql.NewCluster(ip)
cluster.Port = port
cluster.Consistency = gocql.All
p, err := cluster.CreateSession()
Expand All @@ -40,17 +52,18 @@ func isReady(i mt.Instance) bool {
}

func Test(t *testing.T) {
mt.ParallelTest(t, versions, isReady,
func(t *testing.T, i mt.Instance) {
p := &Cassandra{}
portMap := i.NetworkSettings().Ports
port, _ := strconv.Atoi(portMap["9042/tcp"][0].HostPort)
addr := fmt.Sprintf("cassandra://%v:%v/testks", i.Host(), port)
d, err := p.Open(addr)
if err != nil {
t.Fatalf("%v", err)
}
defer d.Close()
dt.Test(t, d, []byte("SELECT table_name from system_schema.tables"))
})
dktesting.ParallelTest(t, specs, func(t *testing.T, c dktest.ContainerInfo) {
ip, port, err := c.Port(9042)
if err != nil {
t.Fatal("Unable to get mapped port:", err)
}
addr := fmt.Sprintf("cassandra://%v:%v/testks", ip, port)
p := &Cassandra{}
d, err := p.Open(addr)
if err != nil {
t.Fatalf("%v", err)
}
defer d.Close()
dt.Test(t, d, []byte("SELECT table_name from system_schema.tables"))
})
}
Loading

0 comments on commit 809c7f0

Please sign in to comment.