github.com/xmidt-org/webpa-common@v1.11.9/service/servicecfg/environment_test.go (about) 1 package servicecfg 2 3 import ( 4 "errors" 5 "strings" 6 "testing" 7 "time" 8 9 "github.com/go-kit/kit/log" 10 "github.com/hashicorp/consul/api" 11 "github.com/spf13/viper" 12 "github.com/stretchr/testify/assert" 13 "github.com/stretchr/testify/require" 14 "github.com/xmidt-org/webpa-common/logging" 15 "github.com/xmidt-org/webpa-common/service" 16 "github.com/xmidt-org/webpa-common/service/consul" 17 "github.com/xmidt-org/webpa-common/service/zk" 18 "github.com/xmidt-org/webpa-common/xviper" 19 ) 20 21 func testNewEnvironmentEmpty(t *testing.T) { 22 var ( 23 assert = assert.New(t) 24 v = viper.New() 25 ) 26 27 e, err := NewEnvironment(nil, v) 28 assert.Nil(e) 29 assert.Error(err) 30 } 31 32 func testNewEnvironmentUnmarshalError(t *testing.T) { 33 var ( 34 assert = assert.New(t) 35 expectedError = errors.New("expected unmarshal error") 36 u = xviper.InvalidUnmarshaler{expectedError} 37 ) 38 39 e, actualError := NewEnvironment(nil, u) 40 assert.Nil(e) 41 assert.Equal(expectedError, actualError) 42 } 43 44 func testNewEnvironmentFixed(t *testing.T) { 45 var ( 46 assert = assert.New(t) 47 require = require.New(t) 48 49 logger = logging.NewTestLogger(nil, t) 50 v = viper.New() 51 52 configuration = strings.NewReader(` 53 { 54 "fixed": ["instance1.com:1234", "instance2.net:8888"] 55 } 56 `) 57 ) 58 59 v.SetConfigType("json") 60 require.NoError(v.ReadConfig(configuration)) 61 62 e, err := NewEnvironment(logger, v) 63 require.NoError(err) 64 require.NotNil(e) 65 66 i := e.Instancers() 67 assert.Len(i, 1) 68 assert.NotNil(i["fixed"]) 69 70 assert.NoError(e.Close()) 71 } 72 73 func testNewEnvironmentZookeeper(t *testing.T) { 74 defer resetEnvironmentFactories() 75 76 var ( 77 assert = assert.New(t) 78 require = require.New(t) 79 80 logger = logging.NewTestLogger(nil, t) 81 v = viper.New() 82 83 expectedEnvironment = service.NewEnvironment() 84 85 configuration = strings.NewReader(` 86 { 87 "zookeeper": { 88 "client": { 89 "connection": "host1.com:1111,host2.com:2222", 90 "connectTimeout": "10s", 91 "sessionTimeout": "20s" 92 }, 93 "watches": ["/some/where"] 94 } 95 } 96 `) 97 ) 98 99 v.SetConfigType("json") 100 require.NoError(v.ReadConfig(configuration)) 101 102 zookeeperEnvironmentFactory = func(l log.Logger, zo zk.Options, eo ...service.Option) (service.Environment, error) { 103 assert.Equal(logger, l) 104 assert.Equal( 105 zk.Options{ 106 Client: zk.Client{ 107 Connection: "host1.com:1111,host2.com:2222", 108 ConnectTimeout: 10 * time.Second, 109 SessionTimeout: 20 * time.Second, 110 }, 111 Watches: []string{"/some/where"}, 112 }, 113 zo, 114 ) 115 116 return expectedEnvironment, nil 117 } 118 119 actualEnvironment, err := NewEnvironment(logger, v) 120 require.NoError(err) 121 require.NotNil(actualEnvironment) 122 assert.Equal(expectedEnvironment, actualEnvironment) 123 124 assert.NoError(actualEnvironment.Close()) 125 } 126 127 func testNewEnvironmentConsul(t *testing.T) { 128 defer resetEnvironmentFactories() 129 130 var ( 131 assert = assert.New(t) 132 require = require.New(t) 133 134 logger = logging.NewTestLogger(nil, t) 135 v = viper.New() 136 137 expectedEnvironment = service.NewEnvironment() 138 139 configuration = strings.NewReader(` 140 { 141 "consul": { 142 "client": { 143 "address": "localhost:8500", 144 "scheme": "https" 145 }, 146 "registrations": [ 147 { 148 "name": "test", 149 "tags": ["tag1", "tag2"], 150 "address": "foobar.com", 151 "port": 2121 152 }, 153 { 154 "name": "test2", 155 "address": "foobar.com", 156 "port": 3131 157 } 158 ], 159 "watches": [ 160 { 161 "service": "test", 162 "tags": ["tag1"], 163 "passingOnly": true 164 }, 165 { 166 "service": "test2", 167 "passingOnly": false 168 } 169 ] 170 } 171 } 172 `) 173 ) 174 175 v.SetConfigType("json") 176 require.NoError(v.ReadConfig(configuration)) 177 178 consulEnvironmentFactory = func(l log.Logger, registrationScheme string, co consul.Options, eo ...service.Option) (service.Environment, error) { 179 assert.Equal(logger, l) 180 assert.Equal( 181 consul.Options{ 182 Client: &api.Config{ 183 Address: "localhost:8500", 184 Scheme: "https", 185 }, 186 Registrations: []api.AgentServiceRegistration{ 187 api.AgentServiceRegistration{ 188 Name: "test", 189 Tags: []string{"tag1", "tag2"}, 190 Address: "foobar.com", 191 Port: 2121, 192 }, 193 api.AgentServiceRegistration{ 194 Name: "test2", 195 Address: "foobar.com", 196 Port: 3131, 197 }, 198 }, 199 Watches: []consul.Watch{ 200 consul.Watch{ 201 Service: "test", 202 Tags: []string{"tag1"}, 203 PassingOnly: true, 204 }, 205 consul.Watch{ 206 Service: "test2", 207 PassingOnly: false, 208 }, 209 }, 210 }, 211 co, 212 ) 213 214 return expectedEnvironment, nil 215 } 216 217 actualEnvironment, err := NewEnvironment(logger, v) 218 require.NoError(err) 219 require.NotNil(actualEnvironment) 220 assert.Equal(expectedEnvironment, actualEnvironment) 221 222 assert.NoError(actualEnvironment.Close()) 223 } 224 225 func TestNewEnvironment(t *testing.T) { 226 t.Run("Empty", testNewEnvironmentEmpty) 227 t.Run("UnmarshalError", testNewEnvironmentUnmarshalError) 228 t.Run("Fixed", testNewEnvironmentFixed) 229 t.Run("Zookeeper", testNewEnvironmentZookeeper) 230 t.Run("Consul", testNewEnvironmentConsul) 231 }