From 916f2a7b8c117befcb4eeaa32b8ed9ec8ff1f96f Mon Sep 17 00:00:00 2001 From: Armon Dadgar Date: Mon, 7 Sep 2015 12:25:23 -0700 Subject: [PATCH] scheduler: util method to diff task groups --- scheduler/util.go | 25 +++++++++++++++++++++++++ scheduler/util_test.go | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/scheduler/util.go b/scheduler/util.go index 2911b9503..7f8278ba4 100644 --- a/scheduler/util.go +++ b/scheduler/util.go @@ -3,6 +3,7 @@ package scheduler import ( "fmt" "math/rand" + "reflect" "github.com/hashicorp/nomad/nomad/structs" ) @@ -205,3 +206,27 @@ func shuffleNodes(nodes []*structs.Node) { nodes[i], nodes[j] = nodes[j], nodes[i] } } + +// tasksUpdated does a diff between task groups to see if the +// tasks, their drivers or config have updated. +func tasksUpdated(a, b *structs.TaskGroup) bool { + // If the number of tasks do not match, clearly there is an update + if len(a.Tasks) != len(b.Tasks) { + return true + } + + // Check each task + for _, at := range a.Tasks { + bt := b.LookupTask(at.Name) + if bt == nil { + return true + } + if at.Driver != bt.Driver { + return true + } + if !reflect.DeepEqual(at.Config, bt.Config) { + return true + } + } + return false +} diff --git a/scheduler/util_test.go b/scheduler/util_test.go index a19186fe2..53fada5f9 100644 --- a/scheduler/util_test.go +++ b/scheduler/util_test.go @@ -227,3 +227,36 @@ func TestShuffleNodes(t *testing.T) { t.Fatalf("shoudl not match") } } + +func TestTasksUpdated(t *testing.T) { + j1 := mock.Job() + j2 := mock.Job() + + if tasksUpdated(j1.TaskGroups[0], j2.TaskGroups[0]) { + t.Fatalf("bad") + } + + j2.TaskGroups[0].Tasks[0].Config["command"] = "/bin/other" + if !tasksUpdated(j1.TaskGroups[0], j2.TaskGroups[0]) { + t.Fatalf("bad") + } + + j3 := mock.Job() + j3.TaskGroups[0].Tasks[0].Name = "foo" + if !tasksUpdated(j1.TaskGroups[0], j3.TaskGroups[0]) { + t.Fatalf("bad") + } + + j4 := mock.Job() + j4.TaskGroups[0].Tasks[0].Driver = "foo" + if !tasksUpdated(j1.TaskGroups[0], j4.TaskGroups[0]) { + t.Fatalf("bad") + } + + j5 := mock.Job() + j5.TaskGroups[0].Tasks = append(j5.TaskGroups[0].Tasks, + j5.TaskGroups[0].Tasks[0]) + if !tasksUpdated(j1.TaskGroups[0], j5.TaskGroups[0]) { + t.Fatalf("bad") + } +}