Skip to content

Commit

Permalink
src,test/system: Respect configuration file in system tests
Browse files Browse the repository at this point in the history
Commit 20f4f68 added support for configuration files. If keys in the
configuration file were set the system test suite would not honor them.
This puts additional strain on distributors overriding the default
settings.

This solves the problem by creating a new hidden command in Toolbx that
provides interface for getting some information Toolbx uses
under-the-hood (e.g., the default container name, image).

This commit does not change the way images are cached in the system test
suite. In case a different image is needed, the tests still need to be
patched.

Fixes containers#959

containers#1050
  • Loading branch information
HarryMichal committed May 24, 2022
1 parent 4469774 commit baf5c29
Show file tree
Hide file tree
Showing 8 changed files with 219 additions and 10 deletions.
166 changes: 166 additions & 0 deletions src/cmd/test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
/*
* Copyright © 2022 Ondřej Míchal
*
* 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.
*/

package cmd

import (
"errors"
"fmt"

"github.com/containers/toolbox/pkg/utils"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

type ResolveFunc func([]string) error

type RequestType struct {
ArgsN int
Resolve ResolveFunc
}

var (
testFlags struct {
requestType string
}

requestTypes = map[string]RequestType{
"config-key": {
1,
resolveConfigKey,
},
"default-container-name": {
0,
resolveDefaultContainerName,
},
"default-image-name": {
0,
resolveImageName,
},
"default-distro": {
0,
resolveDistro,
},
"default-release": {
0,
resolveRelease,
},
}
)

var testCmd = &cobra.Command{
Use: "__test",
Short: "List existing toolbox containers and images",
RunE: test,
Hidden: true,
ValidArgsFunction: completionEmpty,
}

func init() {
flags := testCmd.Flags()

flags.StringVar(&testFlags.requestType,
"type",
"",
"type of data to be processed/retrieved")

rootCmd.AddCommand(testCmd)
}

func test(cmd *cobra.Command, args []string) error {
if !cmd.Flag("type").Changed {
return errors.New("flag --type has to be provided")
}

requestType, ok := requestTypes[testFlags.requestType]
if !ok {
return fmt.Errorf("request type %s is not known", testFlags.requestType)
}

if requestType.ArgsN != len(args) {
return fmt.Errorf("request type %s requires %d arguments", testFlags.requestType, requestType.ArgsN)
}

logrus.Debugf("Resolving request %s with arguments: %s", testFlags.requestType, args)

resolve := requestType.Resolve
if err := resolve(args); err != nil {
return fmt.Errorf("failed to resolve request %s: %s", testFlags.requestType, err)
}

return nil
}

func resolveConfigKey(args []string) error {
fmt.Print(viper.GetString(args[0]))
return nil
}

func resolveDefaultContainerName(_ []string) error {
image, release, err := utils.ResolveImageName("", "", "")
if err != nil {
return err
}

image, err = utils.GetFullyQualifiedImageFromDistros(image, release)
if err != nil {
return err
}

containerName, err := utils.ResolveContainerName("", image, "")
if err != nil {
return err
}

fmt.Print(containerName)
return nil
}

func resolveImageName(_ []string) error {
image, release, err := utils.ResolveImageName("", "", "")
if err != nil {
return err
}

image, err = utils.GetFullyQualifiedImageFromDistros(image, release)
if err != nil {
return err
}

fmt.Print(image)
return nil
}

func resolveDistro(_ []string) error {
distro, err := utils.ResolveDistro("")
if err != nil {
return err
}

fmt.Print(distro)
return nil
}

func resolveRelease(_ []string) error {
_, release, err := utils.ResolveImageName("", "", "")
if err != nil {
return err
}

fmt.Print(release)
return nil
}
1 change: 1 addition & 0 deletions src/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ sources = files(
'cmd/rootDefault.go',
'cmd/rootMigrationPath.go',
'cmd/run.go',
'cmd/test.go',
'cmd/utils.go',
'pkg/podman/podman.go',
'pkg/shell/shell.go',
Expand Down
5 changes: 4 additions & 1 deletion test/system/000-setup.bats
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@ load 'libs/helpers'
local system_version="$(get_system_version)"

_setup_environment

# Cache the default image for the system
_pull_and_cache_distro_image "$system_id" "$system_version" || false
_pull_and_cache_distro_image "$(toolbx_default_distro)" "$(toolbx_default_release)" || false

# Cache all images that will be needed during the tests
_pull_and_cache_distro_image fedora 32 || false
_pull_and_cache_distro_image busybox || false

# If run on Fedora Rawhide, cache 2 extra images (previous Fedora versions)
local rawhide_res="$(awk '/rawhide/' $os_release)"
if [ "$system_id" = "fedora" ] && [ -n "$rawhide_res" ]; then
Expand Down
8 changes: 4 additions & 4 deletions test/system/102-list.bats
Original file line number Diff line number Diff line change
Expand Up @@ -62,24 +62,24 @@ teardown() {
run $TOOLBOX list --images

assert_success
assert_output --partial "$(get_system_id)-toolbox:$(get_system_version)"
assert_output --partial "$(toolbx_default_image_name)"
assert_output --partial "fedora-toolbox:32"

# Check containers
run $TOOLBOX list --containers

assert_success
assert_output --partial "$(get_system_id)-toolbox-$(get_system_version)"
assert_output --partial "$(toolbx_default_container_name)"
assert_output --partial "non-default-one"
assert_output --partial "non-default-two"

# Check all together
run $TOOLBOX list

assert_success
assert_output --partial "$(get_system_id)-toolbox:$(get_system_version)"
assert_output --partial "$(toolbx_default_image_name)"
assert_output --partial "fedora-toolbox:32"
assert_output --partial "$(get_system_id)-toolbox-$(get_system_version)"
assert_output --partial "$(toolbx_default_container_name)"
assert_output --partial "non-default-one"
assert_output --partial "non-default-two"
}
Expand Down
2 changes: 1 addition & 1 deletion test/system/103-container.bats
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ teardown() {


@test "container: Check container starts without issues" {
readonly CONTAINER_NAME="$(get_system_id)-toolbox-$(get_system_version)"
readonly CONTAINER_NAME="$(toolbx_default_container_name)"

create_default_container

Expand Down
4 changes: 2 additions & 2 deletions test/system/104-run.bats
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ teardown() {
}

@test "run: Try to run a command in the default container with no containers created" {
local default_container_name="$(get_system_id)-toolbox-$(get_system_version)"
local default_container_name="$(toolbx_default_container_name)"

run $TOOLBOX run true

Expand All @@ -25,7 +25,7 @@ teardown() {
}

@test "run: Try to run a command in the default container when 1 non-default container is present" {
local default_container_name="$(get_system_id)-toolbox-$(get_system_version)"
local default_container_name="$(toolbx_default_container_name)"

create_container other-container

Expand Down
2 changes: 1 addition & 1 deletion test/system/105-enter.bats
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ teardown() {
}

@test "enter: Try to enter the default container with more than 1 other containers present" {
local default_container_name="$(get_system_id)-toolbox-$(get_system_version)"
local default_container_name="$(toolbx_default_container_name)"

create_container first
create_container second
Expand Down
41 changes: 40 additions & 1 deletion test/system/libs/helpers.bash
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ function pull_distro_image() {
#
# See pull_default_image() for more info.
function pull_default_image() {
pull_distro_image $(get_system_id) $(get_system_version)
pull_distro_image $(toolbx_default_distro) $(toolbx_default_release)
}


Expand Down Expand Up @@ -455,3 +455,42 @@ function check_xdg_runtime_dir() {
export XDG_RUNTIME_DIR="/run/user/${UID}"
fi
}


# Prints a value in Toolbx config
#
# If key does not exist, prints nothing
#
# Parameters:
# ===========
# - config-key - config key
function toolbx_config_key() {
local config_key="$1"

echo $(TOOLBOX __test --type config-key "$config_key")
}


# Prints the default Toolbx container name
function toolbx_default_container_name() {
echo $($TOOLBOX __test --type default-container-name)
}


# Prints the default Toolbx OCI image
function toolbx_default_image_name() {
echo $($TOOLBOX __test --type default-image-name)
}


# Prints the default distro of the default OCI image if it is supported
function toolbx_default_distro() {
echo $($TOOLBOX __test --type default-distro)
}


# Prints the default release of the default distro of the default OCI image
function toolbx_default_release() {
echo $($TOOLBOX __test --type default-release)
}

0 comments on commit baf5c29

Please sign in to comment.