github.com/kardianos/nomad@v0.1.3-0.20151022182107-b13df73ee850/jobspec/parse_test.go (about) 1 package jobspec 2 3 import ( 4 "path/filepath" 5 "reflect" 6 "strings" 7 "testing" 8 "time" 9 10 "github.com/hashicorp/nomad/nomad/structs" 11 ) 12 13 func TestParse(t *testing.T) { 14 cases := []struct { 15 File string 16 Result *structs.Job 17 Err bool 18 }{ 19 { 20 "basic.hcl", 21 &structs.Job{ 22 ID: "binstore-storagelocker", 23 Name: "binstore-storagelocker", 24 Type: "service", 25 Priority: 50, 26 AllAtOnce: true, 27 Datacenters: []string{"us2", "eu1"}, 28 Region: "global", 29 30 Meta: map[string]string{ 31 "foo": "bar", 32 }, 33 34 Constraints: []*structs.Constraint{ 35 &structs.Constraint{ 36 Hard: true, 37 LTarget: "kernel.os", 38 RTarget: "windows", 39 Operand: "=", 40 }, 41 }, 42 43 Update: structs.UpdateStrategy{ 44 Stagger: 60 * time.Second, 45 MaxParallel: 2, 46 }, 47 48 TaskGroups: []*structs.TaskGroup{ 49 &structs.TaskGroup{ 50 Name: "outside", 51 Count: 1, 52 Tasks: []*structs.Task{ 53 &structs.Task{ 54 Name: "outside", 55 Driver: "java", 56 Config: map[string]string{ 57 "jar": "s3://my-cool-store/foo.jar", 58 }, 59 Meta: map[string]string{ 60 "my-cool-key": "foobar", 61 }, 62 }, 63 }, 64 }, 65 66 &structs.TaskGroup{ 67 Name: "binsl", 68 Count: 5, 69 Constraints: []*structs.Constraint{ 70 &structs.Constraint{ 71 Hard: true, 72 LTarget: "kernel.os", 73 RTarget: "linux", 74 Operand: "=", 75 }, 76 }, 77 Meta: map[string]string{ 78 "elb_mode": "tcp", 79 "elb_interval": "10", 80 "elb_checks": "3", 81 }, 82 Tasks: []*structs.Task{ 83 &structs.Task{ 84 Name: "binstore", 85 Driver: "docker", 86 Config: map[string]string{ 87 "image": "hashicorp/binstore", 88 }, 89 Env: map[string]string{ 90 "HELLO": "world", 91 "LOREM": "ipsum", 92 }, 93 Resources: &structs.Resources{ 94 CPU: 500, 95 MemoryMB: 128, 96 Networks: []*structs.NetworkResource{ 97 &structs.NetworkResource{ 98 MBits: 100, 99 ReservedPorts: []int{1, 2, 3}, 100 DynamicPorts: []string{"http", "https", "admin"}, 101 }, 102 }, 103 }, 104 }, 105 &structs.Task{ 106 Name: "storagelocker", 107 Driver: "java", 108 Config: map[string]string{ 109 "image": "hashicorp/storagelocker", 110 }, 111 Resources: &structs.Resources{ 112 CPU: 500, 113 MemoryMB: 128, 114 }, 115 Constraints: []*structs.Constraint{ 116 &structs.Constraint{ 117 Hard: true, 118 LTarget: "kernel.arch", 119 RTarget: "amd64", 120 Operand: "=", 121 }, 122 }, 123 }, 124 }, 125 }, 126 }, 127 }, 128 false, 129 }, 130 131 { 132 "multi-network.hcl", 133 nil, 134 true, 135 }, 136 137 { 138 "multi-resource.hcl", 139 nil, 140 true, 141 }, 142 143 { 144 "default-job.hcl", 145 &structs.Job{ 146 ID: "foo", 147 Name: "foo", 148 Priority: 50, 149 Region: "global", 150 Type: "service", 151 }, 152 false, 153 }, 154 155 { 156 "version-constraint.hcl", 157 &structs.Job{ 158 ID: "foo", 159 Name: "foo", 160 Priority: 50, 161 Region: "global", 162 Type: "service", 163 Constraints: []*structs.Constraint{ 164 &structs.Constraint{ 165 Hard: true, 166 LTarget: "$attr.kernel.version", 167 RTarget: "~> 3.2", 168 Operand: "version", 169 }, 170 }, 171 }, 172 false, 173 }, 174 175 { 176 "regexp-constraint.hcl", 177 &structs.Job{ 178 ID: "foo", 179 Name: "foo", 180 Priority: 50, 181 Region: "global", 182 Type: "service", 183 Constraints: []*structs.Constraint{ 184 &structs.Constraint{ 185 Hard: true, 186 LTarget: "$attr.kernel.version", 187 RTarget: "[0-9.]+", 188 Operand: "regexp", 189 }, 190 }, 191 }, 192 false, 193 }, 194 195 { 196 "specify-job.hcl", 197 &structs.Job{ 198 ID: "job1", 199 Name: "My Job", 200 Priority: 50, 201 Region: "global", 202 Type: "service", 203 }, 204 false, 205 }, 206 } 207 208 for _, tc := range cases { 209 path, err := filepath.Abs(filepath.Join("./test-fixtures", tc.File)) 210 if err != nil { 211 t.Fatalf("file: %s\n\n%s", tc.File, err) 212 continue 213 } 214 215 actual, err := ParseFile(path) 216 if (err != nil) != tc.Err { 217 t.Fatalf("file: %s\n\n%s", tc.File, err) 218 continue 219 } 220 221 if !reflect.DeepEqual(actual, tc.Result) { 222 t.Fatalf("file: %s\n\n%#v\n\n%#v", tc.File, actual, tc.Result) 223 } 224 } 225 } 226 227 func TestBadPorts(t *testing.T) { 228 path, err := filepath.Abs(filepath.Join("./test-fixtures", "bad-ports.hcl")) 229 if err != nil { 230 t.Fatalf("Can't get absoluate path for file: %s", err) 231 } 232 233 _, err = ParseFile(path) 234 235 if !strings.Contains(err.Error(), errDynamicPorts.Error()) { 236 t.Fatalf("\nExpected error\n %s\ngot\n %v", errDynamicPorts, err) 237 } 238 } 239 240 func TestOverlappingPorts(t *testing.T) { 241 path, err := filepath.Abs(filepath.Join("./test-fixtures", "overlapping-ports.hcl")) 242 if err != nil { 243 t.Fatalf("Can't get absoluate path for file: %s", err) 244 } 245 246 _, err = ParseFile(path) 247 248 if err == nil { 249 t.Fatalf("Expected an error") 250 } 251 252 if !strings.Contains(err.Error(), "Found a port label collision") { 253 t.Fatalf("Expected collision error; got %v", err) 254 } 255 }