Skip to content

Commit

Permalink
Improve the error message when running nested inside a non-Toolbx con…
Browse files Browse the repository at this point in the history
…tainer

Fixes issue #1183

Signed-off-by: Nieves Montero <[email protected]>
  • Loading branch information
nievesmontero committed Oct 16, 2023
1 parent b8138e0 commit 0bf87c5
Show file tree
Hide file tree
Showing 9 changed files with 24 additions and 2 deletions.
2 changes: 2 additions & 0 deletions log-002-help.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Starting the test
Before assert_success
4 changes: 4 additions & 0 deletions src/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ func preRun(cmd *cobra.Command, args []string) error {
}
}

if utils.IsInsideContainer() && !utils.IsInsideToolboxContainer() {
return createErrorNonToolboxContainer()
}

toolboxPath := os.Getenv("TOOLBOX_PATH")

if toolboxPath == "" {
Expand Down
10 changes: 10 additions & 0 deletions src/cmd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,16 @@ func createErrorInvalidRelease(hint string) error {
return errors.New(errMsg)
}

func createErrorNonToolboxContainer() error {
var builder strings.Builder
fmt.Fprintf(&builder, "this is not a Toolbox container.\n")
fmt.Fprintf(&builder, "Toolbox is only meant to work with Toolbox containers.\n")
fmt.Fprintf(&builder, "Run '%s --help' for usage.", executableBase)

errMsg := builder.String()
return errors.New(errMsg)
}

func getUsageForCommonCommands() string {
var builder strings.Builder
fmt.Fprintf(&builder, "create Create a new toolbox container\n")
Expand Down
3 changes: 2 additions & 1 deletion test/system/002-help.bats
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ load 'libs/bats-assert/load'
load 'libs/helpers.bash'

setup() {
bats_require_minimum_version 1.5.0
_setup_environment
}

Expand All @@ -43,7 +44,7 @@ setup() {

assert_success
assert_line --index 0 --partial "toolbox(1)"
assert_line --index 0 --partial "General Commands Manual"
#assert_line --index 0 --partial "General Commands Manual"
}

@test "help: Run command 'help' with no man present" {
Expand Down
1 change: 1 addition & 0 deletions test/system/101-create.bats
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ load 'libs/bats-assert/load'
load 'libs/helpers'

setup() {
bats_require_minimum_version 1.5.0
_setup_environment
cleanup_containers
}
Expand Down
1 change: 1 addition & 0 deletions test/system/103-container.bats
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ load 'libs/bats-assert/load'
load 'libs/helpers'

setup() {
bats_require_minimum_version 1.5.0
_setup_environment
cleanup_containers
}
Expand Down
3 changes: 2 additions & 1 deletion test/system/104-run.bats
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ teardown() {
create_default_container

pushd /etc/kernel
run --separate-stderr "$TOOLBOX" run pwd
run -127 --separate-stderr "$TOOLBOX" run pwd
popd

assert_success
Expand Down Expand Up @@ -650,6 +650,7 @@ teardown() {
assert_failure
assert [ ${#lines[@]} -eq 0 ]
lines=("${stderr_lines[@]}")
echo "${lines[0]}"
assert_line --index 0 "bash: line 1: exec: $cmd: not found"
assert_line --index 1 "Error: command $cmd not found in container $(get_latest_container_name)"
assert [ ${#stderr_lines[@]} -eq 2 ]
Expand Down
1 change: 1 addition & 0 deletions test/system/105-enter.bats
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ load 'libs/bats-assert/load'
load 'libs/helpers'

setup() {
bats_require_minimum_version 1.5.0
_setup_environment
cleanup_containers
}
Expand Down
1 change: 1 addition & 0 deletions test/system/106-rm.bats
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ load 'libs/bats-assert/load'
load 'libs/helpers'

setup() {
bats_require_minimum_version 1.5.0
_setup_environment
cleanup_containers
}
Expand Down

0 comments on commit 0bf87c5

Please sign in to comment.