github.com/kardianos/nomad@v0.1.3-0.20151022182107-b13df73ee850/api/tasks_test.go (about) 1 package api 2 3 import ( 4 "reflect" 5 "testing" 6 ) 7 8 func TestTaskGroup_NewTaskGroup(t *testing.T) { 9 grp := NewTaskGroup("grp1", 2) 10 expect := &TaskGroup{ 11 Name: "grp1", 12 Count: 2, 13 } 14 if !reflect.DeepEqual(grp, expect) { 15 t.Fatalf("expect: %#v, got: %#v", expect, grp) 16 } 17 } 18 19 func TestTaskGroup_Constrain(t *testing.T) { 20 grp := NewTaskGroup("grp1", 1) 21 22 // Add a constraint to the group 23 out := grp.Constrain(HardConstraint("kernel.name", "=", "darwin")) 24 if n := len(grp.Constraints); n != 1 { 25 t.Fatalf("expected 1 constraint, got: %d", n) 26 } 27 28 // Check that the group was returned 29 if out != grp { 30 t.Fatalf("expected: %#v, got: %#v", grp, out) 31 } 32 33 // Add a second constraint 34 grp.Constrain(SoftConstraint("memory.totalbytes", ">=", "128000000", 2)) 35 expect := []*Constraint{ 36 &Constraint{ 37 Hard: true, 38 LTarget: "kernel.name", 39 RTarget: "darwin", 40 Operand: "=", 41 Weight: 0, 42 }, 43 &Constraint{ 44 Hard: false, 45 LTarget: "memory.totalbytes", 46 RTarget: "128000000", 47 Operand: ">=", 48 Weight: 2, 49 }, 50 } 51 if !reflect.DeepEqual(grp.Constraints, expect) { 52 t.Fatalf("expect: %#v, got: %#v", expect, grp.Constraints) 53 } 54 } 55 56 func TestTaskGroup_SetMeta(t *testing.T) { 57 grp := NewTaskGroup("grp1", 1) 58 59 // Initializes an empty map 60 out := grp.SetMeta("foo", "bar") 61 if grp.Meta == nil { 62 t.Fatalf("should be initialized") 63 } 64 65 // Check that we returned the group 66 if out != grp { 67 t.Fatalf("expect: %#v, got: %#v", grp, out) 68 } 69 70 // Add a second meta k/v 71 grp.SetMeta("baz", "zip") 72 expect := map[string]string{"foo": "bar", "baz": "zip"} 73 if !reflect.DeepEqual(grp.Meta, expect) { 74 t.Fatalf("expect: %#v, got: %#v", expect, grp.Meta) 75 } 76 } 77 78 func TestTaskGroup_AddTask(t *testing.T) { 79 grp := NewTaskGroup("grp1", 1) 80 81 // Add the task to the task group 82 out := grp.AddTask(NewTask("task1", "java")) 83 if n := len(grp.Tasks); n != 1 { 84 t.Fatalf("expected 1 task, got: %d", n) 85 } 86 87 // Check that we returned the group 88 if out != grp { 89 t.Fatalf("expect: %#v, got: %#v", grp, out) 90 } 91 92 // Add a second task 93 grp.AddTask(NewTask("task2", "exec")) 94 expect := []*Task{ 95 &Task{ 96 Name: "task1", 97 Driver: "java", 98 }, 99 &Task{ 100 Name: "task2", 101 Driver: "exec", 102 }, 103 } 104 if !reflect.DeepEqual(grp.Tasks, expect) { 105 t.Fatalf("expect: %#v, got: %#v", expect, grp.Tasks) 106 } 107 } 108 109 func TestTask_NewTask(t *testing.T) { 110 task := NewTask("task1", "exec") 111 expect := &Task{ 112 Name: "task1", 113 Driver: "exec", 114 } 115 if !reflect.DeepEqual(task, expect) { 116 t.Fatalf("expect: %#v, got: %#v", expect, task) 117 } 118 } 119 120 func TestTask_SetConfig(t *testing.T) { 121 task := NewTask("task1", "exec") 122 123 // Initializes an empty map 124 out := task.SetConfig("foo", "bar") 125 if task.Config == nil { 126 t.Fatalf("should be initialized") 127 } 128 129 // Check that we returned the task 130 if out != task { 131 t.Fatalf("expect: %#v, got: %#v", task, out) 132 } 133 134 // Set another config value 135 task.SetConfig("baz", "zip") 136 expect := map[string]string{"foo": "bar", "baz": "zip"} 137 if !reflect.DeepEqual(task.Config, expect) { 138 t.Fatalf("expect: %#v, got: %#v", expect, task.Config) 139 } 140 } 141 142 func TestTask_SetMeta(t *testing.T) { 143 task := NewTask("task1", "exec") 144 145 // Initializes an empty map 146 out := task.SetMeta("foo", "bar") 147 if task.Meta == nil { 148 t.Fatalf("should be initialized") 149 } 150 151 // Check that we returned the task 152 if out != task { 153 t.Fatalf("expect: %#v, got: %#v", task, out) 154 } 155 156 // Set another meta k/v 157 task.SetMeta("baz", "zip") 158 expect := map[string]string{"foo": "bar", "baz": "zip"} 159 if !reflect.DeepEqual(task.Meta, expect) { 160 t.Fatalf("expect: %#v, got: %#v", expect, task.Meta) 161 } 162 } 163 164 func TestTask_Require(t *testing.T) { 165 task := NewTask("task1", "exec") 166 167 // Create some require resources 168 resources := &Resources{ 169 CPU: 1250, 170 MemoryMB: 128, 171 DiskMB: 2048, 172 IOPS: 500, 173 Networks: []*NetworkResource{ 174 &NetworkResource{ 175 CIDR: "0.0.0.0/0", 176 MBits: 100, 177 ReservedPorts: []int{80, 443}, 178 }, 179 }, 180 } 181 out := task.Require(resources) 182 if !reflect.DeepEqual(task.Resources, resources) { 183 t.Fatalf("expect: %#v, got: %#v", resources, task.Resources) 184 } 185 186 // Check that we returned the task 187 if out != task { 188 t.Fatalf("expect: %#v, got: %#v", task, out) 189 } 190 } 191 192 func TestTask_Constrain(t *testing.T) { 193 task := NewTask("task1", "exec") 194 195 // Add a constraint to the task 196 out := task.Constrain(HardConstraint("kernel.name", "=", "darwin")) 197 if n := len(task.Constraints); n != 1 { 198 t.Fatalf("expected 1 constraint, got: %d", n) 199 } 200 201 // Check that the task was returned 202 if out != task { 203 t.Fatalf("expected: %#v, got: %#v", task, out) 204 } 205 206 // Add a second constraint 207 task.Constrain(SoftConstraint("memory.totalbytes", ">=", "128000000", 2)) 208 expect := []*Constraint{ 209 &Constraint{ 210 Hard: true, 211 LTarget: "kernel.name", 212 RTarget: "darwin", 213 Operand: "=", 214 Weight: 0, 215 }, 216 &Constraint{ 217 Hard: false, 218 LTarget: "memory.totalbytes", 219 RTarget: "128000000", 220 Operand: ">=", 221 Weight: 2, 222 }, 223 } 224 if !reflect.DeepEqual(task.Constraints, expect) { 225 t.Fatalf("expect: %#v, got: %#v", expect, task.Constraints) 226 } 227 }