github.com/Ilhicas/nomad@v1.0.4-0.20210304152020-e86851182bc3/command/util_test.go (about)

     1  package command
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/hashicorp/nomad/api"
     7  	"github.com/hashicorp/nomad/command/agent"
     8  	"github.com/hashicorp/nomad/helper"
     9  )
    10  
    11  func testServer(t *testing.T, runClient bool, cb func(*agent.Config)) (*agent.TestAgent, *api.Client, string) {
    12  	// Make a new test server
    13  	a := agent.NewTestAgent(t, t.Name(), func(config *agent.Config) {
    14  		config.Client.Enabled = runClient
    15  
    16  		if cb != nil {
    17  			cb(config)
    18  		}
    19  	})
    20  	t.Cleanup(func() { a.Shutdown() })
    21  
    22  	c := a.Client()
    23  	return a, c, a.HTTPAddr()
    24  }
    25  
    26  func testJob(jobID string) *api.Job {
    27  	task := api.NewTask("task1", "mock_driver").
    28  		SetConfig("kill_after", "1s").
    29  		SetConfig("run_for", "5s").
    30  		SetConfig("exit_code", 0).
    31  		Require(&api.Resources{
    32  			MemoryMB: helper.IntToPtr(256),
    33  			CPU:      helper.IntToPtr(100),
    34  		}).
    35  		SetLogConfig(&api.LogConfig{
    36  			MaxFiles:      helper.IntToPtr(1),
    37  			MaxFileSizeMB: helper.IntToPtr(2),
    38  		})
    39  
    40  	group := api.NewTaskGroup("group1", 1).
    41  		AddTask(task).
    42  		RequireDisk(&api.EphemeralDisk{
    43  			SizeMB: helper.IntToPtr(20),
    44  		})
    45  
    46  	job := api.NewBatchJob(jobID, jobID, "global", 1).
    47  		AddDatacenter("dc1").
    48  		AddTaskGroup(group)
    49  
    50  	return job
    51  }
    52  
    53  func testMultiRegionJob(jobID, region, datacenter string) *api.Job {
    54  	task := api.NewTask("task1", "mock_driver").
    55  		SetConfig("kill_after", "10s").
    56  		SetConfig("run_for", "15s").
    57  		SetConfig("exit_code", 0).
    58  		Require(&api.Resources{
    59  			MemoryMB: helper.IntToPtr(256),
    60  			CPU:      helper.IntToPtr(100),
    61  		}).
    62  		SetLogConfig(&api.LogConfig{
    63  			MaxFiles:      helper.IntToPtr(1),
    64  			MaxFileSizeMB: helper.IntToPtr(2),
    65  		})
    66  
    67  	group := api.NewTaskGroup("group1", 1).
    68  		AddTask(task).
    69  		RequireDisk(&api.EphemeralDisk{
    70  			SizeMB: helper.IntToPtr(20),
    71  		})
    72  
    73  	job := api.NewServiceJob(jobID, jobID, region, 1).AddDatacenter(datacenter).AddTaskGroup(group)
    74  	job.Region = nil
    75  	job.Multiregion = &api.Multiregion{
    76  		Regions: []*api.MultiregionRegion{
    77  			{
    78  				Name:        "east",
    79  				Datacenters: []string{"east-1"},
    80  			},
    81  			{
    82  				Name:        "west",
    83  				Datacenters: []string{"west-1"},
    84  			},
    85  		},
    86  	}
    87  
    88  	return job
    89  }