github.com/diptanu/nomad@v0.5.7-0.20170516172507-d72e86cbe3d9/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 Env: map[string]string{ 99 "FOO": "bar", 100 }, 101 Services: []*structs.Service{ 102 { 103 Name: "${TASK}-frontend", 104 PortLabel: "http", 105 Tags: []string{"pci:${meta.pci-dss}", "datacenter:${node.datacenter}"}, 106 Checks: []*structs.ServiceCheck{ 107 { 108 Name: "check-table", 109 Type: structs.ServiceCheckScript, 110 Command: "/usr/local/check-table-${meta.database}", 111 Args: []string{"${meta.version}"}, 112 Interval: 30 * time.Second, 113 Timeout: 5 * time.Second, 114 }, 115 }, 116 }, 117 { 118 Name: "${TASK}-admin", 119 PortLabel: "admin", 120 }, 121 }, 122 LogConfig: structs.DefaultLogConfig(), 123 Resources: &structs.Resources{ 124 CPU: 500, 125 MemoryMB: 256, 126 Networks: []*structs.NetworkResource{ 127 &structs.NetworkResource{ 128 MBits: 50, 129 DynamicPorts: []structs.Port{{Label: "http"}, {Label: "admin"}}, 130 }, 131 }, 132 }, 133 Meta: map[string]string{ 134 "foo": "bar", 135 }, 136 }, 137 }, 138 Meta: map[string]string{ 139 "elb_check_type": "http", 140 "elb_check_interval": "30s", 141 "elb_check_min": "3", 142 }, 143 }, 144 }, 145 Meta: map[string]string{ 146 "owner": "armon", 147 }, 148 Status: structs.JobStatusPending, 149 Version: 0, 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 Resources: &structs.Resources{ 194 CPU: 500, 195 MemoryMB: 256, 196 Networks: []*structs.NetworkResource{ 197 &structs.NetworkResource{ 198 MBits: 50, 199 DynamicPorts: []structs.Port{{Label: "http"}}, 200 }, 201 }, 202 }, 203 LogConfig: structs.DefaultLogConfig(), 204 }, 205 }, 206 }, 207 }, 208 Meta: map[string]string{ 209 "owner": "armon", 210 }, 211 Status: structs.JobStatusPending, 212 CreateIndex: 42, 213 ModifyIndex: 99, 214 } 215 job.Canonicalize() 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 job.Status = structs.JobStatusRunning 228 return job 229 } 230 231 func Eval() *structs.Evaluation { 232 eval := &structs.Evaluation{ 233 ID: structs.GenerateUUID(), 234 Priority: 50, 235 Type: structs.JobTypeService, 236 JobID: structs.GenerateUUID(), 237 Status: structs.EvalStatusPending, 238 } 239 return eval 240 } 241 242 func JobSummary(jobID string) *structs.JobSummary { 243 js := &structs.JobSummary{ 244 JobID: jobID, 245 Summary: map[string]structs.TaskGroupSummary{ 246 "web": { 247 Queued: 0, 248 Starting: 0, 249 }, 250 }, 251 } 252 return js 253 } 254 255 func Alloc() *structs.Allocation { 256 alloc := &structs.Allocation{ 257 ID: structs.GenerateUUID(), 258 EvalID: structs.GenerateUUID(), 259 NodeID: "12345678-abcd-efab-cdef-123456789abc", 260 TaskGroup: "web", 261 Resources: &structs.Resources{ 262 CPU: 500, 263 MemoryMB: 256, 264 DiskMB: 150, 265 Networks: []*structs.NetworkResource{ 266 &structs.NetworkResource{ 267 Device: "eth0", 268 IP: "192.168.0.100", 269 ReservedPorts: []structs.Port{{Label: "main", Value: 5000}}, 270 MBits: 50, 271 DynamicPorts: []structs.Port{{Label: "http"}}, 272 }, 273 }, 274 }, 275 TaskResources: map[string]*structs.Resources{ 276 "web": &structs.Resources{ 277 CPU: 500, 278 MemoryMB: 256, 279 Networks: []*structs.NetworkResource{ 280 &structs.NetworkResource{ 281 Device: "eth0", 282 IP: "192.168.0.100", 283 ReservedPorts: []structs.Port{{Label: "main", Value: 5000}}, 284 MBits: 50, 285 DynamicPorts: []structs.Port{{Label: "http"}}, 286 }, 287 }, 288 }, 289 }, 290 SharedResources: &structs.Resources{ 291 DiskMB: 150, 292 }, 293 Job: Job(), 294 DesiredStatus: structs.AllocDesiredStatusRun, 295 ClientStatus: structs.AllocClientStatusPending, 296 } 297 alloc.JobID = alloc.Job.ID 298 return alloc 299 } 300 301 func VaultAccessor() *structs.VaultAccessor { 302 return &structs.VaultAccessor{ 303 Accessor: structs.GenerateUUID(), 304 NodeID: structs.GenerateUUID(), 305 AllocID: structs.GenerateUUID(), 306 CreationTTL: 86400, 307 Task: "foo", 308 } 309 } 310 311 func Deployment() *structs.Deployment { 312 return &structs.Deployment{ 313 ID: structs.GenerateUUID(), 314 JobID: structs.GenerateUUID(), 315 JobVersion: 2, 316 JobModifyIndex: 20, 317 JobCreateIndex: 18, 318 TaskGroups: map[string]*structs.DeploymentState{ 319 "web": &structs.DeploymentState{ 320 DesiredTotal: 10, 321 }, 322 }, 323 Status: structs.DeploymentStatusRunning, 324 StatusDescription: structs.DeploymentStatusRunning, 325 ModifyIndex: 23, 326 CreateIndex: 21, 327 } 328 } 329 330 func Plan() *structs.Plan { 331 return &structs.Plan{ 332 Priority: 50, 333 } 334 } 335 336 func PlanResult() *structs.PlanResult { 337 return &structs.PlanResult{} 338 }