diff --git a/.changelog/26285.txt b/.changelog/26285.txt new file mode 100644 index 000000000..841726c6f --- /dev/null +++ b/.changelog/26285.txt @@ -0,0 +1,3 @@ +```release-note:bug +jobspec: Validate required hook field in lifecycle block +``` diff --git a/api/tasks.go b/api/tasks.go index 3f3ea4f07..e6d77d830 100644 --- a/api/tasks.go +++ b/api/tasks.go @@ -747,13 +747,13 @@ const ( ) type TaskLifecycle struct { - Hook string `mapstructure:"hook" hcl:"hook,optional"` + Hook string `mapstructure:"hook" hcl:"hook"` Sidecar bool `mapstructure:"sidecar" hcl:"sidecar,optional"` } // Determine if lifecycle has user-input values func (l *TaskLifecycle) Empty() bool { - return l == nil || (l.Hook == "") + return l == nil } // Task is a single process in a task group. diff --git a/api/tasks_test.go b/api/tasks_test.go index 38f70c718..392dfc67a 100644 --- a/api/tasks_test.go +++ b/api/tasks_test.go @@ -344,10 +344,36 @@ func TestTask_Canonicalize_TaskLifecycle(t *testing.T) { { name: "empty", task: &Task{ - Lifecycle: &TaskLifecycle{}, + Lifecycle: nil, }, expected: nil, }, + { + name: "missing hook", + task: &Task{ + Lifecycle: &TaskLifecycle{}, + }, + expected: &TaskLifecycle{}, + }, + { + name: "with sidecar", + task: &Task{ + Lifecycle: &TaskLifecycle{ + Sidecar: true, + }, + }, + expected: &TaskLifecycle{Sidecar: true}, + }, + { + name: "valid", + task: &Task{ + Lifecycle: &TaskLifecycle{ + Hook: "prestart", + Sidecar: true, + }, + }, + expected: &TaskLifecycle{Hook: "prestart", Sidecar: true}, + }, } for _, tc := range testCases {