func (s *TestCluster) TearDownTestCluster() {
	s.dropKeyspace()
	s.session.Close()
}
TearDownTestCluster cleans up the test cluster
package metadata
import (
	"bytes"
	"fmt"
	"io/ioutil"
	golangLog "log"
	"math/rand"
	"os/exec"
	"strings"
	"time"
	"github.com/gocql/gocql"
	log "github.com/sirupsen/logrus"
	"github.com/uber/cherami-server/common"
	"github.com/uber/cherami-server/common/configure"
	m "github.com/uber/cherami-thrift/.generated/go/metadata"
)
func DropKeyspace(s *gocql.Session, keyspace string) (err error) {
	err = s.Query(fmt.Sprintf("DROP KEYSPACE IF EXISTS %s", keyspace)).Exec()
	if err != nil {
		log.WithField(common.TagErr, err).Error(`drop keyspace error`)
	}
	log.WithField(`keyspace`, keyspace).Info(`dropped namespace`)
	return
}
func CreateKeyspace(s *gocql.Session, keyspace string, replicas int, overwrite bool) (err error) {
	if overwrite {
		err = s.Query(fmt.Sprintf("DROP KEYSPACE IF EXISTS %s", keyspace)).Exec()
		if err != nil {
			log.WithField(common.TagErr, err).Error(`drop keyspace error`)
		}
	}
	err = s.Query(fmt.Sprintf(`CREATE KEYSPACE %s WITH replication = {
		'class' : 'SimpleStrategy', 'replication_factor' : %d}`, keyspace, replicas)).Exec()
	if err != nil {
		log.WithField(common.TagErr, err).Error(`create keyspace error`)
	}
	log.WithField(`keyspace`, keyspace).Info(`created namespace`)
	return
}
func LoadSchema(cqlshpath string, fileName string, keyspace string) (err error) {
	var out bytes.Buffer
	var stderr bytes.Buffer
	cmd := exec.Command(cqlshpath,
		"--username=cassandra",
		"--password=cassandra",
		fmt.Sprintf("--keyspace=%v", keyspace),
		fmt.Sprintf("--file=%v", fileName),
		`127.0.0.1`)
	cmd.Stdout = &out
	cmd.Stderr = &stderr
	err = cmd.Run()
	if err != nil || strings.Contains(stderr.String(), `Errno`) {
		err = fmt.Errorf("LoadSchema %v returned %v. STDERR: %v", cmd.Path, err, stderr.String())
	}
	return
}
func newCluster(clusterHosts string) *gocql.ClusterConfig {
	var hosts []string
	for _, h := range strings.Split(clusterHosts, ",") {
		if host := strings.TrimSpace(h); len(host) > 0 {
			hosts = append(hosts, host)
		}
	}
	return gocql.NewCluster(hosts...)
}
func CreateKeyspaceNoSession(
	clusterHosts string,
	port int,
	keyspace string,
	replicas int,
	overwrite bool,
	auth configure.Authentication,
) error {
	cluster := newCluster(clusterHosts)
	cluster.Port = port
	cluster.Consistency = gocql.One
	cluster.Keyspace = "system"
	cluster.Timeout = 40 * time.Second
	cluster.ProtoVersion = cassandraProtoVersion
	if auth.Enabled {
		cluster.Authenticator = gocql.PasswordAuthenticator{
			Username: auth.Username,
			Password: auth.Password,
		}
	}
	session, err := cluster.CreateSession()
	if err != nil {
		log.WithField(common.TagErr, err).Error(`CreateKeyspaceNoSession: unable to create session`)
		return err
	}
	return CreateKeyspace(session, keyspace, replicas, overwrite)
}
type TestCluster struct {
	keyspace string
	cluster  *gocql.ClusterConfig
	session  *gocql.Session
	client   m.TChanMetadataService
}
func generateRandomKeyspace(n int) string {
	rand.Seed(time.Now().UnixNano())
	letterRunes := []rune("cherami")
	b := make([]rune, n)
	for i := range b {
		b[i] = letterRunes[rand.Intn(len(letterRunes))]
	}
	return string(b)
}
func (s *TestCluster) SetupTestCluster() {
	golangLog.SetOutput(ioutil.Discard)
	ip := `127.0.0.1`
	port := 9042
	s.createCluster(ip, port, gocql.Consistency(1), generateRandomKeyspace(10))
	s.createKeyspace(1)
	s.loadSchema("schema/metadata.cql")
	auth := configure.Authentication{
		Enabled:  true,
		Username: "cassandra",
		Password: "cassandra",
	}
	var err error
	s.client, err = NewCassandraMetadataService(&configure.MetadataConfig{
		CassandraHosts: ip,
		Port:           port,
		Keyspace:       s.keyspace,
		Consistency:    "One",
		Authentication: auth,
	}, nil)
	if err != nil {
		log.Fatal(err)
	}
}