github.com/ncodes/nomad@v0.5.7-0.20170403112158-97adf4a74fb3/nomad/mock/mock.go (about)

     1  package mock
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/ncodes/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  		CreateIndex:    42,
   150  		ModifyIndex:    99,
   151  		JobModifyIndex: 99,
   152  	}
   153  	job.Canonicalize()
   154  	return job
   155  }
   156  
   157  func SystemJob() *structs.Job {
   158  	job := &structs.Job{
   159  		Region:      "global",
   160  		ID:          structs.GenerateUUID(),
   161  		Name:        "my-job",
   162  		Type:        structs.JobTypeSystem,
   163  		Priority:    100,
   164  		AllAtOnce:   false,
   165  		Datacenters: []string{"dc1"},
   166  		Constraints: []*structs.Constraint{
   167  			&structs.Constraint{
   168  				LTarget: "${attr.kernel.name}",
   169  				RTarget: "linux",
   170  				Operand: "=",
   171  			},
   172  		},
   173  		TaskGroups: []*structs.TaskGroup{
   174  			&structs.TaskGroup{
   175  				Name:  "web",
   176  				Count: 1,
   177  				RestartPolicy: &structs.RestartPolicy{
   178  					Attempts: 3,
   179  					Interval: 10 * time.Minute,
   180  					Delay:    1 * time.Minute,
   181  					Mode:     structs.RestartPolicyModeDelay,
   182  				},
   183  				EphemeralDisk: structs.DefaultEphemeralDisk(),
   184  				Tasks: []*structs.Task{
   185  					&structs.Task{
   186  						Name:   "web",
   187  						Driver: "exec",
   188  						Config: map[string]interface{}{
   189  							"command": "/bin/date",
   190  						},
   191  						Env: map[string]string{},
   192  						Resources: &structs.Resources{
   193  							CPU:      500,
   194  							MemoryMB: 256,
   195  							Networks: []*structs.NetworkResource{
   196  								&structs.NetworkResource{
   197  									MBits:        50,
   198  									DynamicPorts: []structs.Port{{Label: "http"}},
   199  								},
   200  							},
   201  						},
   202  						LogConfig: structs.DefaultLogConfig(),
   203  					},
   204  				},
   205  			},
   206  		},
   207  		Meta: map[string]string{
   208  			"owner": "armon",
   209  		},
   210  		Status:      structs.JobStatusPending,
   211  		CreateIndex: 42,
   212  		ModifyIndex: 99,
   213  	}
   214  	job.Canonicalize()
   215  	return job
   216  }
   217  
   218  func PeriodicJob() *structs.Job {
   219  	job := Job()
   220  	job.Type = structs.JobTypeBatch
   221  	job.Periodic = &structs.PeriodicConfig{
   222  		Enabled:  true,
   223  		SpecType: structs.PeriodicSpecCron,
   224  		Spec:     "*/30 * * * *",
   225  	}
   226  	job.Status = structs.JobStatusRunning
   227  	return job
   228  }
   229  
   230  func Eval() *structs.Evaluation {
   231  	eval := &structs.Evaluation{
   232  		ID:       structs.GenerateUUID(),
   233  		Priority: 50,
   234  		Type:     structs.JobTypeService,
   235  		JobID:    structs.GenerateUUID(),
   236  		Status:   structs.EvalStatusPending,
   237  	}
   238  	return eval
   239  }
   240  
   241  func JobSummary(jobID string) *structs.JobSummary {
   242  	js := &structs.JobSummary{
   243  		JobID: jobID,
   244  		Summary: map[string]structs.TaskGroupSummary{
   245  			"web": {
   246  				Queued:   0,
   247  				Starting: 0,
   248  			},
   249  		},
   250  	}
   251  	return js
   252  }
   253  
   254  func Alloc() *structs.Allocation {
   255  	alloc := &structs.Allocation{
   256  		ID:        structs.GenerateUUID(),
   257  		EvalID:    structs.GenerateUUID(),
   258  		NodeID:    "12345678-abcd-efab-cdef-123456789abc",
   259  		TaskGroup: "web",
   260  		Resources: &structs.Resources{
   261  			CPU:      500,
   262  			MemoryMB: 256,
   263  			DiskMB:   150,
   264  			Networks: []*structs.NetworkResource{
   265  				&structs.NetworkResource{
   266  					Device:        "eth0",
   267  					IP:            "192.168.0.100",
   268  					ReservedPorts: []structs.Port{{Label: "main", Value: 5000}},
   269  					MBits:         50,
   270  					DynamicPorts:  []structs.Port{{Label: "http"}},
   271  				},
   272  			},
   273  		},
   274  		TaskResources: map[string]*structs.Resources{
   275  			"web": &structs.Resources{
   276  				CPU:      500,
   277  				MemoryMB: 256,
   278  				Networks: []*structs.NetworkResource{
   279  					&structs.NetworkResource{
   280  						Device:        "eth0",
   281  						IP:            "192.168.0.100",
   282  						ReservedPorts: []structs.Port{{Label: "main", Value: 5000}},
   283  						MBits:         50,
   284  						DynamicPorts:  []structs.Port{{Label: "http"}},
   285  					},
   286  				},
   287  			},
   288  		},
   289  		SharedResources: &structs.Resources{
   290  			DiskMB: 150,
   291  		},
   292  		Job:           Job(),
   293  		DesiredStatus: structs.AllocDesiredStatusRun,
   294  		ClientStatus:  structs.AllocClientStatusPending,
   295  	}
   296  	alloc.JobID = alloc.Job.ID
   297  	return alloc
   298  }
   299  
   300  func VaultAccessor() *structs.VaultAccessor {
   301  	return &structs.VaultAccessor{
   302  		Accessor:    structs.GenerateUUID(),
   303  		NodeID:      structs.GenerateUUID(),
   304  		AllocID:     structs.GenerateUUID(),
   305  		CreationTTL: 86400,
   306  		Task:        "foo",
   307  	}
   308  }
   309  
   310  func Plan() *structs.Plan {
   311  	return &structs.Plan{
   312  		Priority: 50,
   313  	}
   314  }
   315  
   316  func PlanResult() *structs.PlanResult {
   317  	return &structs.PlanResult{}
   318  }