add canonicalize in the right place

This commit is contained in:
Jasmine Dahilig
2019-12-12 10:59:38 -08:00
committed by Mahmood Ali
parent 262d204096
commit ae2a4bc796
4 changed files with 69 additions and 12 deletions

View File

@@ -6,6 +6,8 @@ import (
"path/filepath"
"strings"
"time"
"github.com/hashicorp/nomad/nomad/structs"
)
const (
@@ -615,6 +617,17 @@ type TaskLifecycle struct {
Deadline time.Duration `mapstructure:"deadline"`
}
func (l *TaskLifecycle) Canonicalize() {
if l.Deadline == 0 {
l.Deadline = structs.TaskLifecycleDeadlineDefault
}
}
// Determine if lifecycle has user-input values
func (l *TaskLifecycle) Empty() bool {
return l.Hook == "" && l.BlockUntil == "" && l.Deadline == 0
}
// Task is a single process in a task group.
type Task struct {
Name string
@@ -672,6 +685,13 @@ func (t *Task) Canonicalize(tg *TaskGroup, job *Job) {
for _, vm := range t.VolumeMounts {
vm.Canonicalize()
}
if t.Lifecycle != nil {
if t.Lifecycle.Empty() {
t.Lifecycle = nil
} else {
t.Lifecycle.Canonicalize()
}
}
}
// TaskArtifact is used to download artifacts before running a task.

View File

@@ -6,6 +6,8 @@ import (
"testing"
"time"
"github.com/hashicorp/nomad/nomad/structs"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
@@ -376,6 +378,50 @@ func TestTask_VolumeMount(t *testing.T) {
require.Equal(t, *vm.PropagationMode, "private")
}
func TestTask_Canonicalize_TaskLifecycle(t *testing.T) {
testCases := []struct {
name string
expected *TaskLifecycle
task *Task
}{
{
name: "empty",
task: &Task{
Lifecycle: &TaskLifecycle{},
},
expected: nil,
},
{
name: "deadline default",
expected: &TaskLifecycle{
Hook: "prestart",
BlockUntil: "completed",
Deadline: structs.TaskLifecycleDeadlineDefault,
},
task: &Task{
Lifecycle: &TaskLifecycle{
Hook: "prestart",
BlockUntil: "completed",
},
},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
tg := &TaskGroup{
Name: stringToPtr("foo"),
}
j := &Job{
ID: stringToPtr("test"),
}
tc.task.Canonicalize(tg, j)
require.Equal(t, tc.expected, tc.task.Lifecycle)
})
}
}
// Ensures no regression on https://github.com/hashicorp/nomad/issues/3132
func TestTaskGroup_Canonicalize_Update(t *testing.T) {
// Job with an Empty() Update

View File

@@ -4390,7 +4390,7 @@ const (
TaskLifecycleBlockUntilRunning = "running"
TaskLifecycleBlockUntilCompleted = "completed"
TaskLifecycleDeadlineMinimum = 0 * time.Second
TaskLifecycleDeadlineDefault = 1 * time.Second
TaskLifecycleDeadlineDefault = 10 * time.Second
)
type TaskLifecycleConfig struct {
@@ -4408,12 +4408,6 @@ func (d *TaskLifecycleConfig) Copy() *TaskLifecycleConfig {
return nd
}
func (d *TaskLifecycleConfig) Canonicalize() {
if &d.Deadline == nil {
d.Deadline = TaskLifecycleDeadlineDefault
}
}
func (d *TaskLifecycleConfig) Validate() error {
if d == nil {
return nil
@@ -5609,10 +5603,6 @@ func (t *Task) Canonicalize(job *Job, tg *TaskGroup) {
for _, template := range t.Templates {
template.Canonicalize()
}
if t.Lifecycle != nil {
t.Lifecycle.Canonicalize()
}
}
func (t *Task) GoString() string {

View File

@@ -2866,6 +2866,7 @@ func TestPeriodicConfig_DST(t *testing.T) {
require.Equal(e1, n1.UTC())
require.Equal(e2, n2.UTC())
}
func TestTaskLifecycleConfig_Validate(t *testing.T) {
testCases := []struct {
name string
@@ -2921,7 +2922,7 @@ func TestTaskLifecycleConfig_Validate(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
err := tc.tlc.Validate()
if tc.err != nil {
require.NotNil(t, err)
require.Error(t, err)
require.Contains(t, err.Error(), tc.err.Error())
} else {
require.Nil(t, err)