github.com/bigcommerce/nomad@v0.9.3-bc/nomad/mock/mock.go (about) 1 package mock 2 3 import ( 4 "fmt" 5 "time" 6 7 "github.com/hashicorp/nomad/helper/uuid" 8 "github.com/hashicorp/nomad/nomad/structs" 9 psstructs "github.com/hashicorp/nomad/plugins/shared/structs" 10 ) 11 12 func Node() *structs.Node { 13 node := &structs.Node{ 14 ID: uuid.Generate(), 15 SecretID: uuid.Generate(), 16 Datacenter: "dc1", 17 Name: "foobar", 18 Attributes: map[string]string{ 19 "kernel.name": "linux", 20 "arch": "x86", 21 "nomad.version": "0.5.0", 22 "driver.exec": "1", 23 "driver.mock_driver": "1", 24 }, 25 26 // TODO Remove once clientv2 gets merged 27 Resources: &structs.Resources{ 28 CPU: 4000, 29 MemoryMB: 8192, 30 DiskMB: 100 * 1024, 31 }, 32 Reserved: &structs.Resources{ 33 CPU: 100, 34 MemoryMB: 256, 35 DiskMB: 4 * 1024, 36 Networks: []*structs.NetworkResource{ 37 { 38 Device: "eth0", 39 IP: "192.168.0.100", 40 ReservedPorts: []structs.Port{{Label: "ssh", Value: 22}}, 41 MBits: 1, 42 }, 43 }, 44 }, 45 46 NodeResources: &structs.NodeResources{ 47 Cpu: structs.NodeCpuResources{ 48 CpuShares: 4000, 49 }, 50 Memory: structs.NodeMemoryResources{ 51 MemoryMB: 8192, 52 }, 53 Disk: structs.NodeDiskResources{ 54 DiskMB: 100 * 1024, 55 }, 56 Networks: []*structs.NetworkResource{ 57 { 58 Device: "eth0", 59 CIDR: "192.168.0.100/32", 60 MBits: 1000, 61 }, 62 }, 63 }, 64 ReservedResources: &structs.NodeReservedResources{ 65 Cpu: structs.NodeReservedCpuResources{ 66 CpuShares: 100, 67 }, 68 Memory: structs.NodeReservedMemoryResources{ 69 MemoryMB: 256, 70 }, 71 Disk: structs.NodeReservedDiskResources{ 72 DiskMB: 4 * 1024, 73 }, 74 Networks: structs.NodeReservedNetworkResources{ 75 ReservedHostPorts: "22", 76 }, 77 }, 78 Links: map[string]string{ 79 "consul": "foobar.dc1", 80 }, 81 Meta: map[string]string{ 82 "pci-dss": "true", 83 "database": "mysql", 84 "version": "5.6", 85 }, 86 NodeClass: "linux-medium-pci", 87 Status: structs.NodeStatusReady, 88 SchedulingEligibility: structs.NodeSchedulingEligible, 89 } 90 node.ComputeClass() 91 return node 92 } 93 94 // NvidiaNode returns a node with two instances of an Nvidia GPU 95 func NvidiaNode() *structs.Node { 96 n := Node() 97 n.NodeResources.Devices = []*structs.NodeDeviceResource{ 98 { 99 Type: "gpu", 100 Vendor: "nvidia", 101 Name: "1080ti", 102 Attributes: map[string]*psstructs.Attribute{ 103 "memory": psstructs.NewIntAttribute(11, psstructs.UnitGiB), 104 "cuda_cores": psstructs.NewIntAttribute(3584, ""), 105 "graphics_clock": psstructs.NewIntAttribute(1480, psstructs.UnitMHz), 106 "memory_bandwidth": psstructs.NewIntAttribute(11, psstructs.UnitGBPerS), 107 }, 108 Instances: []*structs.NodeDevice{ 109 { 110 ID: uuid.Generate(), 111 Healthy: true, 112 }, 113 { 114 ID: uuid.Generate(), 115 Healthy: true, 116 }, 117 }, 118 }, 119 } 120 n.ComputeClass() 121 return n 122 } 123 124 func HCL() string { 125 return `job "my-job" { 126 datacenters = ["dc1"] 127 type = "service" 128 constraint { 129 attribute = "${attr.kernel.name}" 130 value = "linux" 131 } 132 133 group "web" { 134 count = 10 135 restart { 136 attempts = 3 137 interval = "10m" 138 delay = "1m" 139 mode = "delay" 140 } 141 task "web" { 142 driver = "exec" 143 config { 144 command = "/bin/date" 145 } 146 resources { 147 cpu = 500 148 memory = 256 149 } 150 } 151 } 152 } 153 ` 154 } 155 156 func Job() *structs.Job { 157 job := &structs.Job{ 158 Region: "global", 159 ID: fmt.Sprintf("mock-service-%s", uuid.Generate()), 160 Name: "my-job", 161 Namespace: structs.DefaultNamespace, 162 Type: structs.JobTypeService, 163 Priority: 50, 164 AllAtOnce: false, 165 Datacenters: []string{"dc1"}, 166 Constraints: []*structs.Constraint{ 167 { 168 LTarget: "${attr.kernel.name}", 169 RTarget: "linux", 170 Operand: "=", 171 }, 172 }, 173 TaskGroups: []*structs.TaskGroup{ 174 { 175 Name: "web", 176 Count: 10, 177 EphemeralDisk: &structs.EphemeralDisk{ 178 SizeMB: 150, 179 }, 180 RestartPolicy: &structs.RestartPolicy{ 181 Attempts: 3, 182 Interval: 10 * time.Minute, 183 Delay: 1 * time.Minute, 184 Mode: structs.RestartPolicyModeDelay, 185 }, 186 ReschedulePolicy: &structs.ReschedulePolicy{ 187 Attempts: 2, 188 Interval: 10 * time.Minute, 189 Delay: 5 * time.Second, 190 DelayFunction: "constant", 191 }, 192 Migrate: structs.DefaultMigrateStrategy(), 193 Tasks: []*structs.Task{ 194 { 195 Name: "web", 196 Driver: "exec", 197 Config: map[string]interface{}{ 198 "command": "/bin/date", 199 }, 200 Env: map[string]string{ 201 "FOO": "bar", 202 }, 203 Services: []*structs.Service{ 204 { 205 Name: "${TASK}-frontend", 206 PortLabel: "http", 207 Tags: []string{"pci:${meta.pci-dss}", "datacenter:${node.datacenter}"}, 208 Checks: []*structs.ServiceCheck{ 209 { 210 Name: "check-table", 211 Type: structs.ServiceCheckScript, 212 Command: "/usr/local/check-table-${meta.database}", 213 Args: []string{"${meta.version}"}, 214 Interval: 30 * time.Second, 215 Timeout: 5 * time.Second, 216 }, 217 }, 218 }, 219 { 220 Name: "${TASK}-admin", 221 PortLabel: "admin", 222 }, 223 }, 224 LogConfig: structs.DefaultLogConfig(), 225 Resources: &structs.Resources{ 226 CPU: 500, 227 MemoryMB: 256, 228 Networks: []*structs.NetworkResource{ 229 { 230 MBits: 50, 231 DynamicPorts: []structs.Port{ 232 {Label: "http"}, 233 {Label: "admin"}, 234 }, 235 }, 236 }, 237 }, 238 Meta: map[string]string{ 239 "foo": "bar", 240 }, 241 }, 242 }, 243 Meta: map[string]string{ 244 "elb_check_type": "http", 245 "elb_check_interval": "30s", 246 "elb_check_min": "3", 247 }, 248 }, 249 }, 250 Meta: map[string]string{ 251 "owner": "armon", 252 }, 253 Status: structs.JobStatusPending, 254 Version: 0, 255 CreateIndex: 42, 256 ModifyIndex: 99, 257 JobModifyIndex: 99, 258 } 259 job.Canonicalize() 260 return job 261 } 262 263 func BatchJob() *structs.Job { 264 job := &structs.Job{ 265 Region: "global", 266 ID: fmt.Sprintf("mock-batch-%s", uuid.Generate()), 267 Name: "batch-job", 268 Namespace: structs.DefaultNamespace, 269 Type: structs.JobTypeBatch, 270 Priority: 50, 271 AllAtOnce: false, 272 Datacenters: []string{"dc1"}, 273 TaskGroups: []*structs.TaskGroup{ 274 { 275 Name: "web", 276 Count: 10, 277 EphemeralDisk: &structs.EphemeralDisk{ 278 SizeMB: 150, 279 }, 280 RestartPolicy: &structs.RestartPolicy{ 281 Attempts: 3, 282 Interval: 10 * time.Minute, 283 Delay: 1 * time.Minute, 284 Mode: structs.RestartPolicyModeDelay, 285 }, 286 ReschedulePolicy: &structs.ReschedulePolicy{ 287 Attempts: 2, 288 Interval: 10 * time.Minute, 289 Delay: 5 * time.Second, 290 DelayFunction: "constant", 291 }, 292 Tasks: []*structs.Task{ 293 { 294 Name: "web", 295 Driver: "mock_driver", 296 Config: map[string]interface{}{ 297 "run_for": "500ms", 298 }, 299 Env: map[string]string{ 300 "FOO": "bar", 301 }, 302 LogConfig: structs.DefaultLogConfig(), 303 Resources: &structs.Resources{ 304 CPU: 100, 305 MemoryMB: 100, 306 Networks: []*structs.NetworkResource{ 307 { 308 MBits: 50, 309 }, 310 }, 311 }, 312 Meta: map[string]string{ 313 "foo": "bar", 314 }, 315 }, 316 }, 317 }, 318 }, 319 Status: structs.JobStatusPending, 320 Version: 0, 321 CreateIndex: 43, 322 ModifyIndex: 99, 323 JobModifyIndex: 99, 324 } 325 job.Canonicalize() 326 return job 327 } 328 329 func SystemJob() *structs.Job { 330 job := &structs.Job{ 331 Region: "global", 332 Namespace: structs.DefaultNamespace, 333 ID: fmt.Sprintf("mock-system-%s", uuid.Generate()), 334 Name: "my-job", 335 Type: structs.JobTypeSystem, 336 Priority: 100, 337 AllAtOnce: false, 338 Datacenters: []string{"dc1"}, 339 Constraints: []*structs.Constraint{ 340 { 341 LTarget: "${attr.kernel.name}", 342 RTarget: "linux", 343 Operand: "=", 344 }, 345 }, 346 TaskGroups: []*structs.TaskGroup{ 347 { 348 Name: "web", 349 Count: 1, 350 RestartPolicy: &structs.RestartPolicy{ 351 Attempts: 3, 352 Interval: 10 * time.Minute, 353 Delay: 1 * time.Minute, 354 Mode: structs.RestartPolicyModeDelay, 355 }, 356 EphemeralDisk: structs.DefaultEphemeralDisk(), 357 Tasks: []*structs.Task{ 358 { 359 Name: "web", 360 Driver: "exec", 361 Config: map[string]interface{}{ 362 "command": "/bin/date", 363 }, 364 Env: map[string]string{}, 365 Resources: &structs.Resources{ 366 CPU: 500, 367 MemoryMB: 256, 368 Networks: []*structs.NetworkResource{ 369 { 370 MBits: 50, 371 DynamicPorts: []structs.Port{{Label: "http"}}, 372 }, 373 }, 374 }, 375 LogConfig: structs.DefaultLogConfig(), 376 }, 377 }, 378 }, 379 }, 380 Meta: map[string]string{ 381 "owner": "armon", 382 }, 383 Status: structs.JobStatusPending, 384 CreateIndex: 42, 385 ModifyIndex: 99, 386 } 387 job.Canonicalize() 388 return job 389 } 390 391 func PeriodicJob() *structs.Job { 392 job := Job() 393 job.Type = structs.JobTypeBatch 394 job.Periodic = &structs.PeriodicConfig{ 395 Enabled: true, 396 SpecType: structs.PeriodicSpecCron, 397 Spec: "*/30 * * * *", 398 } 399 job.Status = structs.JobStatusRunning 400 job.TaskGroups[0].Migrate = nil 401 return job 402 } 403 404 func Eval() *structs.Evaluation { 405 eval := &structs.Evaluation{ 406 ID: uuid.Generate(), 407 Namespace: structs.DefaultNamespace, 408 Priority: 50, 409 Type: structs.JobTypeService, 410 JobID: uuid.Generate(), 411 Status: structs.EvalStatusPending, 412 } 413 return eval 414 } 415 416 func JobSummary(jobID string) *structs.JobSummary { 417 js := &structs.JobSummary{ 418 JobID: jobID, 419 Namespace: structs.DefaultNamespace, 420 Summary: map[string]structs.TaskGroupSummary{ 421 "web": { 422 Queued: 0, 423 Starting: 0, 424 }, 425 }, 426 } 427 return js 428 } 429 430 func Alloc() *structs.Allocation { 431 alloc := &structs.Allocation{ 432 ID: uuid.Generate(), 433 EvalID: uuid.Generate(), 434 NodeID: "12345678-abcd-efab-cdef-123456789abc", 435 Namespace: structs.DefaultNamespace, 436 TaskGroup: "web", 437 438 // TODO Remove once clientv2 gets merged 439 Resources: &structs.Resources{ 440 CPU: 500, 441 MemoryMB: 256, 442 DiskMB: 150, 443 Networks: []*structs.NetworkResource{ 444 { 445 Device: "eth0", 446 IP: "192.168.0.100", 447 ReservedPorts: []structs.Port{{Label: "admin", Value: 5000}}, 448 MBits: 50, 449 DynamicPorts: []structs.Port{{Label: "http"}}, 450 }, 451 }, 452 }, 453 TaskResources: map[string]*structs.Resources{ 454 "web": { 455 CPU: 500, 456 MemoryMB: 256, 457 Networks: []*structs.NetworkResource{ 458 { 459 Device: "eth0", 460 IP: "192.168.0.100", 461 ReservedPorts: []structs.Port{{Label: "admin", Value: 5000}}, 462 MBits: 50, 463 DynamicPorts: []structs.Port{{Label: "http", Value: 9876}}, 464 }, 465 }, 466 }, 467 }, 468 SharedResources: &structs.Resources{ 469 DiskMB: 150, 470 }, 471 472 AllocatedResources: &structs.AllocatedResources{ 473 Tasks: map[string]*structs.AllocatedTaskResources{ 474 "web": { 475 Cpu: structs.AllocatedCpuResources{ 476 CpuShares: 500, 477 }, 478 Memory: structs.AllocatedMemoryResources{ 479 MemoryMB: 256, 480 }, 481 Networks: []*structs.NetworkResource{ 482 { 483 Device: "eth0", 484 IP: "192.168.0.100", 485 ReservedPorts: []structs.Port{{Label: "admin", Value: 5000}}, 486 MBits: 50, 487 DynamicPorts: []structs.Port{{Label: "http", Value: 9876}}, 488 }, 489 }, 490 }, 491 }, 492 Shared: structs.AllocatedSharedResources{ 493 DiskMB: 150, 494 }, 495 }, 496 Job: Job(), 497 DesiredStatus: structs.AllocDesiredStatusRun, 498 ClientStatus: structs.AllocClientStatusPending, 499 } 500 alloc.JobID = alloc.Job.ID 501 return alloc 502 } 503 504 func BatchAlloc() *structs.Allocation { 505 alloc := &structs.Allocation{ 506 ID: uuid.Generate(), 507 EvalID: uuid.Generate(), 508 NodeID: "12345678-abcd-efab-cdef-123456789abc", 509 Namespace: structs.DefaultNamespace, 510 TaskGroup: "web", 511 512 // TODO Remove once clientv2 gets merged 513 Resources: &structs.Resources{ 514 CPU: 500, 515 MemoryMB: 256, 516 DiskMB: 150, 517 Networks: []*structs.NetworkResource{ 518 { 519 Device: "eth0", 520 IP: "192.168.0.100", 521 ReservedPorts: []structs.Port{{Label: "admin", Value: 5000}}, 522 MBits: 50, 523 DynamicPorts: []structs.Port{{Label: "http"}}, 524 }, 525 }, 526 }, 527 TaskResources: map[string]*structs.Resources{ 528 "web": { 529 CPU: 500, 530 MemoryMB: 256, 531 Networks: []*structs.NetworkResource{ 532 { 533 Device: "eth0", 534 IP: "192.168.0.100", 535 ReservedPorts: []structs.Port{{Label: "admin", Value: 5000}}, 536 MBits: 50, 537 DynamicPorts: []structs.Port{{Label: "http", Value: 9876}}, 538 }, 539 }, 540 }, 541 }, 542 SharedResources: &structs.Resources{ 543 DiskMB: 150, 544 }, 545 546 AllocatedResources: &structs.AllocatedResources{ 547 Tasks: map[string]*structs.AllocatedTaskResources{ 548 "web": { 549 Cpu: structs.AllocatedCpuResources{ 550 CpuShares: 500, 551 }, 552 Memory: structs.AllocatedMemoryResources{ 553 MemoryMB: 256, 554 }, 555 Networks: []*structs.NetworkResource{ 556 { 557 Device: "eth0", 558 IP: "192.168.0.100", 559 ReservedPorts: []structs.Port{{Label: "admin", Value: 5000}}, 560 MBits: 50, 561 DynamicPorts: []structs.Port{{Label: "http", Value: 9876}}, 562 }, 563 }, 564 }, 565 }, 566 Shared: structs.AllocatedSharedResources{ 567 DiskMB: 150, 568 }, 569 }, 570 Job: BatchJob(), 571 DesiredStatus: structs.AllocDesiredStatusRun, 572 ClientStatus: structs.AllocClientStatusPending, 573 } 574 alloc.JobID = alloc.Job.ID 575 return alloc 576 } 577 578 func SystemAlloc() *structs.Allocation { 579 alloc := &structs.Allocation{ 580 ID: uuid.Generate(), 581 EvalID: uuid.Generate(), 582 NodeID: "12345678-abcd-efab-cdef-123456789abc", 583 Namespace: structs.DefaultNamespace, 584 TaskGroup: "web", 585 586 // TODO Remove once clientv2 gets merged 587 Resources: &structs.Resources{ 588 CPU: 500, 589 MemoryMB: 256, 590 DiskMB: 150, 591 Networks: []*structs.NetworkResource{ 592 { 593 Device: "eth0", 594 IP: "192.168.0.100", 595 ReservedPorts: []structs.Port{{Label: "admin", Value: 5000}}, 596 MBits: 50, 597 DynamicPorts: []structs.Port{{Label: "http"}}, 598 }, 599 }, 600 }, 601 TaskResources: map[string]*structs.Resources{ 602 "web": { 603 CPU: 500, 604 MemoryMB: 256, 605 Networks: []*structs.NetworkResource{ 606 { 607 Device: "eth0", 608 IP: "192.168.0.100", 609 ReservedPorts: []structs.Port{{Label: "admin", Value: 5000}}, 610 MBits: 50, 611 DynamicPorts: []structs.Port{{Label: "http", Value: 9876}}, 612 }, 613 }, 614 }, 615 }, 616 SharedResources: &structs.Resources{ 617 DiskMB: 150, 618 }, 619 620 AllocatedResources: &structs.AllocatedResources{ 621 Tasks: map[string]*structs.AllocatedTaskResources{ 622 "web": { 623 Cpu: structs.AllocatedCpuResources{ 624 CpuShares: 500, 625 }, 626 Memory: structs.AllocatedMemoryResources{ 627 MemoryMB: 256, 628 }, 629 Networks: []*structs.NetworkResource{ 630 { 631 Device: "eth0", 632 IP: "192.168.0.100", 633 ReservedPorts: []structs.Port{{Label: "admin", Value: 5000}}, 634 MBits: 50, 635 DynamicPorts: []structs.Port{{Label: "http", Value: 9876}}, 636 }, 637 }, 638 }, 639 }, 640 Shared: structs.AllocatedSharedResources{ 641 DiskMB: 150, 642 }, 643 }, 644 Job: SystemJob(), 645 DesiredStatus: structs.AllocDesiredStatusRun, 646 ClientStatus: structs.AllocClientStatusPending, 647 } 648 alloc.JobID = alloc.Job.ID 649 return alloc 650 } 651 652 func VaultAccessor() *structs.VaultAccessor { 653 return &structs.VaultAccessor{ 654 Accessor: uuid.Generate(), 655 NodeID: uuid.Generate(), 656 AllocID: uuid.Generate(), 657 CreationTTL: 86400, 658 Task: "foo", 659 } 660 } 661 662 func Deployment() *structs.Deployment { 663 return &structs.Deployment{ 664 ID: uuid.Generate(), 665 JobID: uuid.Generate(), 666 Namespace: structs.DefaultNamespace, 667 JobVersion: 2, 668 JobModifyIndex: 20, 669 JobCreateIndex: 18, 670 TaskGroups: map[string]*structs.DeploymentState{ 671 "web": { 672 DesiredTotal: 10, 673 }, 674 }, 675 Status: structs.DeploymentStatusRunning, 676 StatusDescription: structs.DeploymentStatusDescriptionRunning, 677 ModifyIndex: 23, 678 CreateIndex: 21, 679 } 680 } 681 682 func Plan() *structs.Plan { 683 return &structs.Plan{ 684 Priority: 50, 685 } 686 } 687 688 func PlanResult() *structs.PlanResult { 689 return &structs.PlanResult{} 690 } 691 692 func ACLPolicy() *structs.ACLPolicy { 693 ap := &structs.ACLPolicy{ 694 Name: fmt.Sprintf("policy-%s", uuid.Generate()), 695 Description: "Super cool policy!", 696 Rules: ` 697 namespace "default" { 698 policy = "write" 699 } 700 node { 701 policy = "read" 702 } 703 agent { 704 policy = "read" 705 } 706 `, 707 CreateIndex: 10, 708 ModifyIndex: 20, 709 } 710 ap.SetHash() 711 return ap 712 } 713 714 func ACLToken() *structs.ACLToken { 715 tk := &structs.ACLToken{ 716 AccessorID: uuid.Generate(), 717 SecretID: uuid.Generate(), 718 Name: "my cool token " + uuid.Generate(), 719 Type: "client", 720 Policies: []string{"foo", "bar"}, 721 Global: false, 722 CreateTime: time.Now().UTC(), 723 CreateIndex: 10, 724 ModifyIndex: 20, 725 } 726 tk.SetHash() 727 return tk 728 } 729 730 func ACLManagementToken() *structs.ACLToken { 731 return &structs.ACLToken{ 732 AccessorID: uuid.Generate(), 733 SecretID: uuid.Generate(), 734 Name: "management " + uuid.Generate(), 735 Type: "management", 736 Global: true, 737 CreateTime: time.Now().UTC(), 738 CreateIndex: 10, 739 ModifyIndex: 20, 740 } 741 }