github.com/mattyr/nomad@v0.3.3-0.20160919021406-3485a065154a/client/driver/driver_test.go (about) 1 package driver 2 3 import ( 4 "io" 5 "log" 6 "math/rand" 7 "os" 8 "path/filepath" 9 "reflect" 10 "testing" 11 "time" 12 13 "github.com/hashicorp/nomad/client/allocdir" 14 "github.com/hashicorp/nomad/client/config" 15 "github.com/hashicorp/nomad/helper/testtask" 16 "github.com/hashicorp/nomad/nomad/mock" 17 "github.com/hashicorp/nomad/nomad/structs" 18 ) 19 20 var basicResources = &structs.Resources{ 21 CPU: 250, 22 MemoryMB: 256, 23 DiskMB: 20, 24 Networks: []*structs.NetworkResource{ 25 &structs.NetworkResource{ 26 IP: "0.0.0.0", 27 ReservedPorts: []structs.Port{{"main", 12345}}, 28 DynamicPorts: []structs.Port{{"HTTP", 43330}}, 29 }, 30 }, 31 } 32 33 func init() { 34 rand.Seed(49875) 35 } 36 37 func TestMain(m *testing.M) { 38 if !testtask.Run() { 39 os.Exit(m.Run()) 40 } 41 } 42 43 // copyFile moves an existing file to the destination 44 func copyFile(src, dst string, t *testing.T) { 45 in, err := os.Open(src) 46 if err != nil { 47 t.Fatalf("copying %v -> %v failed: %v", src, dst, err) 48 } 49 defer in.Close() 50 out, err := os.Create(dst) 51 if err != nil { 52 t.Fatalf("copying %v -> %v failed: %v", src, dst, err) 53 } 54 defer func() { 55 if err := out.Close(); err != nil { 56 t.Fatalf("copying %v -> %v failed: %v", src, dst, err) 57 } 58 }() 59 if _, err = io.Copy(out, in); err != nil { 60 t.Fatalf("copying %v -> %v failed: %v", src, dst, err) 61 } 62 if err := out.Sync(); err != nil { 63 t.Fatalf("copying %v -> %v failed: %v", src, dst, err) 64 } 65 return 66 } 67 68 func testLogger() *log.Logger { 69 return log.New(os.Stderr, "", log.LstdFlags) 70 } 71 72 func testConfig() *config.Config { 73 conf := config.DefaultConfig() 74 conf.StateDir = os.TempDir() 75 conf.AllocDir = os.TempDir() 76 conf.MaxKillTimeout = 10 * time.Second 77 return conf 78 } 79 80 func testDriverContexts(task *structs.Task) (*DriverContext, *ExecContext) { 81 cfg := testConfig() 82 allocDir := allocdir.NewAllocDir(filepath.Join(cfg.AllocDir, structs.GenerateUUID()), task.Resources.DiskMB) 83 allocDir.Build([]*structs.Task{task}) 84 alloc := mock.Alloc() 85 execCtx := NewExecContext(allocDir, alloc.ID) 86 87 taskEnv, err := GetTaskEnv(allocDir, cfg.Node, task, alloc) 88 if err != nil { 89 return nil, nil 90 } 91 92 driverCtx := NewDriverContext(task.Name, cfg, cfg.Node, testLogger(), taskEnv) 93 return driverCtx, execCtx 94 } 95 96 func TestDriver_GetTaskEnv(t *testing.T) { 97 task := &structs.Task{ 98 Name: "Foo", 99 Env: map[string]string{ 100 "HELLO": "world", 101 "lorem": "ipsum", 102 }, 103 Resources: &structs.Resources{ 104 CPU: 1000, 105 MemoryMB: 500, 106 Networks: []*structs.NetworkResource{ 107 &structs.NetworkResource{ 108 IP: "1.2.3.4", 109 ReservedPorts: []structs.Port{{"one", 80}, {"two", 443}}, 110 DynamicPorts: []structs.Port{{"admin", 8081}, {"web", 8086}}, 111 }, 112 }, 113 }, 114 Meta: map[string]string{ 115 "chocolate": "cake", 116 "strawberry": "icecream", 117 }, 118 } 119 120 alloc := mock.Alloc() 121 alloc.Name = "Bar" 122 env, err := GetTaskEnv(nil, nil, task, alloc) 123 if err != nil { 124 t.Fatalf("GetTaskEnv() failed: %v", err) 125 } 126 exp := map[string]string{ 127 "NOMAD_CPU_LIMIT": "1000", 128 "NOMAD_MEMORY_LIMIT": "500", 129 "NOMAD_ADDR_one": "1.2.3.4:80", 130 "NOMAD_IP_one": "1.2.3.4", 131 "NOMAD_PORT_one": "80", 132 "NOMAD_HOST_PORT_one": "80", 133 "NOMAD_ADDR_two": "1.2.3.4:443", 134 "NOMAD_IP_two": "1.2.3.4", 135 "NOMAD_PORT_two": "443", 136 "NOMAD_HOST_PORT_two": "443", 137 "NOMAD_ADDR_admin": "1.2.3.4:8081", 138 "NOMAD_IP_admin": "1.2.3.4", 139 "NOMAD_PORT_admin": "8081", 140 "NOMAD_HOST_PORT_admin": "8081", 141 "NOMAD_ADDR_web": "1.2.3.4:8086", 142 "NOMAD_IP_web": "1.2.3.4", 143 "NOMAD_PORT_web": "8086", 144 "NOMAD_HOST_PORT_web": "8086", 145 "NOMAD_META_CHOCOLATE": "cake", 146 "NOMAD_META_STRAWBERRY": "icecream", 147 "NOMAD_META_ELB_CHECK_INTERVAL": "30s", 148 "NOMAD_META_ELB_CHECK_TYPE": "http", 149 "NOMAD_META_ELB_CHECK_MIN": "3", 150 "NOMAD_META_OWNER": "armon", 151 "HELLO": "world", 152 "lorem": "ipsum", 153 "NOMAD_ALLOC_ID": alloc.ID, 154 "NOMAD_ALLOC_NAME": alloc.Name, 155 "NOMAD_TASK_NAME": task.Name, 156 } 157 158 act := env.EnvMap() 159 if !reflect.DeepEqual(act, exp) { 160 t.Fatalf("GetTaskEnv() returned %#v; want %#v", act, exp) 161 } 162 } 163 164 func TestMapMergeStrInt(t *testing.T) { 165 a := map[string]int{ 166 "cakes": 5, 167 "cookies": 3, 168 } 169 170 b := map[string]int{ 171 "cakes": 3, 172 "pies": 2, 173 } 174 175 c := mapMergeStrInt(a, b) 176 177 d := map[string]int{ 178 "cakes": 3, 179 "cookies": 3, 180 "pies": 2, 181 } 182 183 if !reflect.DeepEqual(c, d) { 184 t.Errorf("\nExpected\n%+v\nGot\n%+v\n", d, c) 185 } 186 } 187 188 func TestMapMergeStrStr(t *testing.T) { 189 a := map[string]string{ 190 "cake": "chocolate", 191 "cookie": "caramel", 192 } 193 194 b := map[string]string{ 195 "cake": "strawberry", 196 "pie": "apple", 197 } 198 199 c := mapMergeStrStr(a, b) 200 201 d := map[string]string{ 202 "cake": "strawberry", 203 "cookie": "caramel", 204 "pie": "apple", 205 } 206 207 if !reflect.DeepEqual(c, d) { 208 t.Errorf("\nExpected\n%+v\nGot\n%+v\n", d, c) 209 } 210 }