github.com/xmidt-org/webpa-common@v1.11.9/service/consul/environment_test.go (about) 1 package consul 2 3 import ( 4 "testing" 5 6 "github.com/hashicorp/consul/api" 7 "github.com/stretchr/testify/assert" 8 "github.com/stretchr/testify/mock" 9 "github.com/stretchr/testify/require" 10 "github.com/xmidt-org/webpa-common/logging" 11 "github.com/xmidt-org/webpa-common/service" 12 ) 13 14 func testNewEnvironmentEmpty(t *testing.T) { 15 defer resetClientFactory() 16 17 var ( 18 assert = assert.New(t) 19 clientFactory = prepareMockClientFactory() 20 ) 21 22 e, err := NewEnvironment(nil, "http", Options{}) 23 assert.Nil(e) 24 assert.Equal(service.ErrIncomplete, err) 25 26 clientFactory.AssertExpectations(t) 27 } 28 29 func testNewEnvironmentClientError(t *testing.T) { 30 defer resetClientFactory() 31 32 var ( 33 assert = assert.New(t) 34 clientFactory = prepareMockClientFactory() 35 36 co = Options{ 37 Client: &api.Config{ 38 Address: "here is an unknown scheme://grabthar.hammer.com:1856", 39 }, 40 Watches: []Watch{ 41 Watch{ 42 Service: "foobar", 43 Tags: []string{"tag1"}, 44 PassingOnly: true, 45 }, 46 }, 47 } 48 ) 49 50 e, err := NewEnvironment(nil, "http", co) 51 assert.Nil(e) 52 assert.Error(err) 53 54 clientFactory.AssertExpectations(t) 55 } 56 57 func testNewEnvironmentFull(t *testing.T) { 58 defer resetClientFactory() 59 60 var ( 61 assert = assert.New(t) 62 require = require.New(t) 63 64 logger = logging.NewTestLogger(nil, t) 65 clientFactory = prepareMockClientFactory() 66 client = new(mockClient) 67 ttlUpdater = new(mockTTLUpdater) 68 69 co = Options{ 70 Client: &api.Config{ 71 Address: "localhost:8500", 72 Scheme: "https", 73 }, 74 Registrations: []api.AgentServiceRegistration{ 75 api.AgentServiceRegistration{ 76 ID: "service1", 77 Address: "grubly.com", 78 Port: 1111, 79 }, 80 api.AgentServiceRegistration{ 81 ID: "service2", 82 Address: "grubly.com", 83 Port: 1111, 84 }, // duplicates should be ignored 85 }, 86 Watches: []Watch{ 87 Watch{ 88 Service: "foobar", 89 Tags: []string{"tag1"}, 90 PassingOnly: true, 91 }, 92 Watch{ 93 Service: "foobar", 94 Tags: []string{"tag1"}, 95 PassingOnly: true, 96 }, // duplicates should be ignored 97 }, 98 } 99 ) 100 101 clientFactory.On("NewClient", mock.MatchedBy(func(*api.Client) bool { return true })).Return(client, ttlUpdater).Once() 102 103 client.On("Service", 104 "foobar", 105 "tag1", 106 true, 107 mock.MatchedBy(func(qo *api.QueryOptions) bool { return qo != nil }), 108 ).Return([]*api.ServiceEntry{}, new(api.QueryMeta), error(nil)) 109 110 client.On("Register", 111 mock.MatchedBy(func(r *api.AgentServiceRegistration) bool { 112 return r.Address == "grubly.com" && r.Port == 1111 113 }), 114 ).Return(error(nil)).Once() 115 116 client.On("Deregister", 117 mock.MatchedBy(func(r *api.AgentServiceRegistration) bool { 118 return r.Address == "grubly.com" && r.Port == 1111 119 }), 120 ).Return(error(nil)).Twice() 121 122 e, err := NewEnvironment(logger, "", co) 123 require.NoError(err) 124 require.NotNil(e) 125 126 _, ok := e.(Environment) 127 assert.True(ok) 128 129 e.Register() 130 e.Deregister() 131 132 assert.NoError(e.Close()) 133 134 clientFactory.AssertExpectations(t) 135 client.AssertExpectations(t) 136 ttlUpdater.AssertExpectations(t) 137 } 138 139 func TestNewEnvironment(t *testing.T) { 140 t.Run("Empty", testNewEnvironmentEmpty) 141 t.Run("ClientError", testNewEnvironmentClientError) 142 t.Run("Full", testNewEnvironmentFull) 143 }