Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

fleetctl: support truthy values in boolean unit options #1574

Merged
merged 1 commit into from
May 19, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion job/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func (u *Unit) IsGlobal() bool {
}
// Last value found wins
last := values[len(values)-1]
return strings.ToLower(last) == "true"
return isTruthyValue(last)
}

// NewJob creates a new Job based on the given name and Unit.
Expand Down Expand Up @@ -301,3 +301,10 @@ func unitPrintf(s string, nu unit.UnitNameInfo) (out string) {
out = strings.Replace(out, "%i", nu.Instance, -1)
return
}

// isTruthyValue returns true if a given string is any of "truthy" value,
// i.e. "true", "yes", "1", "on", or "t".
func isTruthyValue(s string) bool {
chl := strings.ToLower(s)
return chl == "true" || chl == "yes" || chl == "1" || chl == "on" || chl == "t"
}
32 changes: 32 additions & 0 deletions job/job_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,11 @@ func TestUnitIsGlobal(t *testing.T) {
// correct specifications
{"[X-Fleet]\nMachineOf=foo\nGlobal=true", true},
{"[X-Fleet]\nMachineOf=foo\nGlobal=True", true},
{"[X-Fleet]\nMachineOf=foo\nGlobal=Yes", true},
{"[X-Fleet]\nMachineOf=foo\nGlobal=On", true},
{"[X-Fleet]\nMachineOf=foo\nGlobal=1", true},
{"[X-Fleet]\nMachineOf=foo\nGlobal=t", true},
{"[X-Fleet]\nMachineOf=foo\nGlobal=12", false},
// multiple parameters - last wins
{"[X-Fleet]\nGlobal=true\nGlobal=false", false},
{"[X-Fleet]\nGlobal=false\nGlobal=true", true},
Expand All @@ -523,6 +528,33 @@ func TestUnitIsGlobal(t *testing.T) {
}
}

func TestUnitIsTruthy(t *testing.T) {
for i, tt := range []struct {
contents string
want bool
}{
// empty string
{"", false},
// bad values
{"false", false},
{"no", false},
{"0", false},
{"off", false},
{"f", false},
// correct values
{"true", true},
{"yes", true},
{"1", true},
{"on", true},
{"t", true},
} {
got := isTruthyValue(tt.contents)
if got != tt.want {
t.Errorf("case %d: isTruthyValue returned %t, want %t", i, got, tt.want)
}
}
}

func TestValidateRequirements(t *testing.T) {
tests := []string{
"MachineID=asdf",
Expand Down