Skip to content

Commit fe93f24

Browse files
committed
Separate test command line to a package
1 parent 5c7adac commit fe93f24

8 files changed

+64
-89
lines changed

cassandra_test.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"context"
99
"errors"
1010
"fmt"
11+
"github.com/gocql/gocql/internal/testcmdline"
1112
"math"
1213
"math/big"
1314
"net"
@@ -2126,8 +2127,8 @@ func TestGetKeyspaceMetadata(t *testing.T) {
21262127
if err != nil {
21272128
t.Fatalf("Error converting string to int with err: %v", err)
21282129
}
2129-
if rfInt != *flagRF {
2130-
t.Errorf("Expected replication factor to be %d but was %d", *flagRF, rfInt)
2130+
if rfInt != *testcmdline.RF {
2131+
t.Errorf("Expected replication factor to be %d but was %d", *testcmdline.RF, rfInt)
21312132
}
21322133
}
21332134

@@ -2494,8 +2495,8 @@ func TestUnmarshallNestedTypes(t *testing.T) {
24942495
}
24952496

24962497
func TestSchemaReset(t *testing.T) {
2497-
if flagCassVersion.Major == 0 || flagCassVersion.Before(2, 1, 3) {
2498-
t.Skipf("skipping TestSchemaReset due to CASSANDRA-7910 in Cassandra <2.1.3 version=%v", flagCassVersion)
2498+
if testcmdline.CassVersion.Major == 0 || testcmdline.CassVersion.Before(2, 1, 3) {
2499+
t.Skipf("skipping TestSchemaReset due to CASSANDRA-7910 in Cassandra <2.1.3 version=%v", testcmdline.CassVersion)
24992500
}
25002501

25012502
cluster := createCluster()
@@ -2560,7 +2561,7 @@ func TestCreateSession_DontSwallowError(t *testing.T) {
25602561
t.Fatal("expected to get an error for unsupported protocol")
25612562
}
25622563

2563-
if flagCassVersion.Major < 3 {
2564+
if testcmdline.CassVersion.Major < 3 {
25642565
// TODO: we should get a distinct error type here which include the underlying
25652566
// cassandra error about the protocol version, for now check this here.
25662567
if !strings.Contains(err.Error(), "Invalid or unsupported protocol version") {

cloud_cluster_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"context"
99
"crypto/tls"
1010
"fmt"
11+
"github.com/gocql/gocql/internal/testcmdline"
1112
"io"
1213
"net"
1314
"os"
@@ -22,7 +23,7 @@ import (
2223
)
2324

2425
func TestCloudConnection(t *testing.T) {
25-
if !*gocql.FlagRunSslTest {
26+
if !*testcmdline.RunSslTest {
2627
t.Skip("Skipping because SSL is not enabled on cluster")
2728
}
2829

common_test.go

+20-39
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package gocql
22

33
import (
4-
"flag"
54
"fmt"
5+
"github.com/gocql/gocql/internal/testcmdline"
66
"log"
77
"net"
88
"reflect"
@@ -12,39 +12,20 @@ import (
1212
"time"
1313
)
1414

15-
var (
16-
flagCluster = flag.String("cluster", "127.0.0.1", "a comma-separated list of host:port tuples")
17-
flagMultiNodeCluster = flag.String("multiCluster", "127.0.0.2", "a comma-separated list of host:port tuples")
18-
flagProto = flag.Int("proto", 0, "protcol version")
19-
flagCQL = flag.String("cql", "3.0.0", "CQL version")
20-
flagRF = flag.Int("rf", 1, "replication factor for test keyspace")
21-
clusterSize = flag.Int("clusterSize", 1, "the expected size of the cluster")
22-
flagRetry = flag.Int("retries", 5, "number of times to retry queries")
23-
flagAutoWait = flag.Duration("autowait", 1000*time.Millisecond, "time to wait for autodiscovery to fill the hosts poll")
24-
flagRunSslTest = flag.Bool("runssl", false, "Set to true to run ssl test")
25-
flagRunAuthTest = flag.Bool("runauth", false, "Set to true to run authentication test")
26-
flagCompressTest = flag.String("compressor", "", "compressor to use")
27-
flagTimeout = flag.Duration("gocql.timeout", 5*time.Second, "sets the connection `timeout` for all operations")
28-
29-
flagCassVersion cassVersion
30-
)
31-
3215
func init() {
33-
flag.Var(&flagCassVersion, "gocql.cversion", "the cassandra version being tested against")
34-
3516
log.SetFlags(log.Lshortfile | log.LstdFlags)
3617
}
3718

3819
func getClusterHosts() []string {
39-
return strings.Split(*flagCluster, ",")
20+
return strings.Split(*testcmdline.Cluster, ",")
4021
}
4122

4223
func getMultiNodeClusterHosts() []string {
43-
return strings.Split(*flagMultiNodeCluster, ",")
24+
return strings.Split(*testcmdline.MultiNodeCluster, ",")
4425
}
4526

4627
func addSslOptions(cluster *ClusterConfig) *ClusterConfig {
47-
if *flagRunSslTest {
28+
if *testcmdline.RunSslTest {
4829
cluster.Port = 9142
4930
cluster.SslOpts = &SslOptions{
5031
CertPath: "testdata/pki/gocql.crt",
@@ -81,21 +62,21 @@ func createTable(s *Session, table string) error {
8162
func createCluster(opts ...func(*ClusterConfig)) *ClusterConfig {
8263
clusterHosts := getClusterHosts()
8364
cluster := NewCluster(clusterHosts...)
84-
cluster.ProtoVersion = *flagProto
85-
cluster.CQLVersion = *flagCQL
86-
cluster.Timeout = *flagTimeout
65+
cluster.ProtoVersion = *testcmdline.Proto
66+
cluster.CQLVersion = *testcmdline.CQL
67+
cluster.Timeout = *testcmdline.Timeout
8768
cluster.Consistency = Quorum
8869
cluster.MaxWaitSchemaAgreement = 2 * time.Minute // travis might be slow
89-
if *flagRetry > 0 {
90-
cluster.RetryPolicy = &SimpleRetryPolicy{NumRetries: *flagRetry}
70+
if *testcmdline.Retry > 0 {
71+
cluster.RetryPolicy = &SimpleRetryPolicy{NumRetries: *testcmdline.Retry}
9172
}
9273

93-
switch *flagCompressTest {
74+
switch *testcmdline.CompressTest {
9475
case "snappy":
9576
cluster.Compressor = &SnappyCompressor{}
9677
case "":
9778
default:
98-
panic("invalid compressor: " + *flagCompressTest)
79+
panic("invalid compressor: " + *testcmdline.CompressTest)
9980
}
10081

10182
cluster = addSslOptions(cluster)
@@ -110,21 +91,21 @@ func createCluster(opts ...func(*ClusterConfig)) *ClusterConfig {
11091
func createMultiNodeCluster(opts ...func(*ClusterConfig)) *ClusterConfig {
11192
clusterHosts := getMultiNodeClusterHosts()
11293
cluster := NewCluster(clusterHosts...)
113-
cluster.ProtoVersion = *flagProto
114-
cluster.CQLVersion = *flagCQL
115-
cluster.Timeout = *flagTimeout
94+
cluster.ProtoVersion = *testcmdline.Proto
95+
cluster.CQLVersion = *testcmdline.CQL
96+
cluster.Timeout = *testcmdline.Timeout
11697
cluster.Consistency = Quorum
11798
cluster.MaxWaitSchemaAgreement = 2 * time.Minute // travis might be slow
118-
if *flagRetry > 0 {
119-
cluster.RetryPolicy = &SimpleRetryPolicy{NumRetries: *flagRetry}
99+
if *testcmdline.Retry > 0 {
100+
cluster.RetryPolicy = &SimpleRetryPolicy{NumRetries: *testcmdline.Retry}
120101
}
121102

122-
switch *flagCompressTest {
103+
switch *testcmdline.CompressTest {
123104
case "snappy":
124105
cluster.Compressor = &SnappyCompressor{}
125106
case "":
126107
default:
127-
panic("invalid compressor: " + *flagCompressTest)
108+
panic("invalid compressor: " + *testcmdline.CompressTest)
128109
}
129110

130111
cluster = addSslOptions(cluster)
@@ -156,7 +137,7 @@ func createKeyspace(tb testing.TB, cluster *ClusterConfig, keyspace string) {
156137
WITH replication = {
157138
'class' : 'SimpleStrategy',
158139
'replication_factor' : %d
159-
}`, keyspace, *flagRF))
140+
}`, keyspace, *testcmdline.RF))
160141

161142
if err != nil {
162143
panic(fmt.Sprintf("unable to create keyspace: %v", err))
@@ -232,7 +213,7 @@ func createViews(t *testing.T, session *Session) {
232213
}
233214

234215
func createMaterializedViews(t *testing.T, session *Session) {
235-
if flagCassVersion.Before(3, 0, 0) {
216+
if testcmdline.CassVersion.Before(3, 0, 0) {
236217
return
237218
}
238219
if err := session.Query(`CREATE TABLE IF NOT EXISTS gocql_test.view_table (

export_test.go

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
package gocql
55

6-
var FlagRunSslTest = flagRunSslTest
76
var CreateCluster = createCluster
87
var TestLogger = &testLogger{}
98
var WaitUntilPoolsStopFilling = waitUntilPoolsStopFilling

integration.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ function scylla_down() {
2121
}
2222

2323
function scylla_restart() {
24-
scylla_down
24+
# scylla_down
2525
scylla_up
2626
}
2727

integration_test.go

+8-6
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,18 @@ import (
99
"reflect"
1010
"testing"
1111
"time"
12+
13+
"github.com/gocql/gocql/internal/testcmdline"
1214
)
1315

1416
// TestAuthentication verifies that gocql will work with a host configured to only accept authenticated connections
1517
func TestAuthentication(t *testing.T) {
1618

17-
if *flagProto < 2 {
19+
if *testcmdline.Proto < 2 {
1820
t.Skip("Authentication is not supported with protocol < 2")
1921
}
2022

21-
if !*flagRunAuthTest {
23+
if !*testcmdline.RunAuthTest {
2224
t.Skip("Authentication is not configured in the target cluster")
2325
}
2426

@@ -60,21 +62,21 @@ func TestRingDiscovery(t *testing.T) {
6062
session := createSessionFromCluster(cluster, t)
6163
defer session.Close()
6264

63-
if *clusterSize > 1 {
65+
if *testcmdline.ClusterSize > 1 {
6466
// wait for autodiscovery to update the pool with the list of known hosts
65-
time.Sleep(*flagAutoWait)
67+
time.Sleep(*testcmdline.AutoWait)
6668
}
6769

6870
session.pool.mu.RLock()
6971
defer session.pool.mu.RUnlock()
7072
size := len(session.pool.hostConnPools)
7173

72-
if *clusterSize != size {
74+
if *testcmdline.ClusterSize != size {
7375
for p, pool := range session.pool.hostConnPools {
7476
t.Logf("p=%q host=%v ips=%s", p, pool.host, pool.host.ConnectAddress().String())
7577

7678
}
77-
t.Errorf("Expected a cluster size of %d, but actual size was %d", *clusterSize, size)
79+
t.Errorf("Expected a cluster size of %d, but actual size was %d", *testcmdline.ClusterSize, size)
7880
}
7981
}
8082

internal/testutils/flags.go internal/testcmdline/flags.go

+16-26
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,27 @@
1-
package testutils
1+
package testcmdline
22

33
import (
44
"flag"
55
"fmt"
6-
"log"
76
"strconv"
87
"strings"
98
"time"
10-
11-
"github.com/gocql/gocql"
129
)
1310

1411
var (
15-
flagCluster = flag.String("cluster", "127.0.0.1", "a comma-separated list of host:port tuples")
16-
flagMultiNodeCluster = flag.String("multiCluster", "127.0.0.2", "a comma-separated list of host:port tuples")
17-
flagProto = flag.Int("proto", 0, "protcol version")
18-
flagCQL = flag.String("cql", "3.0.0", "CQL version")
19-
flagRF = flag.Int("rf", 1, "replication factor for test keyspace")
20-
clusterSize = flag.Int("clusterSize", 1, "the expected size of the cluster")
21-
flagRetry = flag.Int("retries", 5, "number of times to retry queries")
22-
flagAutoWait = flag.Duration("autowait", 1000*time.Millisecond, "time to wait for autodiscovery to fill the hosts poll")
23-
flagRunSslTest = flag.Bool("runssl", false, "Set to true to run ssl test")
24-
flagRunAuthTest = flag.Bool("runauth", false, "Set to true to run authentication test")
25-
flagCompressTest = flag.String("compressor", "", "compressor to use")
26-
flagTimeout = flag.Duration("gocql.timeout", 5*time.Second, "sets the connection `timeout` for all operations")
27-
28-
flagCassVersion cassVersion
12+
Cluster = flag.String("cluster", "127.0.0.1", "a comma-separated list of host:port tuples")
13+
MultiNodeCluster = flag.String("multiCluster", "127.0.0.2", "a comma-separated list of host:port tuples")
14+
Proto = flag.Int("proto", 0, "protcol version")
15+
CQL = flag.String("cql", "3.0.0", "CQL version")
16+
RF = flag.Int("rf", 1, "replication factor for test keyspace")
17+
ClusterSize = flag.Int("clusterSize", 1, "the expected size of the cluster")
18+
Retry = flag.Int("retries", 5, "number of times to retry queries")
19+
AutoWait = flag.Duration("autowait", 1000*time.Millisecond, "time to wait for autodiscovery to fill the hosts poll")
20+
RunSslTest = flag.Bool("runssl", false, "Set to true to run ssl test")
21+
RunAuthTest = flag.Bool("runauth", false, "Set to true to run authentication test")
22+
CompressTest = flag.String("compressor", "", "compressor to use")
23+
Timeout = flag.Duration("gocql.timeout", 5*time.Second, "sets the connection `timeout` for all operations")
24+
CassVersion cassVersion
2925
)
3026

3127
type cassVersion struct {
@@ -37,11 +33,7 @@ func (c *cassVersion) Set(v string) error {
3733
return nil
3834
}
3935

40-
return c.UnmarshalCQL(nil, []byte(v))
41-
}
42-
43-
func (c *cassVersion) UnmarshalCQL(info gocql.TypeInfo, data []byte) error {
44-
return c.unmarshal(data)
36+
return c.unmarshal([]byte(v))
4537
}
4638

4739
func (c *cassVersion) unmarshal(data []byte) error {
@@ -108,7 +100,5 @@ func (c cassVersion) nodeUpDelay() time.Duration {
108100
}
109101

110102
func init() {
111-
flag.Var(&flagCassVersion, "gocql.cversion", "the cassandra version being tested against")
112-
113-
log.SetFlags(log.Lshortfile | log.LstdFlags)
103+
flag.Var(&CassVersion, "gocql.cversion", "the cassandra version being tested against")
114104
}

internal/testutils/cluster.go

+11-10
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package testutils
33
import (
44
"context"
55
"fmt"
6+
"github.com/gocql/gocql/internal/testcmdline"
67
"log"
78
"strings"
89
"sync"
@@ -22,21 +23,21 @@ func CreateSession(tb testing.TB, opts ...func(config *gocql.ClusterConfig)) *go
2223
func CreateCluster(opts ...func(*gocql.ClusterConfig)) *gocql.ClusterConfig {
2324
clusterHosts := getClusterHosts()
2425
cluster := gocql.NewCluster(clusterHosts...)
25-
cluster.ProtoVersion = *flagProto
26-
cluster.CQLVersion = *flagCQL
27-
cluster.Timeout = *flagTimeout
26+
cluster.ProtoVersion = *testcmdline.Proto
27+
cluster.CQLVersion = *testcmdline.CQL
28+
cluster.Timeout = *testcmdline.Timeout
2829
cluster.Consistency = gocql.Quorum
2930
cluster.MaxWaitSchemaAgreement = 2 * time.Minute // travis might be slow
30-
if *flagRetry > 0 {
31-
cluster.RetryPolicy = &gocql.SimpleRetryPolicy{NumRetries: *flagRetry}
31+
if *testcmdline.Retry > 0 {
32+
cluster.RetryPolicy = &gocql.SimpleRetryPolicy{NumRetries: *testcmdline.Retry}
3233
}
3334

34-
switch *flagCompressTest {
35+
switch *testcmdline.CompressTest {
3536
case "snappy":
3637
cluster.Compressor = &gocql.SnappyCompressor{}
3738
case "":
3839
default:
39-
panic("invalid compressor: " + *flagCompressTest)
40+
panic("invalid compressor: " + *testcmdline.CompressTest)
4041
}
4142

4243
cluster = addSslOptions(cluster)
@@ -69,7 +70,7 @@ func createSessionFromCluster(cluster *gocql.ClusterConfig, tb testing.TB) *gocq
6970
}
7071

7172
func getClusterHosts() []string {
72-
return strings.Split(*flagCluster, ",")
73+
return strings.Split(*testcmdline.Cluster, ",")
7374
}
7475

7576
func createKeyspace(tb testing.TB, cluster *gocql.ClusterConfig, keyspace string) {
@@ -92,7 +93,7 @@ func createKeyspace(tb testing.TB, cluster *gocql.ClusterConfig, keyspace string
9293
WITH replication = {
9394
'class' : 'SimpleStrategy',
9495
'replication_factor' : %d
95-
}`, keyspace, *flagRF))
96+
}`, keyspace, *testcmdline.RF))
9697

9798
if err != nil {
9899
panic(fmt.Sprintf("unable to create keyspace: %v", err))
@@ -120,7 +121,7 @@ func CreateTable(s *gocql.Session, table string) error {
120121
}
121122

122123
func addSslOptions(cluster *gocql.ClusterConfig) *gocql.ClusterConfig {
123-
if *flagRunSslTest {
124+
if *testcmdline.RunSslTest {
124125
cluster.Port = 9142
125126
cluster.SslOpts = &gocql.SslOptions{
126127
CertPath: "testdata/pki/gocql.crt",

0 commit comments

Comments
 (0)