2015-09-11 18:10:20 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2021-10-13 21:26:56 +00:00
|
|
|
"os"
|
2015-09-11 18:10:20 +00:00
|
|
|
"testing"
|
|
|
|
|
2015-09-12 21:50:05 +00:00
|
|
|
"github.com/hashicorp/nomad/api"
|
2017-07-21 04:07:32 +00:00
|
|
|
"github.com/hashicorp/nomad/command/agent"
|
2017-02-13 23:18:17 +00:00
|
|
|
"github.com/hashicorp/nomad/helper"
|
2021-10-12 20:58:41 +00:00
|
|
|
"github.com/hashicorp/nomad/testutil"
|
2015-09-11 18:10:20 +00:00
|
|
|
)
|
|
|
|
|
2017-07-21 04:07:32 +00:00
|
|
|
func testServer(t *testing.T, runClient bool, cb func(*agent.Config)) (*agent.TestAgent, *api.Client, string) {
|
2015-09-12 23:12:56 +00:00
|
|
|
// Make a new test server
|
2017-10-19 04:45:18 +00:00
|
|
|
a := agent.NewTestAgent(t, t.Name(), func(config *agent.Config) {
|
2017-07-21 04:07:32 +00:00
|
|
|
config.Client.Enabled = runClient
|
|
|
|
|
|
|
|
if cb != nil {
|
|
|
|
cb(config)
|
|
|
|
}
|
|
|
|
})
|
2020-06-25 16:44:19 +00:00
|
|
|
t.Cleanup(func() { a.Shutdown() })
|
2015-09-12 21:50:05 +00:00
|
|
|
|
2017-07-21 04:07:32 +00:00
|
|
|
c := a.Client()
|
|
|
|
return a, c, a.HTTPAddr()
|
2015-09-11 18:10:20 +00:00
|
|
|
}
|
2015-09-16 18:42:28 +00:00
|
|
|
|
2021-10-12 20:58:41 +00:00
|
|
|
// testClient starts a new test client, blocks until it joins, and performs
|
|
|
|
// cleanup after the test is complete.
|
|
|
|
func testClient(t *testing.T, name string, cb func(*agent.Config)) (*agent.TestAgent, *api.Client, string) {
|
|
|
|
t.Logf("[TEST] Starting client agent %s", name)
|
|
|
|
a := agent.NewTestAgent(t, name, func(config *agent.Config) {
|
|
|
|
if cb != nil {
|
|
|
|
cb(config)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
t.Cleanup(func() { a.Shutdown() })
|
|
|
|
|
|
|
|
c := a.Client()
|
|
|
|
t.Logf("[TEST] Waiting for client %s to join server(s) %s", name, a.GetConfig().Client.Servers)
|
|
|
|
testutil.WaitForClient(t, a.Agent.RPC, a.Agent.Client().NodeID(), a.Agent.Client().Region())
|
|
|
|
|
|
|
|
return a, c, a.HTTPAddr()
|
|
|
|
}
|
|
|
|
|
2015-09-16 18:42:28 +00:00
|
|
|
func testJob(jobID string) *api.Job {
|
2016-08-22 16:35:25 +00:00
|
|
|
task := api.NewTask("task1", "mock_driver").
|
|
|
|
SetConfig("kill_after", "1s").
|
|
|
|
SetConfig("run_for", "5s").
|
|
|
|
SetConfig("exit_code", 0).
|
2016-02-02 21:50:30 +00:00
|
|
|
Require(&api.Resources{
|
2017-02-13 23:18:17 +00:00
|
|
|
MemoryMB: helper.IntToPtr(256),
|
|
|
|
CPU: helper.IntToPtr(100),
|
2016-02-19 23:49:32 +00:00
|
|
|
}).
|
2016-02-11 18:42:56 +00:00
|
|
|
SetLogConfig(&api.LogConfig{
|
2017-02-13 23:18:17 +00:00
|
|
|
MaxFiles: helper.IntToPtr(1),
|
|
|
|
MaxFileSizeMB: helper.IntToPtr(2),
|
2016-02-19 23:49:32 +00:00
|
|
|
})
|
2015-09-16 18:42:28 +00:00
|
|
|
|
|
|
|
group := api.NewTaskGroup("group1", 1).
|
2016-08-26 04:05:21 +00:00
|
|
|
AddTask(task).
|
2016-09-14 22:43:42 +00:00
|
|
|
RequireDisk(&api.EphemeralDisk{
|
2017-02-13 23:18:17 +00:00
|
|
|
SizeMB: helper.IntToPtr(20),
|
2016-08-26 04:05:21 +00:00
|
|
|
})
|
2015-09-16 18:42:28 +00:00
|
|
|
|
2019-05-02 20:00:21 +00:00
|
|
|
job := api.NewBatchJob(jobID, jobID, "global", 1).
|
2015-09-16 18:42:28 +00:00
|
|
|
AddDatacenter("dc1").
|
|
|
|
AddTaskGroup(group)
|
|
|
|
|
|
|
|
return job
|
|
|
|
}
|
2020-06-15 14:05:31 +00:00
|
|
|
|
|
|
|
func testMultiRegionJob(jobID, region, datacenter string) *api.Job {
|
|
|
|
task := api.NewTask("task1", "mock_driver").
|
|
|
|
SetConfig("kill_after", "10s").
|
|
|
|
SetConfig("run_for", "15s").
|
|
|
|
SetConfig("exit_code", 0).
|
|
|
|
Require(&api.Resources{
|
|
|
|
MemoryMB: helper.IntToPtr(256),
|
|
|
|
CPU: helper.IntToPtr(100),
|
|
|
|
}).
|
|
|
|
SetLogConfig(&api.LogConfig{
|
|
|
|
MaxFiles: helper.IntToPtr(1),
|
|
|
|
MaxFileSizeMB: helper.IntToPtr(2),
|
|
|
|
})
|
|
|
|
|
|
|
|
group := api.NewTaskGroup("group1", 1).
|
|
|
|
AddTask(task).
|
|
|
|
RequireDisk(&api.EphemeralDisk{
|
|
|
|
SizeMB: helper.IntToPtr(20),
|
|
|
|
})
|
|
|
|
|
|
|
|
job := api.NewServiceJob(jobID, jobID, region, 1).AddDatacenter(datacenter).AddTaskGroup(group)
|
2020-06-15 20:18:14 +00:00
|
|
|
job.Region = nil
|
2020-06-15 14:05:31 +00:00
|
|
|
job.Multiregion = &api.Multiregion{
|
|
|
|
Regions: []*api.MultiregionRegion{
|
|
|
|
{
|
|
|
|
Name: "east",
|
|
|
|
Datacenters: []string{"east-1"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "west",
|
|
|
|
Datacenters: []string{"west-1"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
return job
|
|
|
|
}
|
2021-10-13 21:26:56 +00:00
|
|
|
|
|
|
|
// setEnv wraps os.Setenv(key, value) and restores the environment variable to initial value in test cleanup
|
|
|
|
func setEnv(t *testing.T, key, value string) {
|
|
|
|
initial, ok := os.LookupEnv(key)
|
|
|
|
os.Setenv(key, value)
|
|
|
|
|
|
|
|
t.Cleanup(func() {
|
|
|
|
if ok {
|
|
|
|
os.Setenv(key, initial)
|
|
|
|
} else {
|
|
|
|
os.Unsetenv(key)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|