Skip to content

Commit ce01e4e

Browse files
author
Kent Rancourt
committed
fix(deisctl): fix how target names resolve
1 parent 6365a24 commit ce01e4e

2 files changed

Lines changed: 46 additions & 4 deletions

File tree

deisctl/backend/fleet/unit.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,31 @@ func (c *FleetClient) Units(target string) (units []string, err error) {
3232
if err != nil {
3333
return
3434
}
35+
// Look for units starting with the given target name first. If the given
36+
// name starts with "deis-", this will easily locate platform components,
37+
// but we search without canonicalizing the target name FIRST so we have the
38+
// opportunity to locate application containers (whose containers do not
39+
// adhere to the same naming convention as the platform's own components).
3540
for _, u := range allUnits {
36-
if strings.Contains(u.Name, target) {
41+
if strings.HasPrefix(u.Name, target) {
3742
units = append(units, u.Name)
3843
}
3944
}
45+
// If none are found, canonicalize the target string and search again. This
46+
// will locate platform components that were referenced by a target string
47+
// NOT already beginning with "deis-".
48+
if len(units) == 0 {
49+
canonTarget := strings.ToLower(target)
50+
if !strings.HasPrefix(canonTarget, "deis-") {
51+
canonTarget = "deis-" + canonTarget
52+
}
53+
for _, u := range allUnits {
54+
if strings.HasPrefix(u.Name, canonTarget) {
55+
units = append(units, u.Name)
56+
}
57+
}
58+
}
59+
// If still nothing is found, then we have an error on our hands.
4060
if len(units) == 0 {
4161
err = fmt.Errorf("could not find unit: %s", target)
4262
}

deisctl/backend/fleet/unit_test.go

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"io/ioutil"
55
"path"
66
"reflect"
7+
"strings"
78
"sync"
89
"testing"
910

@@ -27,18 +28,39 @@ func TestUnits(t *testing.T) {
2728
}
2829

2930
c := &FleetClient{Fleet: &stubFleetClient{testUnits: testUnits, unitsMutex: &sync.Mutex{}}}
31+
expected := []string{"deis-router@1.service", "deis-router@2.service", "deis-router@3.service"}
3032

33+
// Test that the correct units are resolved when providing a target string
34+
// that EXCEPT for lacking the "deis-" prefix matches the beginning of one
35+
// or more units' name...
3136
targets, err := c.Units("router")
32-
3337
if err != nil {
3438
t.Fatal(err)
3539
}
40+
if !reflect.DeepEqual(targets, expected) {
41+
t.Fatalf("Expected %v, Got %v", expected, targets)
42+
}
3643

37-
expected := []string{"deis-router@1.service", "deis-router@2.service", "deis-router@3.service"}
38-
44+
// Test that the correct units are resolved when providing a target string
45+
// INCLUDING the "deis-" prefix that matches the beginning of one or more
46+
// units' name...
47+
targets, err = c.Units("deis-router")
48+
if err != nil {
49+
t.Fatal(err)
50+
}
3951
if !reflect.DeepEqual(targets, expected) {
4052
t.Fatalf("Expected %v, Got %v", expected, targets)
4153
}
54+
55+
// Test that no units are resolved and an error is returned when providing
56+
// a target string that does not match the BEGINNING of a service unit name,
57+
// either with or without the "deis-" prefix. We deliberately test here using
58+
// a string that IS a substring (albeit in the wrong position) of service
59+
// units in the stub...
60+
targets, err = c.Units("outer")
61+
if err == nil || !strings.HasPrefix(err.Error(), "could not find unit:") {
62+
t.Fatalf("Expected an error beginning with \"could not find unit:\", but did not get one")
63+
}
4264
}
4365

4466
func TestNextUnit(t *testing.T) {

0 commit comments

Comments
 (0)