github.com/willyham/dosa@v2.3.1-0.20171024181418-1e446d37ee71+incompatible/connectors/cassandra/embedded_test.go (about) 1 // Copyright (c) 2017 Uber Technologies, Inc. 2 // 3 // Permission is hereby granted, free of charge, to any person obtaining a copy 4 // of this software and associated documentation files (the "Software"), to deal 5 // in the Software without restriction, including without limitation the rights 6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 // copies of the Software, and to permit persons to whom the Software is 8 // furnished to do so, subject to the following conditions: 9 // 10 // The above copyright notice and this permission notice shall be included in 11 // all copies or substantial portions of the Software. 12 // 13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 // THE SOFTWARE. 20 21 package cassandra_test 22 23 import ( 24 "os/exec" 25 "strconv" 26 "testing" 27 "time" 28 29 "github.com/gocql/gocql" 30 "github.com/pkg/errors" 31 "github.com/uber-go/dosa" 32 "github.com/uber-go/dosa/connectors/cassandra" 33 . "github.com/uber-go/dosa/connectors/cassandra" 34 ) 35 36 var testConnector dosa.Connector 37 38 func EnsureLocalCassandraStarted() error { 39 if IsRunning() { 40 return nil 41 } 42 return startViaCcm() 43 } 44 45 func GetTestConnector(t *testing.T) dosa.Connector { 46 if testConnector == nil { 47 err := EnsureLocalCassandraStarted() 48 if err != nil { 49 t.Fatal(err) 50 return nil 51 } 52 testConnector, err = NewConnector( 53 gocql.NewCluster("127.0.0.1:"+strconv.Itoa(CassandraPort)), 54 &cassandra.UseNamePrefix{}, 55 nil, 56 ) 57 if err != nil { 58 t.Fatal(err) 59 return nil 60 } 61 testStore = testConnector.(*Connector) 62 err = initTestSchema("test.datastore", testEntityInfo) 63 if err != nil { 64 t.Fatal(err) 65 return nil 66 } 67 } 68 return testConnector 69 } 70 71 func ShutdownTestConnector() { 72 testConnector.Shutdown() 73 testConnector = nil 74 } 75 76 const ccm = "ccm" 77 const cassVersion = "3.0.11" 78 79 // see if we can start up a cassandra instance using ccm 80 func startViaCcm() error { 81 path, err := exec.LookPath(ccm) 82 if err != nil { 83 return errors.Wrap(err, "ccm executable not found") 84 85 } 86 // call remove, but don't care if it fails 87 _ = exec.Command(path, "remove", "test").Run() 88 cmd := exec.Command(path, "create", "test", "-v", cassVersion, "-n", "1", "-d", "--vnodes", "--jvm_arg=-Xmx256 -XX:NewSize=100m") 89 if output, err := cmd.CombinedOutput(); err != nil { 90 return errors.Wrapf(err, "failed to create test cluster: %s", string(output)) 91 } 92 // set up some configuration to make this cluster take less memory 93 cmd = exec.Command(path, "updateconf", 94 "concurrent_reads: 2", 95 "concurrent_writes: 2", 96 "rpc_server_type: sync", 97 "rpc_min_threads: 2", 98 "rpc_max_threads: 2", 99 "write_request_timeout_in_ms: 5000", 100 "read_request_timeout_in_ms: 5000", 101 ) 102 if output, err := cmd.CombinedOutput(); err != nil { 103 return errors.Wrapf(err, "failed to configure test cluster: %s", string(output)) 104 } 105 // start the service 106 if output, err := exec.Command(path, "start", "-v", "--wait-for-binary-proto").CombinedOutput(); err != nil { 107 return errors.Wrapf(err, "failed to start test cluster: %s", string(output)) 108 } 109 time.Sleep(1 * time.Second) 110 return nil 111 }