Skip to content

Commit

Permalink
Merge pull request #8 from kuno1/issue/7
Browse files Browse the repository at this point in the history
Exit with exit code of command
Close #7.
  • Loading branch information
Harai Akihiro authored Jan 16, 2019
2 parents b3017e4 + 927b4e8 commit 608adcb
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 18 deletions.
19 changes: 16 additions & 3 deletions immortalize.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

var log = logrus.New()

func run(minLifetime uint, maxLifetime uint, command string) {
func run(minLifetime uint, maxLifetime uint, command string) int {
cmd := exec.Command(command)
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
Expand Down Expand Up @@ -57,7 +57,20 @@ func run(minLifetime uint, maxLifetime uint, command string) {
}
}()

cmd.Wait()
var status int
if err := cmd.Wait(); err != nil {
if e2, ok := err.(*exec.ExitError); ok {
if s, ok := e2.Sys().(syscall.WaitStatus); ok {
status = s.ExitStatus()
} else {
log.Fatal("Failed to execute command")
}
}
} else {
status = 0
}

return status
}

func configLog(level string) error {
Expand Down Expand Up @@ -97,5 +110,5 @@ func main() {
log.Fatal("min-lifetime cannot be higher than max-lifetime.")
}

run(*minLifetimePtr, *maxLifetimePtr, *commandPtr)
os.Exit(run(*minLifetimePtr, *maxLifetimePtr, *commandPtr))
}
82 changes: 67 additions & 15 deletions test/test.bats
Original file line number Diff line number Diff line change
Expand Up @@ -42,70 +42,122 @@ wait_exit () {
return "$code"
}

assert_exit_with_nonzero () {
if wait_exit; then
exit 1
fi
}

assert_exit_with_0 () {
if ! wait_exit; then
exit 1
fi
}

# * `COM`: Command exited
# * `MAX`: Maximum lifetime exceeded
# * `MIN`: Minimum lifetime exceeded
# * `SIG`: SIGTERM received
#
# MIN -> COM -> MAX -> SIG
# MIN -> COM -> SIG -> MAX
@test "MIN -> COM" {
@test "MIN -> COM (0)" {
measure -min-lifetime 2 -command test-bin/command-zero
wait_exit
assert_exit_with_0
[ "$(result)" == 4 ]
}

@test "MIN -> COM (1)" {
measure -min-lifetime 2 -command test-bin/command-one
assert_exit_with_nonzero
[ "$(result)" == 4 ]
}

# COM -> MIN -> MAX -> SIG
# COM -> MIN -> SIG -> MAX
# COM -> SIG -> MIN -> MAX
@test "COM" {
@test "COM (0)" {
measure -min-lifetime 6 -command test-bin/command-zero
wait_exit
assert_exit_with_0
[ "$(result)" == 4 ]
}

@test "COM (1)" {
measure -min-lifetime 6 -command test-bin/command-one
assert_exit_with_nonzero
[ "$(result)" == 4 ]
}

# MIN -> MAX -> COM -> SIG
# MIN -> MAX -> SIG -> COM
@test "MIN -> MAX" {
@test "MIN -> MAX (0)" {
measure -max-lifetime 2 -command test-bin/command-zero
wait
assert_exit_with_0
[ "$(result)" == 2 ]
}

@test "MIN -> MAX (1)" {
measure -max-lifetime 2 -command test-bin/command-one
assert_exit_with_nonzero
[ "$(result)" == 2 ]
}

# SIG -> MIN -> MAX -> COM
# SIG -> MIN -> COM -> MAX
@test "SIG -> MIN" {
@test "SIG -> MIN (0)" {
measure -min-lifetime 2 -command test-bin/command-zero
sleep 1
sigterm
wait
assert_exit_with_0
[ "$(result)" == 2 ]
}

@test "SIG -> MIN (1)" {
measure -min-lifetime 2 -command test-bin/command-one
sleep 1
sigterm
assert_exit_with_nonzero
[ "$(result)" == 2 ]
}

# MIN -> SIG -> COM -> MAX
# MIN -> SIG -> MAX -> COM
@test "MIN -> SIG" {
@test "MIN -> SIG (0)" {
measure -min-lifetime 2 -command test-bin/command-zero
sleep 3
sigterm
wait
assert_exit_with_0
[ "$(result)" == 3 ]
}

@test "MIN -> SIG (1)" {
measure -min-lifetime 2 -command test-bin/command-one
sleep 3
sigterm
assert_exit_with_nonzero
[ "$(result)" == 3 ]
}

# SIG -> COM -> MIN -> MAX
@test "SIG -> COM" {
@test "SIG -> COM (0)" {
measure -min-lifetime 6 -command test-bin/command-zero
sleep 2
sigterm
wait
assert_exit_with_0
[ "$(result)" == 4 ]
}

@test "SIG -> COM (1)" {
measure -min-lifetime 6 -command test-bin/command-one
sleep 2
sigterm
assert_exit_with_nonzero
[ "$(result)" == 4 ]
}

# Invalid order
@test "MAX -> MIN" {
measure -min-lifetime 4 -max-lifetime 2 -command test-bin/command-zero
if wait_exit; then
exit 1
fi
assert_exit_with_nonzero
[ "$(result)" == 0 ]
}

0 comments on commit 608adcb

Please sign in to comment.