Skip to content

Commit

Permalink
pkg/utils: Ensure that the distro CLI and config file options are valid
Browse files Browse the repository at this point in the history
Currently, if an invalid or unsupported string is specified as the
distro on the command line or in the configuration file, then it would
silently fallback to Fedora.  This shouldn't happen.

It should only fallback to Fedora when no distro was specified and
there's no supported Toolbox image matching the host operating system.
If a distro was explicitly specified then it should either be supported
or it should error out.

The test cases were resurrected from commit 8b6418d.

containers#937
  • Loading branch information
debarshiray committed Sep 1, 2022
1 parent 0f14a3c commit 38aa6db
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,10 +250,6 @@ func getDefaultImageForDistro(distro, release string) string {
panic("distro not specified")
}

if _, supportedDistro := supportedDistros[distro]; !supportedDistro {
distro = distroFallback
}

distroObj, supportedDistro := supportedDistros[distro]
if !supportedDistro {
panicMsg := fmt.Sprintf("failed to find %s in the list of supported distributions", distro)
Expand Down Expand Up @@ -717,6 +713,10 @@ func ResolveContainerAndImageNames(container, distroCLI, imageCLI, releaseCLI st
}
}

if _, ok := supportedDistros[distro]; !ok {
return "", "", "", fmt.Errorf("distribution %s is unsupported", distro)
}

if distro != distroDefault && releaseCLI == "" && !viper.IsSet("general.release") {
return "", "", "", fmt.Errorf("release not found for non-default distribution %s", distro)
}
Expand Down
10 changes: 10 additions & 0 deletions test/system/101-create.bats
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,16 @@ teardown() {
assert_output --regexp "Created[[:blank:]]+fedora-toolbox-32"
}

@test "create: Try to create a container based on unsupported distribution" {
local distro="foo"

run $TOOLBOX --assumeyes create --distro "$distro"

assert_failure
assert_line --index 0 "Error: distribution $distro is unsupported"
assert [ ${#lines[@]} -eq 1 ]
}

@test "create: Try to create a container based on non-existent image" {
run $TOOLBOX -y create -i foo.org/bar

Expand Down
10 changes: 10 additions & 0 deletions test/system/104-run.bats
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@ teardown() {
assert_line --index 2 "Run 'toolbox --help' for usage."
}

@test "run: Try to run a command in a container based on unsupported distribution" {
local distro="foo"

run $TOOLBOX --assumeyes run --distro "$distro" ls

assert_failure
assert_line --index 0 "Error: distribution $distro is unsupported"
assert [ ${#lines[@]} -eq 1 ]
}

@test "run: Try to run a command in a container based on Fedora but with wrong version" {
run $TOOLBOX run -d fedora -r foobar ls

Expand Down
10 changes: 10 additions & 0 deletions test/system/105-enter.bats
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,16 @@ teardown() {
assert_line --index 2 "Run 'toolbox --help' for usage."
}

@test "enter: Try to enter a container based on unsupported distribution" {
local distro="foo"

run $TOOLBOX --assumeyes enter --distro "$distro"

assert_failure
assert_line --index 0 "Error: distribution $distro is unsupported"
assert [ ${#lines[@]} -eq 1 ]
}

@test "enter: Try to enter a container based on Fedora but with wrong version" {
run $TOOLBOX enter -d fedora -r foobar

Expand Down

0 comments on commit 38aa6db

Please sign in to comment.