mirror of
https://github.com/kemko/nomad.git
synced 2026-01-10 12:25:42 +03:00
scheduler: support lexical ordering constraints
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -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{}
|
||||
|
||||
Reference in New Issue
Block a user