1
0
mirror of https://github.com/docker/docs.git synced 2026-10-31 10:34:26 +03:00

add tests and move from label to constraint

Signed-off-by: Victor Vieux <vieux@docker.com>
This commit is contained in:
Victor Vieux
2014-12-18 02:02:32 +00:00
parent ba1688ccef
commit 28a5063168
5 changed files with 34 additions and 18 deletions

View File

@@ -43,7 +43,7 @@ docker run -d -p 80:80 nginx
# clean up cluster
docker rm -f `docker ps -aq`
docker run -d -e constraint:operatingsystem=fedora redis
docker run -d -e "constraint:operatingsystem=fedora*" redis
docker ps
docker run -d -e constraint:storagedriver=devicemapper redis

View File

@@ -57,7 +57,7 @@ var (
}
flFilter = cli.StringSliceFlag{
Name: "filter, f",
Usage: "Filter to use [health, label, port]",
Value: &cli.StringSlice{"health", "label", "port"},
Usage: "Filter to use [constraint, health, port]",
Value: &cli.StringSlice{"constraint", "health", "port"},
}
)

View File

@@ -10,11 +10,11 @@ import (
"github.com/samalba/dockerclient"
)
// LabelFilter selects only nodes that match certain labels.
type LabelFilter struct {
// ConstraintFilter selects only nodes that match certain labels.
type ConstraintFilter struct {
}
func (f *LabelFilter) extractConstraints(env []string) map[string]string {
func (f *ConstraintFilter) extractConstraints(env []string) map[string]string {
constraints := make(map[string]string)
for _, e := range env {
if strings.HasPrefix(e, "constraint:") {
@@ -26,7 +26,7 @@ func (f *LabelFilter) extractConstraints(env []string) map[string]string {
return constraints
}
func (f *LabelFilter) Filter(config *dockerclient.ContainerConfig, nodes []*cluster.Node) ([]*cluster.Node, error) {
func (f *ConstraintFilter) Filter(config *dockerclient.ContainerConfig, nodes []*cluster.Node) ([]*cluster.Node, error) {
constraints := f.extractConstraints(config.Env)
for k, v := range constraints {
regex := "^" + strings.Replace(v, "*", ".*", -1) + "$"

View File

@@ -8,9 +8,9 @@ import (
"github.com/stretchr/testify/assert"
)
func TestLabeleFilter(t *testing.T) {
func TestConstrainteFilter(t *testing.T) {
var (
f = LabelFilter{}
f = ConstraintFilter{}
nodes = []*cluster.Node{
cluster.NewNode("node-0"),
cluster.NewNode("node-1"),
@@ -23,22 +23,25 @@ func TestLabeleFilter(t *testing.T) {
nodes[0].ID = "node-0-id"
nodes[0].Name = "node-0-name"
nodes[0].Labels = map[string]string{
"name": "node0",
"group": "1",
"name": "node0",
"group": "1",
"region": "us-west",
}
nodes[1].ID = "node-1-id"
nodes[1].Name = "node-1-name"
nodes[1].Labels = map[string]string{
"name": "node1",
"group": "1",
"name": "node1",
"group": "1",
"region": "us-east",
}
nodes[2].ID = "node-2-id"
nodes[2].Name = "node-2-name"
nodes[2].Labels = map[string]string{
"name": "node2",
"group": "2",
"name": "node2",
"group": "2",
"region": "eu",
}
// Without constraints we should get the unfiltered list of nodes back.
@@ -91,4 +94,17 @@ func TestLabeleFilter(t *testing.T) {
assert.NoError(t, err)
assert.Len(t, result, 1)
assert.Equal(t, result[0], nodes[0])
// Check matching
result, err = f.Filter(&dockerclient.ContainerConfig{
Env: []string{"constraint:region=us"},
}, nodes)
assert.Error(t, err)
assert.Len(t, result, 0)
result, err = f.Filter(&dockerclient.ContainerConfig{
Env: []string{"constraint:region=us*"},
}, nodes)
assert.NoError(t, err)
assert.Len(t, result, 2)
}

View File

@@ -20,9 +20,9 @@ var (
func init() {
filters = map[string]Filter{
"health": &HealthFilter{},
"label": &LabelFilter{},
"port": &PortFilter{},
"health": &HealthFilter{},
"constraint": &ConstraintFilter{},
"port": &PortFilter{},
}
}