cmd, test/system: Retain exit codes when forwarding to host

Some changes by Debarshi Ray.

https://github.com/containers/toolbox/issues/957
https://github.com/containers/toolbox/pull/1052
This commit is contained in:
Ondřej Míchal 2022-05-13 03:07:43 +03:00 committed by Debarshi Ray
parent 1a3f4ff4c6
commit fa2466029d
13 changed files with 73 additions and 49 deletions

View File

@ -158,7 +158,8 @@ jobs:
test/system/201-ipc.bats \
test/system/203-network.bats \
test/system/220-environment-variables.bats \
test/system/501-create.bats
test/system/501-create.bats \
test/system/505-enter.bats
env:
SHELL: /bin/bash
TMPDIR: /var/tmp

View File

@ -125,11 +125,8 @@ func create(cmd *cobra.Command, args []string) error {
return errors.New("this is not a Toolbx container")
}
if _, err := utils.ForwardToHost(); err != nil {
return err
}
return nil
exitCode, err := utils.ForwardToHost()
return &exitError{exitCode, err}
}
if cmd.Flag("distro").Changed && cmd.Flag("image").Changed {

View File

@ -80,11 +80,8 @@ func enter(cmd *cobra.Command, args []string) error {
return errors.New("this is not a Toolbx container")
}
if _, err := utils.ForwardToHost(); err != nil {
return err
}
return nil
exitCode, err := utils.ForwardToHost()
return &exitError{exitCode, err}
}
var container string

View File

@ -43,11 +43,8 @@ func help(cmd *cobra.Command, args []string) error {
return errors.New("this is not a Toolbx container")
}
if _, err := utils.ForwardToHost(); err != nil {
return err
}
return nil
exitCode, err := utils.ForwardToHost()
return &exitError{exitCode, err}
}
if err := helpShowManual(args); err != nil {

View File

@ -75,11 +75,8 @@ func list(cmd *cobra.Command, args []string) error {
return errors.New("this is not a Toolbx container")
}
if _, err := utils.ForwardToHost(); err != nil {
return err
}
return nil
exitCode, err := utils.ForwardToHost()
return &exitError{exitCode, err}
}
lsContainers := true

View File

@ -62,11 +62,8 @@ func rm(cmd *cobra.Command, args []string) error {
return errors.New("this is not a Toolbx container")
}
if _, err := utils.ForwardToHost(); err != nil {
return err
}
return nil
exitCode, err := utils.ForwardToHost()
return &exitError{exitCode, err}
}
if rmFlags.deleteAll {

View File

@ -62,11 +62,8 @@ func rmi(cmd *cobra.Command, args []string) error {
return errors.New("this is not a Toolbx container")
}
if _, err := utils.ForwardToHost(); err != nil {
return err
}
return nil
exitCode, err := utils.ForwardToHost()
return &exitError{exitCode, err}
}
if rmiFlags.deleteAll {

View File

@ -53,11 +53,8 @@ func rootRunImpl(cmd *cobra.Command, args []string) error {
return errors.New("this is not a Toolbx container")
}
if _, err := utils.ForwardToHost(); err != nil {
return err
}
return nil
exitCode, err := utils.ForwardToHost()
return &exitError{exitCode, err}
}
container, image, release, err := resolveContainerAndImageNames("", "", "", "", "")

View File

@ -115,11 +115,8 @@ func run(cmd *cobra.Command, args []string) error {
return errors.New("this is not a Toolbx container")
}
if _, err := utils.ForwardToHost(); err != nil {
return err
}
return nil
exitCode, err := utils.ForwardToHost()
return &exitError{exitCode, err}
}
var defaultContainer bool = true

View File

@ -32,9 +32,9 @@ teardown() {
@test "create: Try with an invalid custom name (using positional argument, forwarded to host)" {
create_default_container
run --keep-empty-lines --separate-stderr "$TOOLBX" run toolbox create "ßpeci@l.N@m€"
run -1 --keep-empty-lines --separate-stderr "$TOOLBX" run toolbox create "ßpeci@l.N@m€"
assert_success
assert_failure
assert [ ${#lines[@]} -eq 0 ]
lines=("${stderr_lines[@]}")
assert_line --index 0 "Error: invalid argument for 'CONTAINER'"
@ -46,9 +46,9 @@ teardown() {
@test "create: Try with an invalid custom name (using option --container, forwarded to host)" {
create_default_container
run --keep-empty-lines --separate-stderr "$TOOLBX" run toolbox create --container "ßpeci@l.N@m€"
run -1 --keep-empty-lines --separate-stderr "$TOOLBX" run toolbox create --container "ßpeci@l.N@m€"
assert_success
assert_failure
assert [ ${#lines[@]} -eq 0 ]
lines=("${stderr_lines[@]}")
assert_line --index 0 "Error: invalid argument for '--container'"
@ -62,9 +62,9 @@ teardown() {
local distro="foo"
run --keep-empty-lines --separate-stderr "$TOOLBX" run toolbox create --distro "$distro"
run -1 --keep-empty-lines --separate-stderr "$TOOLBX" run toolbox create --distro "$distro"
assert_success
assert_failure
assert [ ${#lines[@]} -eq 0 ]
lines=("${stderr_lines[@]}")
assert_line --index 0 "Error: invalid argument for '--distro'"

View File

@ -34,9 +34,9 @@ teardown() {
local distro="foo"
run --keep-empty-lines --separate-stderr "$TOOLBX" run toolbox run --distro "$distro" ls
run -1 --keep-empty-lines --separate-stderr "$TOOLBX" run toolbox run --distro "$distro" ls
assert_success
assert_failure
assert [ ${#lines[@]} -eq 0 ]
lines=("${stderr_lines[@]}")
assert_line --index 0 "Error: invalid argument for '--distro'"

View File

@ -0,0 +1,46 @@
# shellcheck shell=bats
#
# Copyright © 2024 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
load 'libs/bats-support/load'
load 'libs/bats-assert/load'
load 'libs/helpers'
setup() {
bats_require_minimum_version 1.7.0
_setup_environment
cleanup_all
}
teardown() {
cleanup_all
}
@test "enter: Try an unsupported distribution (forwarded to host)" {
create_default_container
local distro="foo"
run -1 --keep-empty-lines --separate-stderr "$TOOLBX" run toolbox enter --distro "$distro"
assert_failure
assert [ ${#lines[@]} -eq 0 ]
lines=("${stderr_lines[@]}")
assert_line --index 0 "Error: invalid argument for '--distro'"
assert_line --index 1 "Distribution $distro is unsupported."
assert_line --index 2 "Run 'toolbox --help' for usage."
assert [ ${#stderr_lines[@]} -eq 3 ]
}

View File

@ -18,6 +18,7 @@ test_system = files(
'230-cdi.bats',
'501-create.bats',
'504-run.bats',
'505-enter.bats',
'setup_suite.bash',
'libs/helpers.bash',
)