mirror of
https://github.com/kemko/nomad.git
synced 2026-01-07 02:45:42 +03:00
scheduler: util method to diff task groups
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
@@ -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")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user