Skip to content

Commit 6ae04c6

Browse files
authored
Changes to serverAnnotator to support project-decorated/flat hostnames. (#25)
* Changes to serverAnnotator to support project-decorated/flat hostnames. * Switches to using package m-lab/go/host instead of using a custom regex to parse hostname. * Removes unused import regexp. * Returns nil,err if parsing the hostname fails, not using rtx.Must which halts execution. * Assigns host.Parse() to h instead of host. to avoid assigning to a variable named the same as a package. * Changes invalid M-Lab site name 'four0' to 'six02' so that tests can pass. 'four02' violates the new regexp in m-lab/go/host.
1 parent 58c92b0 commit 6ae04c6

File tree

3 files changed

+40
-12
lines changed

3 files changed

+40
-12
lines changed

siteannotator/server.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import (
55
"encoding/json"
66
"errors"
77
"net"
8-
"strings"
98
"sync"
109

10+
"github.com/m-lab/go/host"
1111
"github.com/m-lab/go/rtx"
1212

1313
"github.com/m-lab/tcp-info/inetdiag"
@@ -110,15 +110,14 @@ func (g *siteAnnotator) load(ctx context.Context) (*annotator.ServerAnnotations,
110110
if err != nil {
111111
return nil, err
112112
}
113-
f := strings.Split(g.hostname, ".")
114-
if len(f) < 2 {
115-
return nil, ErrHostnameNotFound
113+
h, err := host.Parse(g.hostname)
114+
if err != nil {
115+
return nil, err
116116
}
117-
site := f[1]
118117
for i := range s {
119-
if s[i].Name == site {
118+
if s[i].Name == h.Site {
120119
result = s[i].Annotation // Copy out of array.
121-
result.Machine = f[0]
120+
result.Machine = h.Machine
122121
g.v4, g.v6, err = parseCIDR(s[i].Network.IPv4, s[i].Network.IPv6)
123122
if err != nil {
124123
return nil, err

siteannotator/server_test.go

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,22 +108,22 @@ func TestNew(t *testing.T) {
108108
name: "success-empty-ipv4-with-ipv6-connection",
109109
localIPs: []net.IP{net.ParseIP("2001:5a0:4300::2")},
110110
provider: localRawfile,
111-
hostname: "mlab1.four0.measurement-lab.org",
111+
hostname: "mlab1.six02.measurement-lab.org",
112112
ID: &inetdiag.SockID{
113113
SPort: 1,
114114
SrcIP: "2001:5a0:4300::2",
115115
DPort: 2,
116116
DstIP: "2600::1",
117117
},
118118
want: annotator.Annotations{
119-
Server: minimalServerAnn("four0"),
119+
Server: minimalServerAnn("six02"),
120120
},
121121
},
122122
{
123123
name: "success-empty-ipv4-with-ipv4-connection",
124124
localIPs: []net.IP{net.ParseIP("64.86.148.137")},
125125
provider: localRawfile,
126-
hostname: "mlab1.four0.measurement-lab.org",
126+
hostname: "mlab1.six02.measurement-lab.org",
127127
ID: &inetdiag.SockID{
128128
SPort: 1,
129129
SrcIP: "64.86.148.137",
@@ -230,6 +230,29 @@ func Test_srvannotator_load(t *testing.T) {
230230
},
231231
},
232232
},
233+
{
234+
name: "success-project-flat-name",
235+
provider: localRawfile,
236+
hostname: "mlab1-lga03.mlab-oti.measurement-lab.org",
237+
want: &annotator.ServerAnnotations{
238+
Site: "lga03",
239+
Machine: "mlab1",
240+
Geo: &annotator.Geolocation{
241+
ContinentCode: "NA",
242+
CountryCode: "US",
243+
City: "New York",
244+
Latitude: 40.7667,
245+
Longitude: -73.8667,
246+
},
247+
Network: &annotator.Network{
248+
ASNumber: 6453,
249+
ASName: "TATA COMMUNICATIONS (AMERICA) INC",
250+
Systems: []annotator.System{
251+
{ASNs: []uint32{6453}},
252+
},
253+
},
254+
},
255+
},
233256
{
234257
name: "success-no-six",
235258
provider: localRawfile,
@@ -275,6 +298,12 @@ func Test_srvannotator_load(t *testing.T) {
275298
hostname: "this-is-not-a-hostname",
276299
wantErr: true,
277300
},
301+
{
302+
name: "error-bad-name-separator",
303+
provider: localRawfile,
304+
hostname: "mlab1=lga03.mlab-oti.measurement-lab.org",
305+
wantErr: true,
306+
},
278307
{
279308
name: "error-hostname-not-in-annotations",
280309
provider: localRawfile,

testdata/annotations.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@
3636
"Network": {
3737
"ASName": "TATA COMMUNICATIONS (AMERICA) INC"
3838
},
39-
"Site": "four0"
39+
"Site": "six02"
4040
},
41-
"Name": "four0",
41+
"Name": "six02",
4242
"Network": {
4343
"IPv4": "",
4444
"IPv6": "2001:5a0:4300::/64"

0 commit comments

Comments
 (0)