From cc0fdf15ef684b9b532f49c82b53b328286ef23d Mon Sep 17 00:00:00 2001 From: tuier Date: Wed, 21 Sep 2016 00:15:09 +0100 Subject: [PATCH] test for task and application constraint --- provider/marathon_test.go | 99 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) diff --git a/provider/marathon_test.go b/provider/marathon_test.go index c8cc6808f..df7153d23 100644 --- a/provider/marathon_test.go +++ b/provider/marathon_test.go @@ -683,6 +683,105 @@ func TestMarathonTaskFilter(t *testing.T) { } } +func TestMarathonAppConstraints(t *testing.T) { + cases := []struct { + application marathon.Application + filteredTasks []marathon.Task + expected bool + }{ + { + application: marathon.Application{ + ID: "foo1", + Labels: &map[string]string{}, + }, + filteredTasks: []marathon.Task{ + { + AppID: "foo1", + }, + }, + expected: false, + }, + { + application: marathon.Application{ + ID: "foo", + Labels: &map[string]string{ + "traefik.tags": "valid", + }, + }, + filteredTasks: []marathon.Task{ + { + AppID: "foo", + }, + }, + expected: true, + }, + } + + provider := &Marathon{} + constraint, _ := types.NewConstraint("tag==valid") + provider.Constraints = []types.Constraint{*constraint} + for _, c := range cases { + actual := provider.applicationFilter(c.application, c.filteredTasks) + if actual != c.expected { + t.Fatalf("expected %v, got %v: %v", c.expected, actual, c.application) + } + } + +} +func TestMarathonTaskConstraints(t *testing.T) { + cases := []struct { + applications []marathon.Application + filteredTask marathon.Task + expected bool + }{ + { + applications: []marathon.Application{ + marathon.Application{ + ID: "bar1", + Labels: &map[string]string{}, + }, marathon.Application{ + ID: "foo1", + Labels: &map[string]string{ + "traefik.tags": "other", + }, + }, + }, + filteredTask: marathon.Task{ + AppID: "foo1", + Ports: []int{80}, + }, + expected: false, + }, + { + applications: []marathon.Application{ + marathon.Application{ + ID: "foo2", + Labels: &map[string]string{ + "traefik.tags": "valid", + }, + }, + }, + filteredTask: marathon.Task{ + AppID: "foo2", + Ports: []int{80}, + }, + expected: true, + }, + } + + provider := &Marathon{} + constraint, _ := types.NewConstraint("tag==valid") + provider.Constraints = []types.Constraint{*constraint} + for _, c := range cases { + apps := new(marathon.Applications) + apps.Apps = c.applications + actual := provider.taskFilter(c.filteredTask, apps, true) + if actual != c.expected { + t.Fatalf("expected %v, got %v: %v", c.expected, actual, c.filteredTask) + } + } +} + func TestMarathonApplicationFilter(t *testing.T) { cases := []struct { application marathon.Application