Skip to content

Commit 70703ec

Browse files
committed
(feat) start moving things to a factory to simplify the test
1 parent 4fb04a0 commit 70703ec

File tree

2 files changed

+33
-41
lines changed

2 files changed

+33
-41
lines changed

pkg/tagit/tagit.go

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111

1212
"github.com/google/shlex"
1313
"github.com/hashicorp/consul/api"
14+
"github.com/ncode/tagit/pkg/consul"
1415
)
1516

1617
// TagIt is the main struct for the tagit flow.
@@ -19,36 +20,11 @@ type TagIt struct {
1920
Script string
2021
Interval time.Duration
2122
TagPrefix string
22-
client ConsulClient
23+
client consul.ConsulClient
2324
commandExecutor CommandExecutor
2425
logger *slog.Logger
2526
}
2627

27-
// ConsulClient is an interface for the Consul client.
28-
type ConsulClient interface {
29-
Agent() ConsulAgent
30-
}
31-
32-
// ConsulAgent is an interface for the Consul agent.
33-
type ConsulAgent interface {
34-
Service(string, *api.QueryOptions) (*api.AgentService, *api.QueryMeta, error)
35-
ServiceRegister(*api.AgentServiceRegistration) error
36-
}
37-
38-
// ConsulAPIWrapper wraps the Consul API client to conform to the ConsulClient interface.
39-
type ConsulAPIWrapper struct {
40-
client *api.Client
41-
}
42-
43-
// NewConsulAPIWrapper creates a new instance of ConsulAPIWrapper.
44-
func NewConsulAPIWrapper(client *api.Client) *ConsulAPIWrapper {
45-
return &ConsulAPIWrapper{client: client}
46-
}
47-
48-
// Agent returns an object that conforms to the ConsulAgent interface.
49-
func (w *ConsulAPIWrapper) Agent() ConsulAgent {
50-
return w.client.Agent()
51-
}
5228

5329
// CommandExecutor is an interface for running commands.
5430
type CommandExecutor interface {
@@ -72,7 +48,7 @@ func (e *CmdExecutor) Execute(command string) ([]byte, error) {
7248
}
7349

7450
// New creates a new TagIt struct.
75-
func New(consulClient ConsulClient, commandExecutor CommandExecutor, serviceID string, script string, interval time.Duration, tagPrefix string, logger *slog.Logger) *TagIt {
51+
func New(consulClient consul.ConsulClient, commandExecutor CommandExecutor, serviceID string, script string, interval time.Duration, tagPrefix string, logger *slog.Logger) *TagIt {
7652
return &TagIt{
7753
ServiceID: serviceID,
7854
Script: script,

pkg/tagit/tagit_test.go

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"time"
1212

1313
"github.com/hashicorp/consul/api"
14+
"github.com/ncode/tagit/pkg/consul"
1415
"github.com/stretchr/testify/assert"
1516
)
1617

@@ -19,7 +20,7 @@ type MockConsulClient struct {
1920
MockAgent *MockAgent
2021
}
2122

22-
func (m *MockConsulClient) Agent() ConsulAgent {
23+
func (m *MockConsulClient) Agent() consul.ConsulAgent {
2324
return m.MockAgent
2425
}
2526

@@ -596,19 +597,34 @@ func TestRun(t *testing.T) {
596597
assert.LessOrEqual(t, updateServiceTagsCalled.Load(), int32(4), "Expected updateServiceTags to be called at most 4 times")
597598
}
598599

599-
func TestNewConsulAPIWrapper(t *testing.T) {
600-
consulClient, err := api.NewClient(api.DefaultConfig())
601-
assert.NoError(t, err, "Failed to create Consul client")
602-
603-
wrapper := NewConsulAPIWrapper(consulClient)
604-
605-
assert.NotNil(t, wrapper, "NewConsulAPIWrapper returned nil")
606-
607-
_, isConsulClient := interface{}(wrapper).(ConsulClient)
608-
assert.True(t, isConsulClient, "NewConsulAPIWrapper does not implement ConsulClient interface")
609-
610-
_, isConsulAgent := wrapper.Agent().(ConsulAgent)
611-
assert.True(t, isConsulAgent, "Wrapper's Agent method does not return a ConsulAgent")
600+
func TestConsulInterfaceCompatibility(t *testing.T) {
601+
// Test that our mocks implement the consul package interfaces correctly
602+
mockAgent := &MockAgent{
603+
ServiceFunc: func(serviceID string, q *api.QueryOptions) (*api.AgentService, *api.QueryMeta, error) {
604+
return &api.AgentService{ID: serviceID}, nil, nil
605+
},
606+
ServiceRegisterFunc: func(reg *api.AgentServiceRegistration) error {
607+
return nil
608+
},
609+
}
610+
611+
mockClient := &MockConsulClient{
612+
MockAgent: mockAgent,
613+
}
614+
615+
// Verify that MockConsulClient implements consul.ConsulClient
616+
var _ consul.ConsulClient = mockClient
617+
618+
// Verify that MockAgent implements consul.ConsulAgent
619+
var _ consul.ConsulAgent = mockAgent
620+
621+
// Test that the mock client works correctly
622+
agent := mockClient.Agent()
623+
assert.NotNil(t, agent, "Agent() should return non-nil")
624+
625+
service, _, err := agent.Service("test-service", nil)
626+
assert.NoError(t, err)
627+
assert.Equal(t, "test-service", service.ID)
612628
}
613629

614630
func TestCmdExecutor_Execute(t *testing.T) {

0 commit comments

Comments
 (0)