github.com/muhammadn/cortex@v1.9.1-0.20220510110439-46bb7000d03d/pkg/chunk/cassandra/fixtures.go (about)

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