Skip to content

Commit c0c4192

Browse files
authored
make UT stable on Mac by getting rid of gomonkey code (#704)
Signed-off-by: spacewander <[email protected]>
1 parent f92a7b8 commit c0c4192

File tree

1 file changed

+32
-47
lines changed

1 file changed

+32
-47
lines changed

controller/registries/consul/config_test.go

+32-47
Original file line numberDiff line numberDiff line change
@@ -68,26 +68,15 @@ func TestStart(t *testing.T) {
6868
lock: sync.RWMutex{},
6969
}
7070

71-
config := &consul.Config{}
72-
73-
patches := gomonkey.ApplyPrivateMethod(reflect.TypeOf(reg), "fetchAllServices", func(_ *Consul, client *Client) (map[consulService]bool, error) {
74-
return map[consulService]bool{
75-
{ServiceName: "service1", Tag: "tag1"}: true,
76-
{ServiceName: "service2", Tag: "tag2"}: true,
77-
}, nil
78-
})
79-
patches.ApplyPrivateMethod(reg, "subscribe", func(tag, serviceName string) error { return nil })
80-
patches.ApplyPrivateMethod(reg, "unsubscribe", func(serviceName string) error { return nil })
81-
patches.ApplyPrivateMethod(reg, "removeService", func(key consulService) {})
71+
config := &consul.Config{
72+
ServerUrl: "::::::::::::",
73+
}
8274

8375
err := reg.Start(config)
84-
assert.Nil(t, err)
85-
76+
assert.Error(t, err)
8677
err = reg.Stop()
8778
assert.Nil(t, err)
8879

89-
patches.Reset()
90-
9180
reg = &Consul{
9281
logger: log.NewLogger(&log.RegistryLoggerOptions{
9382
Name: "test",
@@ -96,15 +85,25 @@ func TestStart(t *testing.T) {
9685
subscriptions: make(map[string]*watch.Plan),
9786
done: make(chan struct{}),
9887
lock: sync.RWMutex{},
88+
store: registry.FakeServiceEntryStore(),
9989
}
10090

101-
config = &consul.Config{
102-
ServerUrl: "::::::::::::",
103-
}
91+
config = &consul.Config{}
92+
93+
patches := gomonkey.ApplyPrivateMethod(reg, "fetchAllServices", func(_ *Consul, client *Client) (map[consulService]bool, error) {
94+
return map[consulService]bool{
95+
{ServiceName: "service1", Tag: "tag1"}: true,
96+
{ServiceName: "service2", Tag: "tag2"}: true,
97+
}, nil
98+
})
99+
patches.ApplyPrivateMethod(reg, "subscribe", func(serviceName string) error { return nil })
100+
defer patches.Reset()
104101

105102
err = reg.Start(config)
106-
assert.Error(t, err)
107-
close(reg.done)
103+
assert.Nil(t, err)
104+
105+
err = reg.Stop()
106+
assert.Nil(t, err)
108107
}
109108

110109
func TestRefresh(t *testing.T) {
@@ -129,7 +128,7 @@ func TestRefresh(t *testing.T) {
129128
"service2": {"tag1"},
130129
}
131130

132-
patches := gomonkey.ApplyPrivateMethod(reflect.TypeOf(reg), "fetchAllServices", func(_ *Consul, client *Client) (map[consulService]bool, error) {
131+
patches := gomonkey.ApplyPrivateMethod(reg, "fetchAllServices", func(_ *Consul, client *Client) (map[consulService]bool, error) {
133132
return map[consulService]bool{
134133
{ServiceName: "service1", Tag: "tag1"}: true,
135134
{ServiceName: "service2", Tag: "tag2"}: true,
@@ -180,7 +179,7 @@ func TestFetchAllServices(t *testing.T) {
180179
Token: "token",
181180
}
182181

183-
patches := gomonkey.ApplyMethod(reflect.TypeOf(client.consulCatalog), "Services", func(_ *api.Catalog, q *api.QueryOptions) (map[string][]string, *api.QueryMeta, error) {
182+
patches := gomonkey.ApplyMethod(client.consulCatalog, "Services", func(_ *api.Catalog, q *api.QueryOptions) (map[string][]string, *api.QueryMeta, error) {
184183
return map[string][]string{
185184
"service1": {"tag1", "tag2"},
186185
"service2": {"tag3"},
@@ -209,7 +208,7 @@ func TestFetchAllServices(t *testing.T) {
209208
Token: "token",
210209
}
211210

212-
patches := gomonkey.ApplyMethod(reflect.TypeOf(client.consulCatalog), "Services", func(_ *api.Catalog, q *api.QueryOptions) (map[string][]string, *api.QueryMeta, error) {
211+
patches := gomonkey.ApplyMethod(client.consulCatalog, "Services", func(_ *api.Catalog, q *api.QueryOptions) (map[string][]string, *api.QueryMeta, error) {
213212
return nil, nil, errors.New("mock error")
214213
})
215214
defer patches.Reset()
@@ -308,30 +307,16 @@ func TestGetServiceEntryKey(t *testing.T) {
308307
}
309308
}
310309

311-
type fakeServiceEntryStore struct {
312-
}
313-
314-
func (f *fakeServiceEntryStore) Delete(service string) {
315-
}
316-
317-
func (f *fakeServiceEntryStore) Update(service string, se *registry.ServiceEntryWrapper) {
318-
}
319-
320310
func TestGetSubscribeCallback(t *testing.T) {
321311
reg := &Consul{
322-
store: &fakeServiceEntryStore{},
312+
store: registry.FakeServiceEntryStore(),
323313
stopped: atomic.Bool{},
314+
client: &Client{
315+
NameSpace: "default",
316+
DataCenter: "default-dc",
317+
},
324318
}
325319

326-
patch := gomonkey.ApplyPrivateMethod(reflect.TypeOf(reg), "getServiceEntryKey", func(_ *Consul, serviceName string) string {
327-
return "test.default.default-dc.earth.consul"
328-
})
329-
defer patch.Reset()
330-
331-
patch.ApplyPrivateMethod(reflect.TypeOf(reg), "generateServiceEntry", func(_ *Consul, host string, services []*api.ServiceEntry) *registry.ServiceEntryWrapper {
332-
return &registry.ServiceEntryWrapper{}
333-
})
334-
335320
callback := reg.getSubscribeCallback("", "test-service")
336321

337322
var services []*api.ServiceEntry
@@ -347,7 +332,7 @@ func TestReload(t *testing.T) {
347332
logger: log.NewLogger(&log.RegistryLoggerOptions{
348333
Name: "test",
349334
}),
350-
store: &fakeServiceEntryStore{},
335+
store: registry.FakeServiceEntryStore(),
351336
lock: sync.RWMutex{},
352337
}
353338

@@ -360,17 +345,17 @@ func TestReload(t *testing.T) {
360345
})
361346

362347
service := consulService{"test-service", "new-datacenter"}
363-
patches.ApplyPrivateMethod(reflect.TypeOf(reg), "fetchAllServices", func(client *Client) (map[consulService]bool, error) {
348+
patches.ApplyPrivateMethod(reg, "fetchAllServices", func(client *Client) (map[consulService]bool, error) {
364349
return map[consulService]bool{
365350
service: true,
366351
}, nil
367352
})
368353

369-
patches.ApplyPrivateMethod(reflect.TypeOf(reg), "subscribe", func(_ *Consul, serviceName string) error {
354+
patches.ApplyPrivateMethod(reg, "subscribe", func(_ *Consul, serviceName string) error {
370355
return nil
371356
})
372357

373-
patches.ApplyPrivateMethod(reflect.TypeOf(reg), "unsubscribe", func(_ *Consul, serviceName string) error {
358+
patches.ApplyPrivateMethod(reg, "unsubscribe", func(_ *Consul, serviceName string) error {
374359
return nil
375360
})
376361
defer patches.Reset()
@@ -395,7 +380,7 @@ func TestSubscribe(t *testing.T) {
395380
logger: log.NewLogger(&log.RegistryLoggerOptions{
396381
Name: "test",
397382
}),
398-
store: &fakeServiceEntryStore{},
383+
store: registry.FakeServiceEntryStore(),
399384
lock: sync.RWMutex{},
400385
}
401386

0 commit comments

Comments
 (0)