common_test.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. package gocql
  2. import (
  3. "flag"
  4. "fmt"
  5. "log"
  6. "net"
  7. "strings"
  8. "sync"
  9. "testing"
  10. "time"
  11. )
  12. var (
  13. flagCluster = flag.String("cluster", "127.0.0.1", "a comma-separated list of host:port tuples")
  14. flagProto = flag.Int("proto", 2, "protcol version")
  15. flagCQL = flag.String("cql", "3.0.0", "CQL version")
  16. flagRF = flag.Int("rf", 1, "replication factor for test keyspace")
  17. clusterSize = flag.Int("clusterSize", 1, "the expected size of the cluster")
  18. flagRetry = flag.Int("retries", 5, "number of times to retry queries")
  19. flagAutoWait = flag.Duration("autowait", 1000*time.Millisecond, "time to wait for autodiscovery to fill the hosts poll")
  20. flagRunSslTest = flag.Bool("runssl", false, "Set to true to run ssl test")
  21. flagRunAuthTest = flag.Bool("runauth", false, "Set to true to run authentication test")
  22. flagCompressTest = flag.String("compressor", "", "compressor to use")
  23. flagTimeout = flag.Duration("gocql.timeout", 5*time.Second, "sets the connection `timeout` for all operations")
  24. flagCassVersion cassVersion
  25. clusterHosts []string
  26. )
  27. func init() {
  28. flag.Var(&flagCassVersion, "gocql.cversion", "the cassandra version being tested against")
  29. flag.Parse()
  30. clusterHosts = strings.Split(*flagCluster, ",")
  31. log.SetFlags(log.Lshortfile | log.LstdFlags)
  32. }
  33. func addSslOptions(cluster *ClusterConfig) *ClusterConfig {
  34. if *flagRunSslTest {
  35. cluster.SslOpts = &SslOptions{
  36. CertPath: "testdata/pki/gocql.crt",
  37. KeyPath: "testdata/pki/gocql.key",
  38. CaPath: "testdata/pki/ca.crt",
  39. EnableHostVerification: false,
  40. }
  41. }
  42. return cluster
  43. }
  44. var initOnce sync.Once
  45. func createTable(s *Session, table string) error {
  46. // lets just be really sure
  47. if err := s.control.awaitSchemaAgreement(); err != nil {
  48. log.Printf("error waiting for schema agreement pre create table=%q err=%v\n", table, err)
  49. return err
  50. }
  51. if err := s.Query(table).RetryPolicy(nil).Exec(); err != nil {
  52. log.Printf("error creating table table=%q err=%v\n", table, err)
  53. return err
  54. }
  55. if err := s.control.awaitSchemaAgreement(); err != nil {
  56. log.Printf("error waiting for schema agreement post create table=%q err=%v\n", table, err)
  57. return err
  58. }
  59. return nil
  60. }
  61. func createCluster() *ClusterConfig {
  62. cluster := NewCluster(clusterHosts...)
  63. cluster.ProtoVersion = *flagProto
  64. cluster.CQLVersion = *flagCQL
  65. cluster.Timeout = *flagTimeout
  66. cluster.Consistency = Quorum
  67. cluster.MaxWaitSchemaAgreement = 2 * time.Minute // travis might be slow
  68. if *flagRetry > 0 {
  69. cluster.RetryPolicy = &SimpleRetryPolicy{NumRetries: *flagRetry}
  70. }
  71. switch *flagCompressTest {
  72. case "snappy":
  73. cluster.Compressor = &SnappyCompressor{}
  74. case "":
  75. default:
  76. panic("invalid compressor: " + *flagCompressTest)
  77. }
  78. cluster = addSslOptions(cluster)
  79. return cluster
  80. }
  81. func createKeyspace(tb testing.TB, cluster *ClusterConfig, keyspace string) {
  82. c := *cluster
  83. c.Keyspace = "system"
  84. c.Timeout = 30 * time.Second
  85. session, err := c.CreateSession()
  86. if err != nil {
  87. panic(err)
  88. }
  89. defer session.Close()
  90. defer log.Println("closing keyspace session")
  91. err = createTable(session, `DROP KEYSPACE IF EXISTS `+keyspace)
  92. if err != nil {
  93. panic(fmt.Sprintf("unable to drop keyspace: %v", err))
  94. }
  95. err = createTable(session, fmt.Sprintf(`CREATE KEYSPACE %s
  96. WITH replication = {
  97. 'class' : 'SimpleStrategy',
  98. 'replication_factor' : %d
  99. }`, keyspace, *flagRF))
  100. if err != nil {
  101. panic(fmt.Sprintf("unable to create keyspace: %v", err))
  102. }
  103. }
  104. func createSessionFromCluster(cluster *ClusterConfig, tb testing.TB) *Session {
  105. // Drop and re-create the keyspace once. Different tests should use their own
  106. // individual tables, but can assume that the table does not exist before.
  107. initOnce.Do(func() {
  108. createKeyspace(tb, cluster, "gocql_test")
  109. })
  110. cluster.Keyspace = "gocql_test"
  111. session, err := cluster.CreateSession()
  112. if err != nil {
  113. tb.Fatal("createSession:", err)
  114. }
  115. if err := session.control.awaitSchemaAgreement(); err != nil {
  116. tb.Fatal(err)
  117. }
  118. return session
  119. }
  120. func createSession(tb testing.TB) *Session {
  121. cluster := createCluster()
  122. return createSessionFromCluster(cluster, tb)
  123. }
  124. // createTestSession is hopefully moderately useful in actual unit tests
  125. func createTestSession() *Session {
  126. config := NewCluster()
  127. config.NumConns = 1
  128. config.Timeout = 0
  129. config.DisableInitialHostLookup = true
  130. config.IgnorePeerAddr = true
  131. config.PoolConfig.HostSelectionPolicy = RoundRobinHostPolicy()
  132. session := &Session{
  133. cfg: *config,
  134. connCfg: &ConnConfig{
  135. Timeout: 10 * time.Millisecond,
  136. Keepalive: 0,
  137. },
  138. policy: config.PoolConfig.HostSelectionPolicy,
  139. }
  140. session.pool = config.PoolConfig.buildPool(session)
  141. return session
  142. }
  143. func staticAddressTranslator(newAddr net.IP, newPort int) AddressTranslator {
  144. return AddressTranslatorFunc(func(addr net.IP, port int) (net.IP, int) {
  145. return newAddr, newPort
  146. })
  147. }
  148. func assertTrue(t *testing.T, description string, value bool) {
  149. if !value {
  150. t.Errorf("expected %s to be true", description)
  151. }
  152. }
  153. func assertEqual(t *testing.T, description string, expected, actual interface{}) {
  154. if expected != actual {
  155. t.Errorf("expected %s to be (%+v) but was (%+v) instead", description, expected, actual)
  156. }
  157. }
  158. func assertNil(t *testing.T, description string, actual interface{}) {
  159. if actual != nil {
  160. t.Errorf("expected %s to be (nil) but was (%+v) instead", description, actual)
  161. }
  162. }
  163. func assertNotNil(t *testing.T, description string, actual interface{}) {
  164. if actual == nil {
  165. t.Errorf("expected %s not to be (nil)", description)
  166. }
  167. }