github.com/sequix/cortex@v1.1.6/pkg/chunk/cassandra/fixtures.go (about) 1 package cassandra 2 3 import ( 4 "context" 5 "os" 6 7 "github.com/sequix/cortex/pkg/chunk" 8 "github.com/sequix/cortex/pkg/chunk/testutils" 9 ) 10 11 // GOCQL doesn't provide nice mocks, so we use a real Cassandra instance. 12 // To enable these tests: 13 // $ docker run --name cassandra --rm -p 9042:9042 cassandra:3.11 14 // $ CASSANDRA_TEST_ADDRESSES=localhost:9042 go test ./pkg/chunk/storage 15 16 type fixture struct { 17 name string 18 indexClient chunk.IndexClient 19 objectClient chunk.ObjectClient 20 tableClient chunk.TableClient 21 schemaConfig chunk.SchemaConfig 22 } 23 24 func (f fixture) Name() string { 25 return f.name 26 } 27 28 func (f fixture) Clients() (chunk.IndexClient, chunk.ObjectClient, chunk.TableClient, chunk.SchemaConfig, error) { 29 return f.indexClient, f.objectClient, f.tableClient, f.schemaConfig, nil 30 } 31 32 func (f fixture) Teardown() error { 33 return nil 34 } 35 36 // Fixtures for unit testing Cassandra integration. 37 func Fixtures() ([]testutils.Fixture, error) { 38 addresses := os.Getenv("CASSANDRA_TEST_ADDRESSES") 39 if addresses == "" { 40 return nil, nil 41 } 42 43 cfg := Config{ 44 Addresses: addresses, 45 Keyspace: "test", 46 Consistency: "QUORUM", 47 ReplicationFactor: 1, 48 } 49 50 // Get a SchemaConfig with the defaults. 51 schemaConfig := testutils.DefaultSchemaConfig("cassandra") 52 53 storageClient, err := NewStorageClient(cfg, schemaConfig) 54 if err != nil { 55 return nil, err 56 } 57 58 tableClient, err := NewTableClient(context.Background(), cfg) 59 if err != nil { 60 return nil, err 61 } 62 63 return []testutils.Fixture{ 64 fixture{ 65 name: "Cassandra", 66 indexClient: storageClient, 67 objectClient: storageClient, 68 tableClient: tableClient, 69 schemaConfig: schemaConfig, 70 }, 71 }, nil 72 }