Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pkg/utils: Offer built-in support for Arch Linux #1311

Merged
merged 2 commits into from
Jun 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 78 additions & 11 deletions src/pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,20 @@ type ParseReleaseFunc func(string) (string, error)
type Distro struct {
ContainerNamePrefix string
ImageBasename string
ReleaseRequired bool
GetDefaultRelease GetDefaultReleaseFunc
GetFullyQualifiedImage GetFullyQualifiedImageFunc
ParseRelease ParseReleaseFunc
}

type OptionValueSource int

const (
optionValueDefault OptionValueSource = iota
optionValueConfigFile
optionValueCLI
)

const (
containerNamePrefixFallback = "fedora-toolbox"
distroFallback = "fedora"
Expand Down Expand Up @@ -99,23 +108,34 @@ var (
releaseDefault string

supportedDistros = map[string]Distro{
"arch": {
"arch-toolbox",
"arch-toolbox",
false,
getDefaultReleaseArch,
getFullyQualifiedImageArch,
parseReleaseArch,
},
"fedora": {
"fedora-toolbox",
"fedora-toolbox",
true,
getDefaultReleaseFedora,
getFullyQualifiedImageFedora,
parseReleaseFedora,
},
"rhel": {
"rhel-toolbox",
"toolbox",
true,
getDefaultReleaseRHEL,
getFullyQualifiedImageRHEL,
parseReleaseRHEL,
},
"ubuntu": {
"ubuntu-toolbox",
"ubuntu-toolbox",
true,
getDefaultReleaseUbuntu,
getFullyQualifiedImageUbuntu,
parseReleaseUbuntu,
Expand Down Expand Up @@ -297,6 +317,10 @@ func getDefaultReleaseForDistro(distro string) (string, error) {
return release, nil
}

func getDefaultReleaseArch() (string, error) {
return "latest", nil
}

func getDefaultReleaseFedora() (string, error) {
release, err := getHostVersionID()
if err != nil {
Expand Down Expand Up @@ -384,6 +408,11 @@ func GetFullyQualifiedImageFromDistros(image, release string) (string, error) {
return "", fmt.Errorf("failed to resolve image %s", image)
}

func getFullyQualifiedImageArch(image, release string) string {
imageFull := "quay.io/toolbx/" + image
return imageFull
}

func getFullyQualifiedImageFedora(image, release string) string {
imageFull := "registry.fedoraproject.org/" + image
return imageFull
Expand Down Expand Up @@ -699,6 +728,14 @@ func parseRelease(distro, release string) (string, error) {
return release, err
}

func parseReleaseArch(release string) (string, error) {
if release != "latest" && release != "rolling" && release != "" {
return "", &ParseReleaseError{"The release must be 'latest'."}
}

return "latest", nil
}

func parseReleaseFedora(release string) (string, error) {
if strings.HasPrefix(release, "F") || strings.HasPrefix(release, "f") {
release = release[1:]
Expand Down Expand Up @@ -820,27 +857,57 @@ func ResolveContainerAndImageNames(container, distroCLI, imageCLI, releaseCLI st
logrus.Debugf("Image (CLI): '%s'", imageCLI)
logrus.Debugf("Release (CLI): '%s'", releaseCLI)

distro, image, release := distroCLI, imageCLI, releaseCLI
distro, distroSource := distroCLI, optionValueCLI
image, release := imageCLI, releaseCLI

if distroCLI == "" {
distro = distroDefault
distro, distroSource = distroDefault, optionValueDefault
if viper.IsSet("general.distro") {
distro = viper.GetString("general.distro")
distro, distroSource = viper.GetString("general.distro"), optionValueConfigFile
}
}

if _, supportedDistro := supportedDistros[distro]; !supportedDistro {
distroObj, supportedDistro := supportedDistros[distro]
if !supportedDistro {
return "", "", "", &DistroError{distro, ErrDistroUnsupported}
}

if distro != distroDefault && releaseCLI == "" && !viper.IsSet("general.release") {
return "", "", "", &DistroError{distro, ErrDistroWithoutRelease}
}
if distro == distroDefault {
if releaseCLI == "" {
release = releaseDefault
if viper.IsSet("general.release") {
release = viper.GetString("general.release")
}
}
} else {
if distroObj.ReleaseRequired {
if releaseCLI == "" && !viper.IsSet("general.release") {
return "", "", "", &DistroError{distro, ErrDistroWithoutRelease}
}

if releaseCLI == "" {
release = viper.GetString("general.release")
}

if releaseCLI == "" {
release = releaseDefault
if viper.IsSet("general.release") {
release = viper.GetString("general.release")
if release == "" {
panicMsg := fmt.Sprintf("cannot find release for non-default distribution %s", distro)
panic(panicMsg)
}
} else {
switch distroSource {
case optionValueCLI:
// 'release' already set to 'releaseCLI'
case optionValueConfigFile:
if releaseCLI == "" {
if viper.IsSet("general.release") {
release = viper.GetString("general.release")
}
}
case optionValueDefault:
panic("distro must be non-default")
default:
panic("cannot handle new OptionValueSource")
}
}
}

Expand Down
20 changes: 20 additions & 0 deletions src/pkg/utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,26 @@ func TestParseRelease(t *testing.T) {
output string
errMsg string
}{
{
inputDistro: "arch",
inputRelease: "",
output: "latest",
},
{
inputDistro: "arch",
inputRelease: "latest",
output: "latest",
},
{
inputDistro: "arch",
inputRelease: "rolling",
output: "latest",
},
{
inputDistro: "arch",
inputRelease: "foo",
errMsg: "The release must be 'latest'.",
},
{
inputDistro: "fedora",
inputRelease: "f34",
Expand Down
1 change: 1 addition & 0 deletions test/system/000-setup.bats
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ load 'libs/helpers'
# Cache the default image for the system
_pull_and_cache_distro_image "$system_id" "$system_version" || false
# Cache all images that will be needed during the tests
_pull_and_cache_distro_image arch latest || false
_pull_and_cache_distro_image fedora 34 || false
_pull_and_cache_distro_image rhel 8.7 || false
_pull_and_cache_distro_image ubuntu 16.04 || false
Expand Down
52 changes: 52 additions & 0 deletions test/system/101-create.bats
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,48 @@ teardown() {
assert [ ${#lines[@]} -eq 4 ]
}

@test "create: Arch Linux" {
pull_distro_image arch latest

run $TOOLBOX --assumeyes create --distro arch

assert_success
assert_output --partial "Created container: arch-toolbox-latest"
assert_output --partial "Enter with: toolbox enter arch-toolbox-latest"

run podman ps -a

assert_output --regexp "Created[[:blank:]]+arch-toolbox-latest"
}

@test "create: Arch Linux ('--release latest')" {
pull_distro_image arch latest

run $TOOLBOX --assumeyes create --distro arch --release latest

assert_success
assert_output --partial "Created container: arch-toolbox-latest"
assert_output --partial "Enter with: toolbox enter arch-toolbox-latest"

run podman ps -a

assert_output --regexp "Created[[:blank:]]+arch-toolbox-latest"
}

@test "create: Arch Linux ('--release rolling')" {
pull_distro_image arch latest

run $TOOLBOX --assumeyes create --distro arch --release rolling

assert_success
assert_output --partial "Created container: arch-toolbox-latest"
assert_output --partial "Enter with: toolbox enter arch-toolbox-latest"

run podman ps -a

assert_output --regexp "Created[[:blank:]]+arch-toolbox-latest"
}

@test "create: Fedora 34" {
pull_distro_image fedora 34

Expand Down Expand Up @@ -178,6 +220,16 @@ teardown() {
assert_line --index 2 "Use 'toolbox --verbose ...' for further details."
}

@test "create: Try Arch Linux with an invalid release ('--release foo')" {
run $TOOLBOX --assumeyes create --distro arch --release foo

assert_failure
assert_line --index 0 "Error: invalid argument for '--release'"
assert_line --index 1 "The release must be 'latest'."
assert_line --index 2 "Run 'toolbox --help' for usage."
assert [ ${#lines[@]} -eq 3 ]
}

@test "create: Try Fedora with an invalid release ('--release -3')" {
run $TOOLBOX --assumeyes create --distro fedora --release -3

Expand Down
30 changes: 30 additions & 0 deletions test/system/102-list.bats
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,36 @@ teardown() {
assert [ ${#stderr_lines[@]} -eq 0 ]
}

@test "list: Arch Linux image" {
pull_distro_image arch latest

local num_of_images
num_of_images="$(list_images)"
assert_equal "$num_of_images" 1

run --keep-empty-lines --separate-stderr "$TOOLBOX" list

assert_success
assert_line --index 1 --partial "quay.io/toolbx/arch-toolbox:latest"
assert [ ${#lines[@]} -eq 3 ]
assert [ ${#stderr_lines[@]} -eq 0 ]
}

@test "list: Arch Linux image (using --images)" {
pull_distro_image arch latest

local num_of_images
num_of_images="$(list_images)"
assert_equal "$num_of_images" 1

run --keep-empty-lines --separate-stderr "$TOOLBOX" list --images

assert_success
assert_line --index 1 --partial "quay.io/toolbx/arch-toolbox:latest"
assert [ ${#lines[@]} -eq 3 ]
assert [ ${#stderr_lines[@]} -eq 0 ]
}

@test "list: Fedora 34 image" {
pull_distro_image fedora 34

Expand Down
30 changes: 30 additions & 0 deletions test/system/104-run.bats
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,36 @@ teardown() {
assert_output ""
}

@test "run: Smoke test with Arch Linux" {
create_distro_container arch latest arch-toolbox-latest

run --separate-stderr $TOOLBOX run --distro arch true

assert_success
assert [ ${#lines[@]} -eq 0 ]
assert [ ${#stderr_lines[@]} -eq 0 ]
}

@test "run: Smoke test with Arch Linux ('--release latest')" {
create_distro_container arch latest arch-toolbox-latest

run --separate-stderr $TOOLBOX run --distro arch --release latest true

assert_success
assert [ ${#lines[@]} -eq 0 ]
assert [ ${#stderr_lines[@]} -eq 0 ]
}

@test "run: Smoke test with Arch Linux ('--release rolling')" {
create_distro_container arch latest arch-toolbox-latest

run --separate-stderr $TOOLBOX run --distro arch --release rolling true

assert_success
assert [ ${#lines[@]} -eq 0 ]
assert [ ${#stderr_lines[@]} -eq 0 ]
}

@test "run: Smoke test with Fedora 34" {
create_distro_container fedora 34 fedora-toolbox-34

Expand Down
3 changes: 2 additions & 1 deletion test/system/libs/helpers.bash
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ readonly TOOLBOX=${TOOLBOX:-$(command -v toolbox)}
readonly SKOPEO=${SKOPEO:-$(command -v skopeo)}

# Images
declare -Ag IMAGES=([busybox]="quay.io/toolbox_tests/busybox" \
declare -Ag IMAGES=([arch]="quay.io/toolbx/arch-toolbox" \
[busybox]="quay.io/toolbox_tests/busybox" \
[docker-reg]="quay.io/toolbox_tests/registry" \
[fedora]="registry.fedoraproject.org/fedora-toolbox" \
[rhel]="registry.access.redhat.com/ubi8/toolbox" \
Expand Down