-
-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1119 from k1LoW/defer
- Loading branch information
Showing
25 changed files
with
506 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package runn | ||
|
||
const deferSectionKey = "defer" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package runn | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
) | ||
|
||
func TestDeferRun(t *testing.T) { | ||
tests := []struct { | ||
book string | ||
}{ | ||
{"testdata/book/defer.yml"}, | ||
{"testdata/book/defer_map.yml"}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.book, func(t *testing.T) { | ||
ctx := context.Background() | ||
o, err := New(Book(tt.book)) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if err := o.Run(ctx); err == nil { | ||
t.Fatal("expected error") | ||
} | ||
|
||
if o.useMap { | ||
if want := 8; len(o.store.stepMap) != want { | ||
t.Errorf("o.store.stepMap got %v, want %v", len(o.store.stepMap), want) | ||
} | ||
} else { | ||
if want := 8; len(o.store.stepList) != want { | ||
t.Errorf("o.store.stepList got %v, want %v", len(o.store.stepList), want) | ||
} | ||
} | ||
r := o.Result() | ||
if want := 8; len(r.StepResults) != want { | ||
t.Errorf("r.StepResults got %v, want %v", len(r.StepResults), want) | ||
} | ||
|
||
t.Run("main steps", func(t *testing.T) { | ||
wantResults := []struct { | ||
desc string | ||
skipped bool | ||
err bool | ||
}{ | ||
{"step 1", false, false}, | ||
{"include step", false, false}, | ||
{"step 2", false, false}, | ||
{"step 3", false, true}, | ||
{"step 4", true, false}, | ||
{"defererd step c", false, false}, | ||
{"defererd step b", false, true}, | ||
{"defererd step a", false, false}, | ||
} | ||
for i, want := range wantResults { | ||
got := r.StepResults[i] | ||
if got.Desc != want.desc { | ||
t.Errorf("got %v, want %v", got.Desc, want.desc) | ||
} | ||
if got.Skipped != want.skipped { | ||
t.Errorf("got %v, want %v", got.Skipped, want.skipped) | ||
} | ||
if (got.Err == nil) == want.err { | ||
t.Errorf("got %v, want %v", got.Err, want.err) | ||
} | ||
} | ||
}) | ||
|
||
t.Run("include steps", func(t *testing.T) { | ||
wantResults := []struct { | ||
desc string | ||
skipped bool | ||
err bool | ||
}{ | ||
{"included step 1", false, false}, | ||
{"included step 2", false, false}, | ||
{"included defererd step d", false, false}, | ||
} | ||
|
||
for i, want := range wantResults { | ||
got := r.StepResults[1].IncludedRunResults[0].StepResults[i] | ||
if got.Desc != want.desc { | ||
t.Errorf("got %v, want %v", got.Desc, want.desc) | ||
} | ||
if got.Skipped != want.skipped { | ||
t.Errorf("got %v, want %v", got.Skipped, want.skipped) | ||
} | ||
if (got.Err == nil) == want.err { | ||
t.Errorf("got %v, want %v", got.Err, want.err) | ||
} | ||
} | ||
}) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
# Behavior of `defer:` | ||
|
||
Authors: @k1low | ||
|
||
Status: Add continuously | ||
|
||
## Objective | ||
|
||
This document describes the `defer:` behavior in runn. | ||
|
||
## Backgroud | ||
|
||
Allow post-processing of the runbook to be described using `defer:` | ||
|
||
## Behavior of `defer:` | ||
|
||
As the name suggests, `defer:` is inspired by the [defer](https://go.dev/blog/defer-panic-and-recover) of the Go programming language. | ||
|
||
Behavior is also similar to the defer of the Go programming language, but some differences. | ||
|
||
The order of run of steps not marked `defer:` are as follows. | ||
|
||
```yaml | ||
# main.yml | ||
steps: | ||
- desc: step 1 | ||
test: true | ||
- desc: step 2 | ||
test: true | ||
- desc: step 3 | ||
test: true | ||
- desc: step 4 | ||
include: | ||
path: include.yml | ||
- desc: step 5 | ||
test: true | ||
``` | ||
```yaml | ||
# include.yml | ||
steps: | ||
- desc: included step 1 | ||
test: true | ||
- desc: included step 2 | ||
test: true | ||
- desc: included step 3 | ||
test: true | ||
``` | ||
``` mermaid | ||
flowchart TB | ||
subgraph "(main.yml)" | ||
A[step 1] | ||
B[step 2] | ||
C[step 3] | ||
G[step 5] | ||
end | ||
|
||
subgraph "step 4 (include.yml)" | ||
D[included step 1] | ||
E[included step 2] | ||
F[included step 3] | ||
end | ||
|
||
A --> B | ||
B --> C | ||
C --> D | ||
D --> E | ||
E --> F | ||
F --> G | ||
``` | ||
|
||
The steps with `defer:` are run as follows. | ||
|
||
```yaml | ||
# main.yml | ||
steps: | ||
- desc: step 1 | ||
test: true | ||
- desc: step 2 | ||
defer: true | ||
test: true | ||
- desc: step 3 | ||
defer: true | ||
test: true | ||
- desc: step 4 | ||
include: | ||
path: include.yml | ||
- desc: step 5 | ||
test: true | ||
``` | ||
```yaml | ||
# include.yml | ||
steps: | ||
- desc: included step 1 | ||
test: true | ||
- desc: included step 2 | ||
defer: true | ||
test: true | ||
- desc: included step 3 | ||
test: true | ||
``` | ||
``` mermaid | ||
flowchart TB | ||
subgraph "(main.yml)" | ||
A[step 1] | ||
B["step 2 (defer: true)"] | ||
C["step 3 (defer: true)"] | ||
G[step 5] | ||
end | ||
|
||
subgraph "step 4 (include.yml)" | ||
D[included step 1] | ||
E["included step 2 (defer: true)"] | ||
F[included step 3] | ||
end | ||
|
||
A --> D | ||
D --> F | ||
F --> G | ||
G --> E | ||
E --> C | ||
C --> B | ||
``` | ||
|
||
The step marked `defer` behaves as follows. | ||
|
||
- If `defer: true` is set, run of the step is deferred until finish of the runbook. | ||
- Steps marked with `defer` are always run even if the running of intermediate steps fails. | ||
- If there are multiple steps marked with `defer`, they are run in LIFO order. | ||
- Also, the included steps are added to run sequence of the parent runbook's deferred steps. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.