Переглянути джерело

Introduce an integration test for authentication

Ben Hood 10 роки тому
батько
коміт
0e0460fac7
3 змінених файлів з 62 додано та 18 видалено
  1. 5 4
      .travis.yml
  2. 28 0
      cassandra_test.go
  3. 29 14
      integration.sh

+ 5 - 4
.travis.yml

@@ -13,9 +13,10 @@ env:
   global:
     - GOMAXPROCS=2
   matrix:
-    - CASS=1.2.19
-    - CASS=2.0.14
-    - CASS=2.1.4
+    - CASS=1.2.19 AUTH=false
+    - CASS=2.0.14 AUTH=false
+    - CASS=2.1.4  AUTH=false
+    - CASS=2.1.4  AUTH=true
 
 go:
   - 1.3
@@ -34,7 +35,7 @@ install:
 script:
   - set -e
   - go test -v -tags unit
-  - PATH=$PATH:$HOME/.local/bin bash -x integration.sh $CASS
+  - PATH=$PATH:$HOME/.local/bin bash -x integration.sh $CASS $AUTH
   - go vet .
 
 notifications:

+ 28 - 0
cassandra_test.go

@@ -30,6 +30,7 @@ var (
 	flagRetry        = flag.Int("retries", 5, "number of times to retry queries")
 	flagAutoWait     = flag.Duration("autowait", 1000*time.Millisecond, "time to wait for autodiscovery to fill the hosts poll")
 	flagRunSslTest   = flag.Bool("runssl", false, "Set to true to run ssl test")
+	flagRunAuthTest  = flag.Bool("runauth", false, "Set to true to run authentication test")
 	flagCompressTest = flag.String("compressor", "", "compressor to use")
 	clusterHosts     []string
 )
@@ -122,6 +123,33 @@ func createSession(tb testing.TB) *Session {
 	return session
 }
 
+// TestAuthentication verifies that gocql will work with a host configured to only accept authenticated connections
+func TestAuthentication(t *testing.T) {
+
+	if *flagProto < 2 {
+		t.Skip("Authentication is not supported with protocol < 2")
+	}
+
+	if !*flagRunAuthTest {
+		t.Skip("Authentication is not configured in the target cluster")
+	}
+
+	cluster := createCluster()
+
+	cluster.Authenticator = PasswordAuthenticator{
+		Username: "cassandra",
+		Password: "cassandra",
+	}
+
+	session, err := cluster.CreateSession()
+
+	if err != nil {
+		t.Fatalf("Authentication error: %s", err)
+	}
+
+	session.Close()
+}
+
 //TestRingDiscovery makes sure that you can autodiscover other cluster members when you seed a cluster config with just one node
 func TestRingDiscovery(t *testing.T) {
 	cluster := createCluster()

+ 29 - 14
integration.sh

@@ -5,6 +5,11 @@ set -e
 function run_tests() {
 	local clusterSize=3
 	local version=$1
+	local auth=$2
+
+	if [ "$auth" = true ]; then
+		clusterSize=1
+	fi
 
 	local keypath="$(pwd)/testdata/pki"
 
@@ -40,24 +45,34 @@ function run_tests() {
 		proto=3
 	fi
 
-	go test -timeout 5m -tags integration -cover -v -runssl -proto=$proto -rf=3 -cluster=$(ccm liveset) -clusterSize=$clusterSize -autowait=2000ms -compressor=snappy ./... | tee results.txt
+	if [ "$auth" = true ]
+	then
+    	ccm node1 cqlsh --version
+    	sleep 30s
+		echo "list users;" | SSL_VALIDATE=false SSL_CERTFILE=$keypath/cassandra.crt ccm node1 cqlsh --ssl -u cassandra -p cassandra
+    	go test -v . -timeout 15s -run=TestAuthentication -tags integration -runssl -runauth -proto=$proto -cluster=$(ccm liveset) -clusterSize=$clusterSize -autowait=1000ms
+	else
 
-	if [ ${PIPESTATUS[0]} -ne 0 ]; then
-		echo "--- FAIL: ccm status follows:"
-		ccm status
-		ccm node1 nodetool status
-		ccm node1 showlog > status.log
-		cat status.log
-		echo "--- FAIL: Received a non-zero exit code from the go test execution, please investigate this"
-		exit 1
-	fi
+		go test -timeout 5m -tags integration -cover -v -runssl -proto=$proto -rf=3 -cluster=$(ccm liveset) -clusterSize=$clusterSize -autowait=2000ms -compressor=snappy ./... | tee results.txt
 
-	cover=`cat results.txt | grep coverage: | grep -o "[0-9]\{1,3\}" | head -n 1`
+		if [ ${PIPESTATUS[0]} -ne 0 ]; then
+			echo "--- FAIL: ccm status follows:"
+			ccm status
+			ccm node1 nodetool status
+			ccm node1 showlog > status.log
+			cat status.log
+			echo "--- FAIL: Received a non-zero exit code from the go test execution, please investigate this"
+			exit 1
+		fi
 
-	if [[ $cover -lt "55" ]]; then
-		echo "--- FAIL: expected coverage of at least 60 %, but coverage was $cover %"
-		exit 1
+		cover=`cat results.txt | grep coverage: | grep -o "[0-9]\{1,3\}" | head -n 1`
+
+		if [[ $cover -lt "55" ]]; then
+			echo "--- FAIL: expected coverage of at least 60 %, but coverage was $cover %"
+			exit 1
+		fi
 	fi
+
 	ccm remove
 }