Merge pull request #13074 from runcom/11824-short-link-if-equals-alias
Allow links to be specified with only the name if it matches the alias
This commit is contained in:
commit
2c617078a7
2 changed files with 46 additions and 0 deletions
|
@ -135,3 +135,17 @@ func ParsePortRange(ports string) (uint64, uint64, error) {
|
||||||
}
|
}
|
||||||
return start, end, nil
|
return start, end, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ParseLink(val string) (string, string, error) {
|
||||||
|
if val == "" {
|
||||||
|
return "", "", fmt.Errorf("empty string specified for links")
|
||||||
|
}
|
||||||
|
arr := strings.Split(val, ":")
|
||||||
|
if len(arr) > 2 {
|
||||||
|
return "", "", fmt.Errorf("bad format for links: %s", val)
|
||||||
|
}
|
||||||
|
if len(arr) == 1 {
|
||||||
|
return val, val, nil
|
||||||
|
}
|
||||||
|
return arr[0], arr[1], nil
|
||||||
|
}
|
||||||
|
|
|
@ -123,3 +123,35 @@ func TestParsePortRangeIncorrectStartRange(t *testing.T) {
|
||||||
t.Fatalf("Expecting error 'Invalid range specified for the Port' but received %s.", err)
|
t.Fatalf("Expecting error 'Invalid range specified for the Port' but received %s.", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestParseLink(t *testing.T) {
|
||||||
|
name, alias, err := ParseLink("name:alias")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Expected not to error out on a valid name:alias format but got: %v", err)
|
||||||
|
}
|
||||||
|
if name != "name" {
|
||||||
|
t.Fatalf("Link name should have been name, got %s instead", name)
|
||||||
|
}
|
||||||
|
if alias != "alias" {
|
||||||
|
t.Fatalf("Link alias should have been alias, got %s instead", alias)
|
||||||
|
}
|
||||||
|
// short format definition
|
||||||
|
name, alias, err = ParseLink("name")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Expected not to error out on a valid name only format but got: %v", err)
|
||||||
|
}
|
||||||
|
if name != "name" {
|
||||||
|
t.Fatalf("Link name should have been name, got %s instead", name)
|
||||||
|
}
|
||||||
|
if alias != "name" {
|
||||||
|
t.Fatalf("Link alias should have been name, got %s instead", alias)
|
||||||
|
}
|
||||||
|
// empty string link definition is not allowed
|
||||||
|
if _, _, err := ParseLink(""); err == nil || !strings.Contains(err.Error(), "empty string specified for links") {
|
||||||
|
t.Fatalf("Expected error 'empty string specified for links' but got: %v", err)
|
||||||
|
}
|
||||||
|
// more than two colons are not allowed
|
||||||
|
if _, _, err := ParseLink("link:alias:wrong"); err == nil || !strings.Contains(err.Error(), "bad format for links: link:alias:wrong") {
|
||||||
|
t.Fatalf("Expected error 'bad format for links: link:alias:wrong' but got: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue