github.com/mattyr/nomad@v0.3.3-0.20160919021406-3485a065154a/nomad/mock/mock.go (about) 1 package mock 2 3 import ( 4 "time" 5 6 "github.com/hashicorp/nomad/nomad/structs" 7 ) 8 9 func Node() *structs.Node { 10 node := &structs.Node{ 11 ID: structs.GenerateUUID(), 12 SecretID: structs.GenerateUUID(), 13 Datacenter: "dc1", 14 Name: "foobar", 15 Attributes: map[string]string{ 16 "kernel.name": "linux", 17 "arch": "x86", 18 "nomad.version": "0.5.0", 19 "driver.exec": "1", 20 }, 21 Resources: &structs.Resources{ 22 CPU: 4000, 23 MemoryMB: 8192, 24 DiskMB: 100 * 1024, 25 IOPS: 150, 26 Networks: []*structs.NetworkResource{ 27 &structs.NetworkResource{ 28 Device: "eth0", 29 CIDR: "192.168.0.100/32", 30 MBits: 1000, 31 }, 32 }, 33 }, 34 Reserved: &structs.Resources{ 35 CPU: 100, 36 MemoryMB: 256, 37 DiskMB: 4 * 1024, 38 Networks: []*structs.NetworkResource{ 39 &structs.NetworkResource{ 40 Device: "eth0", 41 IP: "192.168.0.100", 42 ReservedPorts: []structs.Port{{Label: "main", Value: 22}}, 43 MBits: 1, 44 }, 45 }, 46 }, 47 Links: map[string]string{ 48 "consul": "foobar.dc1", 49 }, 50 Meta: map[string]string{ 51 "pci-dss": "true", 52 "database": "mysql", 53 "version": "5.6", 54 }, 55 NodeClass: "linux-medium-pci", 56 Status: structs.NodeStatusReady, 57 } 58 node.ComputeClass() 59 return node 60 } 61 62 func Job() *structs.Job { 63 job := &structs.Job{ 64 Region: "global", 65 ID: structs.GenerateUUID(), 66 Name: "my-job", 67 Type: structs.JobTypeService, 68 Priority: 50, 69 AllAtOnce: false, 70 Datacenters: []string{"dc1"}, 71 Constraints: []*structs.Constraint{ 72 &structs.Constraint{ 73 LTarget: "${attr.kernel.name}", 74 RTarget: "linux", 75 Operand: "=", 76 }, 77 }, 78 TaskGroups: []*structs.TaskGroup{ 79 &structs.TaskGroup{ 80 Name: "web", 81 Count: 10, 82 EphemeralDisk: &structs.EphemeralDisk{ 83 SizeMB: 150, 84 }, 85 RestartPolicy: &structs.RestartPolicy{ 86 Attempts: 3, 87 Interval: 10 * time.Minute, 88 Delay: 1 * time.Minute, 89 Mode: structs.RestartPolicyModeDelay, 90 }, 91 Tasks: []*structs.Task{ 92 &structs.Task{ 93 Name: "web", 94 Driver: "exec", 95 Config: map[string]interface{}{ 96 "command": "/bin/date", 97 }, 98 ExcludeNomadEnv: false, 99 Env: map[string]string{ 100 "FOO": "bar", 101 }, 102 Services: []*structs.Service{ 103 { 104 Name: "${TASK}-frontend", 105 PortLabel: "http", 106 Tags: []string{"pci:${meta.pci-dss}", "datacenter:${node.datacenter}"}, 107 Checks: []*structs.ServiceCheck{ 108 { 109 Name: "check-table", 110 Type: structs.ServiceCheckScript, 111 Command: "/usr/local/check-table-${meta.database}", 112 Args: []string{"${meta.version}"}, 113 Interval: 30 * time.Second, 114 Timeout: 5 * time.Second, 115 }, 116 }, 117 }, 118 { 119 Name: "${TASK}-admin", 120 PortLabel: "admin", 121 }, 122 }, 123 LogConfig: structs.DefaultLogConfig(), 124 Resources: &structs.Resources{ 125 CPU: 500, 126 MemoryMB: 256, 127 Networks: []*structs.NetworkResource{ 128 &structs.NetworkResource{ 129 MBits: 50, 130 DynamicPorts: []structs.Port{{Label: "http"}, {Label: "admin"}}, 131 }, 132 }, 133 }, 134 Meta: map[string]string{ 135 "foo": "bar", 136 }, 137 }, 138 }, 139 Meta: map[string]string{ 140 "elb_check_type": "http", 141 "elb_check_interval": "30s", 142 "elb_check_min": "3", 143 }, 144 }, 145 }, 146 Meta: map[string]string{ 147 "owner": "armon", 148 }, 149 Status: structs.JobStatusPending, 150 CreateIndex: 42, 151 ModifyIndex: 99, 152 JobModifyIndex: 99, 153 } 154 job.Canonicalize() 155 return job 156 } 157 158 func SystemJob() *structs.Job { 159 job := &structs.Job{ 160 Region: "global", 161 ID: structs.GenerateUUID(), 162 Name: "my-job", 163 Type: structs.JobTypeSystem, 164 Priority: 100, 165 AllAtOnce: false, 166 Datacenters: []string{"dc1"}, 167 Constraints: []*structs.Constraint{ 168 &structs.Constraint{ 169 LTarget: "${attr.kernel.name}", 170 RTarget: "linux", 171 Operand: "=", 172 }, 173 }, 174 TaskGroups: []*structs.TaskGroup{ 175 &structs.TaskGroup{ 176 Name: "web", 177 Count: 1, 178 RestartPolicy: &structs.RestartPolicy{ 179 Attempts: 3, 180 Interval: 10 * time.Minute, 181 Delay: 1 * time.Minute, 182 Mode: structs.RestartPolicyModeDelay, 183 }, 184 EphemeralDisk: structs.DefaultEphemeralDisk(), 185 Tasks: []*structs.Task{ 186 &structs.Task{ 187 Name: "web", 188 Driver: "exec", 189 Config: map[string]interface{}{ 190 "command": "/bin/date", 191 }, 192 Env: map[string]string{}, 193 ExcludeNomadEnv: false, 194 Resources: &structs.Resources{ 195 CPU: 500, 196 MemoryMB: 256, 197 Networks: []*structs.NetworkResource{ 198 &structs.NetworkResource{ 199 MBits: 50, 200 DynamicPorts: []structs.Port{{Label: "http"}}, 201 }, 202 }, 203 }, 204 LogConfig: structs.DefaultLogConfig(), 205 }, 206 }, 207 }, 208 }, 209 Meta: map[string]string{ 210 "owner": "armon", 211 }, 212 Status: structs.JobStatusPending, 213 CreateIndex: 42, 214 ModifyIndex: 99, 215 } 216 return job 217 } 218 219 func PeriodicJob() *structs.Job { 220 job := Job() 221 job.Type = structs.JobTypeBatch 222 job.Periodic = &structs.PeriodicConfig{ 223 Enabled: true, 224 SpecType: structs.PeriodicSpecCron, 225 Spec: "*/30 * * * *", 226 } 227 return job 228 } 229 230 func Eval() *structs.Evaluation { 231 eval := &structs.Evaluation{ 232 ID: structs.GenerateUUID(), 233 Priority: 50, 234 Type: structs.JobTypeService, 235 JobID: structs.GenerateUUID(), 236 Status: structs.EvalStatusPending, 237 } 238 return eval 239 } 240 241 func JobSummary(jobID string) *structs.JobSummary { 242 js := &structs.JobSummary{ 243 JobID: jobID, 244 Summary: map[string]structs.TaskGroupSummary{ 245 "web": { 246 Queued: 0, 247 Starting: 0, 248 }, 249 }, 250 } 251 return js 252 } 253 254 func Alloc() *structs.Allocation { 255 alloc := &structs.Allocation{ 256 ID: structs.GenerateUUID(), 257 EvalID: structs.GenerateUUID(), 258 NodeID: "12345678-abcd-efab-cdef-123456789abc", 259 TaskGroup: "web", 260 Resources: &structs.Resources{ 261 CPU: 500, 262 MemoryMB: 256, 263 DiskMB: 150, 264 Networks: []*structs.NetworkResource{ 265 &structs.NetworkResource{ 266 Device: "eth0", 267 IP: "192.168.0.100", 268 ReservedPorts: []structs.Port{{Label: "main", Value: 5000}}, 269 MBits: 50, 270 DynamicPorts: []structs.Port{{Label: "http"}}, 271 }, 272 }, 273 }, 274 TaskResources: map[string]*structs.Resources{ 275 "web": &structs.Resources{ 276 CPU: 500, 277 MemoryMB: 256, 278 Networks: []*structs.NetworkResource{ 279 &structs.NetworkResource{ 280 Device: "eth0", 281 IP: "192.168.0.100", 282 ReservedPorts: []structs.Port{{Label: "main", Value: 5000}}, 283 MBits: 50, 284 DynamicPorts: []structs.Port{{Label: "http"}}, 285 }, 286 }, 287 }, 288 }, 289 SharedResources: &structs.Resources{ 290 DiskMB: 150, 291 }, 292 Job: Job(), 293 DesiredStatus: structs.AllocDesiredStatusRun, 294 ClientStatus: structs.AllocClientStatusPending, 295 } 296 alloc.JobID = alloc.Job.ID 297 return alloc 298 } 299 300 func VaultAccessor() *structs.VaultAccessor { 301 return &structs.VaultAccessor{ 302 Accessor: structs.GenerateUUID(), 303 NodeID: structs.GenerateUUID(), 304 AllocID: structs.GenerateUUID(), 305 CreationTTL: 86400, 306 Task: "foo", 307 } 308 } 309 310 func Plan() *structs.Plan { 311 return &structs.Plan{ 312 Priority: 50, 313 } 314 } 315 316 func PlanResult() *structs.PlanResult { 317 return &structs.PlanResult{} 318 }