open-nomad/nomad/mock/mock.go

742 lines
17 KiB
Go
Raw Normal View History

package mock
2015-10-31 01:34:23 +00:00
import (
"fmt"
2015-10-31 01:34:23 +00:00
"time"
"github.com/hashicorp/nomad/helper/uuid"
"github.com/hashicorp/nomad/nomad/structs"
2018-10-15 22:15:46 +00:00
psstructs "github.com/hashicorp/nomad/plugins/shared/structs"
2015-10-31 01:34:23 +00:00
)
func Node() *structs.Node {
node := &structs.Node{
ID: uuid.Generate(),
SecretID: uuid.Generate(),
Datacenter: "dc1",
Name: "foobar",
Attributes: map[string]string{
"kernel.name": "linux",
"arch": "x86",
"nomad.version": "0.5.0",
"driver.exec": "1",
"driver.mock_driver": "1",
},
2018-10-03 16:47:18 +00:00
// TODO Remove once clientv2 gets merged
Resources: &structs.Resources{
2015-09-23 18:14:32 +00:00
CPU: 4000,
MemoryMB: 8192,
DiskMB: 100 * 1024,
},
Reserved: &structs.Resources{
2015-09-23 18:14:32 +00:00
CPU: 100,
MemoryMB: 256,
DiskMB: 4 * 1024,
Networks: []*structs.NetworkResource{
2017-09-26 22:26:33 +00:00
{
Device: "eth0",
IP: "192.168.0.100",
2017-12-08 22:50:06 +00:00
ReservedPorts: []structs.Port{{Label: "ssh", Value: 22}},
MBits: 1,
},
},
},
2018-10-03 16:47:18 +00:00
NodeResources: &structs.NodeResources{
Cpu: structs.NodeCpuResources{
2018-10-04 21:33:09 +00:00
CpuShares: 4000,
2018-10-03 16:47:18 +00:00
},
Memory: structs.NodeMemoryResources{
MemoryMB: 8192,
},
Disk: structs.NodeDiskResources{
DiskMB: 100 * 1024,
},
Networks: []*structs.NetworkResource{
{
Device: "eth0",
CIDR: "192.168.0.100/32",
MBits: 1000,
},
},
},
ReservedResources: &structs.NodeReservedResources{
Cpu: structs.NodeReservedCpuResources{
2018-10-04 21:33:09 +00:00
CpuShares: 100,
2018-10-03 16:47:18 +00:00
},
Memory: structs.NodeReservedMemoryResources{
MemoryMB: 256,
},
Disk: structs.NodeReservedDiskResources{
DiskMB: 4 * 1024,
},
Networks: structs.NodeReservedNetworkResources{
ReservedHostPorts: "22",
},
},
Links: map[string]string{
"consul": "foobar.dc1",
},
Meta: map[string]string{
"pci-dss": "true",
"database": "mysql",
"version": "5.6",
},
NodeClass: "linux-medium-pci",
Status: structs.NodeStatusReady,
SchedulingEligibility: structs.NodeSchedulingEligible,
}
node.ComputeClass()
return node
}
2018-10-17 18:04:54 +00:00
// NvidiaNode returns a node with two instances of an Nvidia GPU
2018-10-15 22:15:46 +00:00
func NvidiaNode() *structs.Node {
n := Node()
n.NodeResources.Devices = []*structs.NodeDeviceResource{
{
Type: "gpu",
Vendor: "nvidia",
Name: "1080ti",
Attributes: map[string]*psstructs.Attribute{
"memory": psstructs.NewIntAttribute(11, psstructs.UnitGiB),
"cuda_cores": psstructs.NewIntAttribute(3584, ""),
"graphics_clock": psstructs.NewIntAttribute(1480, psstructs.UnitMHz),
"memory_bandwidth": psstructs.NewIntAttribute(11, psstructs.UnitGBPerS),
},
Instances: []*structs.NodeDevice{
{
ID: uuid.Generate(),
Healthy: true,
},
{
ID: uuid.Generate(),
Healthy: true,
},
},
},
}
n.ComputeClass()
return n
}
func HCL() string {
return `job "my-job" {
datacenters = ["dc1"]
type = "service"
constraint {
attribute = "${attr.kernel.name}"
value = "linux"
}
group "web" {
count = 10
restart {
attempts = 3
interval = "10m"
delay = "1m"
mode = "delay"
}
task "web" {
driver = "exec"
config {
command = "/bin/date"
}
resources {
cpu = 500
memory = 256
}
}
}
}
`
}
func Job() *structs.Job {
job := &structs.Job{
2015-09-15 18:12:46 +00:00
Region: "global",
ID: fmt.Sprintf("mock-service-%s", uuid.Generate()),
2015-08-14 05:07:01 +00:00
Name: "my-job",
2017-09-07 23:56:15 +00:00
Namespace: structs.DefaultNamespace,
2015-08-14 05:07:01 +00:00
Type: structs.JobTypeService,
Priority: 50,
AllAtOnce: false,
Datacenters: []string{"dc1"},
Constraints: []*structs.Constraint{
2017-09-26 22:26:33 +00:00
{
2016-02-05 00:50:20 +00:00
LTarget: "${attr.kernel.name}",
RTarget: "linux",
Operand: "=",
},
},
TaskGroups: []*structs.TaskGroup{
2017-09-26 22:26:33 +00:00
{
Name: "web",
Count: 10,
EphemeralDisk: &structs.EphemeralDisk{
SizeMB: 150,
},
2015-10-31 01:34:23 +00:00
RestartPolicy: &structs.RestartPolicy{
Attempts: 3,
Interval: 10 * time.Minute,
Delay: 1 * time.Minute,
Mode: structs.RestartPolicyModeDelay,
2015-10-31 01:34:23 +00:00
},
ReschedulePolicy: &structs.ReschedulePolicy{
Attempts: 2,
Interval: 10 * time.Minute,
Delay: 5 * time.Second,
2018-03-26 19:45:09 +00:00
DelayFunction: "constant",
},
Migrate: structs.DefaultMigrateStrategy(),
Tasks: []*structs.Task{
2017-09-26 22:26:33 +00:00
{
Name: "web",
2015-08-29 23:24:01 +00:00
Driver: "exec",
Config: map[string]interface{}{
2015-08-29 23:24:01 +00:00
"command": "/bin/date",
},
Env: map[string]string{
"FOO": "bar",
},
2016-06-12 23:36:49 +00:00
Services: []*structs.Service{
{
Name: "${TASK}-frontend",
PortLabel: "http",
2016-03-26 19:49:49 +00:00
Tags: []string{"pci:${meta.pci-dss}", "datacenter:${node.datacenter}"},
Checks: []*structs.ServiceCheck{
{
Name: "check-table",
Type: structs.ServiceCheckScript,
Command: "/usr/local/check-table-${meta.database}",
Args: []string{"${meta.version}"},
Interval: 30 * time.Second,
Timeout: 5 * time.Second,
},
},
},
{
Name: "${TASK}-admin",
2017-12-08 22:50:06 +00:00
PortLabel: "admin",
},
},
2016-02-11 01:46:35 +00:00
LogConfig: structs.DefaultLogConfig(),
Resources: &structs.Resources{
2015-09-23 18:14:32 +00:00
CPU: 500,
MemoryMB: 256,
2015-09-13 23:40:31 +00:00
Networks: []*structs.NetworkResource{
2017-09-26 22:26:33 +00:00
{
2017-12-08 22:50:06 +00:00
MBits: 50,
DynamicPorts: []structs.Port{
{Label: "http"},
{Label: "admin"},
},
2015-09-13 23:40:31 +00:00
},
},
},
Meta: map[string]string{
"foo": "bar",
},
},
},
Meta: map[string]string{
"elb_check_type": "http",
"elb_check_interval": "30s",
"elb_check_min": "3",
},
},
},
Meta: map[string]string{
"owner": "armon",
},
2016-01-12 17:50:33 +00:00
Status: structs.JobStatusPending,
2017-04-13 20:54:57 +00:00
Version: 0,
2016-01-12 17:50:33 +00:00
CreateIndex: 42,
ModifyIndex: 99,
JobModifyIndex: 99,
}
2016-07-20 23:07:15 +00:00
job.Canonicalize()
return job
}
2018-03-20 21:49:17 +00:00
func BatchJob() *structs.Job {
job := &structs.Job{
Region: "global",
ID: fmt.Sprintf("mock-batch-%s", uuid.Generate()),
2018-03-20 21:49:17 +00:00
Name: "batch-job",
Namespace: structs.DefaultNamespace,
Type: structs.JobTypeBatch,
Priority: 50,
AllAtOnce: false,
Datacenters: []string{"dc1"},
TaskGroups: []*structs.TaskGroup{
{
Name: "web",
2018-03-20 21:49:17 +00:00
Count: 10,
EphemeralDisk: &structs.EphemeralDisk{
SizeMB: 150,
},
RestartPolicy: &structs.RestartPolicy{
Attempts: 3,
Interval: 10 * time.Minute,
Delay: 1 * time.Minute,
Mode: structs.RestartPolicyModeDelay,
},
ReschedulePolicy: &structs.ReschedulePolicy{
Attempts: 2,
Interval: 10 * time.Minute,
Delay: 5 * time.Second,
2018-03-26 19:45:09 +00:00
DelayFunction: "constant",
2018-03-20 21:49:17 +00:00
},
Tasks: []*structs.Task{
{
Name: "web",
2018-03-20 21:49:17 +00:00
Driver: "mock_driver",
Config: map[string]interface{}{
"run_for": "500ms",
2018-03-20 21:49:17 +00:00
},
Env: map[string]string{
"FOO": "bar",
},
LogConfig: structs.DefaultLogConfig(),
Resources: &structs.Resources{
CPU: 100,
MemoryMB: 100,
Networks: []*structs.NetworkResource{
{
MBits: 50,
},
},
},
Meta: map[string]string{
"foo": "bar",
},
},
},
},
},
Status: structs.JobStatusPending,
Version: 0,
CreateIndex: 43,
ModifyIndex: 99,
JobModifyIndex: 99,
}
job.Canonicalize()
return job
}
2015-10-14 23:43:06 +00:00
func SystemJob() *structs.Job {
job := &structs.Job{
Region: "global",
2017-09-07 23:56:15 +00:00
Namespace: structs.DefaultNamespace,
ID: fmt.Sprintf("mock-system-%s", uuid.Generate()),
2015-10-14 23:43:06 +00:00
Name: "my-job",
Type: structs.JobTypeSystem,
Priority: 100,
AllAtOnce: false,
Datacenters: []string{"dc1"},
Constraints: []*structs.Constraint{
2017-09-26 22:26:33 +00:00
{
2016-02-05 00:50:20 +00:00
LTarget: "${attr.kernel.name}",
2015-10-14 23:43:06 +00:00
RTarget: "linux",
Operand: "=",
},
},
TaskGroups: []*structs.TaskGroup{
2017-09-26 22:26:33 +00:00
{
Name: "web",
Count: 1,
2015-10-31 01:34:23 +00:00
RestartPolicy: &structs.RestartPolicy{
Attempts: 3,
Interval: 10 * time.Minute,
Delay: 1 * time.Minute,
Mode: structs.RestartPolicyModeDelay,
2015-10-31 01:34:23 +00:00
},
EphemeralDisk: structs.DefaultEphemeralDisk(),
2015-10-14 23:43:06 +00:00
Tasks: []*structs.Task{
2017-09-26 22:26:33 +00:00
{
2015-10-14 23:43:06 +00:00
Name: "web",
Driver: "exec",
Config: map[string]interface{}{
2015-10-14 23:43:06 +00:00
"command": "/bin/date",
},
Env: map[string]string{},
2015-10-14 23:43:06 +00:00
Resources: &structs.Resources{
CPU: 500,
MemoryMB: 256,
Networks: []*structs.NetworkResource{
2017-09-26 22:26:33 +00:00
{
2015-10-14 23:43:06 +00:00
MBits: 50,
2015-11-14 02:35:49 +00:00
DynamicPorts: []structs.Port{{Label: "http"}},
2015-10-14 23:43:06 +00:00
},
},
},
2016-02-11 01:46:35 +00:00
LogConfig: structs.DefaultLogConfig(),
2015-10-14 23:43:06 +00:00
},
},
},
},
Meta: map[string]string{
"owner": "armon",
},
Status: structs.JobStatusPending,
CreateIndex: 42,
ModifyIndex: 99,
}
2016-12-12 21:32:30 +00:00
job.Canonicalize()
2015-10-14 23:43:06 +00:00
return job
}
2015-12-18 20:26:28 +00:00
func PeriodicJob() *structs.Job {
job := Job()
2015-12-05 00:53:36 +00:00
job.Type = structs.JobTypeBatch
2015-12-18 20:26:28 +00:00
job.Periodic = &structs.PeriodicConfig{
Enabled: true,
SpecType: structs.PeriodicSpecCron,
2015-12-05 00:53:36 +00:00
Spec: "*/30 * * * *",
2015-12-18 20:26:28 +00:00
}
2016-12-12 21:32:30 +00:00
job.Status = structs.JobStatusRunning
2018-02-23 18:42:43 +00:00
job.TaskGroups[0].Migrate = nil
2015-12-18 20:26:28 +00:00
return job
}
func Eval() *structs.Evaluation {
eval := &structs.Evaluation{
ID: uuid.Generate(),
2017-09-07 23:56:15 +00:00
Namespace: structs.DefaultNamespace,
Priority: 50,
Type: structs.JobTypeService,
JobID: uuid.Generate(),
2017-09-07 23:56:15 +00:00
Status: structs.EvalStatusPending,
}
return eval
}
func JobSummary(jobID string) *structs.JobSummary {
js := &structs.JobSummary{
2017-09-07 23:56:15 +00:00
JobID: jobID,
Namespace: structs.DefaultNamespace,
Summary: map[string]structs.TaskGroupSummary{
2016-07-21 21:43:21 +00:00
"web": {
Queued: 0,
Starting: 0,
},
},
}
return js
}
func Alloc() *structs.Allocation {
alloc := &structs.Allocation{
ID: uuid.Generate(),
EvalID: uuid.Generate(),
NodeID: "12345678-abcd-efab-cdef-123456789abc",
2017-09-07 23:56:15 +00:00
Namespace: structs.DefaultNamespace,
2015-08-29 23:24:01 +00:00
TaskGroup: "web",
2018-10-03 16:47:18 +00:00
// TODO Remove once clientv2 gets merged
Resources: &structs.Resources{
2015-09-23 18:14:32 +00:00
CPU: 500,
MemoryMB: 256,
2016-08-29 19:49:52 +00:00
DiskMB: 150,
Networks: []*structs.NetworkResource{
2017-09-26 22:26:33 +00:00
{
2015-09-13 02:13:00 +00:00
Device: "eth0",
IP: "192.168.0.100",
2017-12-08 22:50:06 +00:00
ReservedPorts: []structs.Port{{Label: "admin", Value: 5000}},
2016-03-01 22:09:25 +00:00
MBits: 50,
2015-11-14 02:35:49 +00:00
DynamicPorts: []structs.Port{{Label: "http"}},
},
},
},
TaskResources: map[string]*structs.Resources{
2017-09-26 22:26:33 +00:00
"web": {
2015-09-23 18:14:32 +00:00
CPU: 500,
MemoryMB: 256,
Networks: []*structs.NetworkResource{
2017-09-26 22:26:33 +00:00
{
Device: "eth0",
IP: "192.168.0.100",
2017-12-08 22:50:06 +00:00
ReservedPorts: []structs.Port{{Label: "admin", Value: 5000}},
MBits: 50,
DynamicPorts: []structs.Port{{Label: "http", Value: 9876}},
},
},
},
},
2016-08-29 19:49:52 +00:00
SharedResources: &structs.Resources{
DiskMB: 150,
},
2018-10-03 16:47:18 +00:00
AllocatedResources: &structs.AllocatedResources{
Tasks: map[string]*structs.AllocatedTaskResources{
"web": {
Cpu: structs.AllocatedCpuResources{
CpuShares: 500,
},
Memory: structs.AllocatedMemoryResources{
MemoryMB: 256,
},
Networks: []*structs.NetworkResource{
{
Device: "eth0",
IP: "192.168.0.100",
ReservedPorts: []structs.Port{{Label: "admin", Value: 5000}},
MBits: 50,
DynamicPorts: []structs.Port{{Label: "http", Value: 9876}},
},
},
},
},
Shared: structs.AllocatedSharedResources{
DiskMB: 150,
},
},
2015-08-25 23:19:21 +00:00
Job: Job(),
DesiredStatus: structs.AllocDesiredStatusRun,
ClientStatus: structs.AllocClientStatusPending,
}
alloc.JobID = alloc.Job.ID
return alloc
}
func BatchAlloc() *structs.Allocation {
alloc := &structs.Allocation{
ID: uuid.Generate(),
EvalID: uuid.Generate(),
NodeID: "12345678-abcd-efab-cdef-123456789abc",
Namespace: structs.DefaultNamespace,
TaskGroup: "web",
2018-10-03 16:47:18 +00:00
// TODO Remove once clientv2 gets merged
Resources: &structs.Resources{
CPU: 500,
MemoryMB: 256,
DiskMB: 150,
Networks: []*structs.NetworkResource{
{
Device: "eth0",
IP: "192.168.0.100",
ReservedPorts: []structs.Port{{Label: "admin", Value: 5000}},
MBits: 50,
DynamicPorts: []structs.Port{{Label: "http"}},
},
},
},
TaskResources: map[string]*structs.Resources{
2018-10-03 16:47:18 +00:00
"web": {
CPU: 500,
MemoryMB: 256,
Networks: []*structs.NetworkResource{
{
2018-10-03 16:47:18 +00:00
Device: "eth0",
IP: "192.168.0.100",
ReservedPorts: []structs.Port{{Label: "admin", Value: 5000}},
MBits: 50,
DynamicPorts: []structs.Port{{Label: "http", Value: 9876}},
},
},
},
},
SharedResources: &structs.Resources{
DiskMB: 150,
},
2018-10-03 16:47:18 +00:00
AllocatedResources: &structs.AllocatedResources{
Tasks: map[string]*structs.AllocatedTaskResources{
"web": {
Cpu: structs.AllocatedCpuResources{
CpuShares: 500,
},
Memory: structs.AllocatedMemoryResources{
MemoryMB: 256,
},
Networks: []*structs.NetworkResource{
{
Device: "eth0",
IP: "192.168.0.100",
ReservedPorts: []structs.Port{{Label: "admin", Value: 5000}},
MBits: 50,
DynamicPorts: []structs.Port{{Label: "http", Value: 9876}},
},
},
},
},
Shared: structs.AllocatedSharedResources{
DiskMB: 150,
},
},
Job: BatchJob(),
DesiredStatus: structs.AllocDesiredStatusRun,
ClientStatus: structs.AllocClientStatusPending,
}
alloc.JobID = alloc.Job.ID
return alloc
}
func SystemAlloc() *structs.Allocation {
alloc := &structs.Allocation{
ID: uuid.Generate(),
EvalID: uuid.Generate(),
NodeID: "12345678-abcd-efab-cdef-123456789abc",
Namespace: structs.DefaultNamespace,
TaskGroup: "web",
2018-10-03 16:47:18 +00:00
// TODO Remove once clientv2 gets merged
Resources: &structs.Resources{
CPU: 500,
MemoryMB: 256,
DiskMB: 150,
Networks: []*structs.NetworkResource{
{
Device: "eth0",
IP: "192.168.0.100",
ReservedPorts: []structs.Port{{Label: "admin", Value: 5000}},
MBits: 50,
DynamicPorts: []structs.Port{{Label: "http"}},
},
},
},
TaskResources: map[string]*structs.Resources{
"web": {
CPU: 500,
MemoryMB: 256,
Networks: []*structs.NetworkResource{
{
Device: "eth0",
IP: "192.168.0.100",
ReservedPorts: []structs.Port{{Label: "admin", Value: 5000}},
MBits: 50,
DynamicPorts: []structs.Port{{Label: "http", Value: 9876}},
},
},
},
},
SharedResources: &structs.Resources{
DiskMB: 150,
},
2018-10-03 16:47:18 +00:00
AllocatedResources: &structs.AllocatedResources{
Tasks: map[string]*structs.AllocatedTaskResources{
"web": {
Cpu: structs.AllocatedCpuResources{
CpuShares: 500,
},
Memory: structs.AllocatedMemoryResources{
MemoryMB: 256,
},
Networks: []*structs.NetworkResource{
{
Device: "eth0",
IP: "192.168.0.100",
ReservedPorts: []structs.Port{{Label: "admin", Value: 5000}},
MBits: 50,
DynamicPorts: []structs.Port{{Label: "http", Value: 9876}},
},
},
},
},
Shared: structs.AllocatedSharedResources{
DiskMB: 150,
},
},
Job: SystemJob(),
DesiredStatus: structs.AllocDesiredStatusRun,
2015-08-25 23:19:21 +00:00
ClientStatus: structs.AllocClientStatusPending,
}
alloc.JobID = alloc.Job.ID
return alloc
}
func VaultAccessor() *structs.VaultAccessor {
return &structs.VaultAccessor{
Accessor: uuid.Generate(),
NodeID: uuid.Generate(),
AllocID: uuid.Generate(),
CreationTTL: 86400,
Task: "foo",
}
}
func Deployment() *structs.Deployment {
return &structs.Deployment{
ID: uuid.Generate(),
JobID: uuid.Generate(),
2017-09-07 23:56:15 +00:00
Namespace: structs.DefaultNamespace,
JobVersion: 2,
JobModifyIndex: 20,
JobCreateIndex: 18,
TaskGroups: map[string]*structs.DeploymentState{
2017-09-26 22:26:33 +00:00
"web": {
DesiredTotal: 10,
},
},
Status: structs.DeploymentStatusRunning,
2017-06-29 05:00:18 +00:00
StatusDescription: structs.DeploymentStatusDescriptionRunning,
ModifyIndex: 23,
CreateIndex: 21,
}
}
func Plan() *structs.Plan {
return &structs.Plan{
Priority: 50,
}
}
func PlanResult() *structs.PlanResult {
return &structs.PlanResult{}
}
func ACLPolicy() *structs.ACLPolicy {
ap := &structs.ACLPolicy{
Name: fmt.Sprintf("policy-%s", uuid.Generate()),
2017-08-08 06:02:03 +00:00
Description: "Super cool policy!",
Rules: `
namespace "default" {
policy = "write"
}
node {
policy = "read"
}
agent {
policy = "read"
}
`,
CreateIndex: 10,
ModifyIndex: 20,
}
ap.SetHash()
return ap
}
2017-08-12 22:18:47 +00:00
func ACLToken() *structs.ACLToken {
tk := &structs.ACLToken{
AccessorID: uuid.Generate(),
SecretID: uuid.Generate(),
Name: "my cool token " + uuid.Generate(),
2017-08-12 22:18:47 +00:00
Type: "client",
Policies: []string{"foo", "bar"},
Global: false,
CreateTime: time.Now().UTC(),
CreateIndex: 10,
ModifyIndex: 20,
}
tk.SetHash()
return tk
2017-08-12 22:18:47 +00:00
}
func ACLManagementToken() *structs.ACLToken {
return &structs.ACLToken{
AccessorID: uuid.Generate(),
SecretID: uuid.Generate(),
Name: "management " + uuid.Generate(),
Type: "management",
Global: true,
CreateTime: time.Now().UTC(),
CreateIndex: 10,
ModifyIndex: 20,
}
}