Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reverse the exitcode initialization value #278

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 22 additions & 24 deletions cmd/debos/debos.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func main() {
"no_proxy",
eds-collabora marked this conversation as resolved.
Show resolved Hide resolved
}

var exitcode int = 0
var exitcode int = 1
// Allow to run all deferred calls prior to os.Exit()
defer func() {
os.Exit(exitcode)
Expand All @@ -101,24 +101,19 @@ func main() {

args, err := parser.Parse()
if err != nil {
flagsErr, ok := err.(*flags.Error)
if ok && flagsErr.Type == flags.ErrHelp {
return
} else {
exitcode = 1
return
if flagsErr, ok := err.(*flags.Error); ok && flagsErr.Type == flags.ErrHelp {
exitcode = 0
}
return
}

if len(args) != 1 {
log.Println("No recipe given!")
exitcode = 1
return
}

if options.DisableFakeMachine && options.Backend != "auto" {
log.Println("--disable-fakemachine and --fakemachine-backend are mutually exclusive")
exitcode = 1
return
}

Expand All @@ -141,12 +136,10 @@ func main() {
r := actions.Recipe{}
if _, err := os.Stat(file); os.IsNotExist(err) {
log.Println(err)
exitcode = 1
return
}
if err := r.Parse(file, options.PrintRecipe, options.Verbose, options.TemplateVars); err != nil {
log.Println(err)
exitcode = 1
return
}

Expand All @@ -170,7 +163,6 @@ func main() {
if options.Backend == "auto" {
runInFakeMachine = false
} else {
exitcode = 1
return
}
}
Expand Down Expand Up @@ -234,13 +226,14 @@ func main() {

for _, a := range r.Actions {
err = a.Verify(&context)
if exitcode = checkError(&context, err, a, "Verify"); exitcode != 0 {
if ret := checkError(&context, err, a, "Verify"); ret != 0 {
return
}
}

if options.DryRun {
log.Printf("==== Recipe done (Dry run) ====")
exitcode = 0
return
}

Expand All @@ -254,7 +247,6 @@ func main() {
memsize, err := units.RAMInBytes(options.Memory)
if err != nil {
fmt.Printf("Couldn't parse memory size: %v\n", err)
exitcode = 1
return
}
m.SetMemory(int(memsize / 1024 / 1024))
Expand All @@ -269,7 +261,6 @@ func main() {
size, err := units.FromHumanSize(options.ScratchSize)
if err != nil {
fmt.Printf("Couldn't parse scratch size: %v\n", err)
exitcode = 1
return
}
m.SetScratch(size, "")
Expand Down Expand Up @@ -311,30 +302,32 @@ func main() {
defer a.PostMachineCleanup(&context)

err = a.PreMachine(&context, m, &args)
if exitcode = checkError(&context, err, a, "PreMachine"); exitcode != 0 {
if ret := checkError(&context, err, a, "PreMachine"); ret != 0 {
return
}
}

exitcode, err = m.RunInMachineWithArgs(args)
var status int
status, err = m.RunInMachineWithArgs(args)
if err != nil {
fmt.Println(err)
return
}

if exitcode != 0 {
if status != 0 {
context.State = debos.Failed
return
}

for _, a := range r.Actions {
err = a.PostMachine(&context)
if exitcode = checkError(&context, err, a, "Postmachine"); exitcode != 0 {
if ret := checkError(&context, err, a, "Postmachine"); ret != 0 {
return
}
}

log.Printf("==== Recipe done ====")
exitcode = 0
return
}

Expand All @@ -344,7 +337,7 @@ func main() {
defer a.PostMachineCleanup(&context)

err = a.PreNoMachine(&context)
if exitcode = checkError(&context, err, a, "PreNoMachine"); exitcode != 0 {
if err := checkError(&context, err, a, "PreNoMachine"); err != 0 {
return
}
}
Expand All @@ -354,23 +347,28 @@ func main() {
if _, err = os.Stat(context.Rootdir); os.IsNotExist(err) {
err = os.Mkdir(context.Rootdir, 0755)
if err != nil && os.IsNotExist(err) {
exitcode = 1
return
}
}

exitcode = do_run(r, &context)
if exitcode != 0 {
if ret := do_run(r, &context); ret != 0 {
return
}

if !fakemachine.InMachine() {
for _, a := range r.Actions {
err = a.PostMachine(&context)
if exitcode = checkError(&context, err, a, "PostMachine"); exitcode != 0 {
if err := checkError(&context, err, a, "PostMachine"); err != 0 {
return
}
}
log.Printf("==== Recipe done ====")
}
eds-collabora marked this conversation as resolved.
Show resolved Hide resolved

// We reach this point when we have had no errors, and are not
// responsible for spawning a fakemachine to run the
// recipe. This is true both when this instance of debos is
// itself running in a fakemachine, and when no fakemachine is
// being used at all.
exitcode = 0
}
21 changes: 21 additions & 0 deletions docker/exitcode-test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
version: '3.6'
obbardc marked this conversation as resolved.
Show resolved Hide resolved

services:
sut:
build:
context: ..
dockerfile: docker/Dockerfile
volumes:
- type: bind
source: ../tests/exit_test
target: /recipes
tmpfs:
- /scratch:exec
environment:
- TMP=/scratch
cap_add:
- SYS_PTRACE
security_opt:
- label:disable
working_dir: /recipes/
entrypoint: ./exit_test.sh
5 changes: 5 additions & 0 deletions tests/exit_test/bad.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
architecture: amd64

actions:
- action: run
command: exit 1
88 changes: 88 additions & 0 deletions tests/exit_test/exit_test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#!/bin/bash

TEST=0
FAILURES=0
VERBOSE=1

function test_failed {
local MSG="$1"
TEST=$(($TEST + 1))
FAILURES=$(($FAILURES + 1))
echo "Test ${TEST}: ${MSG}"
}

function test_passed {
TEST=$(($TEST + 1))
echo "Test ${TEST}: PASS"
}

function run_cmd {
local CMD="$@"
echo
echo "Running ${CMD}"
if [[ $VERBOSE == 0 ]]; then
$CMD &>/dev/null
else
$CMD
fi
return $?
}

function expect_success {
local CMD="$@"
run_cmd $CMD && test_passed || test_failed "${CMD} failed with exitcode $?, expected success"
}

function expect_failure {
local CMD="$@"
run_cmd $CMD && test_failed "${CMD} succeeded, failure expected." || test_passed
}

function rename_command {
newname="$1"
shift
(exec -a "$newname" "$@")
return $?
}

if [ -v sudo ]; then
SUDO=sudo
else
SUDO=
fi

expect_success debos --help
expect_failure debos --not-a-valid-option
expect_failure debos
expect_failure debos good.yaml good.yaml
expect_failure debos --disable-fakemachine --fakemachine-backend=uml good.yaml
expect_failure debos non-existent-file.yaml
expect_failure debos garbled.yaml
expect_failure debos --fakemachine-backend=kvm good.yaml
expect_failure debos verify-fail.yaml
expect_success debos --dry-run good.yaml
expect_failure debos --memory=NotANumber good.yaml
expect_failure debos --scratchsize=NotANumber good.yaml
expect_success debos good.yaml
expect_failure debos bad.yaml
expect_failure debos pre-machine-failure.yaml
expect_failure debos post-machine-failure.yaml
expect_failure rename_command NOT_DEBOS debos good.yaml

expect_failure $SUDO debos non-existent-file.yaml --disable-fakemachine
expect_failure $SUDO debos garbled.yaml --disable-fakemachine
expect_failure $SUDO debos verify-fail.yaml --disable-fakemachine
expect_success $SUDO debos --dry-run good.yaml --disable-fakemachine
expect_success $SUDO debos good.yaml --disable-fakemachine
expect_failure $SUDO debos bad.yaml --disable-fakemachine
expect_failure $SUDO debos pre-machine-failure.yaml --disable-fakemachine
expect_failure $SUDO debos post-machine-failure.yaml --disable-fakemachine

echo
if [[ $FAILURES -ne 0 ]]; then
SUCCESSES=$(( $TEST - $FAILURES ))
echo "Error: Only $SUCCESSES/$TEST tests passed"
exit 1
fi

echo "All tests passed"
4 changes: 4 additions & 0 deletions tests/exit_test/garbled.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
This isn't
:::

very good YAML
5 changes: 5 additions & 0 deletions tests/exit_test/good.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
architecture: amd64

actions:
- action: run
command: exit 0
6 changes: 6 additions & 0 deletions tests/exit_test/post-machine-failure.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
architecture: amd64

actions:
- action: run
command: exit 1
postprocess: true
8 changes: 8 additions & 0 deletions tests/exit_test/pre-machine-failure.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
architecture: amd64

actions:
- action: image-partition
imagename: test
imagesize: 500000000000000TB
partitiontype: gpt

6 changes: 6 additions & 0 deletions tests/exit_test/verify-fail.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
architecture: amd64

actions:
- action: unpack
compression: gz
file: ~
Loading