cmd, test/system: Make the behaviour of 'toolbox' conditional

Commit 6c86cabbe5 changed the command line interface to behave
a lot similar to that of github.com/coreos/toolbox, which makes things
easier for those switching over from it. Make it conditional so that
only those OS distributors who truly need it may enable it, and
restore the previous behaviour as the default.

The tests were updated to test the default behaviour that the vast
majority of users would be seeing. Ideally, the test suite would be run
twice with the migration path turned off and on. However, that would
require a more intrusive surgery of the test suite and likely make it
slower. It might not be worth the hassle because of the small number
of users who should be using the migration path.

Note that the copyright and license notices really must use C++-style
// line comments, because build constraints can only be preceded by
blank lines and other line comments. C-style /* */ block comments can't
precede the build constraints.

This reverts commit ca899c8a56 and parts
of commit 3aeb7cf288.

[1] go help buildconstraint
    https://pkg.go.dev/cmd/go#hdr-Build_constraints

https://github.com/containers/toolbox/pull/951
This commit is contained in:
Debarshi Ray 2021-11-13 14:16:32 +01:00
parent 063bdf965f
commit 411147988b
6 changed files with 150 additions and 88 deletions

View File

@ -181,67 +181,7 @@ func rootHelp(cmd *cobra.Command, args []string) {
} }
func rootRun(cmd *cobra.Command, args []string) error { func rootRun(cmd *cobra.Command, args []string) error {
if len(args) != 0 { return rootRunImpl(cmd, args)
panic("unexpected argument: commands known or unknown shouldn't reach here")
}
if utils.IsInsideContainer() {
if !utils.IsInsideToolboxContainer() {
return errors.New("this is not a toolbox container")
}
if _, err := utils.ForwardToHost(); err != nil {
return err
}
return nil
}
image, release, err := utils.ResolveImageName("", "", "")
if err != nil {
return err
}
container, err := utils.ResolveContainerName("", image, release)
if err != nil {
return err
}
userShell := os.Getenv("SHELL")
if userShell == "" {
return errors.New("failed to get the current user's default shell")
}
command := []string{userShell, "-l"}
hostID, err := utils.GetHostID()
if err != nil {
return fmt.Errorf("failed to get the host ID: %w", err)
}
hostVariantID, err := utils.GetHostVariantID()
if err != nil {
return errors.New("failed to get the host VARIANT_ID")
}
var emitEscapeSequence bool
if hostID == "fedora" && (hostVariantID == "silverblue" || hostVariantID == "workstation") {
emitEscapeSequence = true
}
if err := runCommand(container,
true,
image,
release,
command,
emitEscapeSequence,
true,
false); err != nil {
return err
}
return nil
} }
func rootUsage(cmd *cobra.Command) error { func rootUsage(cmd *cobra.Command) error {

43
src/cmd/rootDefault.go Normal file
View File

@ -0,0 +1,43 @@
//
// Copyright © 2021 Red Hat Inc.
//
// 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.
//
//go:build !migration_path_for_coreos_toolbox
// +build !migration_path_for_coreos_toolbox
package cmd
import (
"errors"
"fmt"
"strings"
"github.com/spf13/cobra"
)
func rootRunImpl(cmd *cobra.Command, args []string) error {
var builder strings.Builder
fmt.Fprintf(&builder, "missing command\n")
fmt.Fprintf(&builder, "\n")
usage := getUsageForCommonCommands()
fmt.Fprintf(&builder, "%s", usage)
fmt.Fprintf(&builder, "\n")
fmt.Fprintf(&builder, "Run '%s --help' for usage.", executableBase)
errMsg := builder.String()
return errors.New(errMsg)
}

View File

@ -0,0 +1,93 @@
//
// Copyright © 2021 Red Hat Inc.
//
// 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.
//
//go:build migration_path_for_coreos_toolbox
// +build migration_path_for_coreos_toolbox
package cmd
import (
"errors"
"fmt"
"os"
"github.com/containers/toolbox/pkg/utils"
"github.com/spf13/cobra"
)
func rootRunImpl(cmd *cobra.Command, args []string) error {
if len(args) != 0 {
panic("unexpected argument: commands known or unknown shouldn't reach here")
}
if utils.IsInsideContainer() {
if !utils.IsInsideToolboxContainer() {
return errors.New("this is not a toolbox container")
}
if _, err := utils.ForwardToHost(); err != nil {
return err
}
return nil
}
image, release, err := utils.ResolveImageName("", "", "")
if err != nil {
return err
}
container, err := utils.ResolveContainerName("", image, release)
if err != nil {
return err
}
userShell := os.Getenv("SHELL")
if userShell == "" {
return errors.New("failed to get the current user's default shell")
}
command := []string{userShell, "-l"}
hostID, err := utils.GetHostID()
if err != nil {
return fmt.Errorf("failed to get the host ID: %w", err)
}
hostVariantID, err := utils.GetHostVariantID()
if err != nil {
return errors.New("failed to get the host VARIANT_ID")
}
var emitEscapeSequence bool
if hostID == "fedora" && (hostVariantID == "silverblue" || hostVariantID == "workstation") {
emitEscapeSequence = true
}
if err := runCommand(container,
true,
image,
release,
command,
emitEscapeSequence,
true,
false); err != nil {
return err
}
return nil
}

View File

@ -11,6 +11,8 @@ sources = files(
'cmd/rm.go', 'cmd/rm.go',
'cmd/rmi.go', 'cmd/rmi.go',
'cmd/root.go', 'cmd/root.go',
'cmd/rootDefault.go',
'cmd/rootMigrationPath.go',
'cmd/run.go', 'cmd/run.go',
'cmd/utils.go', 'cmd/utils.go',
'pkg/podman/podman.go', 'pkg/podman/podman.go',

View File

@ -8,6 +8,17 @@ setup() {
_setup_environment _setup_environment
} }
@test "help: Try to run toolbox with no command" {
run $TOOLBOX
assert_failure
assert_line --index 0 "Error: missing command"
assert_line --index 1 "create Create a new toolbox container"
assert_line --index 2 "enter Enter an existing toolbox container"
assert_line --index 3 "list List all existing toolbox containers and images"
assert_line --index 4 "Run 'toolbox --help' for usage."
}
@test "help: Run command 'help'" { @test "help: Run command 'help'" {
if ! command -v man 2>/dev/null; then if ! command -v man 2>/dev/null; then
skip "Test works only if man is in PATH" skip "Test works only if man is in PATH"

View File

@ -1,27 +0,0 @@
#!/usr/bin/env bats
load 'libs/bats-support/load'
load 'libs/bats-assert/load'
load 'libs/helpers'
setup() {
_setup_environment
cleanup_containers
}
teardown() {
cleanup_containers
}
@test "root: Try to enter the default container with no containers created" {
run $TOOLBOX <<< "n"
assert_success
assert_line --index 0 "No toolbox containers found. Create now? [y/N] A container can be created later with the 'create' command."
assert_line --index 1 "Run 'toolbox --help' for usage."
}
# TODO: Write the test
@test "root: Enter the default container when 1 non-default container is present" {
skip "Testing of entering toolboxes is not implemented"
}