remove old diff

This commit is contained in:
Alex Dadgar 2016-05-11 11:27:00 -07:00
parent ccc3caae4a
commit 2f74bd9a74
4 changed files with 0 additions and 1898 deletions

View File

@ -1,25 +0,0 @@
package diff
import "github.com/hashicorp/nomad/nomad/structs"
// JobPlanResponse is used to respond to a job plan request. Must be in this
// package to avoid a cyclic dependency.
type JobPlanResponse struct {
// Plan holds the decisions the scheduler made.
Plan *structs.Plan
// The Cas value can be used when running `nomad run` to ensure that the Job
// wasnt modified since the last plan. If the job is being created, the
// value is zero.
Cas uint64
// CreatedEvals is the set of evaluations created by the scheduler. The
// reasons for this can be rolling-updates or blocked evals.
CreatedEvals []*structs.Evaluation
// Diff contains the diff of the job and annotations on whether the change
// causes an in-place update or create/destroy
Diff *JobDiff
structs.QueryMeta
}

File diff suppressed because it is too large Load Diff

View File

@ -1,673 +0,0 @@
package structs
import (
"reflect"
"sort"
"testing"
)
func TestNewJobDiff_Same(t *testing.T) {
job1 := TestJob()
job2 := TestJob()
job2.ID = job1.ID
diff := NewJobDiff(job1, job2)
if diff != nil {
t.Fatalf("expected nil job diff; got %#v", diff)
}
}
func TestNewJobDiff_NilCases(t *testing.T) {
j := TestJob()
// Old job nil
diff := NewJobDiff(nil, j)
if diff == nil {
t.Fatalf("expected non-nil job diff")
}
if diff.Type != DiffTypeAdded {
t.Fatalf("got diff type %v; want %v", diff.Type, DiffTypeAdded)
}
// New job nil
diff = NewJobDiff(j, nil)
if diff == nil {
t.Fatalf("expected non-nil job diff")
}
if diff.Type != DiffTypeDeleted {
t.Fatalf("got diff type %v; want %v", diff.Type, DiffTypeDeleted)
}
}
func TestNewJobDiff_Constraints(t *testing.T) {
c1 := &Constraint{LTarget: "foo"}
c2 := &Constraint{LTarget: "bar"}
c3 := &Constraint{LTarget: "baz"}
// Test the added case.
j1 := &Job{Constraints: []*Constraint{c1, c2}}
j2 := &Job{Constraints: []*Constraint{c1, c2, c3}}
diff := NewJobDiff(j1, j2)
if diff == nil {
t.Fatalf("expected non-nil job diff")
}
if diff.Type != DiffTypeEdited {
t.Fatalf("got diff type %v; want %v", diff.Type, DiffTypeEdited)
}
if len(diff.Constraints) != 1 {
t.Fatalf("expected one constraint diff; got %v", diff.Constraints)
}
cdiff := diff.Constraints[0]
if cdiff.Type != DiffTypeAdded {
t.Fatalf("expected constraint to be added: %#v", cdiff)
}
if len(cdiff.PrimitiveFields) != 3 {
t.Fatalf("bad: %#v", cdiff)
}
// Test the deleted case.
j1 = &Job{Constraints: []*Constraint{c1, c2}}
j2 = &Job{Constraints: []*Constraint{c1}}
diff = NewJobDiff(j1, j2)
if diff == nil {
t.Fatalf("expected non-nil job diff")
}
if diff.Type != DiffTypeEdited {
t.Fatalf("got diff type %v; want %v", diff.Type, DiffTypeEdited)
}
if len(diff.Constraints) != 1 {
t.Fatalf("expected one constraint diff; got %v", diff.Constraints)
}
cdiff = diff.Constraints[0]
if cdiff.Type != DiffTypeDeleted {
t.Fatalf("expected constraint to be deleted: %#v", cdiff)
}
if len(cdiff.PrimitiveFields) != 3 {
t.Fatalf("bad: %#v", cdiff)
}
}
func TestNewJobDiff_Datacenters(t *testing.T) {
j1 := &Job{Datacenters: []string{"a", "b"}}
j2 := &Job{Datacenters: []string{"b", "c"}}
diff := NewJobDiff(j1, j2)
if diff == nil {
t.Fatalf("expected non-nil job diff")
}
if diff.Type != DiffTypeEdited {
t.Fatalf("got diff type %v; want %v", diff.Type, DiffTypeEdited)
}
dd := diff.Datacenters
if dd == nil {
t.Fatalf("expected datacenter diff")
}
if !reflect.DeepEqual(dd.Added, []string{"c"}) ||
!reflect.DeepEqual(dd.Deleted, []string{"a"}) {
t.Fatalf("bad: %#v", dd)
}
}
func TestNewJobDiff_TaskGroups(t *testing.T) {
tg1 := &TaskGroup{Name: "foo"}
tg2 := &TaskGroup{Name: "bar"}
tg2_2 := &TaskGroup{Name: "bar", Count: 2}
tg3 := &TaskGroup{Name: "baz"}
j1 := &Job{TaskGroups: []*TaskGroup{tg1, tg2}}
j2 := &Job{TaskGroups: []*TaskGroup{tg2_2, tg3}}
diff := NewJobDiff(j1, j2)
if diff == nil {
t.Fatalf("expected non-nil job diff")
}
if diff.Type != DiffTypeEdited {
t.Fatalf("got diff type %v; want %v", diff.Type, DiffTypeEdited)
}
tgd := diff.TaskGroups
if tgd == nil {
t.Fatalf("expected task group diff")
}
if !reflect.DeepEqual(tgd.Added, []*TaskGroup{tg3}) ||
!reflect.DeepEqual(tgd.Deleted, []*TaskGroup{tg1}) {
t.Fatalf("bad: %#v", tgd)
}
if len(tgd.Edited) != 1 {
t.Fatalf("expect one edited task group: %#v", tgd)
}
if e := tgd.Edited[0]; tgd.Type != DiffTypeEdited && len(e.PrimitiveFields) != 1 {
t.Fatalf("bad: %#v", e)
}
}
func TestNewTaskDiff_Config(t *testing.T) {
c1 := map[string]interface{}{
"command": "/bin/date",
"args": []string{"1", "2"},
}
c2 := map[string]interface{}{
"args": []string{"1", "2"},
}
c3 := map[string]interface{}{
"command": "/bin/date",
"args": []string{"1", "2"},
"nested": &Port{
Label: "http",
Value: 80,
},
}
c4 := map[string]interface{}{
"command": "/bin/bash",
"args": []string{"1", "2"},
}
// No old case
t1 := &Task{Config: c1}
diff := NewTaskDiff(nil, t1)
if diff == nil {
t.Fatalf("expected non-nil diff")
}
if diff.Config == nil {
t.Fatalf("expected Config diff: %#v", diff)
}
cdiff := diff.Config
if cdiff.Type != DiffTypeAdded {
t.Fatalf("expected Config diff type %v; got %#v", DiffTypeAdded, cdiff.Type)
}
// No new case
diff = NewTaskDiff(t1, nil)
if diff == nil {
t.Fatalf("expected non-nil diff")
}
if diff.Config == nil {
t.Fatalf("expected Config diff: %#v", diff)
}
cdiff = diff.Config
if cdiff.Type != DiffTypeDeleted {
t.Fatalf("expected Config diff type %v; got %#v", DiffTypeDeleted, cdiff.Type)
}
// Same case
diff = NewTaskDiff(t1, t1)
if diff != nil {
t.Fatalf("expected nil diff")
}
// Deleted case
t2 := &Task{Config: c2}
diff = NewTaskDiff(t1, t2)
if diff == nil {
t.Fatalf("expected non-nil diff")
}
if diff.Config == nil {
t.Fatalf("expected Config diff: %#v", diff)
}
cdiff = diff.Config
if cdiff.Type != DiffTypeDeleted {
t.Fatalf("expected Config diff type %v; got %#v", DiffTypeDeleted, cdiff.Type)
}
if len(cdiff.Added)+len(cdiff.Edited) != 0 && len(cdiff.Deleted) != 1 {
t.Fatalf("unexpected config diffs: %#v", cdiff)
}
if v, ok := cdiff.Deleted["command"]; !ok || v != "/bin/date" {
t.Fatalf("bad: %#v", cdiff.Deleted)
}
// Added case
t3 := &Task{Config: c3}
diff = NewTaskDiff(t1, t3)
if diff == nil {
t.Fatalf("expected non-nil diff")
}
if diff.Config == nil {
t.Fatalf("expected Config diff: %#v", diff)
}
cdiff = diff.Config
if cdiff.Type != DiffTypeAdded {
t.Fatalf("expected Config diff type %v; got %#v", DiffTypeAdded, cdiff.Type)
}
if len(cdiff.Deleted)+len(cdiff.Edited) != 0 && len(cdiff.Added) != 2 {
t.Fatalf("unexpected config diffs: %#v", cdiff)
}
if v, ok := cdiff.Added["nested.Value"]; !ok || v != "80" {
t.Fatalf("bad: %#v", cdiff.Added)
}
if v, ok := cdiff.Added["nested.Label"]; !ok || v != "http" {
t.Fatalf("bad: %#v", cdiff.Added)
}
// Edited case
t4 := &Task{Config: c4}
diff = NewTaskDiff(t1, t4)
if diff == nil {
t.Fatalf("expected non-nil diff")
}
if diff.Config == nil {
t.Fatalf("expected Config diff: %#v", diff)
}
cdiff = diff.Config
if cdiff.Type != DiffTypeEdited {
t.Fatalf("expected Config diff type %v; got %#v", DiffTypeEdited, cdiff.Type)
}
if len(cdiff.Deleted)+len(cdiff.Added) != 0 && len(cdiff.Edited) != 1 {
t.Fatalf("unexpected config diffs: %#v", cdiff)
}
exp := StringValueDelta{Old: "/bin/date", New: "/bin/bash"}
exp.Type = DiffTypeEdited
v, ok := cdiff.Edited["command"]
if !ok || !reflect.DeepEqual(v, exp) {
t.Fatalf("bad: %#v %#v %#v", cdiff.Edited, v, exp)
}
}
func TestNewPrimitiveStructDiff(t *testing.T) {
p1 := Port{Label: "1"}
p2 := Port{Label: "2"}
p3 := Port{}
pdiff := NewPrimitiveStructDiff(nil, nil, portFields)
if pdiff != nil {
t.Fatalf("expected no diff: %#v", pdiff)
}
pdiff = NewPrimitiveStructDiff(p1, p1, portFields)
if pdiff != nil {
t.Fatalf("expected no diff: %#v", pdiff)
}
pdiff = NewPrimitiveStructDiff(nil, p1, portFields)
if pdiff == nil {
t.Fatalf("expected diff")
}
if pdiff.Type != DiffTypeAdded {
t.Fatalf("unexpected type: got %v; want %v", pdiff.Type, DiffTypeAdded)
}
pdiff = NewPrimitiveStructDiff(p1, nil, portFields)
if pdiff == nil {
t.Fatalf("expected diff")
}
if pdiff.Type != DiffTypeDeleted {
t.Fatalf("unexpected type: got %v; want %v", pdiff.Type, DiffTypeDeleted)
}
pdiff = NewPrimitiveStructDiff(p1, p2, portFields)
if pdiff == nil {
t.Fatalf("expected diff")
}
if pdiff.Type != DiffTypeEdited {
t.Fatalf("unexpected type: got %v; want %v", pdiff.Type, DiffTypeEdited)
}
if len(pdiff.PrimitiveFields) != 1 {
t.Fatalf("unexpected number of field diffs: %#v", pdiff.PrimitiveFields)
}
f, ok := pdiff.PrimitiveFields["Label"]
if !ok {
t.Fatalf("expected diff on field %q", "label")
}
if f.Type != DiffTypeEdited {
t.Fatalf("unexpected type: got %v; want %v", f.Type, DiffTypeEdited)
}
if !reflect.DeepEqual(f.OldValue, "1") || !reflect.DeepEqual(f.NewValue, "2") {
t.Fatalf("bad: %#v", f)
}
pdiff = NewPrimitiveStructDiff(p1, p3, portFields)
if pdiff == nil {
t.Fatalf("expected diff")
}
if pdiff.Type != DiffTypeEdited {
t.Fatalf("unexpected type: got %v; want %v", pdiff.Type, DiffTypeEdited)
}
if len(pdiff.PrimitiveFields) != 1 {
t.Fatalf("unexpected number of field diffs: %#v", pdiff.PrimitiveFields)
}
f = pdiff.PrimitiveFields["Label"]
if !ok {
t.Fatalf("expected diff on field %q", "Label")
}
if f.Type != DiffTypeEdited {
t.Fatalf("unexpected type: got %v; want %v", f.Type, DiffTypeEdited)
}
if !reflect.DeepEqual(f.OldValue, "1") || !reflect.DeepEqual(f.NewValue, "") {
t.Fatalf("bad: %#v", f)
}
}
func TestSetDiffPrimitiveStructs(t *testing.T) {
p1 := Port{Label: "1"}
p2 := Port{Label: "2"}
p3 := Port{Label: "3"}
p4 := Port{Label: "4"}
p5 := Port{Label: "5"}
p6 := Port{Label: "6"}
old := []Port{p1, p2, p3, p4}
new := []Port{p3, p4, p5, p6}
diffs := setDiffPrimitiveStructs(interfaceSlice(old), interfaceSlice(new), portFields)
if len(diffs) != 4 {
t.Fatalf("expected four diffs: %#v", diffs)
}
var added, deleted int
for _, diff := range diffs {
switch diff.Type {
case DiffTypeAdded:
added++
case DiffTypeDeleted:
deleted++
default:
t.Fatalf("unexpected diff type: %#v", diff.Type)
}
}
if added != 2 && deleted != 2 {
t.Fatalf("incorrect counts")
}
}
func TestNewFieldDiff(t *testing.T) {
cases := []struct {
NilExpected bool
Old, New interface{}
Expected DiffType
}{
{
NilExpected: true,
Old: 1,
New: 1,
},
{
NilExpected: true,
Old: true,
New: true,
},
{
NilExpected: true,
Old: "foo",
New: "foo",
},
{
NilExpected: true,
Old: 2.23,
New: 2.23,
},
{
Old: 1,
New: 4,
Expected: DiffTypeEdited,
},
{
Old: true,
New: false,
Expected: DiffTypeEdited,
},
{
Old: "foo",
New: "bar",
Expected: DiffTypeEdited,
},
{
Old: 2.23,
New: 12.511,
Expected: DiffTypeEdited,
},
{
Old: nil,
New: 4,
Expected: DiffTypeAdded,
},
{
Old: nil,
New: true,
Expected: DiffTypeAdded,
},
{
Old: nil,
New: "bar",
Expected: DiffTypeAdded,
},
{
Old: nil,
New: 12.511,
Expected: DiffTypeAdded,
},
{
Old: 4,
New: nil,
Expected: DiffTypeDeleted,
},
{
Old: true,
New: nil,
Expected: DiffTypeDeleted,
},
{
Old: "bar",
New: nil,
Expected: DiffTypeDeleted,
},
{
Old: 12.511,
New: nil,
Expected: DiffTypeDeleted,
},
}
for i, c := range cases {
diff := NewFieldDiff("foo", c.Old, c.New)
if diff == nil {
if !c.NilExpected {
t.Fatalf("case %d: diff was nil and unexpected", i+1)
}
continue
}
if diff.Type != c.Expected {
t.Fatalf("case %d: wanted type %v; got %v", i+1, diff.Type, c.Expected)
}
}
}
func TestStringSetDiff(t *testing.T) {
values := []string{"1", "2", "3", "4", "5", "6"}
// Edited case
setDiff := NewStringSetDiff(values[:4], values[2:])
if setDiff.Type != DiffTypeEdited {
t.Fatalf("got type %v; want %v", setDiff.Type, DiffTypeEdited)
}
addedExp := []string{"5", "6"}
deletedExp := []string{"1", "2"}
sort.Strings(setDiff.Added)
sort.Strings(setDiff.Deleted)
if !reflect.DeepEqual(addedExp, setDiff.Added) ||
!reflect.DeepEqual(deletedExp, setDiff.Deleted) {
t.Fatalf("bad: %#v", setDiff)
}
// Added case
setDiff = NewStringSetDiff(nil, values)
if setDiff.Type != DiffTypeAdded {
t.Fatalf("got type %v; want %v", setDiff.Type, DiffTypeAdded)
}
// Deleted case
setDiff = NewStringSetDiff(values, nil)
if setDiff.Type != DiffTypeDeleted {
t.Fatalf("got type %v; want %v", setDiff.Type, DiffTypeDeleted)
}
}
func TestStringMapDiff(t *testing.T) {
m1 := map[string]string{
"a": "aval",
"b": "bval",
}
m2 := map[string]string{
"b": "bval2",
"c": "cval",
}
// Edited case
expected := &StringMapDiff{
DiffEntry: DiffEntry{
Type: DiffTypeEdited,
},
Added: map[string]string{"c": "cval"},
Deleted: map[string]string{"a": "aval"},
Edited: map[string]StringValueDelta{
"b": StringValueDelta{Old: "bval",
DiffEntry: DiffEntry{
Type: DiffTypeEdited,
},
New: "bval2",
},
},
}
act := NewStringMapDiff(m1, m2)
if !reflect.DeepEqual(act, expected) {
t.Fatalf("got %#v; want %#v", act, expected)
}
// Added case
diff := NewStringMapDiff(nil, m1)
if diff.Type != DiffTypeAdded {
t.Fatalf("got type %v; want %v", diff.Type, DiffTypeAdded)
}
// Deleted case
diff = NewStringMapDiff(m1, nil)
if diff.Type != DiffTypeDeleted {
t.Fatalf("got type %v; want %v", diff.Type, DiffTypeDeleted)
}
}
func TestSetDifference(t *testing.T) {
old := []interface{}{1, 2}
new := []interface{}{2, 3}
added, deleted := setDifference(old, new)
if len(added) != 1 && len(deleted) != 1 {
t.Fatalf("bad: %#v %#v", added, deleted)
}
a, ok := added[0].(int)
if !ok || a != 3 {
t.Fatalf("bad: %v %v", a, ok)
}
d, ok := deleted[0].(int)
if !ok || d != 1 {
t.Fatalf("bad: %v %v", a, ok)
}
}
func TestKeyedSetDifference(t *testing.T) {
oldMap := map[string]interface{}{
"a": 1,
"b": 2,
"c": 3,
}
newMap := map[string]interface{}{
"b": 3,
"c": 3,
"d": 4,
}
added, deleted, edited, unmodified := keyedSetDifference(oldMap, newMap)
if v, ok := added["d"]; !ok || v.(int) != 4 {
t.Fatalf("bad: %#v", added)
}
if v, ok := deleted["a"]; !ok || v.(int) != 1 {
t.Fatalf("bad: %#v", deleted)
}
if l := len(edited); l != 1 || edited[0] != "b" {
t.Fatalf("bad: %#v", edited)
}
if l := len(unmodified); l != 1 || unmodified[0] != "c" {
t.Fatalf("bad: %#v", edited)
}
}
func TestInterfaceSlice(t *testing.T) {
j1 := TestJob()
j2 := TestJob()
jobs := []*Job{j1, j2}
slice := interfaceSlice(jobs)
if len(slice) != 2 {
t.Fatalf("bad: %#v", slice)
}
f := slice[0]
actJob1, ok := f.(*Job)
if !ok {
t.Fatalf("unexpected type: %v", actJob1)
}
if !reflect.DeepEqual(actJob1, j1) {
t.Fatalf("got %#v, want %#v", actJob1, j1)
}
}
func TestGetField(t *testing.T) {
j := TestJob()
exp := "foo"
j.Type = "foo"
i := getField(j, "Type")
act, ok := i.(string)
if !ok {
t.Fatalf("expected to get string type back")
}
if act != exp {
t.Fatalf("got %v; want %v", act, exp)
}
}

