common_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package gocql
  2. import (
  3. "flag"
  4. "fmt"
  5. "log"
  6. "strings"
  7. "sync"
  8. "testing"
  9. "time"
  10. )
  11. var (
  12. flagCluster = flag.String("cluster", "127.0.0.1", "a comma-separated list of host:port tuples")
  13. flagProto = flag.Int("proto", 2, "protcol version")
  14. flagCQL = flag.String("cql", "3.0.0", "CQL version")
  15. flagRF = flag.Int("rf", 1, "replication factor for test keyspace")
  16. clusterSize = flag.Int("clusterSize", 1, "the expected size of the cluster")
  17. flagRetry = flag.Int("retries", 5, "number of times to retry queries")
  18. flagAutoWait = flag.Duration("autowait", 1000*time.Millisecond, "time to wait for autodiscovery to fill the hosts poll")
  19. flagRunSslTest = flag.Bool("runssl", false, "Set to true to run ssl test")
  20. flagRunAuthTest = flag.Bool("runauth", false, "Set to true to run authentication test")
  21. flagCompressTest = flag.String("compressor", "", "compressor to use")
  22. flagTimeout = flag.Duration("gocql.timeout", 5*time.Second, "sets the connection `timeout` for all operations")
  23. clusterHosts []string
  24. )
  25. func init() {
  26. flag.Parse()
  27. clusterHosts = strings.Split(*flagCluster, ",")
  28. log.SetFlags(log.Lshortfile | log.LstdFlags)
  29. }
  30. func addSslOptions(cluster *ClusterConfig) *ClusterConfig {
  31. if *flagRunSslTest {
  32. cluster.SslOpts = &SslOptions{
  33. CertPath: "testdata/pki/gocql.crt",
  34. KeyPath: "testdata/pki/gocql.key",
  35. CaPath: "testdata/pki/ca.crt",
  36. EnableHostVerification: false,
  37. }
  38. }
  39. return cluster
  40. }
  41. var initOnce sync.Once
  42. func createTable(s *Session, table string) error {
  43. if err := s.control.query(table).Close(); err != nil {
  44. return err
  45. }
  46. return nil
  47. }
  48. func createCluster() *ClusterConfig {
  49. cluster := NewCluster(clusterHosts...)
  50. cluster.ProtoVersion = *flagProto
  51. cluster.CQLVersion = *flagCQL
  52. cluster.Timeout = *flagTimeout
  53. cluster.Consistency = Quorum
  54. cluster.MaxWaitSchemaAgreement = 2 * time.Minute // travis might be slow
  55. if *flagRetry > 0 {
  56. cluster.RetryPolicy = &SimpleRetryPolicy{NumRetries: *flagRetry}
  57. }
  58. switch *flagCompressTest {
  59. case "snappy":
  60. cluster.Compressor = &SnappyCompressor{}
  61. case "":
  62. default:
  63. panic("invalid compressor: " + *flagCompressTest)
  64. }
  65. cluster = addSslOptions(cluster)
  66. return cluster
  67. }
  68. func createKeyspace(tb testing.TB, cluster *ClusterConfig, keyspace string) {
  69. c := *cluster
  70. c.Keyspace = "system"
  71. c.Timeout = 20 * time.Second
  72. session, err := c.CreateSession()
  73. if err != nil {
  74. tb.Fatal("createSession:", err)
  75. }
  76. defer session.Close()
  77. defer log.Println("closing keyspace session")
  78. err = session.control.query(`DROP KEYSPACE IF EXISTS ` + keyspace).Close()
  79. if err != nil {
  80. tb.Fatal(err)
  81. }
  82. err = session.control.query(fmt.Sprintf(`CREATE KEYSPACE %s
  83. WITH replication = {
  84. 'class' : 'SimpleStrategy',
  85. 'replication_factor' : %d
  86. }`, keyspace, *flagRF)).Close()
  87. if err != nil {
  88. tb.Fatal(err)
  89. }
  90. }
  91. func createSessionFromCluster(cluster *ClusterConfig, tb testing.TB) *Session {
  92. // Drop and re-create the keyspace once. Different tests should use their own
  93. // individual tables, but can assume that the table does not exist before.
  94. initOnce.Do(func() {
  95. createKeyspace(tb, cluster, "gocql_test")
  96. })
  97. cluster.Keyspace = "gocql_test"
  98. session, err := cluster.CreateSession()
  99. if err != nil {
  100. tb.Fatal("createSession:", err)
  101. }
  102. return session
  103. }
  104. func createSession(tb testing.TB) *Session {
  105. cluster := createCluster()
  106. return createSessionFromCluster(cluster, tb)
  107. }