scheduler: support lexical ordering constraints

This commit is contained in:
Armon Dadgar
2015-10-11 15:57:06 -04:00
parent a3dda996fa
commit 641b9f3ee4
2 changed files with 72 additions and 2 deletions

View File

@@ -250,8 +250,7 @@ func checkConstraint(operand string, lVal, rVal interface{}) bool {
case "!=", "not":
return !reflect.DeepEqual(lVal, rVal)
case "<", "<=", ">", ">=":
// TODO: Implement
return false
return checkLexicalOrder(operand, lVal, rVal)
case "version":
return checkVersionConstraint(lVal, rVal)
case "regexp":
@@ -261,6 +260,32 @@ func checkConstraint(operand string, lVal, rVal interface{}) bool {
}
}
// checkLexicalOrder is used to check for lexical ordering
func checkLexicalOrder(op string, lVal, rVal interface{}) bool {
// Ensure the values are strings
lStr, ok := lVal.(string)
if !ok {
return false
}
rStr, ok := rVal.(string)
if !ok {
return false
}
switch op {
case "<":
return lStr < rStr
case "<=":
return lStr <= rStr
case ">":
return lStr > rStr
case ">=":
return lStr >= rStr
default:
return false
}
}
// checkVersionConstraint is used to compare a version on the
// left hand side with a set of constraints on the right hand side
func checkVersionConstraint(lVal, rVal interface{}) bool {

View File

@@ -254,6 +254,11 @@ func TestCheckConstraint(t *testing.T) {
lVal: "foobarbaz", rVal: "[\\w]+",
result: true,
},
{
op: "<",
lVal: "foo", rVal: "bar",
result: false,
},
}
for _, tc := range cases {
@@ -263,6 +268,46 @@ func TestCheckConstraint(t *testing.T) {
}
}
func TestCheckLexicalOrder(t *testing.T) {
type tcase struct {
op string
lVal, rVal interface{}
result bool
}
cases := []tcase{
{
op: "<",
lVal: "bar", rVal: "foo",
result: true,
},
{
op: "<=",
lVal: "foo", rVal: "foo",
result: true,
},
{
op: ">",
lVal: "bar", rVal: "foo",
result: false,
},
{
op: ">=",
lVal: "bar", rVal: "bar",
result: true,
},
{
op: ">",
lVal: 1, rVal: "foo",
result: false,
},
}
for _, tc := range cases {
if res := checkLexicalOrder(tc.op, tc.lVal, tc.rVal); res != tc.result {
t.Fatalf("TC: %#v, Result: %v", tc, res)
}
}
}
func TestCheckVersionConstraint(t *testing.T) {
type tcase struct {
lVal, rVal interface{}