mirror of https://github.com/docker/docs.git
add a testcase for escape regexp
Signed-off-by: Chanwit Kaewkasi <chanwit@gmail.com>
This commit is contained in:
parent
790b1ea45d
commit
b4a7abdc83
|
@ -152,12 +152,13 @@ As you can see here, the containers were only scheduled on nodes with the redis
|
||||||
Additionally, you can use a not (`!`) to negate and a regular expression in the form of `/regexp/` for specifying a constraint.
|
Additionally, you can use a not (`!`) to negate and a regular expression in the form of `/regexp/` for specifying a constraint.
|
||||||
For example,
|
For example,
|
||||||
|
|
||||||
* `name=node1` will match nodes named with `node1`.
|
* `constraint:name=node1` will match nodes named with `node1`.
|
||||||
* `name=!node1` will match all nodes, except `node1`.
|
* `constraint:name=!node1` will match all nodes, except `node1`.
|
||||||
* `region=!us*` will match all nodes outside the regions prefixed with `us`.
|
* `constraint:region=!us*` will match all nodes outside the regions prefixed with `us`.
|
||||||
* `name=/node[12]/` will match nodes named `node1` and `node2`.
|
* `constraint:name=/node[12]/` will match nodes named `node1` and `node2`.
|
||||||
* `name=/node\d/` will match all nodes named with `node` + 1 digit
|
* `constraint:name=/node\d/` will match all nodes named with `node` + 1 digit.
|
||||||
* `node=!/node-[01]-id/` will match all nodes, except those with ids `node-0-id` and `node-1-id`
|
* `constraint:node=!/node-[01]-id/` will match all nodes, except those with ids `node-0-id` and `node-1-id`.
|
||||||
|
* `constraint:name=!/foo\[bar\]/` will match all nodes, except those with name `foo[bar]`. You can see the use of escape characters here.
|
||||||
|
|
||||||
## Port Filter
|
## Port Filter
|
||||||
|
|
||||||
|
|
|
@ -189,3 +189,38 @@ func TestConstraintRegExp(t *testing.T) {
|
||||||
assert.Len(t, result, 1)
|
assert.Len(t, result, 1)
|
||||||
assert.Equal(t, result[0], nodes[2])
|
assert.Equal(t, result[0], nodes[2])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestFilterRegExpWithEscape(t *testing.T) {
|
||||||
|
var (
|
||||||
|
f = ConstraintFilter{}
|
||||||
|
nodes = testFixtures()
|
||||||
|
result []*cluster.Node
|
||||||
|
err error
|
||||||
|
)
|
||||||
|
|
||||||
|
// Prepare node with a strange name
|
||||||
|
node3 := cluster.NewNode("node-3")
|
||||||
|
node3.ID = "node-3-id"
|
||||||
|
node3.Name = "node-3-name"
|
||||||
|
node3.Labels = map[string]string{
|
||||||
|
"name": "foo[bar]",
|
||||||
|
"group": "2",
|
||||||
|
"region": "eu",
|
||||||
|
}
|
||||||
|
nodes = append(nodes, node3)
|
||||||
|
|
||||||
|
// Test filter with a strange name
|
||||||
|
result, err = f.Filter(&dockerclient.ContainerConfig{
|
||||||
|
Env: []string{`constraint:name=/foo\[bar\]/`},
|
||||||
|
}, nodes)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Len(t, result, 1)
|
||||||
|
assert.Equal(t, result[0], nodes[3])
|
||||||
|
|
||||||
|
// Test ! filter with a strange name
|
||||||
|
result, err = f.Filter(&dockerclient.ContainerConfig{
|
||||||
|
Env: []string{`constraint:name=!/foo\[bar\]/`},
|
||||||
|
}, nodes)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Len(t, result, 3)
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue