From 918e1eb1238b015c25982cf51c05db586ae1cc16 Mon Sep 17 00:00:00 2001 From: Allison Larson Date: Wed, 16 Jul 2025 11:40:16 -0700 Subject: [PATCH] Correctly canonicalize lifecycle block when missing hook value (#26285) --- .changelog/26285.txt | 3 +++ api/tasks.go | 4 ++-- api/tasks_test.go | 28 +++++++++++++++++++++++++++- 3 files changed, 32 insertions(+), 3 deletions(-) create mode 100644 .changelog/26285.txt 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 {