Skip to content

Commit c2556e7

Browse files
v3 host.Names use equivalent service and machine names (#186)
* v3 service and machine names match
1 parent 96eb26f commit c2556e7

File tree

2 files changed

+19
-11
lines changed

2 files changed

+19
-11
lines changed

host/host.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -222,9 +222,13 @@ func parseHostV3(f []string) (Name, error) {
222222
// Returns a typical M-Lab machine hostname
223223
// Example: mlab2-abc01.mlab-sandbox.measurement-lab.org
224224
func (n Name) String() string {
225+
if (n == Name{}) {
226+
return ""
227+
}
225228
switch n.Version {
226229
case "v3":
227-
return fmt.Sprintf("%s-%s.%s.%s.%s", n.Site, n.Machine, n.Org, n.Project, n.Domain)
230+
// NOTE: v3 names include the service and are equivalent to the machine name.
231+
return fmt.Sprintf("%s-%s-%s.%s.%s.%s", n.Service, n.Site, n.Machine, n.Org, n.Project, n.Domain)
228232
case "v2":
229233
return fmt.Sprintf("%s-%s.%s.%s", n.Machine, n.Site, n.Project, n.Domain)
230234
default:
@@ -235,11 +239,11 @@ func (n Name) String() string {
235239
// Returns an M-lab hostname with any service name preserved
236240
// Example: ndt-mlab1-abc01.mlab-sandbox.measurement-lab.org
237241
func (n Name) StringWithService() string {
238-
if n.Service != "" {
239-
return fmt.Sprintf("%s-%s", n.Service, n.String())
240-
} else {
242+
if n.Version == "v3" || n.Service == "" {
243+
// v3 names are equivalent to the machine name.
241244
return n.String()
242245
}
246+
return fmt.Sprintf("%s-%s", n.Service, n.String())
243247
}
244248

245249
// Returns an M-lab hostname with any suffix preserved

host/host_test.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"reflect"
55
"testing"
66

7-
"github.com/m-lab/go/rtx"
7+
"github.com/m-lab/go/testingx"
88
)
99

1010
func TestName(t *testing.T) {
@@ -295,18 +295,22 @@ func TestName_String(t *testing.T) {
295295
},
296296
{
297297
name: "ndt-lol12345-abcdef01.mlab.sandbox.measurement-lab.org",
298-
want: "lol12345-abcdef01.mlab.sandbox.measurement-lab.org",
298+
want: "ndt-lol12345-abcdef01.mlab.sandbox.measurement-lab.org",
299299
},
300300
}
301301
for _, tt := range tests {
302302
t.Run(tt.name, func(t *testing.T) {
303303
n, err := Parse(tt.name)
304-
rtx.Must(err, "Failed to parse: %s", tt.name)
304+
testingx.Must(t, err, "Failed to parse: %s", tt.name)
305305
if got := n.String(); got != tt.want {
306306
t.Errorf("Name.String() = %v, want %v", got, tt.want)
307307
}
308308
})
309309
}
310+
// Verify an empty Name returns an empty string.
311+
if (Name{}).String() != "" {
312+
t.Errorf("Name.String() = %v, want ''", (Name{}).String())
313+
}
310314
}
311315

312316
func TestName_StringWithService(t *testing.T) {
@@ -338,7 +342,7 @@ func TestName_StringWithService(t *testing.T) {
338342
for _, tt := range tests {
339343
t.Run(tt.name, func(t *testing.T) {
340344
n, err := Parse(tt.name)
341-
rtx.Must(err, "Failed to parse: %s", tt.name)
345+
testingx.Must(t, err, "Failed to parse: %s", tt.name)
342346
if got := n.StringWithService(); got != tt.want {
343347
t.Errorf("Name.StringWithService() = %v, want %v", got, tt.want)
344348
}
@@ -369,13 +373,13 @@ func TestName_StringWithSuffix(t *testing.T) {
369373
},
370374
{
371375
name: "ndt-lol12345-abcdef01.mlab.sandbox.measurement-lab.org",
372-
want: "lol12345-abcdef01.mlab.sandbox.measurement-lab.org",
376+
want: "ndt-lol12345-abcdef01.mlab.sandbox.measurement-lab.org",
373377
},
374378
}
375379
for _, tt := range tests {
376380
t.Run(tt.name, func(t *testing.T) {
377381
n, err := Parse(tt.name)
378-
rtx.Must(err, "Failed to parse: %s", tt.name)
382+
testingx.Must(t, err, "Failed to parse: %s", tt.name)
379383
if got := n.StringWithSuffix(); got != tt.want {
380384
t.Errorf("Name.StringWithSuffix() = %v, want %v", got, tt.want)
381385
}
@@ -412,7 +416,7 @@ func TestName_StringAll(t *testing.T) {
412416
for _, tt := range tests {
413417
t.Run(tt.name, func(t *testing.T) {
414418
n, err := Parse(tt.name)
415-
rtx.Must(err, "Failed to parse: %s", tt.name)
419+
testingx.Must(t, err, "Failed to parse: %s", tt.name)
416420
if got := n.StringAll(); got != tt.want {
417421
t.Errorf("Name.StringAll() = %v, want %v", got, tt.want)
418422
}

0 commit comments

Comments
 (0)