github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/cluster/client/etcd/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 etcd
    22  
    23  import (
    24  	"os"
    25  	"testing"
    26  	"time"
    27  
    28  	"github.com/stretchr/testify/require"
    29  	yaml "gopkg.in/yaml.v2"
    30  )
    31  
    32  func TestKeepAliveConfig(t *testing.T) {
    33  	const cfgStr = `
    34  enabled: true
    35  period: 10s
    36  jitter: 5s
    37  timeout: 1s
    38  `
    39  
    40  	var cfg KeepAliveConfig
    41  	require.NoError(t, yaml.Unmarshal([]byte(cfgStr), &cfg))
    42  
    43  	opts := cfg.NewOptions()
    44  	require.Equal(t, true, opts.KeepAliveEnabled())
    45  	require.Equal(t, 10*time.Second, opts.KeepAlivePeriod())
    46  	require.Equal(t, 5*time.Second, opts.KeepAlivePeriodMaxJitter())
    47  	require.Equal(t, time.Second, opts.KeepAliveTimeout())
    48  }
    49  
    50  func TestConfig(t *testing.T) {
    51  	const testConfig = `
    52  env: env1
    53  zone: z1
    54  service: service1
    55  cacheDir: /tmp/cache.json
    56  watchWithRevision: 1
    57  etcdClusters:
    58    - zone: z1
    59      endpoints:
    60        - etcd1:2379
    61        - etcd2:2379
    62      keepAlive:
    63        enabled: true
    64        period: 10s
    65        jitter: 5s
    66        timeout: 1s
    67      autoSyncInterval: 160s
    68      dialTimeout: 42s
    69    - zone: z2
    70      endpoints:
    71        - etcd3:2379
    72        - etcd4:2379
    73      tls:
    74        crtPath: foo.crt.pem
    75        keyPath: foo.key.pem
    76    - zone: z3
    77      endpoints:
    78        - etcd5:2379
    79        - etcd6:2379
    80      tls:
    81        crtPath: foo.crt.pem
    82        keyPath: foo.key.pem
    83        caCrtPath: foo_ca.pem
    84  m3sd:
    85    initTimeout: 10s
    86  `
    87  
    88  	var cfg Configuration
    89  	require.NoError(t, yaml.Unmarshal([]byte(testConfig), &cfg))
    90  
    91  	require.Equal(t, "env1", cfg.Env)
    92  	require.Equal(t, "z1", cfg.Zone)
    93  	require.Equal(t, "service1", cfg.Service)
    94  	require.Equal(t, "/tmp/cache.json", cfg.CacheDir)
    95  	require.Equal(t, int64(1), cfg.WatchWithRevision)
    96  	require.Equal(t, []ClusterConfig{
    97  		{
    98  			Zone:      "z1",
    99  			Endpoints: []string{"etcd1:2379", "etcd2:2379"},
   100  			KeepAlive: &KeepAliveConfig{
   101  				Enabled: true,
   102  				Period:  10 * time.Second,
   103  				Jitter:  5 * time.Second,
   104  				Timeout: time.Second,
   105  			},
   106  			AutoSyncInterval: 160 * time.Second,
   107  			DialTimeout:      42 * time.Second,
   108  		},
   109  		{
   110  			Zone:      "z2",
   111  			Endpoints: []string{"etcd3:2379", "etcd4:2379"},
   112  			TLS: &TLSConfig{
   113  				CrtPath: "foo.crt.pem",
   114  				KeyPath: "foo.key.pem",
   115  			},
   116  		},
   117  		{
   118  			Zone:      "z3",
   119  			Endpoints: []string{"etcd5:2379", "etcd6:2379"},
   120  			TLS: &TLSConfig{
   121  				CrtPath:   "foo.crt.pem",
   122  				KeyPath:   "foo.key.pem",
   123  				CACrtPath: "foo_ca.pem",
   124  			},
   125  		},
   126  	}, cfg.ETCDClusters)
   127  	require.Equal(t, 10*time.Second, *cfg.SDConfig.InitTimeout)
   128  
   129  	opts := cfg.NewOptions()
   130  	cluster1, exists := opts.ClusterForZone("z1")
   131  	require.True(t, exists)
   132  	keepAliveOpts := cluster1.KeepAliveOptions()
   133  	require.Equal(t, true, keepAliveOpts.KeepAliveEnabled())
   134  	require.Equal(t, 10*time.Second, keepAliveOpts.KeepAlivePeriod())
   135  	require.Equal(t, 5*time.Second, keepAliveOpts.KeepAlivePeriodMaxJitter())
   136  	require.Equal(t, time.Second, keepAliveOpts.KeepAliveTimeout())
   137  	require.Equal(t, 160*time.Second, cluster1.AutoSyncInterval())
   138  	require.Equal(t, 42*time.Second, cluster1.DialTimeout())
   139  
   140  	cluster2, exists := opts.ClusterForZone("z2")
   141  	require.True(t, exists)
   142  	keepAliveOpts = cluster2.KeepAliveOptions()
   143  	require.Equal(t, true, keepAliveOpts.KeepAliveEnabled())
   144  	require.Equal(t, 20*time.Second, keepAliveOpts.KeepAlivePeriod())
   145  	require.Equal(t, 10*time.Second, keepAliveOpts.KeepAlivePeriodMaxJitter())
   146  	require.Equal(t, 10*time.Second, keepAliveOpts.KeepAliveTimeout())
   147  
   148  	t.Run("TestOptionsNewDirectoryMode", func(t *testing.T) {
   149  		opts := cfg.NewOptions()
   150  		require.Equal(t, defaultDirectoryMode, opts.NewDirectoryMode())
   151  
   152  		const testConfigWithDir = `
   153  env: env1
   154  zone: z1
   155  service: service1
   156  cacheDir: /tmp/cache.json
   157  watchWithRevision: 1
   158  newDirectoryMode: 0744
   159  etcdClusters:
   160    - zone: z1
   161      endpoints:
   162        - etcd1:2379
   163        - etcd2:2379
   164      keepAlive:
   165        enabled: true
   166        period: 10s
   167        jitter: 5s
   168        timeout: 1s
   169      autoSyncInterval: 60s
   170  m3sd:
   171    initTimeout: 10s
   172  `
   173  		var cfg2 Configuration
   174  		require.NoError(t, yaml.Unmarshal([]byte(testConfigWithDir), &cfg2))
   175  		require.Equal(t, os.FileMode(0744), *cfg2.NewDirectoryMode)
   176  	})
   177  }
   178  
   179  func TestDefaultConfig(t *testing.T) {
   180  	cluster := ClusterConfig{}.NewCluster()
   181  	require.Equal(t, defaultDialTimeout, cluster.DialTimeout())
   182  	require.Equal(t, defaultAutoSyncInterval, cluster.AutoSyncInterval())
   183  }
   184  
   185  func TestConfig_negativeAutosync(t *testing.T) {
   186  	cluster := ClusterConfig{
   187  		AutoSyncInterval: -5,
   188  	}.NewCluster()
   189  	require.Equal(t, time.Duration(-5), cluster.AutoSyncInterval())
   190  }