github.com/m3db/m3@v1.5.0/src/cluster/client/etcd/options_test.go (about)

     1  // Copyright (c) 2016 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 etcd
    22  
    23  import (
    24  	"testing"
    25  	"time"
    26  
    27  	"github.com/m3db/m3/src/cluster/services"
    28  	"github.com/m3db/m3/src/x/instrument"
    29  
    30  	"github.com/stretchr/testify/assert"
    31  	"github.com/stretchr/testify/require"
    32  )
    33  
    34  func TestKeepAliveOptions(t *testing.T) {
    35  	opts := NewKeepAliveOptions()
    36  	require.Equal(t, defaultKeepAliveEnabled, opts.KeepAliveEnabled())
    37  	require.Equal(t, defaultKeepAlivePeriod, opts.KeepAlivePeriod())
    38  	require.Equal(t, defaultKeepAlivePeriodMaxJitter, opts.KeepAlivePeriodMaxJitter())
    39  	require.Equal(t, defaultKeepAliveTimeout, opts.KeepAliveTimeout())
    40  
    41  	opts = NewKeepAliveOptions().
    42  		SetKeepAliveEnabled(true).
    43  		SetKeepAlivePeriod(1234 * time.Second).
    44  		SetKeepAlivePeriodMaxJitter(5000 * time.Second).
    45  		SetKeepAliveTimeout(time.Hour)
    46  
    47  	require.Equal(t, true, opts.KeepAliveEnabled())
    48  	require.Equal(t, 1234*time.Second, opts.KeepAlivePeriod())
    49  	require.Equal(t, 5000*time.Second, opts.KeepAlivePeriodMaxJitter())
    50  	require.Equal(t, time.Hour, opts.KeepAliveTimeout())
    51  }
    52  
    53  func TestCluster(t *testing.T) {
    54  	c := NewCluster()
    55  	assert.Equal(t, "", c.Zone())
    56  	assert.Equal(t, 0, len(c.Endpoints()))
    57  	assert.Equal(t, NewTLSOptions(), c.TLSOptions())
    58  
    59  	c = c.SetZone("z")
    60  	assert.Equal(t, "z", c.Zone())
    61  	assert.Equal(t, 0, len(c.Endpoints()))
    62  
    63  	c = c.SetEndpoints([]string{"e1"})
    64  	assert.Equal(t, "z", c.Zone())
    65  	assert.Equal(t, []string{"e1"}, c.Endpoints())
    66  
    67  	aOpts := NewTLSOptions().SetCrtPath("cert").SetKeyPath("key").SetCACrtPath("ca")
    68  	c = c.SetTLSOptions(aOpts)
    69  	assert.Equal(t, "z", c.Zone())
    70  	assert.Equal(t, []string{"e1"}, c.Endpoints())
    71  	assert.Equal(t, aOpts, c.TLSOptions())
    72  	assert.Equal(t, defaultAutoSyncInterval, c.AutoSyncInterval())
    73  	assert.Equal(t, defaultDialTimeout, c.DialTimeout())
    74  
    75  	c = c.SetAutoSyncInterval(123 * time.Minute)
    76  	assert.Equal(t, "z", c.Zone())
    77  	assert.Equal(t, []string{"e1"}, c.Endpoints())
    78  	assert.Equal(t, aOpts, c.TLSOptions())
    79  	assert.Equal(t, 123*time.Minute, c.AutoSyncInterval())
    80  	assert.Equal(t, defaultDialTimeout, c.DialTimeout())
    81  
    82  	c = c.SetDialTimeout(42 * time.Hour)
    83  	assert.Equal(t, "z", c.Zone())
    84  	assert.Equal(t, []string{"e1"}, c.Endpoints())
    85  	assert.Equal(t, aOpts, c.TLSOptions())
    86  	assert.Equal(t, 123*time.Minute, c.AutoSyncInterval())
    87  	assert.Equal(t, 42*time.Hour, c.DialTimeout())
    88  }
    89  
    90  func TestTLSOptions(t *testing.T) {
    91  	aOpts := NewTLSOptions()
    92  	assert.Equal(t, "", aOpts.CrtPath())
    93  	assert.Equal(t, "", aOpts.KeyPath())
    94  	assert.Equal(t, "", aOpts.CACrtPath())
    95  
    96  	aOpts = aOpts.SetCrtPath("cert").SetKeyPath("key").SetCACrtPath("ca")
    97  	assert.Equal(t, "cert", aOpts.CrtPath())
    98  	assert.Equal(t, "key", aOpts.KeyPath())
    99  	assert.Equal(t, "ca", aOpts.CACrtPath())
   100  }
   101  
   102  func TestOptions(t *testing.T) {
   103  	opts := NewOptions()
   104  	assert.Equal(t, "", opts.Zone())
   105  	assert.Equal(t, "", opts.Env())
   106  	assert.Equal(t,
   107  		services.NewOptions().SetInstrumentsOptions(opts.ServicesOptions().InstrumentsOptions()),
   108  		opts.ServicesOptions())
   109  	assert.Equal(t, "", opts.CacheDir())
   110  	assert.Equal(t, "", opts.Service())
   111  	assert.Equal(t, []Cluster{}, opts.Clusters())
   112  	_, ok := opts.ClusterForZone("z")
   113  	assert.False(t, ok)
   114  	assert.NotNil(t, opts.InstrumentOptions())
   115  	assert.Equal(t, defaultRequestTimeout, opts.RequestTimeout())
   116  	assert.Equal(t, defaultWatchChanCheckInterval, opts.WatchChanCheckInterval())
   117  	assert.Equal(t, defaultWatchChanResetInterval, opts.WatchChanCheckInterval())
   118  	assert.Equal(t, defaultWatchChanInitTimeout, opts.WatchChanInitTimeout())
   119  	assert.False(t, opts.EnableFastGets())
   120  	ropts := opts.RetryOptions()
   121  	assert.Equal(t, defaultRetryJitter, ropts.Jitter())
   122  	assert.Equal(t, defaultRetryInitialBackoff, ropts.InitialBackoff())
   123  	assert.Equal(t, defaultRetryBackoffFactor, ropts.BackoffFactor())
   124  	assert.Equal(t, defaultRetryMaxRetries, ropts.MaxRetries())
   125  	assert.Equal(t, defaultRetryMaxBackoff, ropts.MaxBackoff())
   126  
   127  	c1 := NewCluster().SetZone("z1")
   128  	c2 := NewCluster().SetZone("z2")
   129  	iopts := instrument.NewOptions().SetReportInterval(time.Minute)
   130  
   131  	sdOpts := services.NewOptions().SetInitTimeout(time.Millisecond)
   132  	opts = opts.SetEnv("env").
   133  		SetZone("zone").
   134  		SetServicesOptions(sdOpts).
   135  		SetCacheDir("/dir").
   136  		SetService("app").
   137  		SetClusters([]Cluster{c1, c2}).
   138  		SetInstrumentOptions(iopts).
   139  		SetWatchWithRevision(1)
   140  	assert.Equal(t, "env", opts.Env())
   141  	assert.Equal(t, "zone", opts.Zone())
   142  	assert.Equal(t, sdOpts, opts.ServicesOptions())
   143  	assert.Equal(t, "/dir", opts.CacheDir())
   144  	assert.Equal(t, "app", opts.Service())
   145  	assert.Equal(t, 2, len(opts.Clusters()))
   146  	assert.Equal(t, int64(1), opts.WatchWithRevision())
   147  	c, ok := opts.ClusterForZone("z1")
   148  	assert.True(t, ok)
   149  	assert.Equal(t, c, c1)
   150  	c, ok = opts.ClusterForZone("z2")
   151  	assert.True(t, ok)
   152  	assert.Equal(t, c, c2)
   153  	assert.Equal(t, iopts, opts.InstrumentOptions())
   154  }
   155  
   156  func TestValidate(t *testing.T) {
   157  	opts := NewOptions()
   158  	assert.Error(t, opts.Validate())
   159  
   160  	opts = opts.SetService("app")
   161  	assert.Error(t, opts.Validate())
   162  
   163  	c1 := NewCluster().SetZone("z1")
   164  	c2 := NewCluster().SetZone("z2")
   165  	opts = opts.SetClusters([]Cluster{c1, c2})
   166  	assert.NoError(t, opts.Validate())
   167  
   168  	opts = opts.SetInstrumentOptions(nil)
   169  	assert.Error(t, opts.Validate())
   170  }