Skip to content

Commit f62e790

Browse files
committed
adds ConsulAgent interface to allow for consul testing
1 parent 933c9c5 commit f62e790

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

pkg/tagit/tagit.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,13 @@ type TagIt struct {
2525

2626
// ConsulClient is an interface for the Consul client.
2727
type ConsulClient interface {
28-
Agent() *api.Agent
28+
Agent() ConsulAgent
29+
}
30+
31+
// ConsulClientAgent is an interface for the Consul agent.
32+
type ConsulAgent interface {
33+
Services() (map[string]*api.AgentService, error)
34+
ServiceRegister(*api.AgentServiceRegistration) error
2935
}
3036

3137
// CommandExecutor is an interface for running commands.

pkg/tagit/tagit_test.go

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -290,13 +290,26 @@ func TestRunScript(t *testing.T) {
290290
}
291291
}
292292

293-
// MockConsulClient is a mock implementation of the ConsulClient interface
294-
type MockConsulClient struct{}
293+
// MockConsulClient implements the ConsulClient interface for testing.
294+
type MockConsulClient struct {
295+
MockAgent *MockAgent
296+
}
297+
298+
func (m *MockConsulClient) Agent() ConsulAgent {
299+
return m.MockAgent
300+
}
301+
302+
// MockAgent simulates the Agent part of the Consul client.
303+
type MockAgent struct {
304+
ServicesFunc func() (map[string]*api.AgentService, error)
305+
}
306+
307+
func (m *MockAgent) Services() (map[string]*api.AgentService, error) {
308+
return m.ServicesFunc()
309+
}
295310

296-
// TODO: Implement the Agent method properly so that we can test the rest of the methods
297-
func (m *MockConsulClient) Agent() *api.Agent {
298-
// Return a mock *api.Agent if needed
299-
return &api.Agent{}
311+
func (m *MockAgent) ServiceRegister(reg *api.AgentServiceRegistration) error {
312+
return nil
300313
}
301314

302315
func TestNew(t *testing.T) {

0 commit comments

Comments
 (0)