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 an additional image is needed, the tests still need to be
patched.

Fixes containers#959

containers#1050
  • Loading branch information
HarryMichal committed Nov 15, 2022
1 parent f779c79 commit 73cef2d
Show file tree
Hide file tree
Showing 8 changed files with 238 additions and 26 deletions.
128 changes: 128 additions & 0 deletions src/cmd/test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/*
* 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": {
0,
resolveDefaultImage,
},
}
)

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 {
containerName, _, _, err := utils.ResolveContainerAndImageNames("", "", "", "")
if err != nil {
return err
}

fmt.Print(containerName)
return nil
}

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

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

fmt.Print(image)
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
4 changes: 3 additions & 1 deletion test/system/000-setup.bats
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ 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_image "$(toolbx_default_image)"

# 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
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
6 changes: 4 additions & 2 deletions 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 All @@ -33,7 +33,9 @@ teardown() {
skip "This test is only for Fedora Rawhide"
fi

create_distro_container "$system_id" "$system_version" latest
# The default image is not cached in the same way, so requested explicitly
# an image the tests cached instead of relying on distro+version resolution.
create_container latest
run container_started latest
assert_success

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 @@ -91,7 +91,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 @@ -102,7 +102,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
Loading

0 comments on commit 73cef2d

Please sign in to comment.