Correctly canonicalize lifecycle block when missing hook value (#26285)

This commit is contained in:
Allison Larson
2025-07-16 11:40:16 -07:00
committed by GitHub
parent 0d620607fe
commit 918e1eb123
3 changed files with 32 additions and 3 deletions

3
.changelog/26285.txt Normal file
View File

@@ -0,0 +1,3 @@
```release-note:bug
jobspec: Validate required hook field in lifecycle block
```

View File

@@ -747,13 +747,13 @@ const (
) )
type TaskLifecycle struct { type TaskLifecycle struct {
Hook string `mapstructure:"hook" hcl:"hook,optional"` Hook string `mapstructure:"hook" hcl:"hook"`
Sidecar bool `mapstructure:"sidecar" hcl:"sidecar,optional"` Sidecar bool `mapstructure:"sidecar" hcl:"sidecar,optional"`
} }
// Determine if lifecycle has user-input values // Determine if lifecycle has user-input values
func (l *TaskLifecycle) Empty() bool { func (l *TaskLifecycle) Empty() bool {
return l == nil || (l.Hook == "") return l == nil
} }
// Task is a single process in a task group. // Task is a single process in a task group.

View File

@@ -344,10 +344,36 @@ func TestTask_Canonicalize_TaskLifecycle(t *testing.T) {
{ {
name: "empty", name: "empty",
task: &Task{ task: &Task{
Lifecycle: &TaskLifecycle{}, Lifecycle: nil,
}, },
expected: 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 { for _, tc := range testCases {