This repository has been archived by the owner on Dec 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
cql_test.go
135 lines (123 loc) · 4.3 KB
/
cql_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package cql
import (
"database/sql/driver"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"testing"
"time"
)
var (
TestLogStderr = log.New(os.Stderr, "cql ", log.Ldate|log.Ltime|log.LUTC|log.Lshortfile)
TestHostValid string
TestHostInvalid string
ConnectTimeoutValidString string
ConnectTimeoutValid time.Duration
ConnectTimeoutInvalid time.Duration
TimeoutValidString string
TimeoutValid time.Duration
DisableDestructiveTests bool
KeyspaceName = "cqltest"
TableName = "cqltest_"
EnableAuthentication bool
Username string
Password string
TestTimeNow time.Time
)
func TestMain(m *testing.M) {
code := setupForTesting()
if code != 0 {
os.Exit(code)
}
code = m.Run()
os.Exit(code)
}
func setupForTesting() int {
flag.StringVar(&TestHostValid, "hostValid", "127.0.0.1", "a host where a Cassandra database is running")
flag.StringVar(&TestHostInvalid, "hostInvalid", "169.254.200.200", "a host where a Cassandra database is not running")
flag.StringVar(&ConnectTimeoutValidString, "connectTimeoutValid", "20s", "the connect timeout time duration for host valid tests (ClusterConfig.ConnectTimeout)")
connectTimeoutInvalidString := flag.String("connectTimeoutInvalid", "1ms", "the connect timeout time duration for host invalid tests (ClusterConfig.ConnectTimeout)")
flag.StringVar(&TimeoutValidString, "timeoutValid", "10s", "the timeout time duration for host valid tests (ClusterConfig.Timeout)")
flag.BoolVar(&DisableDestructiveTests, "disableDestructiveTests", false, "set to disable the destructive database tests on cqltest keyspace")
flag.BoolVar(&EnableAuthentication, "enableAuthentication", false, "set to enable authentication when database requires username and password")
flag.StringVar(&Username, "username", "cassandra", "the username to use when database requires username and password")
flag.StringVar(&Password, "password", "cassandra", "the password to use when database requires username and password")
flag.Parse()
var err error
ConnectTimeoutValid, err = time.ParseDuration(ConnectTimeoutValidString)
if err != nil {
fmt.Println("connectTimeoutValid ParseDuration error:", err)
return 2
}
ConnectTimeoutInvalid, err = time.ParseDuration(*connectTimeoutInvalidString)
if err != nil {
fmt.Println("connectTimeoutInvalid ParseDuration error:", err)
return 4
}
TimeoutValid, err = time.ParseDuration(TimeoutValidString)
if err != nil {
fmt.Println("timeoutValid ParseDuration error:", err)
return 6
}
TestTimeNow = time.Now().UTC().Truncate(time.Millisecond)
TableName += TestTimeNow.Format("20060102150405")
return 0
}
func TestDriverOpen(t *testing.T) {
CqlDriver.Logger = nil
conn, err := CqlDriver.Open("")
if err != nil {
t.Fatalf("Open error - received: %v - expected: %v ", err, nil)
}
if conn == nil {
t.Fatal("conn is nil")
}
CqlDriver.Logger = TestLogStderr
conn, err = CqlDriver.Open("")
if err != nil {
t.Fatalf("Open error - received: %v - expected: %v ", err, nil)
}
if conn == nil {
t.Fatal("conn is nil")
}
conn, err = CqlDriver.Open("?blah")
expectedError := "ConfigStringToClusterConfig error: missing ="
if err == nil || err.Error() != expectedError {
t.Fatalf("Open error - received: %v - expected: %v ", err, expectedError)
}
if conn != nil {
t.Fatalf("Open conn - received: %v - expected: %v ", conn, nil)
}
}
func testGetConnectionHostValid(t *testing.T) driver.Conn {
openString := TestHostValid
if EnableAuthentication {
openString += "?username=" + Username + "&password=" + Password
}
conn, err := CqlDriver.Open(openString)
if err != nil {
t.Fatalf("Open error - received: %v - expected: %v ", err, nil)
}
if conn == nil {
t.Fatal("conn is nil")
}
cqlConn := conn.(*cqlConnStruct)
cqlConn.clusterConfig.ConnectTimeout = ConnectTimeoutValid
cqlConn.clusterConfig.Timeout = TimeoutValid
return conn
}
func testGetConnectionHostInvalid(t *testing.T) driver.Conn {
conn, err := CqlDriver.Open(TestHostInvalid)
if err != nil {
t.Fatalf("Open error - received: %v - expected: %v ", err, nil)
}
if conn == nil {
t.Fatal("conn is nil")
}
cqlConn := conn.(*cqlConnStruct)
cqlConn.clusterConfig.ConnectTimeout = ConnectTimeoutInvalid
cqlConn.logger = log.New(ioutil.Discard, "", 0)
return conn
}