Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 809c7f0

Browse files
committed
Use dktest to run docker tests
- Leaving migrate/testing in case there are unknown consumers - Add migrate/dktesting package - Update tests to use migrate/dktesting instead of migrate/testing
1 parent fd47ba9 commit 809c7f0

File tree

12 files changed

+1019
-697
lines changed

12 files changed

+1019
-697
lines changed

Gopkg.lock

Lines changed: 53 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Gopkg.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,3 +100,7 @@
100100
[prune]
101101
go-tests = true
102102
unused-packages = true
103+
104+
[[constraint]]
105+
name = "github.com/dhui/dktest"
106+
version = "0.2.2"

database/cassandra/cassandra_test.go

Lines changed: 38 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,39 @@ import (
44
"fmt"
55
"strconv"
66
"testing"
7+
)
78

9+
import (
10+
"github.com/dhui/dktest"
811
"github.com/gocql/gocql"
12+
)
913

14+
import (
1015
dt "github.com/golang-migrate/migrate/v4/database/testing"
11-
mt "github.com/golang-migrate/migrate/v4/testing"
16+
"github.com/golang-migrate/migrate/v4/dktesting"
1217
)
1318

14-
var versions = []mt.Version{
15-
{Image: "cassandra:3.0.10"},
16-
{Image: "cassandra:3.0"},
17-
}
19+
var (
20+
opts = dktest.Options{PortRequired: true, ReadyFunc: isReady}
21+
specs = []dktesting.ContainerSpec{
22+
{ImageName: "cassandra:3.0.10", Options: opts},
23+
{ImageName: "cassandra:3.0", Options: opts},
24+
}
25+
)
1826

19-
func isReady(i mt.Instance) bool {
27+
func isReady(c dktest.ContainerInfo) bool {
2028
// Cassandra exposes 5 ports (7000, 7001, 7199, 9042 & 9160)
21-
// We only need the port bound to 9042, but we can only access to the first one
22-
// through 'i.Port()' (which calls DockerContainer.firstPortMapping())
23-
// So we need to get port mapping to retrieve correct port number bound to 9042
24-
portMap := i.NetworkSettings().Ports
25-
port, _ := strconv.Atoi(portMap["9042/tcp"][0].HostPort)
29+
// We only need the port bound to 9042
30+
ip, portStr, err := c.Port(9042)
31+
if err != nil {
32+
return false
33+
}
34+
port, err := strconv.Atoi(portStr)
35+
if err != nil {
36+
return false
37+
}
2638

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

4254
func Test(t *testing.T) {
43-
mt.ParallelTest(t, versions, isReady,
44-
func(t *testing.T, i mt.Instance) {
45-
p := &Cassandra{}
46-
portMap := i.NetworkSettings().Ports
47-
port, _ := strconv.Atoi(portMap["9042/tcp"][0].HostPort)
48-
addr := fmt.Sprintf("cassandra://%v:%v/testks", i.Host(), port)
49-
d, err := p.Open(addr)
50-
if err != nil {
51-
t.Fatalf("%v", err)
52-
}
53-
defer d.Close()
54-
dt.Test(t, d, []byte("SELECT table_name from system_schema.tables"))
55-
})
55+
dktesting.ParallelTest(t, specs, func(t *testing.T, c dktest.ContainerInfo) {
56+
ip, port, err := c.Port(9042)
57+
if err != nil {
58+
t.Fatal("Unable to get mapped port:", err)
59+
}
60+
addr := fmt.Sprintf("cassandra://%v:%v/testks", ip, port)
61+
p := &Cassandra{}
62+
d, err := p.Open(addr)
63+
if err != nil {
64+
t.Fatalf("%v", err)
65+
}
66+
defer d.Close()
67+
dt.Test(t, d, []byte("SELECT table_name from system_schema.tables"))
68+
})
5669
}

0 commit comments

Comments
 (0)