github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/command/agent/testingutils_test.go (about) 1 package agent 2 3 import ( 4 "time" 5 6 "github.com/hashicorp/nomad/api" 7 "github.com/hashicorp/nomad/helper/pointer" 8 "github.com/hashicorp/nomad/helper/uuid" 9 ) 10 11 func MockJob() *api.Job { 12 job := &api.Job{ 13 Region: pointer.Of("global"), 14 ID: pointer.Of(uuid.Generate()), 15 Name: pointer.Of("my-job"), 16 Type: pointer.Of("service"), 17 Priority: pointer.Of(50), 18 AllAtOnce: pointer.Of(false), 19 Datacenters: []string{"dc1"}, 20 Constraints: []*api.Constraint{ 21 { 22 LTarget: "${attr.kernel.name}", 23 RTarget: "linux", 24 Operand: "=", 25 }, 26 }, 27 TaskGroups: []*api.TaskGroup{ 28 { 29 Name: pointer.Of("web"), 30 Count: pointer.Of(10), 31 EphemeralDisk: &api.EphemeralDisk{ 32 SizeMB: pointer.Of(150), 33 }, 34 RestartPolicy: &api.RestartPolicy{ 35 Attempts: pointer.Of(3), 36 Interval: pointer.Of(10 * time.Minute), 37 Delay: pointer.Of(1 * time.Minute), 38 Mode: pointer.Of("delay"), 39 }, 40 Networks: []*api.NetworkResource{ 41 { 42 Mode: "host", 43 DynamicPorts: []api.Port{{Label: "http"}, {Label: "admin"}}, 44 }, 45 }, 46 Tasks: []*api.Task{ 47 { 48 Name: "web", 49 Driver: "exec", 50 Config: map[string]interface{}{ 51 "command": "/bin/date", 52 }, 53 Env: map[string]string{ 54 "FOO": "bar", 55 }, 56 Services: []*api.Service{ 57 { 58 Name: "${TASK}-frontend", 59 PortLabel: "http", 60 Tags: []string{"pci:${meta.pci-dss}", "datacenter:${node.datacenter}"}, 61 Checks: []api.ServiceCheck{ 62 { 63 Name: "check-table", 64 Type: "script", 65 Command: "/usr/local/check-table-${meta.database}", 66 Args: []string{"${meta.version}"}, 67 Interval: 30 * time.Second, 68 Timeout: 5 * time.Second, 69 }, 70 }, 71 }, 72 { 73 Name: "${TASK}-admin", 74 PortLabel: "admin", 75 }, 76 }, 77 LogConfig: api.DefaultLogConfig(), 78 Resources: &api.Resources{ 79 CPU: pointer.Of(500), 80 MemoryMB: pointer.Of(256), 81 }, 82 Meta: map[string]string{ 83 "foo": "bar", 84 }, 85 }, 86 }, 87 Meta: map[string]string{ 88 "elb_check_type": "http", 89 "elb_check_interval": "30s", 90 "elb_check_min": "3", 91 }, 92 }, 93 }, 94 Meta: map[string]string{ 95 "owner": "armon", 96 }, 97 } 98 job.Canonicalize() 99 return job 100 } 101 102 func MockRegionalJob() *api.Job { 103 j := MockJob() 104 j.Region = pointer.Of("north-america") 105 return j 106 } 107 108 // MockRunnableJob returns a mock job that has a configuration that allows it to be 109 // placed on a TestAgent. 110 func MockRunnableJob() *api.Job { 111 job := MockJob() 112 113 // Configure job so it can be run on a TestAgent 114 job.Constraints = nil 115 job.TaskGroups[0].Constraints = nil 116 job.TaskGroups[0].Count = pointer.Of(1) 117 job.TaskGroups[0].Tasks[0].Driver = "mock_driver" 118 job.TaskGroups[0].Tasks[0].Services = nil 119 job.TaskGroups[0].Tasks[0].Config = map[string]interface{}{ 120 "run_for": "10s", 121 } 122 123 return job 124 }