github.com/willyham/dosa@v2.3.1-0.20171024181418-1e446d37ee71+incompatible/connectors/cassandra/config_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
    22  
    23  import (
    24  	"testing"
    25  	"time"
    26  
    27  	"github.com/gocql/gocql"
    28  	"github.com/stretchr/testify/assert"
    29  	"gopkg.in/yaml.v2"
    30  )
    31  
    32  func TestConfigUnmarshalYAML(t *testing.T) {
    33  	str := `hosts: ["127.0.0.1"]
    34  cqlVersion: 3.0.1
    35  protoVersion:      3
    36  timeout:           2000ms
    37  connectTimeout:    1000ms
    38  port:              9091
    39  numConns:          5
    40  consistency:       ONE
    41  retryPolicy:       1
    42  socketKeepalive:   100ms
    43  maxPreparedStmts:  5000
    44  maxRoutingKeyInfo: 5000
    45  pageSize:          2000
    46  serialConsistency:  SERIAL
    47  hostSelectionPolicy: TokenAwareHostPolicy
    48  dataCenter: dca1
    49  `
    50  	config := Config{}
    51  	err := yaml.Unmarshal([]byte(str), &config)
    52  	assert.NoError(t, err)
    53  	expected := gocql.NewCluster("127.0.0.1")
    54  	timeout, _ := time.ParseDuration("2000ms")
    55  	expected.CQLVersion = "3.0.1"
    56  	expected.Timeout = timeout
    57  	connectTimeout, _ := time.ParseDuration("1000ms")
    58  	expected.ConnectTimeout = connectTimeout
    59  	expected.ProtoVersion = 3
    60  	expected.Port = 9091
    61  	expected.NumConns = 5
    62  	expected.Consistency = gocql.One
    63  	expected.RetryPolicy = &gocql.SimpleRetryPolicy{NumRetries: 1}
    64  	socketKeepalive, _ := time.ParseDuration("100ms")
    65  	expected.SocketKeepalive = socketKeepalive
    66  	expected.MaxRoutingKeyInfo = 5000
    67  	expected.MaxPreparedStmts = 5000
    68  	expected.PageSize = 2000
    69  	expected.SerialConsistency = gocql.Serial
    70  	expected.PoolConfig = gocql.PoolConfig{HostSelectionPolicy: gocql.TokenAwareHostPolicy(gocql.RoundRobinHostPolicy())}
    71  	expected.HostFilter = gocql.DataCentreHostFilter("dca1")
    72  	assert.Equal(t, expected.PoolConfig, config.ClusterConfig.PoolConfig)
    73  	assert.Equal(t, expected.RetryPolicy, config.ClusterConfig.RetryPolicy)
    74  	assert.Equal(t, expected.CQLVersion, config.ClusterConfig.CQLVersion)
    75  	assert.Equal(t, expected.ProtoVersion, config.ClusterConfig.ProtoVersion)
    76  	assert.Equal(t, expected.Timeout, config.ClusterConfig.Timeout)
    77  	assert.Equal(t, expected.MaxPreparedStmts, config.ClusterConfig.MaxPreparedStmts)
    78  	assert.Equal(t, expected.MaxRoutingKeyInfo, config.ClusterConfig.MaxRoutingKeyInfo)
    79  	assert.Equal(t, expected.NumConns, config.ClusterConfig.NumConns)
    80  	assert.Equal(t, expected.SerialConsistency, config.ClusterConfig.SerialConsistency)
    81  	assert.Equal(t, expected.SocketKeepalive, config.ClusterConfig.SocketKeepalive)
    82  	assert.Equal(t, expected.PageSize, config.ClusterConfig.PageSize)
    83  	// no way I can test this. The hostfilter is an anonymous func
    84  	//assert.Equal(t, expected.HostFilter, config.ClusterConfig.HostFilter)
    85  	assert.Equal(t, expected.Port, config.ClusterConfig.Port)
    86  	assert.Equal(t, expected.ConnectTimeout, config.ClusterConfig.ConnectTimeout)
    87  	assert.Equal(t, expected.Hosts, config.ClusterConfig.Hosts)
    88  	assert.Equal(t, expected.Consistency, config.ClusterConfig.Consistency)
    89  }
    90  
    91  func TestSerialConsistency(t *testing.T) {
    92  	config := Config{}
    93  	err := yaml.Unmarshal([]byte("serialConsistency: LOCAL_SERIAL"), &config)
    94  	assert.NoError(t, err)
    95  	assert.Equal(t, gocql.LocalSerial, config.ClusterConfig.SerialConsistency)
    96  }
    97  
    98  func TestRoundRobinHostPolicy(t *testing.T) {
    99  	config := Config{}
   100  	err := yaml.Unmarshal([]byte("hostSelectionPolicy: RoundRobinHostPolicy"), &config)
   101  	assert.NoError(t, err)
   102  }
   103  
   104  func TestConfigInvalid(t *testing.T) {
   105  	cases := []struct {
   106  		conf, expected string
   107  	}{
   108  		{"hostSelectionPolicy: InvalidHostSelectionPolicy", "InvalidHostSelectionPolicy"},
   109  		{"serialConsistency: InvalidSerialConsistency", "InvalidSerialConsistency"},
   110  	}
   111  
   112  	for _, tc := range cases {
   113  		config := Config{}
   114  		err := yaml.Unmarshal([]byte(tc.conf), &config)
   115  		assert.Error(t, err)
   116  		assert.Contains(t, err.Error(), tc.expected)
   117  	}
   118  }