Skip to content

Commit

Permalink
pkg/utils, test/system: Offer built-in support for Ubuntu
Browse files Browse the repository at this point in the history
This allows using the --distro and --release options to create and enter
Ubuntu containers.

Note that Ubuntu 4.10 was the first ever Ubuntu release [1].  Hence,
values older than that are not permitted for the --release option.

Some changes by Debarshi Ray.

[1] https://wiki.ubuntu.com/Releases

containers#483
containers#1284

Signed-off-by: Ievgen Popovych <[email protected]>
  • Loading branch information
Jmennius authored and debarshiray committed Mar 27, 2023
1 parent 8c41d85 commit a84a358
Show file tree
Hide file tree
Showing 7 changed files with 530 additions and 1 deletion.
55 changes: 55 additions & 0 deletions src/pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"strings"
"syscall"
"time"
"unicode/utf8"

"github.com/acobaugh/osrelease"
"github.com/containers/toolbox/pkg/shell"
Expand Down Expand Up @@ -108,6 +109,12 @@ var (
getFullyQualifiedImageRHEL,
parseReleaseRHEL,
},
"ubuntu": {
"ubuntu-toolbox",
"ubuntu-toolbox",
getFullyQualifiedImageUbuntu,
parseReleaseUbuntu,
},
}
)

Expand Down Expand Up @@ -343,6 +350,11 @@ func getFullyQualifiedImageRHEL(image, release string) string {
return imageFull
}

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

// GetGroupForSudo returns the name of the sudoers group.
//
// Some distros call it 'sudo' (eg. Ubuntu) and some call it 'wheel' (eg. Fedora).
Expand Down Expand Up @@ -672,6 +684,49 @@ func parseReleaseRHEL(release string) (string, error) {
return release, nil
}

func parseReleaseUbuntu(release string) (string, error) {
releaseParts := strings.Split(release, ".")
if len(releaseParts) != 2 {
return "", &ParseReleaseError{"The release must be in the 'YY.MM' format."}
}

releaseYear, err := strconv.Atoi(releaseParts[0])
if err != nil {
logrus.Debugf("Parsing release year %s as an integer failed: %s", releaseParts[0], err)
return "", &ParseReleaseError{"The release must be in the 'YY.MM' format."}
}

if releaseYear < 4 {
return "", &ParseReleaseError{"The release year must be 4 or more."}
}

releaseYearLen := utf8.RuneCountInString(releaseParts[0])
if releaseYearLen > 2 {
return "", &ParseReleaseError{"The release year cannot have more than two digits."}
} else if releaseYear < 10 && releaseYearLen == 2 {
return "", &ParseReleaseError{"The release year cannot have a leading zero."}
}

releaseMonth, err := strconv.Atoi(releaseParts[1])
if err != nil {
logrus.Debugf("Parsing release month %s as an integer failed: %s", releaseParts[1], err)
return "", &ParseReleaseError{"The release must be in the 'YY.MM' format."}
}

if releaseMonth < 1 {
return "", &ParseReleaseError{"The release month must be between 01 and 12."}
} else if releaseMonth > 12 {
return "", &ParseReleaseError{"The release month must be between 01 and 12."}
}

releaseMonthLen := utf8.RuneCountInString(releaseParts[1])
if releaseMonthLen != 2 {
return "", &ParseReleaseError{"The release month must have two digits."}
}

return release, nil
}

// PathExists wraps around os.Stat providing a nice interface for checking an existence of a path.
func PathExists(path string) bool {
if _, err := os.Stat(path); !os.IsNotExist(err) {
Expand Down
115 changes: 115 additions & 0 deletions src/pkg/utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,121 @@ func TestParseRelease(t *testing.T) {
inputRelease: "2.-1",
errMsg: "The release must be in the '<major>.<minor>' format.",
},
{
inputDistro: "ubuntu",
inputRelease: "4.10",
output: "4.10",
},
{
inputDistro: "ubuntu",
inputRelease: "5.04",
output: "5.04",
},
{
inputDistro: "ubuntu",
inputRelease: "20.04",
output: "20.04",
},
{
inputDistro: "ubuntu",
inputRelease: "20.10",
output: "20.10",
},
{
inputDistro: "ubuntu",
inputRelease: "20",
errMsg: "The release must be in the 'YY.MM' format.",
},
{
inputDistro: "ubuntu",
inputRelease: "20.04.0",
errMsg: "The release must be in the 'YY.MM' format.",
},
{
inputDistro: "ubuntu",
inputRelease: "20.04.1",
errMsg: "The release must be in the 'YY.MM' format.",
},
{
inputDistro: "ubuntu",
inputRelease: "foo",
errMsg: "The release must be in the 'YY.MM' format.",
},
{
inputDistro: "ubuntu",
inputRelease: "20foo",
errMsg: "The release must be in the 'YY.MM' format.",
},
{
inputDistro: "ubuntu",
inputRelease: "foo.bar",
errMsg: "The release must be in the 'YY.MM' format.",
},
{
inputDistro: "ubuntu",
inputRelease: "foo.bar.baz",
errMsg: "The release must be in the 'YY.MM' format.",
},
{
inputDistro: "ubuntu",
inputRelease: "3.10",
errMsg: "The release year must be 4 or more.",
},
{
inputDistro: "ubuntu",
inputRelease: "202.4",
errMsg: "The release year cannot have more than two digits.",
},
{
inputDistro: "ubuntu",
inputRelease: "202.04",
errMsg: "The release year cannot have more than two digits.",
},
{
inputDistro: "ubuntu",
inputRelease: "2020.4",
errMsg: "The release year cannot have more than two digits.",
},
{
inputDistro: "ubuntu",
inputRelease: "2020.04",
errMsg: "The release year cannot have more than two digits.",
},
{
inputDistro: "ubuntu",
inputRelease: "04.10",
errMsg: "The release year cannot have a leading zero.",
},
{
inputDistro: "ubuntu",
inputRelease: "4.bar",
errMsg: "The release must be in the 'YY.MM' format.",
},
{
inputDistro: "ubuntu",
inputRelease: "4.bar.baz",
errMsg: "The release must be in the 'YY.MM' format.",
},
{
inputDistro: "ubuntu",
inputRelease: "4.0",
errMsg: "The release month must be between 01 and 12.",
},
{
inputDistro: "ubuntu",
inputRelease: "4.00",
errMsg: "The release month must be between 01 and 12.",
},
{
inputDistro: "ubuntu",
inputRelease: "4.13",
errMsg: "The release month must be between 01 and 12.",
},
{
inputDistro: "ubuntu",
inputRelease: "20.4",
errMsg: "The release month must have two digits.",
},
}

for _, tc := range testCases {
Expand Down
3 changes: 3 additions & 0 deletions test/system/000-setup.bats
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ load 'libs/helpers'
# Cache all images that will be needed during the tests
_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
_pull_and_cache_distro_image ubuntu 18.04 || false
_pull_and_cache_distro_image ubuntu 20.04 || 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)"
Expand Down
Loading

0 comments on commit a84a358

Please sign in to comment.