common_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. if err := s.control.awaitSchemaAgreement(); err != nil {
  47. return err
  48. }
  49. return nil
  50. }
  51. func createCluster() *ClusterConfig {
  52. cluster := NewCluster(clusterHosts...)
  53. cluster.ProtoVersion = *flagProto
  54. cluster.CQLVersion = *flagCQL
  55. cluster.Timeout = *flagTimeout
  56. cluster.Consistency = Quorum
  57. cluster.MaxWaitSchemaAgreement = 2 * time.Minute // travis might be slow
  58. if *flagRetry > 0 {
  59. cluster.RetryPolicy = &SimpleRetryPolicy{NumRetries: *flagRetry}
  60. }
  61. switch *flagCompressTest {
  62. case "snappy":
  63. cluster.Compressor = &SnappyCompressor{}
  64. case "":
  65. default:
  66. panic("invalid compressor: " + *flagCompressTest)
  67. }
  68. cluster = addSslOptions(cluster)
  69. return cluster
  70. }
  71. func createKeyspace(tb testing.TB, cluster *ClusterConfig, keyspace string) {
  72. c := *cluster
  73. c.Keyspace = "system"
  74. c.Timeout = 20 * time.Second
  75. session, err := c.CreateSession()
  76. if err != nil {
  77. tb.Fatal("createSession:", err)
  78. }
  79. defer session.Close()
  80. defer log.Println("closing keyspace session")
  81. err = session.control.query(`DROP KEYSPACE IF EXISTS ` + keyspace).Close()
  82. if err != nil {
  83. tb.Fatal(err)
  84. }
  85. err = session.control.query(fmt.Sprintf(`CREATE KEYSPACE %s
  86. WITH replication = {
  87. 'class' : 'SimpleStrategy',
  88. 'replication_factor' : %d
  89. }`, keyspace, *flagRF)).Close()
  90. if err != nil {
  91. tb.Fatal(err)
  92. }
  93. // lets just be sure
  94. if err := session.control.awaitSchemaAgreement(); err != nil {
  95. tb.Fatal(err)
  96. }
  97. }
  98. func createSessionFromCluster(cluster *ClusterConfig, tb testing.TB) *Session {
  99. // Drop and re-create the keyspace once. Different tests should use their own
  100. // individual tables, but can assume that the table does not exist before.
  101. initOnce.Do(func() {
  102. createKeyspace(tb, cluster, "gocql_test")
  103. })
  104. cluster.Keyspace = "gocql_test"
  105. session, err := cluster.CreateSession()
  106. if err != nil {
  107. tb.Fatal("createSession:", err)
  108. }
  109. return session
  110. }
  111. func createSession(tb testing.TB) *Session {
  112. cluster := createCluster()
  113. return createSessionFromCluster(cluster, tb)
  114. }