github.com/ryanslade/nomad@v0.2.4-0.20160128061903-fc95782f2089/nomad/mock/mock.go (about)

     1  package mock
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/hashicorp/nomad/nomad/structs"
     7  )
     8  
     9  func Node() *structs.Node {
    10  	node := &structs.Node{
    11  		ID:         structs.GenerateUUID(),
    12  		Datacenter: "dc1",
    13  		Name:       "foobar",
    14  		Attributes: map[string]string{
    15  			"kernel.name": "linux",
    16  			"arch":        "x86",
    17  			"version":     "0.1.0",
    18  			"driver.exec": "1",
    19  		},
    20  		Resources: &structs.Resources{
    21  			CPU:      4000,
    22  			MemoryMB: 8192,
    23  			DiskMB:   100 * 1024,
    24  			IOPS:     150,
    25  			Networks: []*structs.NetworkResource{
    26  				&structs.NetworkResource{
    27  					Device: "eth0",
    28  					CIDR:   "192.168.0.100/32",
    29  					MBits:  1000,
    30  				},
    31  			},
    32  		},
    33  		Reserved: &structs.Resources{
    34  			CPU:      100,
    35  			MemoryMB: 256,
    36  			DiskMB:   4 * 1024,
    37  			Networks: []*structs.NetworkResource{
    38  				&structs.NetworkResource{
    39  					Device:        "eth0",
    40  					IP:            "192.168.0.100",
    41  					ReservedPorts: []structs.Port{{Label: "main", Value: 22}},
    42  					MBits:         1,
    43  				},
    44  			},
    45  		},
    46  		Links: map[string]string{
    47  			"consul": "foobar.dc1",
    48  		},
    49  		Meta: map[string]string{
    50  			"pci-dss": "true",
    51  		},
    52  		NodeClass: "linux-medium-pci",
    53  		Status:    structs.NodeStatusReady,
    54  	}
    55  	node.ComputeClass()
    56  	return node
    57  }
    58  
    59  func Job() *structs.Job {
    60  	job := &structs.Job{
    61  		Region:      "global",
    62  		ID:          structs.GenerateUUID(),
    63  		Name:        "my-job",
    64  		Type:        structs.JobTypeService,
    65  		Priority:    50,
    66  		AllAtOnce:   false,
    67  		Datacenters: []string{"dc1"},
    68  		Constraints: []*structs.Constraint{
    69  			&structs.Constraint{
    70  				LTarget: "$attr.kernel.name",
    71  				RTarget: "linux",
    72  				Operand: "=",
    73  			},
    74  		},
    75  		TaskGroups: []*structs.TaskGroup{
    76  			&structs.TaskGroup{
    77  				Name:  "web",
    78  				Count: 10,
    79  				RestartPolicy: &structs.RestartPolicy{
    80  					Attempts:         3,
    81  					Interval:         10 * time.Minute,
    82  					Delay:            1 * time.Minute,
    83  					RestartOnSuccess: true,
    84  					Mode:             structs.RestartPolicyModeDelay,
    85  				},
    86  				Tasks: []*structs.Task{
    87  					&structs.Task{
    88  						Name:   "web",
    89  						Driver: "exec",
    90  						Config: map[string]interface{}{
    91  							"command": "/bin/date",
    92  						},
    93  						Env: map[string]string{
    94  							"FOO": "bar",
    95  						},
    96  						Services: []*structs.Service{
    97  							{
    98  								Name:      "${TASK}-frontend",
    99  								PortLabel: "http",
   100  							},
   101  							{
   102  								Name:      "${TASK}-admin",
   103  								PortLabel: "admin",
   104  							},
   105  						},
   106  						Resources: &structs.Resources{
   107  							CPU:      500,
   108  							MemoryMB: 256,
   109  							Networks: []*structs.NetworkResource{
   110  								&structs.NetworkResource{
   111  									MBits:        50,
   112  									DynamicPorts: []structs.Port{{Label: "http"}, {Label: "admin"}},
   113  								},
   114  							},
   115  						},
   116  					},
   117  				},
   118  				Meta: map[string]string{
   119  					"elb_check_type":     "http",
   120  					"elb_check_interval": "30s",
   121  					"elb_check_min":      "3",
   122  				},
   123  			},
   124  		},
   125  		Meta: map[string]string{
   126  			"owner": "armon",
   127  		},
   128  		Status:         structs.JobStatusPending,
   129  		CreateIndex:    42,
   130  		ModifyIndex:    99,
   131  		JobModifyIndex: 99,
   132  	}
   133  	job.InitFields()
   134  	return job
   135  }
   136  
   137  func SystemJob() *structs.Job {
   138  	job := &structs.Job{
   139  		Region:      "global",
   140  		ID:          structs.GenerateUUID(),
   141  		Name:        "my-job",
   142  		Type:        structs.JobTypeSystem,
   143  		Priority:    100,
   144  		AllAtOnce:   false,
   145  		Datacenters: []string{"dc1"},
   146  		Constraints: []*structs.Constraint{
   147  			&structs.Constraint{
   148  				LTarget: "$attr.kernel.name",
   149  				RTarget: "linux",
   150  				Operand: "=",
   151  			},
   152  		},
   153  		TaskGroups: []*structs.TaskGroup{
   154  			&structs.TaskGroup{
   155  				Name:  "web",
   156  				Count: 1,
   157  				RestartPolicy: &structs.RestartPolicy{
   158  					Attempts:         3,
   159  					Interval:         10 * time.Minute,
   160  					Delay:            1 * time.Minute,
   161  					RestartOnSuccess: true,
   162  					Mode:             structs.RestartPolicyModeDelay,
   163  				},
   164  				Tasks: []*structs.Task{
   165  					&structs.Task{
   166  						Name:   "web",
   167  						Driver: "exec",
   168  						Config: map[string]interface{}{
   169  							"command": "/bin/date",
   170  						},
   171  						Resources: &structs.Resources{
   172  							CPU:      500,
   173  							MemoryMB: 256,
   174  							Networks: []*structs.NetworkResource{
   175  								&structs.NetworkResource{
   176  									MBits:        50,
   177  									DynamicPorts: []structs.Port{{Label: "http"}},
   178  								},
   179  							},
   180  						},
   181  					},
   182  				},
   183  			},
   184  		},
   185  		Meta: map[string]string{
   186  			"owner": "armon",
   187  		},
   188  		Status:      structs.JobStatusPending,
   189  		CreateIndex: 42,
   190  		ModifyIndex: 99,
   191  	}
   192  	return job
   193  }
   194  
   195  func PeriodicJob() *structs.Job {
   196  	job := Job()
   197  	job.Type = structs.JobTypeBatch
   198  	job.Periodic = &structs.PeriodicConfig{
   199  		Enabled:  true,
   200  		SpecType: structs.PeriodicSpecCron,
   201  		Spec:     "*/30 * * * *",
   202  	}
   203  	return job
   204  }
   205  
   206  func Eval() *structs.Evaluation {
   207  	eval := &structs.Evaluation{
   208  		ID:       structs.GenerateUUID(),
   209  		Priority: 50,
   210  		Type:     structs.JobTypeService,
   211  		JobID:    structs.GenerateUUID(),
   212  		Status:   structs.EvalStatusPending,
   213  	}
   214  	return eval
   215  }
   216  
   217  func Alloc() *structs.Allocation {
   218  	alloc := &structs.Allocation{
   219  		ID:        structs.GenerateUUID(),
   220  		EvalID:    structs.GenerateUUID(),
   221  		NodeID:    "12345678-abcd-efab-cdef-123456789abc",
   222  		TaskGroup: "web",
   223  		Resources: &structs.Resources{
   224  			CPU:      500,
   225  			MemoryMB: 256,
   226  			Networks: []*structs.NetworkResource{
   227  				&structs.NetworkResource{
   228  					Device:        "eth0",
   229  					IP:            "192.168.0.100",
   230  					ReservedPorts: []structs.Port{{Label: "main", Value: 12345}},
   231  					MBits:         100,
   232  					DynamicPorts:  []structs.Port{{Label: "http"}},
   233  				},
   234  			},
   235  		},
   236  		TaskResources: map[string]*structs.Resources{
   237  			"web": &structs.Resources{
   238  				CPU:      500,
   239  				MemoryMB: 256,
   240  				Networks: []*structs.NetworkResource{
   241  					&structs.NetworkResource{
   242  						Device:        "eth0",
   243  						IP:            "192.168.0.100",
   244  						ReservedPorts: []structs.Port{{Label: "main", Value: 5000}},
   245  						MBits:         50,
   246  						DynamicPorts:  []structs.Port{{Label: "http"}},
   247  					},
   248  				},
   249  			},
   250  		},
   251  		TaskStates: map[string]*structs.TaskState{
   252  			"web": &structs.TaskState{
   253  				State: structs.TaskStatePending,
   254  			},
   255  		},
   256  		Job:           Job(),
   257  		DesiredStatus: structs.AllocDesiredStatusRun,
   258  		ClientStatus:  structs.AllocClientStatusPending,
   259  	}
   260  	alloc.JobID = alloc.Job.ID
   261  	return alloc
   262  }
   263  
   264  func Plan() *structs.Plan {
   265  	return &structs.Plan{
   266  		Priority: 50,
   267  	}
   268  }
   269  
   270  func PlanResult() *structs.PlanResult {
   271  	return &structs.PlanResult{}
   272  }