github.com/blixtra/nomad@v0.7.2-0.20171221000451-da9a1d7bb050/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  		},
    23  		Resources: &structs.Resources{
    24  			CPU:      4000,
    25  			MemoryMB: 8192,
    26  			DiskMB:   100 * 1024,
    27  			IOPS:     150,
    28  			Networks: []*structs.NetworkResource{
    29  				{
    30  					Device: "eth0",
    31  					CIDR:   "192.168.0.100/32",
    32  					MBits:  1000,
    33  				},
    34  			},
    35  		},
    36  		Reserved: &structs.Resources{
    37  			CPU:      100,
    38  			MemoryMB: 256,
    39  			DiskMB:   4 * 1024,
    40  			Networks: []*structs.NetworkResource{
    41  				{
    42  					Device:        "eth0",
    43  					IP:            "192.168.0.100",
    44  					ReservedPorts: []structs.Port{{Label: "ssh", Value: 22}},
    45  					MBits:         1,
    46  				},
    47  			},
    48  		},
    49  		Links: map[string]string{
    50  			"consul": "foobar.dc1",
    51  		},
    52  		Meta: map[string]string{
    53  			"pci-dss":  "true",
    54  			"database": "mysql",
    55  			"version":  "5.6",
    56  		},
    57  		NodeClass: "linux-medium-pci",
    58  		Status:    structs.NodeStatusReady,
    59  	}
    60  	node.ComputeClass()
    61  	return node
    62  }
    63  
    64  func Job() *structs.Job {
    65  	job := &structs.Job{
    66  		Region:      "global",
    67  		ID:          uuid.Generate(),
    68  		Name:        "my-job",
    69  		Namespace:   structs.DefaultNamespace,
    70  		Type:        structs.JobTypeService,
    71  		Priority:    50,
    72  		AllAtOnce:   false,
    73  		Datacenters: []string{"dc1"},
    74  		Constraints: []*structs.Constraint{
    75  			{
    76  				LTarget: "${attr.kernel.name}",
    77  				RTarget: "linux",
    78  				Operand: "=",
    79  			},
    80  		},
    81  		TaskGroups: []*structs.TaskGroup{
    82  			{
    83  				Name:  "web",
    84  				Count: 10,
    85  				EphemeralDisk: &structs.EphemeralDisk{
    86  					SizeMB: 150,
    87  				},
    88  				RestartPolicy: &structs.RestartPolicy{
    89  					Attempts: 3,
    90  					Interval: 10 * time.Minute,
    91  					Delay:    1 * time.Minute,
    92  					Mode:     structs.RestartPolicyModeDelay,
    93  				},
    94  				Tasks: []*structs.Task{
    95  					{
    96  						Name:   "web",
    97  						Driver: "exec",
    98  						Config: map[string]interface{}{
    99  							"command": "/bin/date",
   100  						},
   101  						Env: map[string]string{
   102  							"FOO": "bar",
   103  						},
   104  						Services: []*structs.Service{
   105  							{
   106  								Name:      "${TASK}-frontend",
   107  								PortLabel: "http",
   108  								Tags:      []string{"pci:${meta.pci-dss}", "datacenter:${node.datacenter}"},
   109  								Checks: []*structs.ServiceCheck{
   110  									{
   111  										Name:     "check-table",
   112  										Type:     structs.ServiceCheckScript,
   113  										Command:  "/usr/local/check-table-${meta.database}",
   114  										Args:     []string{"${meta.version}"},
   115  										Interval: 30 * time.Second,
   116  										Timeout:  5 * time.Second,
   117  									},
   118  								},
   119  							},
   120  							{
   121  								Name:      "${TASK}-admin",
   122  								PortLabel: "admin",
   123  							},
   124  						},
   125  						LogConfig: structs.DefaultLogConfig(),
   126  						Resources: &structs.Resources{
   127  							CPU:      500,
   128  							MemoryMB: 256,
   129  							Networks: []*structs.NetworkResource{
   130  								{
   131  									MBits: 50,
   132  									DynamicPorts: []structs.Port{
   133  										{Label: "http"},
   134  										{Label: "admin"},
   135  									},
   136  								},
   137  							},
   138  						},
   139  						Meta: map[string]string{
   140  							"foo": "bar",
   141  						},
   142  					},
   143  				},
   144  				Meta: map[string]string{
   145  					"elb_check_type":     "http",
   146  					"elb_check_interval": "30s",
   147  					"elb_check_min":      "3",
   148  				},
   149  			},
   150  		},
   151  		Meta: map[string]string{
   152  			"owner": "armon",
   153  		},
   154  		Status:         structs.JobStatusPending,
   155  		Version:        0,
   156  		CreateIndex:    42,
   157  		ModifyIndex:    99,
   158  		JobModifyIndex: 99,
   159  	}
   160  	job.Canonicalize()
   161  	return job
   162  }
   163  
   164  func SystemJob() *structs.Job {
   165  	job := &structs.Job{
   166  		Region:      "global",
   167  		Namespace:   structs.DefaultNamespace,
   168  		ID:          uuid.Generate(),
   169  		Name:        "my-job",
   170  		Type:        structs.JobTypeSystem,
   171  		Priority:    100,
   172  		AllAtOnce:   false,
   173  		Datacenters: []string{"dc1"},
   174  		Constraints: []*structs.Constraint{
   175  			{
   176  				LTarget: "${attr.kernel.name}",
   177  				RTarget: "linux",
   178  				Operand: "=",
   179  			},
   180  		},
   181  		TaskGroups: []*structs.TaskGroup{
   182  			{
   183  				Name:  "web",
   184  				Count: 1,
   185  				RestartPolicy: &structs.RestartPolicy{
   186  					Attempts: 3,
   187  					Interval: 10 * time.Minute,
   188  					Delay:    1 * time.Minute,
   189  					Mode:     structs.RestartPolicyModeDelay,
   190  				},
   191  				EphemeralDisk: structs.DefaultEphemeralDisk(),
   192  				Tasks: []*structs.Task{
   193  					{
   194  						Name:   "web",
   195  						Driver: "exec",
   196  						Config: map[string]interface{}{
   197  							"command": "/bin/date",
   198  						},
   199  						Env: map[string]string{},
   200  						Resources: &structs.Resources{
   201  							CPU:      500,
   202  							MemoryMB: 256,
   203  							Networks: []*structs.NetworkResource{
   204  								{
   205  									MBits:        50,
   206  									DynamicPorts: []structs.Port{{Label: "http"}},
   207  								},
   208  							},
   209  						},
   210  						LogConfig: structs.DefaultLogConfig(),
   211  					},
   212  				},
   213  			},
   214  		},
   215  		Meta: map[string]string{
   216  			"owner": "armon",
   217  		},
   218  		Status:      structs.JobStatusPending,
   219  		CreateIndex: 42,
   220  		ModifyIndex: 99,
   221  	}
   222  	job.Canonicalize()
   223  	return job
   224  }
   225  
   226  func PeriodicJob() *structs.Job {
   227  	job := Job()
   228  	job.Type = structs.JobTypeBatch
   229  	job.Periodic = &structs.PeriodicConfig{
   230  		Enabled:  true,
   231  		SpecType: structs.PeriodicSpecCron,
   232  		Spec:     "*/30 * * * *",
   233  	}
   234  	job.Status = structs.JobStatusRunning
   235  	return job
   236  }
   237  
   238  func Eval() *structs.Evaluation {
   239  	eval := &structs.Evaluation{
   240  		ID:        uuid.Generate(),
   241  		Namespace: structs.DefaultNamespace,
   242  		Priority:  50,
   243  		Type:      structs.JobTypeService,
   244  		JobID:     uuid.Generate(),
   245  		Status:    structs.EvalStatusPending,
   246  	}
   247  	return eval
   248  }
   249  
   250  func JobSummary(jobID string) *structs.JobSummary {
   251  	js := &structs.JobSummary{
   252  		JobID:     jobID,
   253  		Namespace: structs.DefaultNamespace,
   254  		Summary: map[string]structs.TaskGroupSummary{
   255  			"web": {
   256  				Queued:   0,
   257  				Starting: 0,
   258  			},
   259  		},
   260  	}
   261  	return js
   262  }
   263  
   264  func Alloc() *structs.Allocation {
   265  	alloc := &structs.Allocation{
   266  		ID:        uuid.Generate(),
   267  		EvalID:    uuid.Generate(),
   268  		NodeID:    "12345678-abcd-efab-cdef-123456789abc",
   269  		Namespace: structs.DefaultNamespace,
   270  		TaskGroup: "web",
   271  		Resources: &structs.Resources{
   272  			CPU:      500,
   273  			MemoryMB: 256,
   274  			DiskMB:   150,
   275  			Networks: []*structs.NetworkResource{
   276  				{
   277  					Device:        "eth0",
   278  					IP:            "192.168.0.100",
   279  					ReservedPorts: []structs.Port{{Label: "admin", Value: 5000}},
   280  					MBits:         50,
   281  					DynamicPorts:  []structs.Port{{Label: "http"}},
   282  				},
   283  			},
   284  		},
   285  		TaskResources: map[string]*structs.Resources{
   286  			"web": {
   287  				CPU:      500,
   288  				MemoryMB: 256,
   289  				Networks: []*structs.NetworkResource{
   290  					{
   291  						Device:        "eth0",
   292  						IP:            "192.168.0.100",
   293  						ReservedPorts: []structs.Port{{Label: "admin", Value: 5000}},
   294  						MBits:         50,
   295  						DynamicPorts:  []structs.Port{{Label: "http", Value: 9876}},
   296  					},
   297  				},
   298  			},
   299  		},
   300  		SharedResources: &structs.Resources{
   301  			DiskMB: 150,
   302  		},
   303  		Job:           Job(),
   304  		DesiredStatus: structs.AllocDesiredStatusRun,
   305  		ClientStatus:  structs.AllocClientStatusPending,
   306  	}
   307  	alloc.JobID = alloc.Job.ID
   308  	return alloc
   309  }
   310  
   311  func VaultAccessor() *structs.VaultAccessor {
   312  	return &structs.VaultAccessor{
   313  		Accessor:    uuid.Generate(),
   314  		NodeID:      uuid.Generate(),
   315  		AllocID:     uuid.Generate(),
   316  		CreationTTL: 86400,
   317  		Task:        "foo",
   318  	}
   319  }
   320  
   321  func Deployment() *structs.Deployment {
   322  	return &structs.Deployment{
   323  		ID:             uuid.Generate(),
   324  		JobID:          uuid.Generate(),
   325  		Namespace:      structs.DefaultNamespace,
   326  		JobVersion:     2,
   327  		JobModifyIndex: 20,
   328  		JobCreateIndex: 18,
   329  		TaskGroups: map[string]*structs.DeploymentState{
   330  			"web": {
   331  				DesiredTotal: 10,
   332  			},
   333  		},
   334  		Status:            structs.DeploymentStatusRunning,
   335  		StatusDescription: structs.DeploymentStatusDescriptionRunning,
   336  		ModifyIndex:       23,
   337  		CreateIndex:       21,
   338  	}
   339  }
   340  
   341  func Plan() *structs.Plan {
   342  	return &structs.Plan{
   343  		Priority: 50,
   344  	}
   345  }
   346  
   347  func PlanResult() *structs.PlanResult {
   348  	return &structs.PlanResult{}
   349  }
   350  
   351  func ACLPolicy() *structs.ACLPolicy {
   352  	ap := &structs.ACLPolicy{
   353  		Name:        fmt.Sprintf("policy-%s", uuid.Generate()),
   354  		Description: "Super cool policy!",
   355  		Rules: `
   356  		namespace "default" {
   357  			policy = "write"
   358  		}
   359  		node {
   360  			policy = "read"
   361  		}
   362  		agent {
   363  			policy = "read"
   364  		}
   365  		`,
   366  		CreateIndex: 10,
   367  		ModifyIndex: 20,
   368  	}
   369  	ap.SetHash()
   370  	return ap
   371  }
   372  
   373  func ACLToken() *structs.ACLToken {
   374  	tk := &structs.ACLToken{
   375  		AccessorID:  uuid.Generate(),
   376  		SecretID:    uuid.Generate(),
   377  		Name:        "my cool token " + uuid.Generate(),
   378  		Type:        "client",
   379  		Policies:    []string{"foo", "bar"},
   380  		Global:      false,
   381  		CreateTime:  time.Now().UTC(),
   382  		CreateIndex: 10,
   383  		ModifyIndex: 20,
   384  	}
   385  	tk.SetHash()
   386  	return tk
   387  }
   388  
   389  func ACLManagementToken() *structs.ACLToken {
   390  	return &structs.ACLToken{
   391  		AccessorID:  uuid.Generate(),
   392  		SecretID:    uuid.Generate(),
   393  		Name:        "management " + uuid.Generate(),
   394  		Type:        "management",
   395  		Global:      true,
   396  		CreateTime:  time.Now().UTC(),
   397  		CreateIndex: 10,
   398  		ModifyIndex: 20,
   399  	}
   400  }