github.com/m3db/m3@v1.5.0/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: 60s 68 - zone: z2 69 endpoints: 70 - etcd3:2379 71 - etcd4:2379 72 tls: 73 crtPath: foo.crt.pem 74 keyPath: foo.key.pem 75 - zone: z3 76 endpoints: 77 - etcd5:2379 78 - etcd6:2379 79 tls: 80 crtPath: foo.crt.pem 81 keyPath: foo.key.pem 82 caCrtPath: foo_ca.pem 83 m3sd: 84 initTimeout: 10s 85 ` 86 87 var cfg Configuration 88 require.NoError(t, yaml.Unmarshal([]byte(testConfig), &cfg)) 89 90 require.Equal(t, "env1", cfg.Env) 91 require.Equal(t, "z1", cfg.Zone) 92 require.Equal(t, "service1", cfg.Service) 93 require.Equal(t, "/tmp/cache.json", cfg.CacheDir) 94 require.Equal(t, int64(1), cfg.WatchWithRevision) 95 require.Equal(t, []ClusterConfig{ 96 ClusterConfig{ 97 Zone: "z1", 98 Endpoints: []string{"etcd1:2379", "etcd2:2379"}, 99 KeepAlive: &KeepAliveConfig{ 100 Enabled: true, 101 Period: 10 * time.Second, 102 Jitter: 5 * time.Second, 103 Timeout: time.Second, 104 }, 105 AutoSyncInterval: time.Second * 60, 106 }, 107 ClusterConfig{ 108 Zone: "z2", 109 Endpoints: []string{"etcd3:2379", "etcd4:2379"}, 110 TLS: &TLSConfig{ 111 CrtPath: "foo.crt.pem", 112 KeyPath: "foo.key.pem", 113 }, 114 }, 115 ClusterConfig{ 116 Zone: "z3", 117 Endpoints: []string{"etcd5:2379", "etcd6:2379"}, 118 TLS: &TLSConfig{ 119 CrtPath: "foo.crt.pem", 120 KeyPath: "foo.key.pem", 121 CACrtPath: "foo_ca.pem", 122 }, 123 }, 124 }, cfg.ETCDClusters) 125 require.Equal(t, 10*time.Second, *cfg.SDConfig.InitTimeout) 126 127 opts := cfg.NewOptions() 128 cluster1, exists := opts.ClusterForZone("z1") 129 require.True(t, exists) 130 keepAliveOpts := cluster1.KeepAliveOptions() 131 require.Equal(t, true, keepAliveOpts.KeepAliveEnabled()) 132 require.Equal(t, 10*time.Second, keepAliveOpts.KeepAlivePeriod()) 133 require.Equal(t, 5*time.Second, keepAliveOpts.KeepAlivePeriodMaxJitter()) 134 require.Equal(t, time.Second, keepAliveOpts.KeepAliveTimeout()) 135 require.Equal(t, 60*time.Second, cluster1.AutoSyncInterval()) 136 137 cluster2, exists := opts.ClusterForZone("z2") 138 require.True(t, exists) 139 keepAliveOpts = cluster2.KeepAliveOptions() 140 require.Equal(t, true, keepAliveOpts.KeepAliveEnabled()) 141 require.Equal(t, 20*time.Second, keepAliveOpts.KeepAlivePeriod()) 142 require.Equal(t, 10*time.Second, keepAliveOpts.KeepAlivePeriodMaxJitter()) 143 require.Equal(t, 10*time.Second, keepAliveOpts.KeepAliveTimeout()) 144 145 t.Run("TestOptionsNewDirectoryMode", func(t *testing.T) { 146 opts := cfg.NewOptions() 147 require.Equal(t, defaultDirectoryMode, opts.NewDirectoryMode()) 148 149 const testConfigWithDir = ` 150 env: env1 151 zone: z1 152 service: service1 153 cacheDir: /tmp/cache.json 154 watchWithRevision: 1 155 newDirectoryMode: 0744 156 etcdClusters: 157 - zone: z1 158 endpoints: 159 - etcd1:2379 160 - etcd2:2379 161 keepAlive: 162 enabled: true 163 period: 10s 164 jitter: 5s 165 timeout: 1s 166 autoSyncInterval: 60s 167 m3sd: 168 initTimeout: 10s 169 ` 170 var cfg2 Configuration 171 require.NoError(t, yaml.Unmarshal([]byte(testConfigWithDir), &cfg2)) 172 require.Equal(t, os.FileMode(0744), *cfg2.NewDirectoryMode) 173 }) 174 }