2015-08-20 23:07:26 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
2015-09-12 03:32:55 +00:00
|
|
|
"io/ioutil"
|
2015-08-30 01:03:00 +00:00
|
|
|
"os"
|
2015-09-12 03:32:55 +00:00
|
|
|
"path/filepath"
|
2015-08-20 23:07:26 +00:00
|
|
|
"reflect"
|
|
|
|
"testing"
|
2015-08-21 00:49:04 +00:00
|
|
|
"time"
|
2015-08-23 21:47:51 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/nomad/mock"
|
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
2015-08-20 23:07:26 +00:00
|
|
|
)
|
|
|
|
|
2015-08-23 21:47:51 +00:00
|
|
|
func TestDiffAllocs(t *testing.T) {
|
|
|
|
alloc1 := mock.Alloc() // Ignore
|
|
|
|
alloc2 := mock.Alloc() // Update
|
|
|
|
alloc2u := new(structs.Allocation)
|
|
|
|
*alloc2u = *alloc2
|
2016-02-01 21:57:35 +00:00
|
|
|
alloc2u.AllocModifyIndex += 1
|
2015-08-23 21:47:51 +00:00
|
|
|
alloc3 := mock.Alloc() // Remove
|
|
|
|
alloc4 := mock.Alloc() // Add
|
|
|
|
|
|
|
|
exist := []*structs.Allocation{
|
|
|
|
alloc1,
|
|
|
|
alloc2,
|
|
|
|
alloc3,
|
|
|
|
}
|
2016-02-01 21:57:35 +00:00
|
|
|
update := &allocUpdates{
|
|
|
|
pulled: map[string]*structs.Allocation{
|
|
|
|
alloc2u.ID: alloc2u,
|
|
|
|
alloc4.ID: alloc4,
|
|
|
|
},
|
|
|
|
filtered: map[string]struct{}{
|
|
|
|
alloc1.ID: struct{}{},
|
|
|
|
},
|
2015-08-23 21:47:51 +00:00
|
|
|
}
|
|
|
|
|
2016-02-01 21:57:35 +00:00
|
|
|
result := diffAllocs(exist, update)
|
2015-08-23 21:47:51 +00:00
|
|
|
|
|
|
|
if len(result.ignore) != 1 || result.ignore[0] != alloc1 {
|
|
|
|
t.Fatalf("Bad: %#v", result.ignore)
|
|
|
|
}
|
|
|
|
if len(result.added) != 1 || result.added[0] != alloc4 {
|
|
|
|
t.Fatalf("Bad: %#v", result.added)
|
|
|
|
}
|
|
|
|
if len(result.removed) != 1 || result.removed[0] != alloc3 {
|
|
|
|
t.Fatalf("Bad: %#v", result.removed)
|
|
|
|
}
|
|
|
|
if len(result.updated) != 1 {
|
|
|
|
t.Fatalf("Bad: %#v", result.updated)
|
|
|
|
}
|
|
|
|
if result.updated[0].exist != alloc2 || result.updated[0].updated != alloc2u {
|
|
|
|
t.Fatalf("Bad: %#v", result.updated)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-21 00:49:04 +00:00
|
|
|
func TestRandomStagger(t *testing.T) {
|
|
|
|
intv := time.Minute
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
stagger := randomStagger(intv)
|
|
|
|
if stagger < 0 || stagger >= intv {
|
|
|
|
t.Fatalf("Bad: %v", stagger)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-20 23:07:26 +00:00
|
|
|
func TestShuffleStrings(t *testing.T) {
|
|
|
|
// Generate input
|
|
|
|
inp := make([]string, 10)
|
|
|
|
for idx := range inp {
|
2015-09-07 22:23:03 +00:00
|
|
|
inp[idx] = structs.GenerateUUID()
|
2015-08-20 23:07:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Copy the input
|
|
|
|
orig := make([]string, len(inp))
|
|
|
|
copy(orig, inp)
|
|
|
|
|
|
|
|
// Shuffle
|
|
|
|
shuffleStrings(inp)
|
|
|
|
|
|
|
|
// Ensure order is not the same
|
|
|
|
if reflect.DeepEqual(inp, orig) {
|
|
|
|
t.Fatalf("shuffle failed")
|
|
|
|
}
|
|
|
|
}
|
2015-08-30 01:03:00 +00:00
|
|
|
|
|
|
|
func TestPersistRestoreState(t *testing.T) {
|
2015-09-12 03:32:55 +00:00
|
|
|
dir, err := ioutil.TempDir("", "nomad")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
|
|
|
|
// Use a state path inside a non-existent directory. This
|
|
|
|
// verifies that the directory is created properly.
|
|
|
|
statePath := filepath.Join(dir, "subdir", "test-persist")
|
|
|
|
|
2015-08-30 01:03:00 +00:00
|
|
|
type stateTest struct {
|
|
|
|
Foo int
|
|
|
|
Bar string
|
|
|
|
Baz bool
|
|
|
|
}
|
|
|
|
state := stateTest{
|
|
|
|
Foo: 42,
|
|
|
|
Bar: "the quick brown fox",
|
|
|
|
Baz: true,
|
|
|
|
}
|
|
|
|
|
2015-09-12 03:32:55 +00:00
|
|
|
err = persistState(statePath, &state)
|
2015-08-30 01:03:00 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var out stateTest
|
2015-09-12 03:32:55 +00:00
|
|
|
err = restoreState(statePath, &out)
|
2015-08-30 01:03:00 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(state, out) {
|
|
|
|
t.Fatalf("bad: %#v %#v", state, out)
|
|
|
|
}
|
|
|
|
}
|