From 430615085aced5a55b768dc87b7b10aceaef6d16 Mon Sep 17 00:00:00 2001 From: Debarshi Ray Date: Sat, 30 Jul 2022 19:38:32 +0200 Subject: [PATCH] pkg/utils: Ensure that the distro CLI and config file option is valid 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. https://github.com/containers/toolbox/issues/937 --- src/pkg/utils/utils.go | 8 ++++---- test/system/101-create.bats | 10 ++++++++++ test/system/104-run.bats | 10 ++++++++++ test/system/105-enter.bats | 10 ++++++++++ 4 files changed, 34 insertions(+), 4 deletions(-) diff --git a/src/pkg/utils/utils.go b/src/pkg/utils/utils.go index 358cb39f1..f5c77a788 100644 --- a/src/pkg/utils/utils.go +++ b/src/pkg/utils/utils.go @@ -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) @@ -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) } diff --git a/test/system/101-create.bats b/test/system/101-create.bats index d52f02808..4abd869e2 100644 --- a/test/system/101-create.bats +++ b/test/system/101-create.bats @@ -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 diff --git a/test/system/104-run.bats b/test/system/104-run.bats index 67c9b8a3b..628d0978f 100644 --- a/test/system/104-run.bats +++ b/test/system/104-run.bats @@ -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 diff --git a/test/system/105-enter.bats b/test/system/105-enter.bats index d593ca0d1..3ecd4b951 100644 --- a/test/system/105-enter.bats +++ b/test/system/105-enter.bats @@ -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