From fa8dbda31cc1d61d1a8a6f1f83461b40833cecd4 Mon Sep 17 00:00:00 2001 From: Jaume Marhuenda Date: Fri, 19 Oct 2018 15:02:42 -0700 Subject: [PATCH] run TestWriteFailure against all versions (#1203) * run TestWriteFailure against all versions * Build independently tests that need environment variables * Move ccm dependant tests under integration tag, add cassandra build tag, remove unit build tag --- batch_test.go | 2 +- cass1batch_test.go | 2 +- cassandra_test.go | 131 +----------------------------- compressor_test.go | 2 - errors_test.go | 2 +- host_source_test.go | 13 +-- integration.sh | 1 + integration_test.go | 188 ++++++++++++++++++++++++++++++++++++++++++++ marshal_test.go | 2 +- session_test.go | 2 +- stress_test.go | 2 +- udt_test.go | 2 +- wiki_test.go | 2 +- 13 files changed, 199 insertions(+), 152 deletions(-) create mode 100644 integration_test.go diff --git a/batch_test.go b/batch_test.go index 0ebfe1d52..df98b13e2 100644 --- a/batch_test.go +++ b/batch_test.go @@ -1,4 +1,4 @@ -// +build all integration +// +build all cassandra package gocql diff --git a/cass1batch_test.go b/cass1batch_test.go index f5f5c619d..ff0672a7d 100644 --- a/cass1batch_test.go +++ b/cass1batch_test.go @@ -1,4 +1,4 @@ -// +build all integration +// +build all cassandra package gocql diff --git a/cassandra_test.go b/cassandra_test.go index 6d7696ad6..023a809ca 100644 --- a/cassandra_test.go +++ b/cassandra_test.go @@ -1,4 +1,4 @@ -// +build all integration +// +build all cassandra package gocql @@ -22,59 +22,6 @@ import ( inf "gopkg.in/inf.v0" ) -// 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() - cluster.Hosts = clusterHosts[:1] - - session := createSessionFromCluster(cluster, t) - defer session.Close() - - if *clusterSize > 1 { - // wait for autodiscovery to update the pool with the list of known hosts - time.Sleep(*flagAutoWait) - } - - session.pool.mu.RLock() - defer session.pool.mu.RUnlock() - size := len(session.pool.hostConnPools) - - if *clusterSize != size { - for p, pool := range session.pool.hostConnPools { - t.Logf("p=%q host=%v ips=%s", p, pool.host, pool.host.ConnectAddress().String()) - - } - t.Errorf("Expected a cluster size of %d, but actual size was %d", *clusterSize, size) - } -} - func TestEmptyHosts(t *testing.T) { cluster := createCluster() cluster.Hosts = nil @@ -2467,64 +2414,6 @@ func TestTokenAwareConnPool(t *testing.T) { // TODO add verification that the query went to the correct host } -func TestCustomPayloadMessages(t *testing.T) { - cluster := createCluster() - session := createSessionFromCluster(cluster, t) - defer session.Close() - - if err := createTable(session, "CREATE TABLE gocql_test.testCustomPayloadMessages (id int, value int, PRIMARY KEY (id))"); err != nil { - t.Fatal(err) - } - - // QueryMessage - var customPayload = map[string][]byte{"a": []byte{10, 20}, "b": []byte{20, 30}} - query := session.Query("SELECT id FROM testCustomPayloadMessages where id = ?", 42).Consistency(One).CustomPayload(customPayload) - iter := query.Iter() - rCustomPayload := iter.GetCustomPayload() - if !reflect.DeepEqual(customPayload, rCustomPayload) { - t.Fatal("The received custom payload should match the sent") - } - iter.Close() - - // Insert query - query = session.Query("INSERT INTO testCustomPayloadMessages(id,value) VALUES(1, 1)").Consistency(One).CustomPayload(customPayload) - iter = query.Iter() - rCustomPayload = iter.GetCustomPayload() - if !reflect.DeepEqual(customPayload, rCustomPayload) { - t.Fatal("The received custom payload should match the sent") - } - iter.Close() - - // Batch Message - b := session.NewBatch(LoggedBatch) - b.CustomPayload = customPayload - b.Query("INSERT INTO testCustomPayloadMessages(id,value) VALUES(1, 1)") - if err := session.ExecuteBatch(b); err != nil { - t.Fatalf("query failed. %v", err) - } -} - -func TestCustomPayloadValues(t *testing.T) { - cluster := createCluster() - session := createSessionFromCluster(cluster, t) - defer session.Close() - - if err := createTable(session, "CREATE TABLE gocql_test.testCustomPayloadValues (id int, value int, PRIMARY KEY (id))"); err != nil { - t.Fatal(err) - } - - values := []map[string][]byte{map[string][]byte{"a": []byte{10, 20}, "b": []byte{20, 30}}, nil, map[string][]byte{"a": []byte{10, 20}, "b": nil}} - - for _, customPayload := range values { - query := session.Query("SELECT id FROM testCustomPayloadValues where id = ?", 42).Consistency(One).CustomPayload(customPayload) - iter := query.Iter() - rCustomPayload := iter.GetCustomPayload() - if !reflect.DeepEqual(customPayload, rCustomPayload) { - t.Fatal("The received custom payload should match the sent") - } - } -} - func TestNegativeStream(t *testing.T) { session := createSession(t) defer session.Close() @@ -2703,24 +2592,6 @@ func TestJSONSupport(t *testing.T) { } } -func TestUDF(t *testing.T) { - session := createSession(t) - defer session.Close() - - if session.cfg.ProtoVersion < 4 { - t.Skip("skipping UDF support on proto < 4") - } - - const query = `CREATE OR REPLACE FUNCTION uniq(state set, val text) - CALLED ON NULL INPUT RETURNS set LANGUAGE java - AS 'state.add(val); return state;'` - - err := session.Query(query).Exec() - if err != nil { - t.Fatal(err) - } -} - func TestDiscoverViaProxy(t *testing.T) { // This (complicated) test tests that when the driver is given an initial host // that is infact a proxy it discovers the rest of the ring behind the proxy diff --git a/compressor_test.go b/compressor_test.go index cbf16a468..835093932 100644 --- a/compressor_test.go +++ b/compressor_test.go @@ -1,5 +1,3 @@ -// +build all unit - package gocql import ( diff --git a/errors_test.go b/errors_test.go index b774d8a34..1214ade51 100644 --- a/errors_test.go +++ b/errors_test.go @@ -1,4 +1,4 @@ -// +build all integration +// +build all cassandra package gocql diff --git a/host_source_test.go b/host_source_test.go index e0e89e5d9..44c66b127 100644 --- a/host_source_test.go +++ b/host_source_test.go @@ -1,4 +1,4 @@ -// +build all integration +// +build all cassandra package gocql @@ -68,17 +68,6 @@ func TestIsValidPeer(t *testing.T) { } } -func TestGetHosts(t *testing.T) { - cluster := createCluster() - session := createSessionFromCluster(cluster, t) - - hosts, partitioner, err := session.hostSource.GetHosts() - - assertTrue(t, "err == nil", err == nil) - assertEqual(t, "len(hosts)", len(clusterHosts), len(hosts)) - assertTrue(t, "len(partitioner) != 0", len(partitioner) != 0) -} - func TestHostInfo_ConnectAddress(t *testing.T) { var localhost = net.IPv4(127, 0, 0, 1) tests := []struct { diff --git a/integration.sh b/integration.sh index 7a72020d7..6e49f8445 100755 --- a/integration.sh +++ b/integration.sh @@ -74,6 +74,7 @@ function run_tests() { go test -run=TestAuthentication -tags "integration gocql_debug" -timeout=15s -runauth $args else sleep 1s + go test -tags "cassandra gocql_debug" -timeout=5m -race $args go test -tags "integration gocql_debug" -timeout=5m -race $args ccm clear diff --git a/integration_test.go b/integration_test.go new file mode 100644 index 000000000..5fab2a7fa --- /dev/null +++ b/integration_test.go @@ -0,0 +1,188 @@ +// +build all integration + +package gocql + +// This file groups integration tests where Cassandra has to be set up with some special integration variables +import ( + "reflect" + "testing" + "time" +) + +// 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() +} + +func TestGetHosts(t *testing.T) { + cluster := createCluster() + session := createSessionFromCluster(cluster, t) + + hosts, partitioner, err := session.hostSource.GetHosts() + + assertTrue(t, "err == nil", err == nil) + assertEqual(t, "len(hosts)", len(clusterHosts), len(hosts)) + assertTrue(t, "len(partitioner) != 0", len(partitioner) != 0) +} + +//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() + cluster.Hosts = clusterHosts[:1] + + session := createSessionFromCluster(cluster, t) + defer session.Close() + + if *clusterSize > 1 { + // wait for autodiscovery to update the pool with the list of known hosts + time.Sleep(*flagAutoWait) + } + + session.pool.mu.RLock() + defer session.pool.mu.RUnlock() + size := len(session.pool.hostConnPools) + + if *clusterSize != size { + for p, pool := range session.pool.hostConnPools { + t.Logf("p=%q host=%v ips=%s", p, pool.host, pool.host.ConnectAddress().String()) + + } + t.Errorf("Expected a cluster size of %d, but actual size was %d", *clusterSize, size) + } +} + +func TestWriteFailure(t *testing.T) { + cluster := createCluster() + createKeyspace(t, cluster, "test") + cluster.Keyspace = "test" + session, err := cluster.CreateSession() + if err != nil { + t.Fatal("create session:", err) + } + defer session.Close() + + if err := createTable(session, "CREATE TABLE test.test (id int,value int,PRIMARY KEY (id))"); err != nil { + t.Fatalf("failed to create table with error '%v'", err) + } + if err := session.Query(`INSERT INTO test.test (id, value) VALUES (1, 1)`).Exec(); err != nil { + errWrite, ok := err.(*RequestErrWriteFailure) + if ok { + if session.cfg.ProtoVersion >= 5 { + // ErrorMap should be filled with some hosts that should've errored + if len(errWrite.ErrorMap) == 0 { + t.Fatal("errWrite.ErrorMap should have some failed hosts but it didn't have any") + } + } else { + // Map doesn't get filled for V4 + if len(errWrite.ErrorMap) != 0 { + t.Fatal("errWrite.ErrorMap should have length 0, it's: ", len(errWrite.ErrorMap)) + } + } + } else { + t.Fatal("error should be RequestErrWriteFailure, it's: ", errWrite) + } + } else { + t.Fatal("a write fail error should have happened when querying test keyspace") + } + + if err = session.Query("DROP KEYSPACE test").Exec(); err != nil { + t.Fatal(err) + } +} + +func TestCustomPayloadMessages(t *testing.T) { + cluster := createCluster() + session := createSessionFromCluster(cluster, t) + defer session.Close() + + if err := createTable(session, "CREATE TABLE gocql_test.testCustomPayloadMessages (id int, value int, PRIMARY KEY (id))"); err != nil { + t.Fatal(err) + } + + // QueryMessage + var customPayload = map[string][]byte{"a": []byte{10, 20}, "b": []byte{20, 30}} + query := session.Query("SELECT id FROM testCustomPayloadMessages where id = ?", 42).Consistency(One).CustomPayload(customPayload) + iter := query.Iter() + rCustomPayload := iter.GetCustomPayload() + if !reflect.DeepEqual(customPayload, rCustomPayload) { + t.Fatal("The received custom payload should match the sent") + } + iter.Close() + + // Insert query + query = session.Query("INSERT INTO testCustomPayloadMessages(id,value) VALUES(1, 1)").Consistency(One).CustomPayload(customPayload) + iter = query.Iter() + rCustomPayload = iter.GetCustomPayload() + if !reflect.DeepEqual(customPayload, rCustomPayload) { + t.Fatal("The received custom payload should match the sent") + } + iter.Close() + + // Batch Message + b := session.NewBatch(LoggedBatch) + b.CustomPayload = customPayload + b.Query("INSERT INTO testCustomPayloadMessages(id,value) VALUES(1, 1)") + if err := session.ExecuteBatch(b); err != nil { + t.Fatalf("query failed. %v", err) + } +} + +func TestCustomPayloadValues(t *testing.T) { + cluster := createCluster() + session := createSessionFromCluster(cluster, t) + defer session.Close() + + if err := createTable(session, "CREATE TABLE gocql_test.testCustomPayloadValues (id int, value int, PRIMARY KEY (id))"); err != nil { + t.Fatal(err) + } + + values := []map[string][]byte{map[string][]byte{"a": []byte{10, 20}, "b": []byte{20, 30}}, nil, map[string][]byte{"a": []byte{10, 20}, "b": nil}} + + for _, customPayload := range values { + query := session.Query("SELECT id FROM testCustomPayloadValues where id = ?", 42).Consistency(One).CustomPayload(customPayload) + iter := query.Iter() + rCustomPayload := iter.GetCustomPayload() + if !reflect.DeepEqual(customPayload, rCustomPayload) { + t.Fatal("The received custom payload should match the sent") + } + } +} + +func TestUDF(t *testing.T) { + session := createSession(t) + defer session.Close() + if session.cfg.ProtoVersion < 4 { + t.Skip("skipping UDF support on proto < 4") + } + + const query = `CREATE OR REPLACE FUNCTION uniq(state set, val text) + CALLED ON NULL INPUT RETURNS set LANGUAGE java + AS 'state.add(val); return state;'` + + err := session.Query(query).Exec() + if err != nil { + t.Fatal(err) + } +} diff --git a/marshal_test.go b/marshal_test.go index 16fcd25df..52a2c054c 100644 --- a/marshal_test.go +++ b/marshal_test.go @@ -1,4 +1,4 @@ -// +build all unit +//+build all unit package gocql diff --git a/session_test.go b/session_test.go index c1a99f064..dc0761eec 100644 --- a/session_test.go +++ b/session_test.go @@ -1,4 +1,4 @@ -// +build all integration +// +build all cassandra package gocql diff --git a/stress_test.go b/stress_test.go index e4abac516..ecba18a28 100644 --- a/stress_test.go +++ b/stress_test.go @@ -1,4 +1,4 @@ -// +build all integration +// +build all cassandra package gocql diff --git a/udt_test.go b/udt_test.go index f36fd6fdf..e9064c460 100644 --- a/udt_test.go +++ b/udt_test.go @@ -1,4 +1,4 @@ -// +build all integration +// +build all cassandra package gocql diff --git a/wiki_test.go b/wiki_test.go index 04a965aed..c1a8a5fc4 100644 --- a/wiki_test.go +++ b/wiki_test.go @@ -1,4 +1,4 @@ -// +build all integration +// +build all cassandra package gocql