github.com/zoomfoo/nomad@v0.8.5-0.20180907175415-f28fd3a1a056/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  			"driver.mock_driver": "1",
    23  		},
    24  		Resources: &structs.Resources{
    25  			CPU:      4000,
    26  			MemoryMB: 8192,
    27  			DiskMB:   100 * 1024,
    28  			IOPS:     150,
    29  			Networks: []*structs.NetworkResource{
    30  				{
    31  					Device: "eth0",
    32  					CIDR:   "192.168.0.100/32",
    33  					MBits:  1000,
    34  				},
    35  			},
    36  		},
    37  		Reserved: &structs.Resources{
    38  			CPU:      100,
    39  			MemoryMB: 256,
    40  			DiskMB:   4 * 1024,
    41  			Networks: []*structs.NetworkResource{
    42  				{
    43  					Device:        "eth0",
    44  					IP:            "192.168.0.100",
    45  					ReservedPorts: []structs.Port{{Label: "ssh", Value: 22}},
    46  					MBits:         1,
    47  				},
    48  			},
    49  		},
    50  		Links: map[string]string{
    51  			"consul": "foobar.dc1",
    52  		},
    53  		Meta: map[string]string{
    54  			"pci-dss":  "true",
    55  			"database": "mysql",
    56  			"version":  "5.6",
    57  		},
    58  		NodeClass:             "linux-medium-pci",
    59  		Status:                structs.NodeStatusReady,
    60  		SchedulingEligibility: structs.NodeSchedulingEligible,
    61  	}
    62  	node.ComputeClass()
    63  	return node
    64  }
    65  
    66  func HCL() string {
    67  	return `job "my-job" {
    68  	datacenters = ["dc1"]
    69  	type = "service"
    70  	constraint {
    71  		attribute = "${attr.kernel.name}"
    72  		value = "linux"
    73  	}
    74  
    75  	group "web" {
    76  		count = 10
    77  		restart {
    78  			attempts = 3
    79  			interval = "10m"
    80  			delay = "1m"
    81  			mode = "delay"
    82  		}
    83  		task "web" {
    84  			driver = "exec"
    85  			config {
    86  				command = "/bin/date"
    87  			}
    88  			resources {
    89  				cpu = 500
    90  				memory = 256
    91  			}
    92  		}
    93  	}
    94  }
    95  `
    96  }
    97  
    98  func Job() *structs.Job {
    99  	job := &structs.Job{
   100  		Region:      "global",
   101  		ID:          fmt.Sprintf("mock-service-%s", uuid.Generate()),
   102  		Name:        "my-job",
   103  		Namespace:   structs.DefaultNamespace,
   104  		Type:        structs.JobTypeService,
   105  		Priority:    50,
   106  		AllAtOnce:   false,
   107  		Datacenters: []string{"dc1"},
   108  		Constraints: []*structs.Constraint{
   109  			{
   110  				LTarget: "${attr.kernel.name}",
   111  				RTarget: "linux",
   112  				Operand: "=",
   113  			},
   114  		},
   115  		TaskGroups: []*structs.TaskGroup{
   116  			{
   117  				Name:  "web",
   118  				Count: 10,
   119  				EphemeralDisk: &structs.EphemeralDisk{
   120  					SizeMB: 150,
   121  				},
   122  				RestartPolicy: &structs.RestartPolicy{
   123  					Attempts: 3,
   124  					Interval: 10 * time.Minute,
   125  					Delay:    1 * time.Minute,
   126  					Mode:     structs.RestartPolicyModeDelay,
   127  				},
   128  				ReschedulePolicy: &structs.ReschedulePolicy{
   129  					Attempts:      2,
   130  					Interval:      10 * time.Minute,
   131  					Delay:         5 * time.Second,
   132  					DelayFunction: "constant",
   133  				},
   134  				Migrate: structs.DefaultMigrateStrategy(),
   135  				Tasks: []*structs.Task{
   136  					{
   137  						Name:   "web",
   138  						Driver: "exec",
   139  						Config: map[string]interface{}{
   140  							"command": "/bin/date",
   141  						},
   142  						Env: map[string]string{
   143  							"FOO": "bar",
   144  						},
   145  						Services: []*structs.Service{
   146  							{
   147  								Name:      "${TASK}-frontend",
   148  								PortLabel: "http",
   149  								Tags:      []string{"pci:${meta.pci-dss}", "datacenter:${node.datacenter}"},
   150  								Checks: []*structs.ServiceCheck{
   151  									{
   152  										Name:     "check-table",
   153  										Type:     structs.ServiceCheckScript,
   154  										Command:  "/usr/local/check-table-${meta.database}",
   155  										Args:     []string{"${meta.version}"},
   156  										Interval: 30 * time.Second,
   157  										Timeout:  5 * time.Second,
   158  									},
   159  								},
   160  							},
   161  							{
   162  								Name:      "${TASK}-admin",
   163  								PortLabel: "admin",
   164  							},
   165  						},
   166  						LogConfig: structs.DefaultLogConfig(),
   167  						Resources: &structs.Resources{
   168  							CPU:      500,
   169  							MemoryMB: 256,
   170  							Networks: []*structs.NetworkResource{
   171  								{
   172  									MBits: 50,
   173  									DynamicPorts: []structs.Port{
   174  										{Label: "http"},
   175  										{Label: "admin"},
   176  									},
   177  								},
   178  							},
   179  						},
   180  						Meta: map[string]string{
   181  							"foo": "bar",
   182  						},
   183  					},
   184  				},
   185  				Meta: map[string]string{
   186  					"elb_check_type":     "http",
   187  					"elb_check_interval": "30s",
   188  					"elb_check_min":      "3",
   189  				},
   190  			},
   191  		},
   192  		Meta: map[string]string{
   193  			"owner": "armon",
   194  		},
   195  		Status:         structs.JobStatusPending,
   196  		Version:        0,
   197  		CreateIndex:    42,
   198  		ModifyIndex:    99,
   199  		JobModifyIndex: 99,
   200  	}
   201  	job.Canonicalize()
   202  	return job
   203  }
   204  
   205  func BatchJob() *structs.Job {
   206  	job := &structs.Job{
   207  		Region:      "global",
   208  		ID:          fmt.Sprintf("mock-batch-%s", uuid.Generate()),
   209  		Name:        "batch-job",
   210  		Namespace:   structs.DefaultNamespace,
   211  		Type:        structs.JobTypeBatch,
   212  		Priority:    50,
   213  		AllAtOnce:   false,
   214  		Datacenters: []string{"dc1"},
   215  		TaskGroups: []*structs.TaskGroup{
   216  			{
   217  				Name:  "worker",
   218  				Count: 10,
   219  				EphemeralDisk: &structs.EphemeralDisk{
   220  					SizeMB: 150,
   221  				},
   222  				RestartPolicy: &structs.RestartPolicy{
   223  					Attempts: 3,
   224  					Interval: 10 * time.Minute,
   225  					Delay:    1 * time.Minute,
   226  					Mode:     structs.RestartPolicyModeDelay,
   227  				},
   228  				ReschedulePolicy: &structs.ReschedulePolicy{
   229  					Attempts:      2,
   230  					Interval:      10 * time.Minute,
   231  					Delay:         5 * time.Second,
   232  					DelayFunction: "constant",
   233  				},
   234  				Tasks: []*structs.Task{
   235  					{
   236  						Name:   "worker",
   237  						Driver: "mock_driver",
   238  						Config: map[string]interface{}{
   239  							"run_for": "500ms",
   240  						},
   241  						Env: map[string]string{
   242  							"FOO": "bar",
   243  						},
   244  						LogConfig: structs.DefaultLogConfig(),
   245  						Resources: &structs.Resources{
   246  							CPU:      100,
   247  							MemoryMB: 100,
   248  							Networks: []*structs.NetworkResource{
   249  								{
   250  									MBits: 50,
   251  								},
   252  							},
   253  						},
   254  						Meta: map[string]string{
   255  							"foo": "bar",
   256  						},
   257  					},
   258  				},
   259  			},
   260  		},
   261  		Status:         structs.JobStatusPending,
   262  		Version:        0,
   263  		CreateIndex:    43,
   264  		ModifyIndex:    99,
   265  		JobModifyIndex: 99,
   266  	}
   267  	job.Canonicalize()
   268  	return job
   269  }
   270  
   271  func SystemJob() *structs.Job {
   272  	job := &structs.Job{
   273  		Region:      "global",
   274  		Namespace:   structs.DefaultNamespace,
   275  		ID:          fmt.Sprintf("mock-system-%s", uuid.Generate()),
   276  		Name:        "my-job",
   277  		Type:        structs.JobTypeSystem,
   278  		Priority:    100,
   279  		AllAtOnce:   false,
   280  		Datacenters: []string{"dc1"},
   281  		Constraints: []*structs.Constraint{
   282  			{
   283  				LTarget: "${attr.kernel.name}",
   284  				RTarget: "linux",
   285  				Operand: "=",
   286  			},
   287  		},
   288  		TaskGroups: []*structs.TaskGroup{
   289  			{
   290  				Name:  "web",
   291  				Count: 1,
   292  				RestartPolicy: &structs.RestartPolicy{
   293  					Attempts: 3,
   294  					Interval: 10 * time.Minute,
   295  					Delay:    1 * time.Minute,
   296  					Mode:     structs.RestartPolicyModeDelay,
   297  				},
   298  				EphemeralDisk: structs.DefaultEphemeralDisk(),
   299  				Tasks: []*structs.Task{
   300  					{
   301  						Name:   "web",
   302  						Driver: "exec",
   303  						Config: map[string]interface{}{
   304  							"command": "/bin/date",
   305  						},
   306  						Env: map[string]string{},
   307  						Resources: &structs.Resources{
   308  							CPU:      500,
   309  							MemoryMB: 256,
   310  							Networks: []*structs.NetworkResource{
   311  								{
   312  									MBits:        50,
   313  									DynamicPorts: []structs.Port{{Label: "http"}},
   314  								},
   315  							},
   316  						},
   317  						LogConfig: structs.DefaultLogConfig(),
   318  					},
   319  				},
   320  			},
   321  		},
   322  		Meta: map[string]string{
   323  			"owner": "armon",
   324  		},
   325  		Status:      structs.JobStatusPending,
   326  		CreateIndex: 42,
   327  		ModifyIndex: 99,
   328  	}
   329  	job.Canonicalize()
   330  	return job
   331  }
   332  
   333  func PeriodicJob() *structs.Job {
   334  	job := Job()
   335  	job.Type = structs.JobTypeBatch
   336  	job.Periodic = &structs.PeriodicConfig{
   337  		Enabled:  true,
   338  		SpecType: structs.PeriodicSpecCron,
   339  		Spec:     "*/30 * * * *",
   340  	}
   341  	job.Status = structs.JobStatusRunning
   342  	job.TaskGroups[0].Migrate = nil
   343  	return job
   344  }
   345  
   346  func Eval() *structs.Evaluation {
   347  	eval := &structs.Evaluation{
   348  		ID:        uuid.Generate(),
   349  		Namespace: structs.DefaultNamespace,
   350  		Priority:  50,
   351  		Type:      structs.JobTypeService,
   352  		JobID:     uuid.Generate(),
   353  		Status:    structs.EvalStatusPending,
   354  	}
   355  	return eval
   356  }
   357  
   358  func JobSummary(jobID string) *structs.JobSummary {
   359  	js := &structs.JobSummary{
   360  		JobID:     jobID,
   361  		Namespace: structs.DefaultNamespace,
   362  		Summary: map[string]structs.TaskGroupSummary{
   363  			"web": {
   364  				Queued:   0,
   365  				Starting: 0,
   366  			},
   367  		},
   368  	}
   369  	return js
   370  }
   371  
   372  func Alloc() *structs.Allocation {
   373  	alloc := &structs.Allocation{
   374  		ID:        uuid.Generate(),
   375  		EvalID:    uuid.Generate(),
   376  		NodeID:    "12345678-abcd-efab-cdef-123456789abc",
   377  		Namespace: structs.DefaultNamespace,
   378  		TaskGroup: "web",
   379  		Resources: &structs.Resources{
   380  			CPU:      500,
   381  			MemoryMB: 256,
   382  			DiskMB:   150,
   383  			Networks: []*structs.NetworkResource{
   384  				{
   385  					Device:        "eth0",
   386  					IP:            "192.168.0.100",
   387  					ReservedPorts: []structs.Port{{Label: "admin", Value: 5000}},
   388  					MBits:         50,
   389  					DynamicPorts:  []structs.Port{{Label: "http"}},
   390  				},
   391  			},
   392  		},
   393  		TaskResources: map[string]*structs.Resources{
   394  			"web": {
   395  				CPU:      500,
   396  				MemoryMB: 256,
   397  				Networks: []*structs.NetworkResource{
   398  					{
   399  						Device:        "eth0",
   400  						IP:            "192.168.0.100",
   401  						ReservedPorts: []structs.Port{{Label: "admin", Value: 5000}},
   402  						MBits:         50,
   403  						DynamicPorts:  []structs.Port{{Label: "http", Value: 9876}},
   404  					},
   405  				},
   406  			},
   407  		},
   408  		SharedResources: &structs.Resources{
   409  			DiskMB: 150,
   410  		},
   411  		Job:           Job(),
   412  		DesiredStatus: structs.AllocDesiredStatusRun,
   413  		ClientStatus:  structs.AllocClientStatusPending,
   414  	}
   415  	alloc.JobID = alloc.Job.ID
   416  	return alloc
   417  }
   418  
   419  func BatchAlloc() *structs.Allocation {
   420  	alloc := &structs.Allocation{
   421  		ID:        uuid.Generate(),
   422  		EvalID:    uuid.Generate(),
   423  		NodeID:    "12345678-abcd-efab-cdef-123456789abc",
   424  		Namespace: structs.DefaultNamespace,
   425  		TaskGroup: "worker",
   426  		Resources: &structs.Resources{
   427  			CPU:      500,
   428  			MemoryMB: 256,
   429  			DiskMB:   150,
   430  			Networks: []*structs.NetworkResource{
   431  				{
   432  					Device:        "eth0",
   433  					IP:            "192.168.0.100",
   434  					ReservedPorts: []structs.Port{{Label: "admin", Value: 5000}},
   435  					MBits:         50,
   436  					DynamicPorts:  []structs.Port{{Label: "http"}},
   437  				},
   438  			},
   439  		},
   440  		TaskResources: map[string]*structs.Resources{
   441  			"worker": {
   442  				CPU:      100,
   443  				MemoryMB: 100,
   444  				Networks: []*structs.NetworkResource{
   445  					{
   446  						Device: "eth0",
   447  						IP:     "192.168.0.100",
   448  						MBits:  50,
   449  					},
   450  				},
   451  			},
   452  		},
   453  		SharedResources: &structs.Resources{
   454  			DiskMB: 150,
   455  		},
   456  		Job:           BatchJob(),
   457  		DesiredStatus: structs.AllocDesiredStatusRun,
   458  		ClientStatus:  structs.AllocClientStatusPending,
   459  	}
   460  	alloc.JobID = alloc.Job.ID
   461  	return alloc
   462  }
   463  
   464  func SystemAlloc() *structs.Allocation {
   465  	alloc := &structs.Allocation{
   466  		ID:        uuid.Generate(),
   467  		EvalID:    uuid.Generate(),
   468  		NodeID:    "12345678-abcd-efab-cdef-123456789abc",
   469  		Namespace: structs.DefaultNamespace,
   470  		TaskGroup: "web",
   471  		Resources: &structs.Resources{
   472  			CPU:      500,
   473  			MemoryMB: 256,
   474  			DiskMB:   150,
   475  			Networks: []*structs.NetworkResource{
   476  				{
   477  					Device:        "eth0",
   478  					IP:            "192.168.0.100",
   479  					ReservedPorts: []structs.Port{{Label: "admin", Value: 5000}},
   480  					MBits:         50,
   481  					DynamicPorts:  []structs.Port{{Label: "http"}},
   482  				},
   483  			},
   484  		},
   485  		TaskResources: map[string]*structs.Resources{
   486  			"web": {
   487  				CPU:      500,
   488  				MemoryMB: 256,
   489  				Networks: []*structs.NetworkResource{
   490  					{
   491  						Device:        "eth0",
   492  						IP:            "192.168.0.100",
   493  						ReservedPorts: []structs.Port{{Label: "admin", Value: 5000}},
   494  						MBits:         50,
   495  						DynamicPorts:  []structs.Port{{Label: "http", Value: 9876}},
   496  					},
   497  				},
   498  			},
   499  		},
   500  		SharedResources: &structs.Resources{
   501  			DiskMB: 150,
   502  		},
   503  		Job:           SystemJob(),
   504  		DesiredStatus: structs.AllocDesiredStatusRun,
   505  		ClientStatus:  structs.AllocClientStatusPending,
   506  	}
   507  	alloc.JobID = alloc.Job.ID
   508  	return alloc
   509  }
   510  
   511  func VaultAccessor() *structs.VaultAccessor {
   512  	return &structs.VaultAccessor{
   513  		Accessor:    uuid.Generate(),
   514  		NodeID:      uuid.Generate(),
   515  		AllocID:     uuid.Generate(),
   516  		CreationTTL: 86400,
   517  		Task:        "foo",
   518  	}
   519  }
   520  
   521  func Deployment() *structs.Deployment {
   522  	return &structs.Deployment{
   523  		ID:             uuid.Generate(),
   524  		JobID:          uuid.Generate(),
   525  		Namespace:      structs.DefaultNamespace,
   526  		JobVersion:     2,
   527  		JobModifyIndex: 20,
   528  		JobCreateIndex: 18,
   529  		TaskGroups: map[string]*structs.DeploymentState{
   530  			"web": {
   531  				DesiredTotal: 10,
   532  			},
   533  		},
   534  		Status:            structs.DeploymentStatusRunning,
   535  		StatusDescription: structs.DeploymentStatusDescriptionRunning,
   536  		ModifyIndex:       23,
   537  		CreateIndex:       21,
   538  	}
   539  }
   540  
   541  func Plan() *structs.Plan {
   542  	return &structs.Plan{
   543  		Priority: 50,
   544  	}
   545  }
   546  
   547  func PlanResult() *structs.PlanResult {
   548  	return &structs.PlanResult{}
   549  }
   550  
   551  func ACLPolicy() *structs.ACLPolicy {
   552  	ap := &structs.ACLPolicy{
   553  		Name:        fmt.Sprintf("policy-%s", uuid.Generate()),
   554  		Description: "Super cool policy!",
   555  		Rules: `
   556  		namespace "default" {
   557  			policy = "write"
   558  		}
   559  		node {
   560  			policy = "read"
   561  		}
   562  		agent {
   563  			policy = "read"
   564  		}
   565  		`,
   566  		CreateIndex: 10,
   567  		ModifyIndex: 20,
   568  	}
   569  	ap.SetHash()
   570  	return ap
   571  }
   572  
   573  func ACLToken() *structs.ACLToken {
   574  	tk := &structs.ACLToken{
   575  		AccessorID:  uuid.Generate(),
   576  		SecretID:    uuid.Generate(),
   577  		Name:        "my cool token " + uuid.Generate(),
   578  		Type:        "client",
   579  		Policies:    []string{"foo", "bar"},
   580  		Global:      false,
   581  		CreateTime:  time.Now().UTC(),
   582  		CreateIndex: 10,
   583  		ModifyIndex: 20,
   584  	}
   585  	tk.SetHash()
   586  	return tk
   587  }
   588  
   589  func ACLManagementToken() *structs.ACLToken {
   590  	return &structs.ACLToken{
   591  		AccessorID:  uuid.Generate(),
   592  		SecretID:    uuid.Generate(),
   593  		Name:        "management " + uuid.Generate(),
   594  		Type:        "management",
   595  		Global:      true,
   596  		CreateTime:  time.Now().UTC(),
   597  		CreateIndex: 10,
   598  		ModifyIndex: 20,
   599  	}
   600  }