View File

@ -1,52 +0,0 @@
package structs
// JobVisitor is the set of types a visitor must implement to traverse a JobDiff
// structure.
type JobVisitor interface {
VisitJob(*JobDiff)
VisitTaskGroups(*TaskGroupsDiff)
VisitTaskGroup(*TaskGroupDiff)
VisitTasks(*TasksDiff)
VisitTask(*TaskDiff)
VisitServices(*ServicesDiff)
VisitService(*ServiceDiff)
VisitServiceChecks(*ServiceChecksDiff)
VisitServiceCheck(*ServiceCheckDiff)
VisitTaskArtifactsDiff(*TaskArtifactsDiff)
VisitTaskArtifactDiff(*TaskArtifactDiff)
VisitResources(*ResourcesDiff)
VisitNetworkResources(*NetworkResourcesDiff)
VisitNetworkResource(*NetworkResourceDiff)
VisitPorts(*PortsDiff)
VisitPrimitiveStruct(*PrimitiveStructDiff)
VisitField(*FieldDiff)
VisitStringSet(*StringSetDiff)
VisitStringMap(*StringMapDiff)
VisitStringValueDelta(*StringValueDelta)
}
// JobDiffComponent is the method a diff component must implement to be visited.
type JobDiffComponent interface {
Accept(JobVisitor)
}
func (j *JobDiff) Accept(v JobVisitor) { v.VisitJob(j) }
func (t *TaskGroupsDiff) Accept(v JobVisitor) { v.VisitTaskGroups(t) }
func (t *TaskGroupDiff) Accept(v JobVisitor) { v.VisitTaskGroup(t) }
func (t *TasksDiff) Accept(v JobVisitor) { v.VisitTasks(t) }
func (t *TaskDiff) Accept(v JobVisitor) { v.VisitTask(t) }
func (s *ServicesDiff) Accept(v JobVisitor) { v.VisitServices(s) }
func (s *ServiceDiff) Accept(v JobVisitor) { v.VisitService(s) }
func (s *ServiceChecksDiff) Accept(v JobVisitor) { v.VisitServiceChecks(s) }
func (s *ServiceCheckDiff) Accept(v JobVisitor) { v.VisitServiceCheck(s) }
func (t *TaskArtifactsDiff) Accept(v JobVisitor) { v.VisitTaskArtifactsDiff(t) }
func (t *TaskArtifactDiff) Accept(v JobVisitor) { v.VisitTaskArtifactDiff(t) }
func (r *ResourcesDiff) Accept(v JobVisitor) { v.VisitResources(r) }
func (n *NetworkResourcesDiff) Accept(v JobVisitor) { v.VisitNetworkResources(n) }
func (n *NetworkResourceDiff) Accept(v JobVisitor) { v.VisitNetworkResource(n) }
func (p *PortsDiff) Accept(v JobVisitor) { v.VisitPorts(p) }
func (p *PrimitiveStructDiff) Accept(v JobVisitor) { v.VisitPrimitiveStruct(p) }
func (f *FieldDiff) Accept(v JobVisitor) { v.VisitField(f) }
func (s *StringSetDiff) Accept(v JobVisitor) { v.VisitStringSet(s) }
func (s *StringMapDiff) Accept(v JobVisitor) { v.VisitStringMap(s) }
func (s *StringValueDelta) Accept(v JobVisitor) { v.VisitStringValueDelta(s) }