@@ -4,27 +4,39 @@ import (
4
4
"fmt"
5
5
"strconv"
6
6
"testing"
7
+ )
7
8
9
+ import (
10
+ "github.com/dhui/dktest"
8
11
"github.com/gocql/gocql"
12
+ )
9
13
14
+ import (
10
15
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 "
12
17
)
13
18
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
+ )
18
26
19
- func isReady (i mt. Instance ) bool {
27
+ func isReady (c dktest. ContainerInfo ) bool {
20
28
// 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
+ }
26
38
27
- cluster := gocql .NewCluster (i . Host () )
39
+ cluster := gocql .NewCluster (ip )
28
40
cluster .Port = port
29
41
cluster .Consistency = gocql .All
30
42
p , err := cluster .CreateSession ()
@@ -40,17 +52,18 @@ func isReady(i mt.Instance) bool {
40
52
}
41
53
42
54
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
+ })
56
69
}
0 commit comments