github.com/hspak/nomad@v0.7.2-0.20180309000617-bc4ae22a39a5/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 ) 10 11 func Node() *structs.Node { 12 node := &structs.Node{ 13 ID: uuid.Generate(), 14 SecretID: uuid.Generate(), 15 Datacenter: "dc1", 16 Name: "foobar", 17 Attributes: map[string]string{ 18 "kernel.name": "linux", 19 "arch": "x86", 20 "nomad.version": "0.5.0", 21 "driver.exec": "1", 22 }, 23 Resources: &structs.Resources{ 24 CPU: 4000, 25 MemoryMB: 8192, 26 DiskMB: 100 * 1024, 27 IOPS: 150, 28 Networks: []*structs.NetworkResource{ 29 { 30 Device: "eth0", 31 CIDR: "192.168.0.100/32", 32 MBits: 1000, 33 }, 34 }, 35 }, 36 Reserved: &structs.Resources{ 37 CPU: 100, 38 MemoryMB: 256, 39 DiskMB: 4 * 1024, 40 Networks: []*structs.NetworkResource{ 41 { 42 Device: "eth0", 43 IP: "192.168.0.100", 44 ReservedPorts: []structs.Port{{Label: "ssh", Value: 22}}, 45 MBits: 1, 46 }, 47 }, 48 }, 49 Links: map[string]string{ 50 "consul": "foobar.dc1", 51 }, 52 Meta: map[string]string{ 53 "pci-dss": "true", 54 "database": "mysql", 55 "version": "5.6", 56 }, 57 NodeClass: "linux-medium-pci", 58 Status: structs.NodeStatusReady, 59 } 60 node.ComputeClass() 61 return node 62 } 63 64 func Job() *structs.Job { 65 job := &structs.Job{ 66 Region: "global", 67 ID: uuid.Generate(), 68 Name: "my-job", 69 Namespace: structs.DefaultNamespace, 70 Type: structs.JobTypeService, 71 Priority: 50, 72 AllAtOnce: false, 73 Datacenters: []string{"dc1"}, 74 Constraints: []*structs.Constraint{ 75 { 76 LTarget: "${attr.kernel.name}", 77 RTarget: "linux", 78 Operand: "=", 79 }, 80 }, 81 TaskGroups: []*structs.TaskGroup{ 82 { 83 Name: "web", 84 Count: 10, 85 EphemeralDisk: &structs.EphemeralDisk{ 86 SizeMB: 150, 87 }, 88 RestartPolicy: &structs.RestartPolicy{ 89 Attempts: 3, 90 Interval: 10 * time.Minute, 91 Delay: 1 * time.Minute, 92 Mode: structs.RestartPolicyModeDelay, 93 }, 94 ReschedulePolicy: &structs.ReschedulePolicy{ 95 Attempts: 2, 96 Interval: 10 * time.Minute, 97 }, 98 Tasks: []*structs.Task{ 99 { 100 Name: "web", 101 Driver: "exec", 102 Config: map[string]interface{}{ 103 "command": "/bin/date", 104 }, 105 Env: map[string]string{ 106 "FOO": "bar", 107 }, 108 Services: []*structs.Service{ 109 { 110 Name: "${TASK}-frontend", 111 PortLabel: "http", 112 Tags: []string{"pci:${meta.pci-dss}", "datacenter:${node.datacenter}"}, 113 Checks: []*structs.ServiceCheck{ 114 { 115 Name: "check-table", 116 Type: structs.ServiceCheckScript, 117 Command: "/usr/local/check-table-${meta.database}", 118 Args: []string{"${meta.version}"}, 119 Interval: 30 * time.Second, 120 Timeout: 5 * time.Second, 121 }, 122 }, 123 }, 124 { 125 Name: "${TASK}-admin", 126 PortLabel: "admin", 127 }, 128 }, 129 LogConfig: structs.DefaultLogConfig(), 130 Resources: &structs.Resources{ 131 CPU: 500, 132 MemoryMB: 256, 133 Networks: []*structs.NetworkResource{ 134 { 135 MBits: 50, 136 DynamicPorts: []structs.Port{ 137 {Label: "http"}, 138 {Label: "admin"}, 139 }, 140 }, 141 }, 142 }, 143 Meta: map[string]string{ 144 "foo": "bar", 145 }, 146 }, 147 }, 148 Meta: map[string]string{ 149 "elb_check_type": "http", 150 "elb_check_interval": "30s", 151 "elb_check_min": "3", 152 }, 153 }, 154 }, 155 Meta: map[string]string{ 156 "owner": "armon", 157 }, 158 Status: structs.JobStatusPending, 159 Version: 0, 160 CreateIndex: 42, 161 ModifyIndex: 99, 162 JobModifyIndex: 99, 163 } 164 job.Canonicalize() 165 return job 166 } 167 168 func SystemJob() *structs.Job { 169 job := &structs.Job{ 170 Region: "global", 171 Namespace: structs.DefaultNamespace, 172 ID: uuid.Generate(), 173 Name: "my-job", 174 Type: structs.JobTypeSystem, 175 Priority: 100, 176 AllAtOnce: false, 177 Datacenters: []string{"dc1"}, 178 Constraints: []*structs.Constraint{ 179 { 180 LTarget: "${attr.kernel.name}", 181 RTarget: "linux", 182 Operand: "=", 183 }, 184 }, 185 TaskGroups: []*structs.TaskGroup{ 186 { 187 Name: "web", 188 Count: 1, 189 RestartPolicy: &structs.RestartPolicy{ 190 Attempts: 3, 191 Interval: 10 * time.Minute, 192 Delay: 1 * time.Minute, 193 Mode: structs.RestartPolicyModeDelay, 194 }, 195 EphemeralDisk: structs.DefaultEphemeralDisk(), 196 Tasks: []*structs.Task{ 197 { 198 Name: "web", 199 Driver: "exec", 200 Config: map[string]interface{}{ 201 "command": "/bin/date", 202 }, 203 Env: map[string]string{}, 204 Resources: &structs.Resources{ 205 CPU: 500, 206 MemoryMB: 256, 207 Networks: []*structs.NetworkResource{ 208 { 209 MBits: 50, 210 DynamicPorts: []structs.Port{{Label: "http"}}, 211 }, 212 }, 213 }, 214 LogConfig: structs.DefaultLogConfig(), 215 }, 216 }, 217 }, 218 }, 219 Meta: map[string]string{ 220 "owner": "armon", 221 }, 222 Status: structs.JobStatusPending, 223 CreateIndex: 42, 224 ModifyIndex: 99, 225 } 226 job.Canonicalize() 227 return job 228 } 229 230 func PeriodicJob() *structs.Job { 231 job := Job() 232 job.Type = structs.JobTypeBatch 233 job.Periodic = &structs.PeriodicConfig{ 234 Enabled: true, 235 SpecType: structs.PeriodicSpecCron, 236 Spec: "*/30 * * * *", 237 } 238 job.Status = structs.JobStatusRunning 239 return job 240 } 241 242 func Eval() *structs.Evaluation { 243 eval := &structs.Evaluation{ 244 ID: uuid.Generate(), 245 Namespace: structs.DefaultNamespace, 246 Priority: 50, 247 Type: structs.JobTypeService, 248 JobID: uuid.Generate(), 249 Status: structs.EvalStatusPending, 250 } 251 return eval 252 } 253 254 func JobSummary(jobID string) *structs.JobSummary { 255 js := &structs.JobSummary{ 256 JobID: jobID, 257 Namespace: structs.DefaultNamespace, 258 Summary: map[string]structs.TaskGroupSummary{ 259 "web": { 260 Queued: 0, 261 Starting: 0, 262 }, 263 }, 264 } 265 return js 266 } 267 268 func Alloc() *structs.Allocation { 269 alloc := &structs.Allocation{ 270 ID: uuid.Generate(), 271 EvalID: uuid.Generate(), 272 NodeID: "12345678-abcd-efab-cdef-123456789abc", 273 Namespace: structs.DefaultNamespace, 274 TaskGroup: "web", 275 Resources: &structs.Resources{ 276 CPU: 500, 277 MemoryMB: 256, 278 DiskMB: 150, 279 Networks: []*structs.NetworkResource{ 280 { 281 Device: "eth0", 282 IP: "192.168.0.100", 283 ReservedPorts: []structs.Port{{Label: "admin", Value: 5000}}, 284 MBits: 50, 285 DynamicPorts: []structs.Port{{Label: "http"}}, 286 }, 287 }, 288 }, 289 TaskResources: map[string]*structs.Resources{ 290 "web": { 291 CPU: 500, 292 MemoryMB: 256, 293 Networks: []*structs.NetworkResource{ 294 { 295 Device: "eth0", 296 IP: "192.168.0.100", 297 ReservedPorts: []structs.Port{{Label: "admin", Value: 5000}}, 298 MBits: 50, 299 DynamicPorts: []structs.Port{{Label: "http", Value: 9876}}, 300 }, 301 }, 302 }, 303 }, 304 SharedResources: &structs.Resources{ 305 DiskMB: 150, 306 }, 307 Job: Job(), 308 DesiredStatus: structs.AllocDesiredStatusRun, 309 ClientStatus: structs.AllocClientStatusPending, 310 } 311 alloc.JobID = alloc.Job.ID 312 return alloc 313 } 314 315 func VaultAccessor() *structs.VaultAccessor { 316 return &structs.VaultAccessor{ 317 Accessor: uuid.Generate(), 318 NodeID: uuid.Generate(), 319 AllocID: uuid.Generate(), 320 CreationTTL: 86400, 321 Task: "foo", 322 } 323 } 324 325 func Deployment() *structs.Deployment { 326 return &structs.Deployment{ 327 ID: uuid.Generate(), 328 JobID: uuid.Generate(), 329 Namespace: structs.DefaultNamespace, 330 JobVersion: 2, 331 JobModifyIndex: 20, 332 JobCreateIndex: 18, 333 TaskGroups: map[string]*structs.DeploymentState{ 334 "web": { 335 DesiredTotal: 10, 336 }, 337 }, 338 Status: structs.DeploymentStatusRunning, 339 StatusDescription: structs.DeploymentStatusDescriptionRunning, 340 ModifyIndex: 23, 341 CreateIndex: 21, 342 } 343 } 344 345 func Plan() *structs.Plan { 346 return &structs.Plan{ 347 Priority: 50, 348 } 349 } 350 351 func PlanResult() *structs.PlanResult { 352 return &structs.PlanResult{} 353 } 354 355 func ACLPolicy() *structs.ACLPolicy { 356 ap := &structs.ACLPolicy{ 357 Name: fmt.Sprintf("policy-%s", uuid.Generate()), 358 Description: "Super cool policy!", 359 Rules: ` 360 namespace "default" { 361 policy = "write" 362 } 363 node { 364 policy = "read" 365 } 366 agent { 367 policy = "read" 368 } 369 `, 370 CreateIndex: 10, 371 ModifyIndex: 20, 372 } 373 ap.SetHash() 374 return ap 375 } 376 377 func ACLToken() *structs.ACLToken { 378 tk := &structs.ACLToken{ 379 AccessorID: uuid.Generate(), 380 SecretID: uuid.Generate(), 381 Name: "my cool token " + uuid.Generate(), 382 Type: "client", 383 Policies: []string{"foo", "bar"}, 384 Global: false, 385 CreateTime: time.Now().UTC(), 386 CreateIndex: 10, 387 ModifyIndex: 20, 388 } 389 tk.SetHash() 390 return tk 391 } 392 393 func ACLManagementToken() *structs.ACLToken { 394 return &structs.ACLToken{ 395 AccessorID: uuid.Generate(), 396 SecretID: uuid.Generate(), 397 Name: "management " + uuid.Generate(), 398 Type: "management", 399 Global: true, 400 CreateTime: time.Now().UTC(), 401 CreateIndex: 10, 402 ModifyIndex: 20, 403 } 404 }