Skip to content

Commit

Permalink
fleetctl: support truthy values in boolean unit options
Browse files Browse the repository at this point in the history
Support truthy values in boolean unit options(true, yes, on, 1, t).

Originally written by wuqixuan <wuqixuan@huawei.com>
Supersedes coreos#1271
Fixes: coreos#1108
  • Loading branch information
Dongsu Park committed May 18, 2016
1 parent 8685cfb commit fabe005
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
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

0 comments on commit fabe005

Please sign in to comment.