Skip to content

Commit 645fdf5

Browse files
authored
Add unit tests for model package (#38)
1 parent e691754 commit 645fdf5

File tree

3 files changed

+141
-6
lines changed

3 files changed

+141
-6
lines changed

pkg/cloudmap/client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ func (sdc *serviceDiscoveryClient) RegisterEndpoints(ctx context.Context, servic
179179

180180
for _, endpt := range service.Endpoints {
181181
go func(endpt *model.Endpoint) {
182-
opId, endptErr := sdc.sdApi.RegisterInstance(ctx, svcId, endpt.Id, endpt.GetAttributes())
182+
opId, endptErr := sdc.sdApi.RegisterInstance(ctx, svcId, endpt.Id, endpt.GetCloudMapAttributes())
183183
opCollector.Add(endpt.Id, opId, endptErr)
184184
}(endpt)
185185
}

pkg/model/types.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ func NewEndpointFromInstance(inst *types.InstanceSummary) (*Endpoint, error) {
6262
return &endpoint, nil
6363
}
6464

65-
// GetAttributes extracts endpoint attributes for Cloud Map service instance registration
66-
func (e *Endpoint) GetAttributes() map[string]string {
65+
// GetCloudMapAttributes extracts endpoint attributes for Cloud Map service instance registration
66+
func (e *Endpoint) GetCloudMapAttributes() map[string]string {
6767
attrs := make(map[string]string, 0)
6868

6969
attrs[Ipv4Attr] = e.IP
@@ -78,6 +78,7 @@ func (e *Endpoint) GetAttributes() map[string]string {
7878
return attrs
7979
}
8080

81+
// Equals evaluates if two Endpoints are "deeply equal" (including all fields)
8182
func (e *Endpoint) Equals(other *Endpoint) bool {
8283
return reflect.DeepEqual(e, other)
8384
}

pkg/model/types_test.go

Lines changed: 137 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
)
88

99
var instId = "my-instance"
10+
var ip = "192.168.0.1"
1011

1112
func TestNewEndpointFromInstance(t *testing.T) {
1213
tests := []struct {
@@ -20,14 +21,14 @@ func TestNewEndpointFromInstance(t *testing.T) {
2021
inst: &types.InstanceSummary{
2122
Id: &instId,
2223
Attributes: map[string]string{
23-
Ipv4Attr: "192.168.0.1",
24+
Ipv4Attr: ip,
2425
PortAttr: "65535",
2526
"custom-attr": "custom-val",
2627
},
2728
},
2829
want: &Endpoint{
2930
Id: instId,
30-
IP: "192.168.0.1",
31+
IP: ip,
3132
Port: 65535,
3233
Attributes: map[string]string{
3334
"custom-attr": "custom-val",
@@ -39,13 +40,35 @@ func TestNewEndpointFromInstance(t *testing.T) {
3940
inst: &types.InstanceSummary{
4041
Id: &instId,
4142
Attributes: map[string]string{
42-
Ipv4Attr: "192.168.0.1",
43+
Ipv4Attr: ip,
4344
PortAttr: "99999",
4445
"custom-attr": "custom-val",
4546
},
4647
},
4748
wantErr: true,
4849
},
50+
{
51+
name: "missing IP",
52+
inst: &types.InstanceSummary{
53+
Id: &instId,
54+
Attributes: map[string]string{
55+
PortAttr: "80",
56+
"custom-attr": "custom-val",
57+
},
58+
},
59+
wantErr: true,
60+
},
61+
{
62+
name: "missing port",
63+
inst: &types.InstanceSummary{
64+
Id: &instId,
65+
Attributes: map[string]string{
66+
Ipv4Attr: ip,
67+
"custom-attr": "custom-val",
68+
},
69+
},
70+
wantErr: true,
71+
},
4972
}
5073
for _, tt := range tests {
5174
t.Run(tt.name, func(t *testing.T) {
@@ -60,3 +83,114 @@ func TestNewEndpointFromInstance(t *testing.T) {
6083
})
6184
}
6285
}
86+
87+
func TestEndpoint_GetAttributes(t *testing.T) {
88+
type fields struct {
89+
Id string
90+
IP string
91+
Port int32
92+
Attributes map[string]string
93+
}
94+
tests := []struct {
95+
name string
96+
fields fields
97+
want map[string]string
98+
}{
99+
{
100+
name: "happy case",
101+
fields: fields{
102+
IP: ip,
103+
Port: 30,
104+
Attributes: map[string]string{
105+
"custom-attr": "custom-val",
106+
},
107+
},
108+
want: map[string]string{
109+
Ipv4Attr: ip,
110+
PortAttr: "30",
111+
"custom-attr": "custom-val",
112+
},
113+
},
114+
}
115+
for _, tt := range tests {
116+
t.Run(tt.name, func(t *testing.T) {
117+
e := &Endpoint{
118+
Id: tt.fields.Id,
119+
IP: tt.fields.IP,
120+
Port: tt.fields.Port,
121+
Attributes: tt.fields.Attributes,
122+
}
123+
if got := e.GetCloudMapAttributes(); !reflect.DeepEqual(got, tt.want) {
124+
t.Errorf("GetAttributes() = %v, want %v", got, tt.want)
125+
}
126+
})
127+
}
128+
}
129+
130+
func TestEndpointIdFromIPAddress(t *testing.T) {
131+
tests := []struct {
132+
name string
133+
address string
134+
want string
135+
}{
136+
{
137+
name: "happy case",
138+
address: "192.168.0.1",
139+
want: "192_168_0_1",
140+
},
141+
}
142+
for _, tt := range tests {
143+
t.Run(tt.name, func(t *testing.T) {
144+
if got := EndpointIdFromIPAddress(tt.address); got != tt.want {
145+
t.Errorf("EndpointIdFromIPAddress() = %v, want %v", got, tt.want)
146+
}
147+
})
148+
}
149+
}
150+
151+
func TestEndpoint_Equals(t *testing.T) {
152+
firstEndpoint := Endpoint{
153+
Id: instId,
154+
IP: ip,
155+
Port: 80,
156+
Attributes: map[string]string{
157+
"custom-key": "custom-val",
158+
},
159+
}
160+
161+
secondEndpoint := Endpoint{
162+
Id: instId,
163+
IP: ip,
164+
Port: 80,
165+
Attributes: map[string]string{
166+
"custom-key": "different-val",
167+
},
168+
}
169+
170+
tests := []struct {
171+
name string
172+
x Endpoint
173+
y Endpoint
174+
want bool
175+
}{
176+
{
177+
name: "identical",
178+
x: firstEndpoint,
179+
y: firstEndpoint,
180+
want: true,
181+
},
182+
{
183+
name: "different",
184+
x: firstEndpoint,
185+
y: secondEndpoint,
186+
want: false,
187+
},
188+
}
189+
for _, tt := range tests {
190+
t.Run(tt.name, func(t *testing.T) {
191+
if got := tt.x.Equals(&tt.y); got != tt.want {
192+
t.Errorf("Equals() = %v, want %v", got, tt.want)
193+
}
194+
})
195+
}
196+
}

0 commit comments

Comments
 (0)