github.com/uchennaokeke444/nomad@v0.11.8/nomad/structs/testing.go (about)

     1  package structs
     2  
     3  import (
     4  	"fmt"
     5  	"time"
     6  
     7  	"github.com/hashicorp/nomad/helper/uuid"
     8  	psstructs "github.com/hashicorp/nomad/plugins/shared/structs"
     9  )
    10  
    11  // NodeResourcesToAllocatedResources converts a node resources to an allocated
    12  // resources. The task name used is "web" and network is omitted. This is
    13  // useful when trying to make an allocation fill an entire node.
    14  func NodeResourcesToAllocatedResources(n *NodeResources) *AllocatedResources {
    15  	if n == nil {
    16  		return nil
    17  	}
    18  
    19  	return &AllocatedResources{
    20  		Tasks: map[string]*AllocatedTaskResources{
    21  			"web": {
    22  				Cpu: AllocatedCpuResources{
    23  					CpuShares: n.Cpu.CpuShares,
    24  				},
    25  				Memory: AllocatedMemoryResources{
    26  					MemoryMB: n.Memory.MemoryMB,
    27  				},
    28  			},
    29  		},
    30  		Shared: AllocatedSharedResources{
    31  			DiskMB: n.Disk.DiskMB,
    32  		},
    33  	}
    34  }
    35  
    36  func MockNode() *Node {
    37  	node := &Node{
    38  		ID:         uuid.Generate(),
    39  		SecretID:   uuid.Generate(),
    40  		Datacenter: "dc1",
    41  		Name:       "foobar",
    42  		Attributes: map[string]string{
    43  			"kernel.name":        "linux",
    44  			"arch":               "x86",
    45  			"nomad.version":      "0.5.0",
    46  			"driver.exec":        "1",
    47  			"driver.mock_driver": "1",
    48  		},
    49  		NodeResources: &NodeResources{
    50  			Cpu: NodeCpuResources{
    51  				CpuShares: 4000,
    52  			},
    53  			Memory: NodeMemoryResources{
    54  				MemoryMB: 8192,
    55  			},
    56  			Disk: NodeDiskResources{
    57  				DiskMB: 100 * 1024,
    58  			},
    59  			Networks: []*NetworkResource{
    60  				{
    61  					Device: "eth0",
    62  					CIDR:   "192.168.0.100/32",
    63  					MBits:  1000,
    64  				},
    65  			},
    66  		},
    67  		ReservedResources: &NodeReservedResources{
    68  			Cpu: NodeReservedCpuResources{
    69  				CpuShares: 100,
    70  			},
    71  			Memory: NodeReservedMemoryResources{
    72  				MemoryMB: 256,
    73  			},
    74  			Disk: NodeReservedDiskResources{
    75  				DiskMB: 4 * 1024,
    76  			},
    77  			Networks: NodeReservedNetworkResources{
    78  				ReservedHostPorts: "22",
    79  			},
    80  		},
    81  		Links: map[string]string{
    82  			"consul": "foobar.dc1",
    83  		},
    84  		Meta: map[string]string{
    85  			"pci-dss":  "true",
    86  			"database": "mysql",
    87  			"version":  "5.6",
    88  		},
    89  		NodeClass:             "linux-medium-pci",
    90  		Status:                NodeStatusReady,
    91  		SchedulingEligibility: NodeSchedulingEligible,
    92  	}
    93  	node.ComputeClass()
    94  	return node
    95  }
    96  
    97  // NvidiaNode returns a node with two instances of an Nvidia GPU
    98  func MockNvidiaNode() *Node {
    99  	n := MockNode()
   100  	n.NodeResources.Devices = []*NodeDeviceResource{
   101  		{
   102  			Type:   "gpu",
   103  			Vendor: "nvidia",
   104  			Name:   "1080ti",
   105  			Attributes: map[string]*psstructs.Attribute{
   106  				"memory":           psstructs.NewIntAttribute(11, psstructs.UnitGiB),
   107  				"cuda_cores":       psstructs.NewIntAttribute(3584, ""),
   108  				"graphics_clock":   psstructs.NewIntAttribute(1480, psstructs.UnitMHz),
   109  				"memory_bandwidth": psstructs.NewIntAttribute(11, psstructs.UnitGBPerS),
   110  			},
   111  			Instances: []*NodeDevice{
   112  				{
   113  					ID:      uuid.Generate(),
   114  					Healthy: true,
   115  				},
   116  				{
   117  					ID:      uuid.Generate(),
   118  					Healthy: true,
   119  				},
   120  			},
   121  		},
   122  	}
   123  	n.ComputeClass()
   124  	return n
   125  }
   126  
   127  func MockJob() *Job {
   128  	job := &Job{
   129  		Region:      "global",
   130  		ID:          fmt.Sprintf("mock-service-%s", uuid.Generate()),
   131  		Name:        "my-job",
   132  		Namespace:   DefaultNamespace,
   133  		Type:        JobTypeService,
   134  		Priority:    50,
   135  		AllAtOnce:   false,
   136  		Datacenters: []string{"dc1"},
   137  		Constraints: []*Constraint{
   138  			{
   139  				LTarget: "${attr.kernel.name}",
   140  				RTarget: "linux",
   141  				Operand: "=",
   142  			},
   143  		},
   144  		TaskGroups: []*TaskGroup{
   145  			{
   146  				Name:  "web",
   147  				Count: 10,
   148  				EphemeralDisk: &EphemeralDisk{
   149  					SizeMB: 150,
   150  				},
   151  				RestartPolicy: &RestartPolicy{
   152  					Attempts: 3,
   153  					Interval: 10 * time.Minute,
   154  					Delay:    1 * time.Minute,
   155  					Mode:     RestartPolicyModeDelay,
   156  				},
   157  				ReschedulePolicy: &ReschedulePolicy{
   158  					Attempts:      2,
   159  					Interval:      10 * time.Minute,
   160  					Delay:         5 * time.Second,
   161  					DelayFunction: "constant",
   162  				},
   163  				Migrate: DefaultMigrateStrategy(),
   164  				Tasks: []*Task{
   165  					{
   166  						Name:   "web",
   167  						Driver: "exec",
   168  						Config: map[string]interface{}{
   169  							"command": "/bin/date",
   170  						},
   171  						Env: map[string]string{
   172  							"FOO": "bar",
   173  						},
   174  						Services: []*Service{
   175  							{
   176  								Name:      "${TASK}-frontend",
   177  								PortLabel: "http",
   178  								Tags:      []string{"pci:${meta.pci-dss}", "datacenter:${node.datacenter}"},
   179  								Checks: []*ServiceCheck{
   180  									{
   181  										Name:     "check-table",
   182  										Type:     ServiceCheckScript,
   183  										Command:  "/usr/local/check-table-${meta.database}",
   184  										Args:     []string{"${meta.version}"},
   185  										Interval: 30 * time.Second,
   186  										Timeout:  5 * time.Second,
   187  									},
   188  								},
   189  							},
   190  							{
   191  								Name:      "${TASK}-admin",
   192  								PortLabel: "admin",
   193  							},
   194  						},
   195  						LogConfig: DefaultLogConfig(),
   196  						Resources: &Resources{
   197  							CPU:      500,
   198  							MemoryMB: 256,
   199  							Networks: []*NetworkResource{
   200  								{
   201  									MBits: 50,
   202  									DynamicPorts: []Port{
   203  										{Label: "http"},
   204  										{Label: "admin"},
   205  									},
   206  								},
   207  							},
   208  						},
   209  						Meta: map[string]string{
   210  							"foo": "bar",
   211  						},
   212  					},
   213  				},
   214  				Meta: map[string]string{
   215  					"elb_check_type":     "http",
   216  					"elb_check_interval": "30s",
   217  					"elb_check_min":      "3",
   218  				},
   219  			},
   220  		},
   221  		Meta: map[string]string{
   222  			"owner": "armon",
   223  		},
   224  		Status:         JobStatusPending,
   225  		Version:        0,
   226  		CreateIndex:    42,
   227  		ModifyIndex:    99,
   228  		JobModifyIndex: 99,
   229  	}
   230  	job.Canonicalize()
   231  	return job
   232  }
   233  
   234  func MockAlloc() *Allocation {
   235  	alloc := &Allocation{
   236  		ID:        uuid.Generate(),
   237  		EvalID:    uuid.Generate(),
   238  		NodeID:    "12345678-abcd-efab-cdef-123456789abc",
   239  		Namespace: DefaultNamespace,
   240  		TaskGroup: "web",
   241  		AllocatedResources: &AllocatedResources{
   242  			Tasks: map[string]*AllocatedTaskResources{
   243  				"web": {
   244  					Cpu: AllocatedCpuResources{
   245  						CpuShares: 500,
   246  					},
   247  					Memory: AllocatedMemoryResources{
   248  						MemoryMB: 256,
   249  					},
   250  					Networks: []*NetworkResource{
   251  						{
   252  							Device:        "eth0",
   253  							IP:            "192.168.0.100",
   254  							ReservedPorts: []Port{{Label: "admin", Value: 5000}},
   255  							MBits:         50,
   256  							DynamicPorts:  []Port{{Label: "http", Value: 9876}},
   257  						},
   258  					},
   259  				},
   260  			},
   261  			Shared: AllocatedSharedResources{
   262  				DiskMB: 150,
   263  			},
   264  		},
   265  		Job:           MockJob(),
   266  		DesiredStatus: AllocDesiredStatusRun,
   267  		ClientStatus:  AllocClientStatusPending,
   268  	}
   269  	alloc.JobID = alloc.Job.ID
   270  	return alloc
   271  }