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