github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/dbnode/discovery/config_test.go (about) 1 // Copyright (c) 2020 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 discovery 22 23 import ( 24 "io/ioutil" 25 "os" 26 "testing" 27 28 "github.com/stretchr/testify/assert" 29 30 "github.com/m3db/m3/src/dbnode/environment" 31 "github.com/m3db/m3/src/x/config" 32 ) 33 34 func TestM3DBSingleNodeType(t *testing.T) { 35 in := ` 36 type: m3db_single_node 37 ` 38 39 hostID := "test_id" 40 envConfig := getEnvConfig(t, in, hostID) 41 42 assert.Equal(t, 1, len(envConfig.Services)) 43 assert.Equal(t, 1, len(envConfig.SeedNodes.InitialCluster)) 44 45 s := envConfig.Services[0].Service 46 assert.Equal(t, defaultM3DBService, s.Service) 47 assert.Equal(t, defaultEnvironment, s.Env) 48 assert.Equal(t, defaultZone, s.Zone) 49 assert.Equal(t, defaultCacheDirectory, s.CacheDir) 50 assert.Equal(t, 1, len(s.ETCDClusters)) 51 assert.Equal(t, defaultZone, s.ETCDClusters[0].Zone) 52 assert.Equal(t, 1, len(s.ETCDClusters[0].Endpoints)) 53 assert.Equal(t, defaultSingleNodeClusterEndpoint, s.ETCDClusters[0].Endpoints[0]) 54 55 c := envConfig.SeedNodes.InitialCluster[0] 56 assert.Equal(t, defaultSingleNodeClusterSeedEndpoint, c.Endpoint) 57 assert.Equal(t, hostID, c.HostID) 58 } 59 60 func TestM3DBClusterType(t *testing.T) { 61 in := ` 62 type: m3db_cluster 63 m3dbCluster: 64 env: a 65 zone: b 66 endpoints: 67 - end_1 68 - end_2 69 ` 70 71 envConfig := getEnvConfig(t, in, "") 72 validateClusterConfig(t, envConfig, defaultM3DBService) 73 } 74 75 func TestM3AggregatorClusterType(t *testing.T) { 76 in := ` 77 type: m3aggregator_cluster 78 m3AggregatorCluster: 79 env: a 80 zone: b 81 endpoints: 82 - end_1 83 - end_2 84 ` 85 86 envConfig := getEnvConfig(t, in, "") 87 validateClusterConfig(t, envConfig, defaultM3AggregatorService) 88 } 89 90 func TestConfigType(t *testing.T) { 91 in := ` 92 config: 93 service: 94 env: test_env 95 zone: test_zone 96 service: test_service 97 cacheDir: test/cache 98 etcdClusters: 99 - zone: test_zone_2 100 endpoints: 101 - 127.0.0.1:2379 102 seedNodes: 103 initialCluster: 104 - hostID: host_id 105 endpoint: http://127.0.0.1:2380 106 ` 107 108 hostID := "test_id" 109 envConfig := getEnvConfig(t, in, hostID) 110 111 assert.Equal(t, 1, len(envConfig.Services)) 112 assert.Equal(t, 1, len(envConfig.SeedNodes.InitialCluster)) 113 114 s := envConfig.Services[0].Service 115 assert.Equal(t, "test_service", s.Service) 116 assert.Equal(t, "test_env", s.Env) 117 assert.Equal(t, "test_zone", s.Zone) 118 assert.Equal(t, "test/cache", s.CacheDir) 119 assert.Equal(t, 1, len(s.ETCDClusters)) 120 assert.Equal(t, "test_zone_2", s.ETCDClusters[0].Zone) 121 assert.Equal(t, 1, len(s.ETCDClusters[0].Endpoints)) 122 assert.Equal(t, "127.0.0.1:2379", s.ETCDClusters[0].Endpoints[0]) 123 124 c := envConfig.SeedNodes.InitialCluster[0] 125 assert.Equal(t, "http://127.0.0.1:2380", c.Endpoint) 126 assert.Equal(t, "host_id", c.HostID) 127 } 128 129 func getEnvConfig(t *testing.T, in string, hostID string) environment.Configuration { 130 fd, err := ioutil.TempFile("", "config.yaml") 131 assert.NoError(t, err) 132 defer func() { 133 assert.NoError(t, fd.Close()) 134 assert.NoError(t, os.Remove(fd.Name())) 135 }() 136 137 _, err = fd.Write([]byte(in)) 138 assert.NoError(t, err) 139 140 var cfg Configuration 141 err = config.LoadFile(&cfg, fd.Name(), config.Options{}) 142 assert.NoError(t, err) 143 144 envConfig, err := cfg.EnvironmentConfig(hostID) 145 assert.NoError(t, err) 146 147 return envConfig 148 } 149 150 func validateClusterConfig(t *testing.T, 151 envConfig environment.Configuration, 152 expectedService string, 153 ) { 154 assert.Equal(t, 1, len(envConfig.Services)) 155 assert.Nil(t, envConfig.SeedNodes) 156 s := envConfig.Services[0].Service 157 assert.Equal(t, expectedService, s.Service) 158 assert.Equal(t, "a", s.Env) 159 assert.Equal(t, "b", s.Zone) 160 assert.Equal(t, defaultCacheDirectory, s.CacheDir) 161 assert.Equal(t, 1, len(s.ETCDClusters)) 162 assert.Equal(t, "b", s.ETCDClusters[0].Zone) 163 assert.Equal(t, 2, len(s.ETCDClusters[0].Endpoints)) 164 assert.Equal(t, "end_1", s.ETCDClusters[0].Endpoints[0]) 165 assert.Equal(t, "end_2", s.ETCDClusters[0].Endpoints[1]) 166 }