From f9295631c47a38ec8009742352182ed2d51f89ba Mon Sep 17 00:00:00 2001 From: Mahmood Ali Date: Tue, 6 Nov 2018 09:04:07 -0800 Subject: [PATCH] Set clean config for mock driver The default job here contains some exec task config (for setting command and args) that aren't used for mock driver. Now, the alloc runner seems stricter about validating fields and errors on unexpected fields. Updating configs in tests so we can have an explicit task config whenever driver is set explicitly. --- .../alloc_runner_test.go | 46 ++++++++++++++----- .../taskrunner/task_runner_test.go | 4 +- client/allocrunnerdeprecated/testing.go | 4 +- client/allocwatcher/alloc_watcher_test.go | 4 +- client/client_test.go | 5 +- client/fs_endpoint_test.go | 8 +++- client/gc_test.go | 4 +- command/agent/alloc_endpoint_test.go | 4 +- 8 files changed, 58 insertions(+), 21 deletions(-) diff --git a/client/allocrunnerdeprecated/alloc_runner_test.go b/client/allocrunnerdeprecated/alloc_runner_test.go index 7b4c56bb4..4cd665ea1 100644 --- a/client/allocrunnerdeprecated/alloc_runner_test.go +++ b/client/allocrunnerdeprecated/alloc_runner_test.go @@ -157,8 +157,10 @@ func TestAllocRunner_DeploymentHealth_Unhealthy_Deadline(t *testing.T) { // Make the task block task := ar.alloc.Job.TaskGroups[0].Tasks[0] task.Driver = "mock_driver" - task.Config["start_block_for"] = "4s" - task.Config["run_for"] = "10s" + task.Config = map[string]interface{}{ + "start_block_for": "4s", + "run_for": "10s", + } // Make the alloc be part of a deployment ar.alloc.DeploymentID = uuid.Generate() @@ -216,7 +218,9 @@ func TestAllocRunner_DeploymentHealth_Healthy_NoChecks(t *testing.T) { // Make the task run healthy task := ar.alloc.Job.TaskGroups[0].Tasks[0] task.Driver = "mock_driver" - task.Config["run_for"] = "10s" + task.Config = map[string]interface{}{ + "run_for": "10s", + } // Create a task that takes longer to become healthy ar.alloc.Job.TaskGroups[0].Tasks = append(ar.alloc.Job.TaskGroups[0].Tasks, task.Copy()) @@ -264,7 +268,9 @@ func TestAllocRunner_DeploymentHealth_Healthy_Checks(t *testing.T) { // Make the task fail task := ar.alloc.Job.TaskGroups[0].Tasks[0] task.Driver = "mock_driver" - task.Config["run_for"] = "10s" + task.Config = map[string]interface{}{ + "run_for": "10s", + } // Create a task that has no checks ar.alloc.Job.TaskGroups[0].Tasks = append(ar.alloc.Job.TaskGroups[0].Tasks, task.Copy()) @@ -357,7 +363,9 @@ func TestAllocRunner_DeploymentHealth_Unhealthy_Checks(t *testing.T) { // Make the task fail task := ar.alloc.Job.TaskGroups[0].Tasks[0] task.Driver = "mock_driver" - task.Config["run_for"] = "10s" + task.Config = map[string]interface{}{ + "run_for": "10s", + } // Make the alloc be part of a deployment ar.alloc.DeploymentID = uuid.Generate() @@ -426,7 +434,9 @@ func TestAllocRunner_DeploymentHealth_Healthy_UpdatedDeployment(t *testing.T) { // Make the task run healthy task := ar.alloc.Job.TaskGroups[0].Tasks[0] task.Driver = "mock_driver" - task.Config["run_for"] = "30s" + task.Config = map[string]interface{}{ + "run_for": "30s", + } // Make the alloc be part of a deployment ar.alloc.DeploymentID = uuid.Generate() @@ -484,7 +494,9 @@ func TestAllocRunner_DeploymentHealth_Healthy_Migration(t *testing.T) { tg := ar.alloc.Job.TaskGroups[0] task := tg.Tasks[0] task.Driver = "mock_driver" - task.Config["run_for"] = "30s" + task.Config = map[string]interface{}{ + "run_for": "30s", + } // Shorten the default migration healthy time tg.Migrate = structs.DefaultMigrateStrategy() @@ -530,7 +542,9 @@ func TestAllocRunner_DeploymentHealth_BatchDisabled(t *testing.T) { task := tg.Tasks[0] task.Driver = "mock_driver" - task.Config["run_for"] = "5s" + task.Config = map[string]interface{}{ + "run_for": "5s", + } upd, ar := TestAllocRunnerFromAlloc(t, alloc, false) go ar.Run() @@ -621,7 +635,9 @@ func TestAllocRunner_TerminalUpdate_Destroy(t *testing.T) { // Ensure task takes some time task := ar.alloc.Job.TaskGroups[0].Tasks[0] task.Driver = "mock_driver" - task.Config["run_for"] = "10s" + task.Config = map[string]interface{}{ + "run_for": "10s", + } go ar.Run() testutil.WaitForResult(func() (bool, error) { @@ -720,7 +736,9 @@ func TestAllocRunner_Destroy(t *testing.T) { // Ensure task takes some time task := ar.alloc.Job.TaskGroups[0].Tasks[0] task.Driver = "mock_driver" - task.Config["run_for"] = "10s" + task.Config = map[string]interface{}{ + "run_for": "10s", + } go ar.Run() start := time.Now() @@ -779,7 +797,9 @@ func TestAllocRunner_Update(t *testing.T) { // Ensure task takes some time task := ar.alloc.Job.TaskGroups[0].Tasks[0] task.Driver = "mock_driver" - task.Config["run_for"] = "10s" + task.Config = map[string]interface{}{ + "run_for": "10s", + } go ar.Run() defer ar.Destroy() @@ -880,7 +900,9 @@ func TestAllocRunner_SaveRestoreState_TerminalAlloc(t *testing.T) { // Ensure task takes some time ar.alloc.Job.TaskGroups[0].Tasks[0].Driver = "mock_driver" task := ar.alloc.Job.TaskGroups[0].Tasks[0] - task.Config["run_for"] = "10s" + task.Config = map[string]interface{}{ + "run_for": "10s", + } go ar.Run() defer ar.Destroy() diff --git a/client/allocrunnerdeprecated/taskrunner/task_runner_test.go b/client/allocrunnerdeprecated/taskrunner/task_runner_test.go index 8bd03976b..526475ebf 100644 --- a/client/allocrunnerdeprecated/taskrunner/task_runner_test.go +++ b/client/allocrunnerdeprecated/taskrunner/task_runner_test.go @@ -86,7 +86,9 @@ func testTaskRunner(t *testing.T, restarts bool) *taskRunnerTestCtx { alloc := mock.Alloc() task := alloc.Job.TaskGroups[0].Tasks[0] task.Driver = "mock_driver" - task.Config["run_for"] = "500ms" + task.Config = map[string]interface{}{ + "run_for": "500ms", + } return testTaskRunnerFromAlloc(t, restarts, alloc) } diff --git a/client/allocrunnerdeprecated/testing.go b/client/allocrunnerdeprecated/testing.go index 87c0eaae7..92a3b56d3 100644 --- a/client/allocrunnerdeprecated/testing.go +++ b/client/allocrunnerdeprecated/testing.go @@ -63,6 +63,8 @@ func TestAllocRunner(t *testing.T, restarts bool) (*MockAllocStateUpdater, *Allo alloc := mock.Alloc() task := alloc.Job.TaskGroups[0].Tasks[0] task.Driver = "mock_driver" - task.Config["run_for"] = "500ms" + task.Config = map[string]interface{}{ + "run_for": "500ms", + } return TestAllocRunnerFromAlloc(t, alloc, restarts) } diff --git a/client/allocwatcher/alloc_watcher_test.go b/client/allocwatcher/alloc_watcher_test.go index 6eb7bcc3d..44a48fb94 100644 --- a/client/allocwatcher/alloc_watcher_test.go +++ b/client/allocwatcher/alloc_watcher_test.go @@ -123,7 +123,9 @@ func TestPrevAlloc_LocalPrevAlloc(t *testing.T) { defer cleanup() conf.Alloc.Job.TaskGroups[0].Tasks[0].Driver = "mock_driver" - conf.Alloc.Job.TaskGroups[0].Tasks[0].Config["run_for"] = "500ms" + conf.Alloc.Job.TaskGroups[0].Tasks[0].Config = map[string]interface{}{ + "run_for": "500ms", + } waiter := NewAllocWatcher(conf) diff --git a/client/client_test.go b/client/client_test.go index 58020ea6f..2b203094c 100644 --- a/client/client_test.go +++ b/client/client_test.go @@ -570,8 +570,9 @@ func TestClient_SaveRestoreState(t *testing.T) { alloc1.Job = job alloc1.JobID = job.ID alloc1.Job.TaskGroups[0].Tasks[0].Driver = "mock_driver" - task := alloc1.Job.TaskGroups[0].Tasks[0] - task.Config["run_for"] = "10s" + alloc1.Job.TaskGroups[0].Tasks[0].Config = map[string]interface{}{ + "run_for": "10s", + } state := s1.State() if err := state.UpsertJob(100, job); err != nil { diff --git a/client/fs_endpoint_test.go b/client/fs_endpoint_test.go index f3cf6d646..8c93a638b 100644 --- a/client/fs_endpoint_test.go +++ b/client/fs_endpoint_test.go @@ -86,7 +86,9 @@ func TestFS_Stat(t *testing.T) { a := mock.Alloc() task := a.Job.TaskGroups[0].Tasks[0] task.Driver = "mock_driver" - task.Config["run_for"] = "500ms" + task.Config = map[string]interface{}{ + "run_for": "500ms", + } c.addAlloc(a, "") // Wait for the client to start it @@ -225,7 +227,9 @@ func TestFS_List(t *testing.T) { a := mock.Alloc() task := a.Job.TaskGroups[0].Tasks[0] task.Driver = "mock_driver" - task.Config["run_for"] = "500ms" + task.Config = map[string]interface{}{ + "run_for": "500ms", + } c.addAlloc(a, "") // Wait for the client to start it diff --git a/client/gc_test.go b/client/gc_test.go index 5ae118047..8b9b33bbe 100644 --- a/client/gc_test.go +++ b/client/gc_test.go @@ -372,7 +372,9 @@ func TestAllocGarbageCollector_MaxAllocs(t *testing.T) { state := server.State() job := mock.Job() job.TaskGroups[0].Tasks[0].Driver = "mock_driver" - job.TaskGroups[0].Tasks[0].Config["run_for"] = "30s" + job.TaskGroups[0].Tasks[0].Config = map[string]interface{}{ + "run_for": "30s", + } nodeID := client.Node().ID if err := state.UpsertJob(98, job); err != nil { t.Fatalf("error upserting job: %v", err) diff --git a/command/agent/alloc_endpoint_test.go b/command/agent/alloc_endpoint_test.go index d576817f1..1b6008cec 100644 --- a/command/agent/alloc_endpoint_test.go +++ b/command/agent/alloc_endpoint_test.go @@ -444,7 +444,9 @@ func TestHTTP_AllocSnapshot_Atomic(t *testing.T) { state := s.server.State() alloc := mock.Alloc() alloc.Job.TaskGroups[0].Tasks[0].Driver = "mock_driver" - alloc.Job.TaskGroups[0].Tasks[0].Config["run_for"] = "30s" + alloc.Job.TaskGroups[0].Tasks[0].Config = map[string]interface{}{ + "run_for": "30s", + } alloc.NodeID = s.client.NodeID() state.UpsertJobSummary(998, mock.JobSummary(alloc.JobID)) if err := state.UpsertAllocs(1000, []*structs.Allocation{alloc.Copy()}); err != nil {