open-nomad/client/driver/driver_test.go

97 lines
2.3 KiB
Go
Raw Normal View History

2015-09-01 21:56:42 +00:00
package driver
import (
"log"
"os"
2015-09-25 23:49:14 +00:00
"path/filepath"
2015-09-26 22:37:48 +00:00
"reflect"
"sort"
2015-09-24 07:17:33 +00:00
"testing"
2015-09-25 23:49:14 +00:00
"github.com/hashicorp/nomad/client/allocdir"
"github.com/hashicorp/nomad/client/config"
2015-09-24 07:17:33 +00:00
"github.com/hashicorp/nomad/nomad/structs"
2015-09-01 21:56:42 +00:00
)
var basicResources = &structs.Resources{
CPU: 250,
MemoryMB: 256,
Networks: []*structs.NetworkResource{
&structs.NetworkResource{
IP: "1.2.3.4",
ReservedPorts: []int{12345},
DynamicPorts: []string{"HTTP"},
},
},
}
2015-09-01 21:56:42 +00:00
func testLogger() *log.Logger {
return log.New(os.Stderr, "", log.LstdFlags)
}
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-25 23:49:14 +00:00
func testDriverContext(task string) *DriverContext {
cfg := testConfig()
2015-09-25 23:49:14 +00:00
return NewDriverContext(task, cfg, cfg.Node, testLogger())
}
func testDriverExecContext(task *structs.Task, driverCtx *DriverContext) *ExecContext {
allocDir := allocdir.NewAllocDir(filepath.Join(driverCtx.config.AllocDir, structs.GenerateUUID()))
allocDir.Build([]*structs.Task{task})
ctx := NewExecContext(allocDir)
return ctx
}
2015-09-24 07:17:33 +00:00
2015-09-26 22:37:48 +00:00
func TestDriver_TaskEnvironmentVariables(t *testing.T) {
2015-09-24 07:17:33 +00:00
ctx := &ExecContext{}
task := &structs.Task{
Resources: &structs.Resources{
CPU: 1000,
MemoryMB: 500,
Networks: []*structs.NetworkResource{
&structs.NetworkResource{
IP: "1.2.3.4",
ReservedPorts: []int{80, 443, 8080, 12345},
DynamicPorts: []string{"admin", "5000"},
},
},
},
Meta: map[string]string{
"chocolate": "cake",
"strawberry": "icecream",
},
}
2015-09-26 22:37:48 +00:00
env := TaskEnvironmentVariables(ctx, task)
exp := map[string]string{
"NOMAD_CPU_LIMIT": "1000",
"NOMAD_MEMORY_LIMIT": "500",
"NOMAD_IP": "1.2.3.4",
"NOMAD_PORT_admin": "8080",
"NOMAD_PORT_5000": "12345",
"NOMAD_META_CHOCOLATE": "cake",
"NOMAD_META_STRAWBERRY": "icecream",
"NOMAD_ALLOC_DIR": "",
2015-09-24 07:17:33 +00:00
}
2015-09-26 22:37:48 +00:00
if !reflect.DeepEqual(env, exp) {
t.Fatalf("TaskEnvironmentVariables(%#v, %#v) returned %#v; want %#v", ctx, task, env, exp)
2015-09-24 07:17:33 +00:00
}
2015-09-26 22:37:48 +00:00
}
2015-09-24 07:17:33 +00:00
2015-09-26 22:37:48 +00:00
func TestDriver_PopulateEnvironment(t *testing.T) {
envVars := map[string]string{"foo": "bar", "BAZ": "baM"}
act := PopulateEnvironment(envVars)
sort.Strings(act)
exp := []string{"foo=bar", "BAZ=baM"}
if !reflect.DeepEqual(act, exp) {
t.Fatalf("PopulateEnvironment(%v) returned %v; want %v", envVars, act, exp)
}
2015-09-24 07:17:33 +00:00
}