7
7
)
8
8
9
9
var instId = "my-instance"
10
+ var ip = "192.168.0.1"
10
11
11
12
func TestNewEndpointFromInstance (t * testing.T ) {
12
13
tests := []struct {
@@ -20,14 +21,14 @@ func TestNewEndpointFromInstance(t *testing.T) {
20
21
inst : & types.InstanceSummary {
21
22
Id : & instId ,
22
23
Attributes : map [string ]string {
23
- Ipv4Attr : "192.168.0.1" ,
24
+ Ipv4Attr : ip ,
24
25
PortAttr : "65535" ,
25
26
"custom-attr" : "custom-val" ,
26
27
},
27
28
},
28
29
want : & Endpoint {
29
30
Id : instId ,
30
- IP : "192.168.0.1" ,
31
+ IP : ip ,
31
32
Port : 65535 ,
32
33
Attributes : map [string ]string {
33
34
"custom-attr" : "custom-val" ,
@@ -39,13 +40,35 @@ func TestNewEndpointFromInstance(t *testing.T) {
39
40
inst : & types.InstanceSummary {
40
41
Id : & instId ,
41
42
Attributes : map [string ]string {
42
- Ipv4Attr : "192.168.0.1" ,
43
+ Ipv4Attr : ip ,
43
44
PortAttr : "99999" ,
44
45
"custom-attr" : "custom-val" ,
45
46
},
46
47
},
47
48
wantErr : true ,
48
49
},
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
+ },
49
72
}
50
73
for _ , tt := range tests {
51
74
t .Run (tt .name , func (t * testing.T ) {
@@ -60,3 +83,114 @@ func TestNewEndpointFromInstance(t *testing.T) {
60
83
})
61
84
}
62
85
}
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