2015-09-01 21:56:42 +00:00
|
|
|
package driver
|
|
|
|
|
|
|
|
import (
|
2015-11-18 03:45:33 +00:00
|
|
|
"fmt"
|
2015-09-01 21:56:42 +00:00
|
|
|
"log"
|
2015-11-18 03:45:33 +00:00
|
|
|
"math/rand"
|
2015-09-01 21:56:42 +00:00
|
|
|
"os"
|
2015-09-25 23:49:14 +00:00
|
|
|
"path/filepath"
|
2015-09-26 22:37:48 +00:00
|
|
|
"reflect"
|
2015-09-24 07:17:33 +00:00
|
|
|
"testing"
|
2015-12-23 00:10:30 +00:00
|
|
|
"time"
|
2015-09-08 19:43:02 +00:00
|
|
|
|
2015-09-25 23:49:14 +00:00
|
|
|
"github.com/hashicorp/nomad/client/allocdir"
|
2015-09-08 19:43:02 +00:00
|
|
|
"github.com/hashicorp/nomad/client/config"
|
2015-11-24 20:11:26 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/testtask"
|
2015-09-24 07:17:33 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
2015-09-01 21:56:42 +00:00
|
|
|
)
|
|
|
|
|
2015-09-24 07:59:57 +00:00
|
|
|
var basicResources = &structs.Resources{
|
|
|
|
CPU: 250,
|
|
|
|
MemoryMB: 256,
|
|
|
|
Networks: []*structs.NetworkResource{
|
|
|
|
&structs.NetworkResource{
|
2015-11-13 05:46:59 +00:00
|
|
|
IP: "0.0.0.0",
|
2015-11-15 08:37:00 +00:00
|
|
|
ReservedPorts: []structs.Port{{"main", 12345}},
|
2015-11-18 03:21:36 +00:00
|
|
|
DynamicPorts: []structs.Port{{"HTTP", 43330}},
|
2015-09-24 07:59:57 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2015-11-18 03:45:33 +00:00
|
|
|
func init() {
|
|
|
|
rand.Seed(49875)
|
|
|
|
}
|
|
|
|
|
2015-11-24 20:11:26 +00:00
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
if !testtask.Run() {
|
|
|
|
os.Exit(m.Run())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-01 21:56:42 +00:00
|
|
|
func testLogger() *log.Logger {
|
|
|
|
return log.New(os.Stderr, "", log.LstdFlags)
|
|
|
|
}
|
2015-09-08 19:43:02 +00:00
|
|
|
|
|
|
|
func testConfig() *config.Config {
|
2015-09-25 23:49:14 +00:00
|
|
|
conf := &config.Config{}
|
|
|
|
conf.StateDir = os.TempDir()
|
|
|
|
conf.AllocDir = os.TempDir()
|
|
|
|
return conf
|
2015-09-08 19:43:02 +00:00
|
|
|
}
|
2015-09-10 01:38:52 +00:00
|
|
|
|
2016-01-06 02:02:11 +00:00
|
|
|
func testDriverContexts(task *structs.Task) (*DriverContext, *ExecContext) {
|
2015-09-10 01:38:52 +00:00
|
|
|
cfg := testConfig()
|
2016-01-06 02:02:11 +00:00
|
|
|
allocDir := allocdir.NewAllocDir(filepath.Join(cfg.AllocDir, structs.GenerateUUID()))
|
2015-09-25 23:49:14 +00:00
|
|
|
allocDir.Build([]*structs.Task{task})
|
2016-01-06 02:02:11 +00:00
|
|
|
execCtx := NewExecContext(allocDir, fmt.Sprintf("alloc-id-%d", int(rand.Int31())))
|
|
|
|
|
|
|
|
taskEnv, err := GetTaskEnv(allocDir, cfg.Node, task)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
driverCtx := NewDriverContext(task.Name, cfg, cfg.Node, testLogger(), taskEnv)
|
|
|
|
return driverCtx, execCtx
|
2015-09-10 01:38:52 +00:00
|
|
|
}
|
2015-09-24 07:17:33 +00:00
|
|
|
|
2015-12-23 00:10:30 +00:00
|
|
|
func TestDriver_KillTimeout(t *testing.T) {
|
|
|
|
expected := 1 * time.Second
|
2016-01-06 02:02:11 +00:00
|
|
|
task := &structs.Task{Name: "foo", KillTimeout: expected}
|
|
|
|
ctx, _ := testDriverContexts(task)
|
|
|
|
ctx.config.MaxKillTimeout = 10 * time.Second
|
2015-12-23 00:10:30 +00:00
|
|
|
|
|
|
|
if actual := ctx.KillTimeout(task); expected != actual {
|
|
|
|
t.Fatalf("KillTimeout(%v) returned %v; want %v", task, actual, expected)
|
|
|
|
}
|
|
|
|
|
|
|
|
expected = 10 * time.Second
|
|
|
|
task = &structs.Task{KillTimeout: 11 * time.Second}
|
|
|
|
|
|
|
|
if actual := ctx.KillTimeout(task); expected != actual {
|
|
|
|
t.Fatalf("KillTimeout(%v) returned %v; want %v", task, actual, expected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-06 02:02:11 +00:00
|
|
|
func TestDriver_GetTaskEnv(t *testing.T) {
|
2015-11-24 20:12:31 +00:00
|
|
|
t.Parallel()
|
2015-09-24 07:17:33 +00:00
|
|
|
task := &structs.Task{
|
2015-10-01 11:22:26 +00:00
|
|
|
Env: map[string]string{
|
|
|
|
"HELLO": "world",
|
|
|
|
"lorem": "ipsum",
|
|
|
|
},
|
2015-09-24 07:17:33 +00:00
|
|
|
Resources: &structs.Resources{
|
|
|
|
CPU: 1000,
|
|
|
|
MemoryMB: 500,
|
|
|
|
Networks: []*structs.NetworkResource{
|
|
|
|
&structs.NetworkResource{
|
|
|
|
IP: "1.2.3.4",
|
2015-11-15 08:37:00 +00:00
|
|
|
ReservedPorts: []structs.Port{{"one", 80}, {"two", 443}, {"three", 8080}, {"four", 12345}},
|
|
|
|
DynamicPorts: []structs.Port{{"admin", 8081}, {"web", 8086}},
|
2015-09-24 07:17:33 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Meta: map[string]string{
|
|
|
|
"chocolate": "cake",
|
|
|
|
"strawberry": "icecream",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-01-11 17:58:26 +00:00
|
|
|
env, err := GetTaskEnv(nil, nil, task)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("GetTaskEnv() failed: %v", err)
|
|
|
|
}
|
2015-09-26 22:37:48 +00:00
|
|
|
exp := map[string]string{
|
|
|
|
"NOMAD_CPU_LIMIT": "1000",
|
|
|
|
"NOMAD_MEMORY_LIMIT": "500",
|
|
|
|
"NOMAD_IP": "1.2.3.4",
|
2015-11-15 09:13:42 +00:00
|
|
|
"NOMAD_PORT_one": "80",
|
|
|
|
"NOMAD_PORT_two": "443",
|
|
|
|
"NOMAD_PORT_three": "8080",
|
|
|
|
"NOMAD_PORT_four": "12345",
|
|
|
|
"NOMAD_PORT_admin": "8081",
|
|
|
|
"NOMAD_PORT_web": "8086",
|
2015-09-26 22:37:48 +00:00
|
|
|
"NOMAD_META_CHOCOLATE": "cake",
|
|
|
|
"NOMAD_META_STRAWBERRY": "icecream",
|
2015-10-01 11:22:26 +00:00
|
|
|
"HELLO": "world",
|
2015-10-03 00:49:18 +00:00
|
|
|
"lorem": "ipsum",
|
2015-09-24 07:17:33 +00:00
|
|
|
}
|
|
|
|
|
2016-01-06 02:02:11 +00:00
|
|
|
act := env.EnvMap()
|
2015-09-26 22:37:48 +00:00
|
|
|
if !reflect.DeepEqual(act, exp) {
|
2016-01-11 17:58:26 +00:00
|
|
|
t.Fatalf("GetTaskEnv() returned %#v; want %#v", act, exp)
|
2015-09-25 23:49:26 +00:00
|
|
|
}
|
2015-09-24 07:17:33 +00:00
|
|
|
}
|
2015-11-20 05:29:37 +00:00
|
|
|
|
|
|
|
func TestMapMergeStrInt(t *testing.T) {
|
2015-11-24 20:12:31 +00:00
|
|
|
t.Parallel()
|
2015-11-20 05:29:37 +00:00
|
|
|
a := map[string]int{
|
|
|
|
"cakes": 5,
|
|
|
|
"cookies": 3,
|
|
|
|
}
|
|
|
|
|
|
|
|
b := map[string]int{
|
|
|
|
"cakes": 3,
|
|
|
|
"pies": 2,
|
|
|
|
}
|
|
|
|
|
|
|
|
c := mapMergeStrInt(a, b)
|
|
|
|
|
|
|
|
d := map[string]int{
|
|
|
|
"cakes": 3,
|
|
|
|
"cookies": 3,
|
|
|
|
"pies": 2,
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(c, d) {
|
|
|
|
t.Errorf("\nExpected\n%+v\nGot\n%+v\n", d, c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMapMergeStrStr(t *testing.T) {
|
2015-11-24 20:12:31 +00:00
|
|
|
t.Parallel()
|
2015-11-20 05:29:37 +00:00
|
|
|
a := map[string]string{
|
|
|
|
"cake": "chocolate",
|
|
|
|
"cookie": "caramel",
|
|
|
|
}
|
|
|
|
|
|
|
|
b := map[string]string{
|
|
|
|
"cake": "strawberry",
|
|
|
|
"pie": "apple",
|
|
|
|
}
|
|
|
|
|
|
|
|
c := mapMergeStrStr(a, b)
|
|
|
|
|
|
|
|
d := map[string]string{
|
|
|
|
"cake": "strawberry",
|
|
|
|
"cookie": "caramel",
|
|
|
|
"pie": "apple",
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(c, d) {
|
|
|
|
t.Errorf("\nExpected\n%+v\nGot\n%+v\n", d, c)
|
|
|
|
}
|
|
|
|
}
|