implement alternative negation syntax

Signed-off-by: Chanwit Kaewkasi <chanwit@gmail.com>
This commit is contained in:
Chanwit Kaewkasi 2015-01-14 22:08:11 +07:00
parent 25231f3bb3
commit d335f59757
2 changed files with 45 additions and 2 deletions

View File

@ -45,12 +45,17 @@ func (f *ConstraintFilter) Filter(config *dockerclient.ContainerConfig, nodes []
// keep the original for display in case of error // keep the original for display in case of error
v0 := v v0 := v
k0 := k
negate := false negate := false
if strings.HasPrefix(v, "!") { if strings.HasPrefix(v, "!") {
log.Debugf("negate detected") log.Debugf("negate detected in value")
v = strings.TrimPrefix(v, "!") v = strings.TrimPrefix(v, "!")
negate = true negate = true
} else if strings.HasSuffix(k, "!") {
log.Debugf("negate detected in key")
k = strings.TrimSuffix(k, "!")
negate = true
} }
useRegex := false useRegex := false
@ -84,7 +89,7 @@ func (f *ConstraintFilter) Filter(config *dockerclient.ContainerConfig, nodes []
} }
} }
if len(candidates) == 0 { if len(candidates) == 0 {
return nil, fmt.Errorf("unable to find a node that satisfies %s = %s", k, v0) return nil, fmt.Errorf("unable to find a node that satisfies %s=%s", k0, v0)
} }
nodes = candidates nodes = candidates
} }

View File

@ -151,6 +151,44 @@ func TestConstraintNotExpr(t *testing.T) {
assert.Equal(t, result[0].Labels["region"], "eu") assert.Equal(t, result[0].Labels["region"], "eu")
} }
func TestConstraintAlternativeNotExpr(t *testing.T) {
var (
f = ConstraintFilter{}
nodes = testFixtures()
result []*cluster.Node
err error
)
// Check not (!) expression
result, err = f.Filter(&dockerclient.ContainerConfig{
Env: []string{"constraint:name!=node0"},
}, nodes)
assert.NoError(t, err)
assert.Len(t, result, 2)
// Check not does_not_exist. All should be found
result, err = f.Filter(&dockerclient.ContainerConfig{
Env: []string{"constraint:name!=does_not_exist"},
}, nodes)
assert.NoError(t, err)
assert.Len(t, result, 3)
// Check name must not start with n
result, err = f.Filter(&dockerclient.ContainerConfig{
Env: []string{"constraint:name!=n*"},
}, nodes)
assert.Error(t, err)
assert.Len(t, result, 0)
// Check not with globber pattern
result, err = f.Filter(&dockerclient.ContainerConfig{
Env: []string{"constraint:region!=us*"},
}, nodes)
assert.NoError(t, err)
assert.Len(t, result, 1)
assert.Equal(t, result[0].Labels["region"], "eu")
}
func TestConstraintRegExp(t *testing.T) { func TestConstraintRegExp(t *testing.T) {
var ( var (
f = ConstraintFilter{} f = ConstraintFilter{}