github.com/manicqin/nomad@v0.9.5/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  	psstructs "github.com/hashicorp/nomad/plugins/shared/structs"
    10  )
    11  
    12  func Node() *structs.Node {
    13  	node := &structs.Node{
    14  		ID:         uuid.Generate(),
    15  		SecretID:   uuid.Generate(),
    16  		Datacenter: "dc1",
    17  		Name:       "foobar",
    18  		Drivers: map[string]*structs.DriverInfo{
    19  			"exec": {
    20  				Detected: true,
    21  				Healthy:  true,
    22  			},
    23  			"mock_driver": {
    24  				Detected: true,
    25  				Healthy:  true,
    26  			},
    27  		},
    28  		Attributes: map[string]string{
    29  			"kernel.name":        "linux",
    30  			"arch":               "x86",
    31  			"nomad.version":      "0.5.0",
    32  			"driver.exec":        "1",
    33  			"driver.mock_driver": "1",
    34  		},
    35  
    36  		// TODO Remove once clientv2 gets merged
    37  		Resources: &structs.Resources{
    38  			CPU:      4000,
    39  			MemoryMB: 8192,
    40  			DiskMB:   100 * 1024,
    41  		},
    42  		Reserved: &structs.Resources{
    43  			CPU:      100,
    44  			MemoryMB: 256,
    45  			DiskMB:   4 * 1024,
    46  			Networks: []*structs.NetworkResource{
    47  				{
    48  					Device:        "eth0",
    49  					IP:            "192.168.0.100",
    50  					ReservedPorts: []structs.Port{{Label: "ssh", Value: 22}},
    51  					MBits:         1,
    52  				},
    53  			},
    54  		},
    55  
    56  		NodeResources: &structs.NodeResources{
    57  			Cpu: structs.NodeCpuResources{
    58  				CpuShares: 4000,
    59  			},
    60  			Memory: structs.NodeMemoryResources{
    61  				MemoryMB: 8192,
    62  			},
    63  			Disk: structs.NodeDiskResources{
    64  				DiskMB: 100 * 1024,
    65  			},
    66  			Networks: []*structs.NetworkResource{
    67  				{
    68  					Device: "eth0",
    69  					CIDR:   "192.168.0.100/32",
    70  					MBits:  1000,
    71  				},
    72  			},
    73  		},
    74  		ReservedResources: &structs.NodeReservedResources{
    75  			Cpu: structs.NodeReservedCpuResources{
    76  				CpuShares: 100,
    77  			},
    78  			Memory: structs.NodeReservedMemoryResources{
    79  				MemoryMB: 256,
    80  			},
    81  			Disk: structs.NodeReservedDiskResources{
    82  				DiskMB: 4 * 1024,
    83  			},
    84  			Networks: structs.NodeReservedNetworkResources{
    85  				ReservedHostPorts: "22",
    86  			},
    87  		},
    88  		Links: map[string]string{
    89  			"consul": "foobar.dc1",
    90  		},
    91  		Meta: map[string]string{
    92  			"pci-dss":  "true",
    93  			"database": "mysql",
    94  			"version":  "5.6",
    95  		},
    96  		NodeClass:             "linux-medium-pci",
    97  		Status:                structs.NodeStatusReady,
    98  		SchedulingEligibility: structs.NodeSchedulingEligible,
    99  	}
   100  	node.ComputeClass()
   101  	return node
   102  }
   103  
   104  // NvidiaNode returns a node with two instances of an Nvidia GPU
   105  func NvidiaNode() *structs.Node {
   106  	n := Node()
   107  	n.NodeResources.Devices = []*structs.NodeDeviceResource{
   108  		{
   109  			Type:   "gpu",
   110  			Vendor: "nvidia",
   111  			Name:   "1080ti",
   112  			Attributes: map[string]*psstructs.Attribute{
   113  				"memory":           psstructs.NewIntAttribute(11, psstructs.UnitGiB),
   114  				"cuda_cores":       psstructs.NewIntAttribute(3584, ""),
   115  				"graphics_clock":   psstructs.NewIntAttribute(1480, psstructs.UnitMHz),
   116  				"memory_bandwidth": psstructs.NewIntAttribute(11, psstructs.UnitGBPerS),
   117  			},
   118  			Instances: []*structs.NodeDevice{
   119  				{
   120  					ID:      uuid.Generate(),
   121  					Healthy: true,
   122  				},
   123  				{
   124  					ID:      uuid.Generate(),
   125  					Healthy: true,
   126  				},
   127  			},
   128  		},
   129  	}
   130  	n.ComputeClass()
   131  	return n
   132  }
   133  
   134  func HCL() string {
   135  	return `job "my-job" {
   136  	datacenters = ["dc1"]
   137  	type = "service"
   138  	constraint {
   139  		attribute = "${attr.kernel.name}"
   140  		value = "linux"
   141  	}
   142  
   143  	group "web" {
   144  		count = 10
   145  		restart {
   146  			attempts = 3
   147  			interval = "10m"
   148  			delay = "1m"
   149  			mode = "delay"
   150  		}
   151  		task "web" {
   152  			driver = "exec"
   153  			config {
   154  				command = "/bin/date"
   155  			}
   156  			resources {
   157  				cpu = 500
   158  				memory = 256
   159  			}
   160  		}
   161  	}
   162  }
   163  `
   164  }
   165  
   166  func Job() *structs.Job {
   167  	job := &structs.Job{
   168  		Region:      "global",
   169  		ID:          fmt.Sprintf("mock-service-%s", uuid.Generate()),
   170  		Name:        "my-job",
   171  		Namespace:   structs.DefaultNamespace,
   172  		Type:        structs.JobTypeService,
   173  		Priority:    50,
   174  		AllAtOnce:   false,
   175  		Datacenters: []string{"dc1"},
   176  		Constraints: []*structs.Constraint{
   177  			{
   178  				LTarget: "${attr.kernel.name}",
   179  				RTarget: "linux",
   180  				Operand: "=",
   181  			},
   182  		},
   183  		TaskGroups: []*structs.TaskGroup{
   184  			{
   185  				Name:  "web",
   186  				Count: 10,
   187  				EphemeralDisk: &structs.EphemeralDisk{
   188  					SizeMB: 150,
   189  				},
   190  				RestartPolicy: &structs.RestartPolicy{
   191  					Attempts: 3,
   192  					Interval: 10 * time.Minute,
   193  					Delay:    1 * time.Minute,
   194  					Mode:     structs.RestartPolicyModeDelay,
   195  				},
   196  				ReschedulePolicy: &structs.ReschedulePolicy{
   197  					Attempts:      2,
   198  					Interval:      10 * time.Minute,
   199  					Delay:         5 * time.Second,
   200  					DelayFunction: "constant",
   201  				},
   202  				Migrate: structs.DefaultMigrateStrategy(),
   203  				Tasks: []*structs.Task{
   204  					{
   205  						Name:   "web",
   206  						Driver: "exec",
   207  						Config: map[string]interface{}{
   208  							"command": "/bin/date",
   209  						},
   210  						Env: map[string]string{
   211  							"FOO": "bar",
   212  						},
   213  						Services: []*structs.Service{
   214  							{
   215  								Name:      "${TASK}-frontend",
   216  								PortLabel: "http",
   217  								Tags:      []string{"pci:${meta.pci-dss}", "datacenter:${node.datacenter}"},
   218  								Checks: []*structs.ServiceCheck{
   219  									{
   220  										Name:     "check-table",
   221  										Type:     structs.ServiceCheckScript,
   222  										Command:  "/usr/local/check-table-${meta.database}",
   223  										Args:     []string{"${meta.version}"},
   224  										Interval: 30 * time.Second,
   225  										Timeout:  5 * time.Second,
   226  									},
   227  								},
   228  							},
   229  							{
   230  								Name:      "${TASK}-admin",
   231  								PortLabel: "admin",
   232  							},
   233  						},
   234  						LogConfig: structs.DefaultLogConfig(),
   235  						Resources: &structs.Resources{
   236  							CPU:      500,
   237  							MemoryMB: 256,
   238  							Networks: []*structs.NetworkResource{
   239  								{
   240  									MBits: 50,
   241  									DynamicPorts: []structs.Port{
   242  										{Label: "http"},
   243  										{Label: "admin"},
   244  									},
   245  								},
   246  							},
   247  						},
   248  						Meta: map[string]string{
   249  							"foo": "bar",
   250  						},
   251  					},
   252  				},
   253  				Meta: map[string]string{
   254  					"elb_check_type":     "http",
   255  					"elb_check_interval": "30s",
   256  					"elb_check_min":      "3",
   257  				},
   258  			},
   259  		},
   260  		Meta: map[string]string{
   261  			"owner": "armon",
   262  		},
   263  		Status:         structs.JobStatusPending,
   264  		Version:        0,
   265  		CreateIndex:    42,
   266  		ModifyIndex:    99,
   267  		JobModifyIndex: 99,
   268  	}
   269  	job.Canonicalize()
   270  	return job
   271  }
   272  
   273  func MaxParallelJob() *structs.Job {
   274  	update := *structs.DefaultUpdateStrategy
   275  	update.MaxParallel = 0
   276  	job := &structs.Job{
   277  		Region:      "global",
   278  		ID:          fmt.Sprintf("mock-service-%s", uuid.Generate()),
   279  		Name:        "my-job",
   280  		Namespace:   structs.DefaultNamespace,
   281  		Type:        structs.JobTypeService,
   282  		Priority:    50,
   283  		AllAtOnce:   false,
   284  		Datacenters: []string{"dc1"},
   285  		Constraints: []*structs.Constraint{
   286  			{
   287  				LTarget: "${attr.kernel.name}",
   288  				RTarget: "linux",
   289  				Operand: "=",
   290  			},
   291  		},
   292  		Update: update,
   293  		TaskGroups: []*structs.TaskGroup{
   294  			{
   295  				Name:  "web",
   296  				Count: 10,
   297  				EphemeralDisk: &structs.EphemeralDisk{
   298  					SizeMB: 150,
   299  				},
   300  				RestartPolicy: &structs.RestartPolicy{
   301  					Attempts: 3,
   302  					Interval: 10 * time.Minute,
   303  					Delay:    1 * time.Minute,
   304  					Mode:     structs.RestartPolicyModeDelay,
   305  				},
   306  				ReschedulePolicy: &structs.ReschedulePolicy{
   307  					Attempts:      2,
   308  					Interval:      10 * time.Minute,
   309  					Delay:         5 * time.Second,
   310  					DelayFunction: "constant",
   311  				},
   312  				Migrate: structs.DefaultMigrateStrategy(),
   313  				Update:  &update,
   314  				Tasks: []*structs.Task{
   315  					{
   316  						Name:   "web",
   317  						Driver: "exec",
   318  						Config: map[string]interface{}{
   319  							"command": "/bin/date",
   320  						},
   321  						Env: map[string]string{
   322  							"FOO": "bar",
   323  						},
   324  						Services: []*structs.Service{
   325  							{
   326  								Name:      "${TASK}-frontend",
   327  								PortLabel: "http",
   328  								Tags:      []string{"pci:${meta.pci-dss}", "datacenter:${node.datacenter}"},
   329  								Checks: []*structs.ServiceCheck{
   330  									{
   331  										Name:     "check-table",
   332  										Type:     structs.ServiceCheckScript,
   333  										Command:  "/usr/local/check-table-${meta.database}",
   334  										Args:     []string{"${meta.version}"},
   335  										Interval: 30 * time.Second,
   336  										Timeout:  5 * time.Second,
   337  									},
   338  								},
   339  							},
   340  							{
   341  								Name:      "${TASK}-admin",
   342  								PortLabel: "admin",
   343  							},
   344  						},
   345  						LogConfig: structs.DefaultLogConfig(),
   346  						Resources: &structs.Resources{
   347  							CPU:      500,
   348  							MemoryMB: 256,
   349  							Networks: []*structs.NetworkResource{
   350  								{
   351  									MBits: 50,
   352  									DynamicPorts: []structs.Port{
   353  										{Label: "http"},
   354  										{Label: "admin"},
   355  									},
   356  								},
   357  							},
   358  						},
   359  						Meta: map[string]string{
   360  							"foo": "bar",
   361  						},
   362  					},
   363  				},
   364  				Meta: map[string]string{
   365  					"elb_check_type":     "http",
   366  					"elb_check_interval": "30s",
   367  					"elb_check_min":      "3",
   368  				},
   369  			},
   370  		},
   371  		Meta: map[string]string{
   372  			"owner": "armon",
   373  		},
   374  		Status:         structs.JobStatusPending,
   375  		Version:        0,
   376  		CreateIndex:    42,
   377  		ModifyIndex:    99,
   378  		JobModifyIndex: 99,
   379  	}
   380  	job.Canonicalize()
   381  	return job
   382  }
   383  
   384  // ConnectJob adds a Connect proxy sidecar group service to mock.Job.
   385  func ConnectJob() *structs.Job {
   386  	job := Job()
   387  	tg := job.TaskGroups[0]
   388  	tg.Networks = []*structs.NetworkResource{
   389  		{
   390  			Mode: "bridge",
   391  		},
   392  	}
   393  	tg.Services = []*structs.Service{
   394  		{
   395  			Name:      "testconnect",
   396  			PortLabel: "9999",
   397  			Connect: &structs.ConsulConnect{
   398  				SidecarService: &structs.ConsulSidecarService{},
   399  			},
   400  		},
   401  	}
   402  	return job
   403  }
   404  
   405  func BatchJob() *structs.Job {
   406  	job := &structs.Job{
   407  		Region:      "global",
   408  		ID:          fmt.Sprintf("mock-batch-%s", uuid.Generate()),
   409  		Name:        "batch-job",
   410  		Namespace:   structs.DefaultNamespace,
   411  		Type:        structs.JobTypeBatch,
   412  		Priority:    50,
   413  		AllAtOnce:   false,
   414  		Datacenters: []string{"dc1"},
   415  		TaskGroups: []*structs.TaskGroup{
   416  			{
   417  				Name:  "web",
   418  				Count: 10,
   419  				EphemeralDisk: &structs.EphemeralDisk{
   420  					SizeMB: 150,
   421  				},
   422  				RestartPolicy: &structs.RestartPolicy{
   423  					Attempts: 3,
   424  					Interval: 10 * time.Minute,
   425  					Delay:    1 * time.Minute,
   426  					Mode:     structs.RestartPolicyModeDelay,
   427  				},
   428  				ReschedulePolicy: &structs.ReschedulePolicy{
   429  					Attempts:      2,
   430  					Interval:      10 * time.Minute,
   431  					Delay:         5 * time.Second,
   432  					DelayFunction: "constant",
   433  				},
   434  				Tasks: []*structs.Task{
   435  					{
   436  						Name:   "web",
   437  						Driver: "mock_driver",
   438  						Config: map[string]interface{}{
   439  							"run_for": "500ms",
   440  						},
   441  						Env: map[string]string{
   442  							"FOO": "bar",
   443  						},
   444  						LogConfig: structs.DefaultLogConfig(),
   445  						Resources: &structs.Resources{
   446  							CPU:      100,
   447  							MemoryMB: 100,
   448  							Networks: []*structs.NetworkResource{
   449  								{
   450  									MBits: 50,
   451  								},
   452  							},
   453  						},
   454  						Meta: map[string]string{
   455  							"foo": "bar",
   456  						},
   457  					},
   458  				},
   459  			},
   460  		},
   461  		Status:         structs.JobStatusPending,
   462  		Version:        0,
   463  		CreateIndex:    43,
   464  		ModifyIndex:    99,
   465  		JobModifyIndex: 99,
   466  	}
   467  	job.Canonicalize()
   468  	return job
   469  }
   470  
   471  func SystemJob() *structs.Job {
   472  	job := &structs.Job{
   473  		Region:      "global",
   474  		Namespace:   structs.DefaultNamespace,
   475  		ID:          fmt.Sprintf("mock-system-%s", uuid.Generate()),
   476  		Name:        "my-job",
   477  		Type:        structs.JobTypeSystem,
   478  		Priority:    100,
   479  		AllAtOnce:   false,
   480  		Datacenters: []string{"dc1"},
   481  		Constraints: []*structs.Constraint{
   482  			{
   483  				LTarget: "${attr.kernel.name}",
   484  				RTarget: "linux",
   485  				Operand: "=",
   486  			},
   487  		},
   488  		TaskGroups: []*structs.TaskGroup{
   489  			{
   490  				Name:  "web",
   491  				Count: 1,
   492  				RestartPolicy: &structs.RestartPolicy{
   493  					Attempts: 3,
   494  					Interval: 10 * time.Minute,
   495  					Delay:    1 * time.Minute,
   496  					Mode:     structs.RestartPolicyModeDelay,
   497  				},
   498  				EphemeralDisk: structs.DefaultEphemeralDisk(),
   499  				Tasks: []*structs.Task{
   500  					{
   501  						Name:   "web",
   502  						Driver: "exec",
   503  						Config: map[string]interface{}{
   504  							"command": "/bin/date",
   505  						},
   506  						Env: map[string]string{},
   507  						Resources: &structs.Resources{
   508  							CPU:      500,
   509  							MemoryMB: 256,
   510  							Networks: []*structs.NetworkResource{
   511  								{
   512  									MBits:        50,
   513  									DynamicPorts: []structs.Port{{Label: "http"}},
   514  								},
   515  							},
   516  						},
   517  						LogConfig: structs.DefaultLogConfig(),
   518  					},
   519  				},
   520  			},
   521  		},
   522  		Meta: map[string]string{
   523  			"owner": "armon",
   524  		},
   525  		Status:      structs.JobStatusPending,
   526  		CreateIndex: 42,
   527  		ModifyIndex: 99,
   528  	}
   529  	job.Canonicalize()
   530  	return job
   531  }
   532  
   533  func PeriodicJob() *structs.Job {
   534  	job := Job()
   535  	job.Type = structs.JobTypeBatch
   536  	job.Periodic = &structs.PeriodicConfig{
   537  		Enabled:  true,
   538  		SpecType: structs.PeriodicSpecCron,
   539  		Spec:     "*/30 * * * *",
   540  	}
   541  	job.Status = structs.JobStatusRunning
   542  	job.TaskGroups[0].Migrate = nil
   543  	return job
   544  }
   545  
   546  func Eval() *structs.Evaluation {
   547  	now := time.Now().UTC().UnixNano()
   548  	eval := &structs.Evaluation{
   549  		ID:         uuid.Generate(),
   550  		Namespace:  structs.DefaultNamespace,
   551  		Priority:   50,
   552  		Type:       structs.JobTypeService,
   553  		JobID:      uuid.Generate(),
   554  		Status:     structs.EvalStatusPending,
   555  		CreateTime: now,
   556  		ModifyTime: now,
   557  	}
   558  	return eval
   559  }
   560  
   561  func JobSummary(jobID string) *structs.JobSummary {
   562  	js := &structs.JobSummary{
   563  		JobID:     jobID,
   564  		Namespace: structs.DefaultNamespace,
   565  		Summary: map[string]structs.TaskGroupSummary{
   566  			"web": {
   567  				Queued:   0,
   568  				Starting: 0,
   569  			},
   570  		},
   571  	}
   572  	return js
   573  }
   574  
   575  func Alloc() *structs.Allocation {
   576  	alloc := &structs.Allocation{
   577  		ID:        uuid.Generate(),
   578  		EvalID:    uuid.Generate(),
   579  		NodeID:    "12345678-abcd-efab-cdef-123456789abc",
   580  		Namespace: structs.DefaultNamespace,
   581  		TaskGroup: "web",
   582  
   583  		// TODO Remove once clientv2 gets merged
   584  		Resources: &structs.Resources{
   585  			CPU:      500,
   586  			MemoryMB: 256,
   587  			DiskMB:   150,
   588  			Networks: []*structs.NetworkResource{
   589  				{
   590  					Device:        "eth0",
   591  					IP:            "192.168.0.100",
   592  					ReservedPorts: []structs.Port{{Label: "admin", Value: 5000}},
   593  					MBits:         50,
   594  					DynamicPorts:  []structs.Port{{Label: "http"}},
   595  				},
   596  			},
   597  		},
   598  		TaskResources: map[string]*structs.Resources{
   599  			"web": {
   600  				CPU:      500,
   601  				MemoryMB: 256,
   602  				Networks: []*structs.NetworkResource{
   603  					{
   604  						Device:        "eth0",
   605  						IP:            "192.168.0.100",
   606  						ReservedPorts: []structs.Port{{Label: "admin", Value: 5000}},
   607  						MBits:         50,
   608  						DynamicPorts:  []structs.Port{{Label: "http", Value: 9876}},
   609  					},
   610  				},
   611  			},
   612  		},
   613  		SharedResources: &structs.Resources{
   614  			DiskMB: 150,
   615  		},
   616  
   617  		AllocatedResources: &structs.AllocatedResources{
   618  			Tasks: map[string]*structs.AllocatedTaskResources{
   619  				"web": {
   620  					Cpu: structs.AllocatedCpuResources{
   621  						CpuShares: 500,
   622  					},
   623  					Memory: structs.AllocatedMemoryResources{
   624  						MemoryMB: 256,
   625  					},
   626  					Networks: []*structs.NetworkResource{
   627  						{
   628  							Device:        "eth0",
   629  							IP:            "192.168.0.100",
   630  							ReservedPorts: []structs.Port{{Label: "admin", Value: 5000}},
   631  							MBits:         50,
   632  							DynamicPorts:  []structs.Port{{Label: "http", Value: 9876}},
   633  						},
   634  					},
   635  				},
   636  			},
   637  			Shared: structs.AllocatedSharedResources{
   638  				DiskMB: 150,
   639  			},
   640  		},
   641  		Job:           Job(),
   642  		DesiredStatus: structs.AllocDesiredStatusRun,
   643  		ClientStatus:  structs.AllocClientStatusPending,
   644  	}
   645  	alloc.JobID = alloc.Job.ID
   646  	return alloc
   647  }
   648  
   649  // ConnectJob adds a Connect proxy sidecar group service to mock.Alloc.
   650  func ConnectAlloc() *structs.Allocation {
   651  	alloc := Alloc()
   652  	alloc.Job = ConnectJob()
   653  	alloc.AllocatedResources.Shared.Networks = []*structs.NetworkResource{
   654  		{
   655  			Mode: "bridge",
   656  			IP:   "10.0.0.1",
   657  			DynamicPorts: []structs.Port{
   658  				{
   659  					Label: "connect-proxy-testconnect",
   660  					Value: 9999,
   661  					To:    9999,
   662  				},
   663  			},
   664  		},
   665  	}
   666  	return alloc
   667  }
   668  
   669  func BatchAlloc() *structs.Allocation {
   670  	alloc := &structs.Allocation{
   671  		ID:        uuid.Generate(),
   672  		EvalID:    uuid.Generate(),
   673  		NodeID:    "12345678-abcd-efab-cdef-123456789abc",
   674  		Namespace: structs.DefaultNamespace,
   675  		TaskGroup: "web",
   676  
   677  		// TODO Remove once clientv2 gets merged
   678  		Resources: &structs.Resources{
   679  			CPU:      500,
   680  			MemoryMB: 256,
   681  			DiskMB:   150,
   682  			Networks: []*structs.NetworkResource{
   683  				{
   684  					Device:        "eth0",
   685  					IP:            "192.168.0.100",
   686  					ReservedPorts: []structs.Port{{Label: "admin", Value: 5000}},
   687  					MBits:         50,
   688  					DynamicPorts:  []structs.Port{{Label: "http"}},
   689  				},
   690  			},
   691  		},
   692  		TaskResources: map[string]*structs.Resources{
   693  			"web": {
   694  				CPU:      500,
   695  				MemoryMB: 256,
   696  				Networks: []*structs.NetworkResource{
   697  					{
   698  						Device:        "eth0",
   699  						IP:            "192.168.0.100",
   700  						ReservedPorts: []structs.Port{{Label: "admin", Value: 5000}},
   701  						MBits:         50,
   702  						DynamicPorts:  []structs.Port{{Label: "http", Value: 9876}},
   703  					},
   704  				},
   705  			},
   706  		},
   707  		SharedResources: &structs.Resources{
   708  			DiskMB: 150,
   709  		},
   710  
   711  		AllocatedResources: &structs.AllocatedResources{
   712  			Tasks: map[string]*structs.AllocatedTaskResources{
   713  				"web": {
   714  					Cpu: structs.AllocatedCpuResources{
   715  						CpuShares: 500,
   716  					},
   717  					Memory: structs.AllocatedMemoryResources{
   718  						MemoryMB: 256,
   719  					},
   720  					Networks: []*structs.NetworkResource{
   721  						{
   722  							Device:        "eth0",
   723  							IP:            "192.168.0.100",
   724  							ReservedPorts: []structs.Port{{Label: "admin", Value: 5000}},
   725  							MBits:         50,
   726  							DynamicPorts:  []structs.Port{{Label: "http", Value: 9876}},
   727  						},
   728  					},
   729  				},
   730  			},
   731  			Shared: structs.AllocatedSharedResources{
   732  				DiskMB: 150,
   733  			},
   734  		},
   735  		Job:           BatchJob(),
   736  		DesiredStatus: structs.AllocDesiredStatusRun,
   737  		ClientStatus:  structs.AllocClientStatusPending,
   738  	}
   739  	alloc.JobID = alloc.Job.ID
   740  	return alloc
   741  }
   742  
   743  func SystemAlloc() *structs.Allocation {
   744  	alloc := &structs.Allocation{
   745  		ID:        uuid.Generate(),
   746  		EvalID:    uuid.Generate(),
   747  		NodeID:    "12345678-abcd-efab-cdef-123456789abc",
   748  		Namespace: structs.DefaultNamespace,
   749  		TaskGroup: "web",
   750  
   751  		// TODO Remove once clientv2 gets merged
   752  		Resources: &structs.Resources{
   753  			CPU:      500,
   754  			MemoryMB: 256,
   755  			DiskMB:   150,
   756  			Networks: []*structs.NetworkResource{
   757  				{
   758  					Device:        "eth0",
   759  					IP:            "192.168.0.100",
   760  					ReservedPorts: []structs.Port{{Label: "admin", Value: 5000}},
   761  					MBits:         50,
   762  					DynamicPorts:  []structs.Port{{Label: "http"}},
   763  				},
   764  			},
   765  		},
   766  		TaskResources: map[string]*structs.Resources{
   767  			"web": {
   768  				CPU:      500,
   769  				MemoryMB: 256,
   770  				Networks: []*structs.NetworkResource{
   771  					{
   772  						Device:        "eth0",
   773  						IP:            "192.168.0.100",
   774  						ReservedPorts: []structs.Port{{Label: "admin", Value: 5000}},
   775  						MBits:         50,
   776  						DynamicPorts:  []structs.Port{{Label: "http", Value: 9876}},
   777  					},
   778  				},
   779  			},
   780  		},
   781  		SharedResources: &structs.Resources{
   782  			DiskMB: 150,
   783  		},
   784  
   785  		AllocatedResources: &structs.AllocatedResources{
   786  			Tasks: map[string]*structs.AllocatedTaskResources{
   787  				"web": {
   788  					Cpu: structs.AllocatedCpuResources{
   789  						CpuShares: 500,
   790  					},
   791  					Memory: structs.AllocatedMemoryResources{
   792  						MemoryMB: 256,
   793  					},
   794  					Networks: []*structs.NetworkResource{
   795  						{
   796  							Device:        "eth0",
   797  							IP:            "192.168.0.100",
   798  							ReservedPorts: []structs.Port{{Label: "admin", Value: 5000}},
   799  							MBits:         50,
   800  							DynamicPorts:  []structs.Port{{Label: "http", Value: 9876}},
   801  						},
   802  					},
   803  				},
   804  			},
   805  			Shared: structs.AllocatedSharedResources{
   806  				DiskMB: 150,
   807  			},
   808  		},
   809  		Job:           SystemJob(),
   810  		DesiredStatus: structs.AllocDesiredStatusRun,
   811  		ClientStatus:  structs.AllocClientStatusPending,
   812  	}
   813  	alloc.JobID = alloc.Job.ID
   814  	return alloc
   815  }
   816  
   817  func VaultAccessor() *structs.VaultAccessor {
   818  	return &structs.VaultAccessor{
   819  		Accessor:    uuid.Generate(),
   820  		NodeID:      uuid.Generate(),
   821  		AllocID:     uuid.Generate(),
   822  		CreationTTL: 86400,
   823  		Task:        "foo",
   824  	}
   825  }
   826  
   827  func Deployment() *structs.Deployment {
   828  	return &structs.Deployment{
   829  		ID:             uuid.Generate(),
   830  		JobID:          uuid.Generate(),
   831  		Namespace:      structs.DefaultNamespace,
   832  		JobVersion:     2,
   833  		JobModifyIndex: 20,
   834  		JobCreateIndex: 18,
   835  		TaskGroups: map[string]*structs.DeploymentState{
   836  			"web": {
   837  				DesiredTotal: 10,
   838  			},
   839  		},
   840  		Status:            structs.DeploymentStatusRunning,
   841  		StatusDescription: structs.DeploymentStatusDescriptionRunning,
   842  		ModifyIndex:       23,
   843  		CreateIndex:       21,
   844  	}
   845  }
   846  
   847  func Plan() *structs.Plan {
   848  	return &structs.Plan{
   849  		Priority: 50,
   850  	}
   851  }
   852  
   853  func PlanResult() *structs.PlanResult {
   854  	return &structs.PlanResult{}
   855  }
   856  
   857  func ACLPolicy() *structs.ACLPolicy {
   858  	ap := &structs.ACLPolicy{
   859  		Name:        fmt.Sprintf("policy-%s", uuid.Generate()),
   860  		Description: "Super cool policy!",
   861  		Rules: `
   862  		namespace "default" {
   863  			policy = "write"
   864  		}
   865  		node {
   866  			policy = "read"
   867  		}
   868  		agent {
   869  			policy = "read"
   870  		}
   871  		`,
   872  		CreateIndex: 10,
   873  		ModifyIndex: 20,
   874  	}
   875  	ap.SetHash()
   876  	return ap
   877  }
   878  
   879  func ACLToken() *structs.ACLToken {
   880  	tk := &structs.ACLToken{
   881  		AccessorID:  uuid.Generate(),
   882  		SecretID:    uuid.Generate(),
   883  		Name:        "my cool token " + uuid.Generate(),
   884  		Type:        "client",
   885  		Policies:    []string{"foo", "bar"},
   886  		Global:      false,
   887  		CreateTime:  time.Now().UTC(),
   888  		CreateIndex: 10,
   889  		ModifyIndex: 20,
   890  	}
   891  	tk.SetHash()
   892  	return tk
   893  }
   894  
   895  func ACLManagementToken() *structs.ACLToken {
   896  	return &structs.ACLToken{
   897  		AccessorID:  uuid.Generate(),
   898  		SecretID:    uuid.Generate(),
   899  		Name:        "management " + uuid.Generate(),
   900  		Type:        "management",
   901  		Global:      true,
   902  		CreateTime:  time.Now().UTC(),
   903  		CreateIndex: 10,
   904  		ModifyIndex: 20,
   905  	}
   906  }