common_test.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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", 0, "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. err = createTable(session, `DROP KEYSPACE IF EXISTS `+keyspace)
  91. if err != nil {
  92. panic(fmt.Sprintf("unable to drop keyspace: %v", err))
  93. }
  94. err = createTable(session, fmt.Sprintf(`CREATE KEYSPACE %s
  95. WITH replication = {
  96. 'class' : 'SimpleStrategy',
  97. 'replication_factor' : %d
  98. }`, keyspace, *flagRF))
  99. if err != nil {
  100. panic(fmt.Sprintf("unable to create keyspace: %v", err))
  101. }
  102. }
  103. func createSessionFromCluster(cluster *ClusterConfig, tb testing.TB) *Session {
  104. // Drop and re-create the keyspace once. Different tests should use their own
  105. // individual tables, but can assume that the table does not exist before.
  106. initOnce.Do(func() {
  107. createKeyspace(tb, cluster, "gocql_test")
  108. })
  109. cluster.Keyspace = "gocql_test"
  110. session, err := cluster.CreateSession()
  111. if err != nil {
  112. tb.Fatal("createSession:", err)
  113. }
  114. if err := session.control.awaitSchemaAgreement(); err != nil {
  115. tb.Fatal(err)
  116. }
  117. return session
  118. }
  119. func createSession(tb testing.TB) *Session {
  120. cluster := createCluster()
  121. return createSessionFromCluster(cluster, tb)
  122. }
  123. // createTestSession is hopefully moderately useful in actual unit tests
  124. func createTestSession() *Session {
  125. config := NewCluster()
  126. config.NumConns = 1
  127. config.Timeout = 0
  128. config.DisableInitialHostLookup = true
  129. config.IgnorePeerAddr = true
  130. config.PoolConfig.HostSelectionPolicy = RoundRobinHostPolicy()
  131. session := &Session{
  132. cfg: *config,
  133. connCfg: &ConnConfig{
  134. Timeout: 10 * time.Millisecond,
  135. Keepalive: 0,
  136. },
  137. policy: config.PoolConfig.HostSelectionPolicy,
  138. }
  139. session.pool = config.PoolConfig.buildPool(session)
  140. return session
  141. }
  142. func staticAddressTranslator(newAddr net.IP, newPort int) AddressTranslator {
  143. return AddressTranslatorFunc(func(addr net.IP, port int) (net.IP, int) {
  144. return newAddr, newPort
  145. })
  146. }
  147. func assertTrue(t *testing.T, description string, value bool) {
  148. if !value {
  149. t.Errorf("expected %s to be true", description)
  150. }
  151. }
  152. func assertEqual(t *testing.T, description string, expected, actual interface{}) {
  153. if expected != actual {
  154. t.Errorf("expected %s to be (%+v) but was (%+v) instead", description, expected, actual)
  155. }
  156. }
  157. func assertNil(t *testing.T, description string, actual interface{}) {
  158. if actual != nil {
  159. t.Errorf("expected %s to be (nil) but was (%+v) instead", description, actual)
  160. }
  161. }
  162. func assertNotNil(t *testing.T, description string, actual interface{}) {
  163. if actual == nil {
  164. t.Errorf("expected %s not to be (nil)", description)
  165. }
  166. }