scheduler: util method to diff task groups

This commit is contained in:
Armon Dadgar
2015-09-07 12:25:23 -07:00
parent 885f6e05bb
commit 916f2a7b8c
2 changed files with 58 additions and 0 deletions

View File

@@ -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
}

View File

@@ -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")
}
}