diff --git a/pkg/dockerinstall/constants.go b/pkg/dockerinstall/constants.go index 5c5462d0a..bcf5f8140 100644 --- a/pkg/dockerinstall/constants.go +++ b/pkg/dockerinstall/constants.go @@ -45,6 +45,10 @@ const ( DistroRaspbian = "raspbian" DistroOSMC = "osmc" DistroDebian = "debian" + DistroCentOS = "centos" + DistroFedora = "fedora" + DistroRHEL = "rhel" + DistroSLES = "sles" // Debian release codenames. CodenameTrixie = "trixie" @@ -53,4 +57,10 @@ const ( CodenameBuster = "buster" CodenameStretch = "stretch" CodenameJessie = "jessie" + + // Ubuntu release. + ubuntuRelease1809 = "18.09" + ubuntuRelease2010 = "20.10" + ubuntuRelease2204 = "22.04" + ubuntuRelease230 = "23.0" ) diff --git a/pkg/dockerinstall/debian.go b/pkg/dockerinstall/debian.go index 7a10829bf..60ac4a490 100644 --- a/pkg/dockerinstall/debian.go +++ b/pkg/dockerinstall/debian.go @@ -94,7 +94,7 @@ func (i *DebianInstaller) findVersions() (string, string, error) { } cliPkgVersion := "" - if versionGte(i.opts.version, "18.09") { + if versionGte(i.opts.version, ubuntuRelease1809) { cliPkgVersion, err = i.findPackageVersion("docker-ce-cli") if err != nil { return "", "", err diff --git a/pkg/dockerinstall/detector_test.go b/pkg/dockerinstall/detector_test.go index 1f9fe50b6..e80735c6a 100644 --- a/pkg/dockerinstall/detector_test.go +++ b/pkg/dockerinstall/detector_test.go @@ -42,7 +42,11 @@ VERSION_ID="22.04"` distro := s.detector.parseOSRelease(strings.NewReader(osRelease)) s.Equal(DistroUbuntu, distro.ID) - s.Equal("22.04", distro.Version, "Should fall back to VERSION_ID when codename missing") + s.Equal( + ubuntuRelease2204, + distro.Version, + "Should fall back to VERSION_ID when codename missing", + ) } func (s *DetectorTestSuite) TestParseOSRelease_Debian_WithCodename() { @@ -156,7 +160,7 @@ func (s *DetectorTestSuite) TestIsNumericVersion() { version string expected bool }{ - {"22.04", true}, + {ubuntuRelease2204, true}, {"11", true}, {"12.5", true}, {"jammy", false}, @@ -187,7 +191,7 @@ func (s *DetectorTestSuite) TestDistro_HasCodename() { }{ {&Distro{ID: DistroUbuntu, Version: "jammy"}, true}, {&Distro{ID: DistroDebian, Version: CodenameBookworm}, true}, - {&Distro{ID: DistroUbuntu, Version: "22.04"}, false}, + {&Distro{ID: DistroUbuntu, Version: ubuntuRelease2204}, false}, {&Distro{ID: DistroDebian, Version: "12"}, false}, {&Distro{ID: "fedora", Version: "39"}, false}, {&Distro{ID: DistroUbuntu, Version: ""}, false}, diff --git a/pkg/dockerinstall/install.go b/pkg/dockerinstall/install.go index bde2b8614..601464a35 100644 --- a/pkg/dockerinstall/install.go +++ b/pkg/dockerinstall/install.go @@ -141,7 +141,7 @@ func echoDockerAsNonroot(opts *InstallOptions) { fprintln(opts.stdout, ` ================================================================================`) - if versionGte(opts.version, "20.10") { + if versionGte(opts.version, ubuntuRelease2010) { fprintln(opts.stdout, ` To run Docker as a non-privileged user, consider setting up the Docker daemon in rootless mode for your user: diff --git a/pkg/dockerinstall/packages.go b/pkg/dockerinstall/packages.go index 1cd55b7a0..8754d46ac 100644 --- a/pkg/dockerinstall/packages.go +++ b/pkg/dockerinstall/packages.go @@ -12,7 +12,7 @@ func BuildPackageList(version, pkgVersion, cliPkgVersion string, extraPkgs ...st pkgs = append(pkgs, PkgDockerCE+pkgVersion) // CLI and containerd (18.09+) - if versionGte(version, "18.09") { + if versionGte(version, ubuntuRelease1809) { if cliPkgVersion != "" { pkgs = append(pkgs, PkgDockerCECLI+cliPkgVersion, PkgContainerd) } else { @@ -21,12 +21,12 @@ func BuildPackageList(version, pkgVersion, cliPkgVersion string, extraPkgs ...st } // Compose plugin (20.10+) - if versionGte(version, "20.10") { + if versionGte(version, ubuntuRelease2010) { pkgs = append(pkgs, PkgDockerCompose) } // Buildx plugin (23.0+) - if versionGte(version, "23.0") { + if versionGte(version, ubuntuRelease230) { pkgs = append(pkgs, PkgDockerBuildx) } diff --git a/pkg/dockerinstall/packages_test.go b/pkg/dockerinstall/packages_test.go new file mode 100644 index 000000000..be5c17e84 --- /dev/null +++ b/pkg/dockerinstall/packages_test.go @@ -0,0 +1,131 @@ +package dockerinstall + +import ( + "strings" + "testing" + + "github.com/stretchr/testify/suite" +) + +type PackagesTestSuite struct { + suite.Suite +} + +func TestPackagesSuite(t *testing.T) { + suite.Run(t, new(PackagesTestSuite)) +} + +func (s *PackagesTestSuite) TestBuildPackageList_PreCLI() { + version := "17.09" + got := BuildPackageList(version, "=1:17.09.0", "", "docker-ce-rootless-extras") + + pkgs := strings.Split(got, " ") + s.Equal([]string{PkgDockerCE + "=1:17.09.0", "docker-ce-rootless-extras"}, pkgs) + s.NotContains(got, PkgDockerCECLI) + s.NotContains(got, PkgContainerd) + s.NotContains(got, PkgDockerCompose) + s.NotContains(got, PkgDockerBuildx) +} + +func (s *PackagesTestSuite) TestBuildPackageList_CLIWithPinnedCLI() { + version := ubuntuRelease1809 + got := BuildPackageList(version, "=1:18.09.0", "=1:18.09.0-3") + + pkgs := strings.Split(got, " ") + s.Equal( + []string{ + PkgDockerCE + "=1:18.09.0", + PkgDockerCECLI + "=1:18.09.0-3", + PkgContainerd, + }, + pkgs, + ) + s.NotContains(got, PkgDockerCompose) + s.NotContains(got, PkgDockerBuildx) +} + +func (s *PackagesTestSuite) TestBuildPackageList_CLIWithoutPinnedCLI() { + version := ubuntuRelease1809 + got := BuildPackageList(version, "=1:18.09.0", "") + + pkgs := strings.Split(got, " ") + s.Equal( + []string{PkgDockerCE + "=1:18.09.0", PkgDockerCECLI, PkgContainerd}, + pkgs, + ) +} + +func (s *PackagesTestSuite) TestBuildPackageList_ComposeAt2010() { + version := ubuntuRelease2010 + got := BuildPackageList(version, "=5:20.10.0", "=5:20.10.0") + + s.Contains(got, PkgDockerCompose) + s.NotContains(got, PkgDockerBuildx) +} + +func (s *PackagesTestSuite) TestBuildPackageList_BuildxAt230() { + version := ubuntuRelease230 + got := BuildPackageList(version, "=5:23.0.0", "=5:23.0.0") + + pkgs := strings.Split(got, " ") + s.Equal( + []string{ + PkgDockerCE + "=5:23.0.0", + PkgDockerCECLI + "=5:23.0.0", + PkgContainerd, + PkgDockerCompose, + PkgDockerBuildx, + }, + pkgs, + ) +} + +func (s *PackagesTestSuite) TestBuildPackageList_EmptyVersionEnablesAllFeatures() { + got := BuildPackageList("", "", "") + + pkgs := strings.Split(got, " ") + s.Equal( + []string{ + PkgDockerCE, + PkgDockerCECLI, + PkgContainerd, + PkgDockerCompose, + PkgDockerBuildx, + }, + pkgs, + ) +} + +func (s *PackagesTestSuite) TestBuildPackageList_ExtraPackagesAppended() { + got := BuildPackageList(ubuntuRelease230, "", "", PkgDockerRootlessExtras, PkgDockerScan) + + s.True(strings.HasSuffix(got, PkgDockerRootlessExtras+" "+PkgDockerScan)) + s.Contains(got, PkgDockerBuildx) +} + +func (s *PackagesTestSuite) TestVersionGte_Table() { + tests := []struct { + version string + target string + want bool + }{ + {"", ubuntuRelease1809, true}, + {ubuntuRelease1809, ubuntuRelease1809, true}, + {ubuntuRelease2010, ubuntuRelease1809, true}, + {"18.08", ubuntuRelease1809, false}, + {"17.09", ubuntuRelease1809, false}, + {ubuntuRelease2204, ubuntuRelease230, false}, + {"24.0", ubuntuRelease230, true}, + {"23", ubuntuRelease230, true}, + {ubuntuRelease230, ubuntuRelease230, true}, + {"20.10-ce", ubuntuRelease2010, true}, + {"18.09-0~debian", ubuntuRelease1809, true}, + {"abc.0", ubuntuRelease1809, false}, + {"1.2.3", "1.2", true}, + } + + for _, tt := range tests { + got := versionGte(tt.version, tt.target) + s.Equal(tt.want, got, "versionGte(%q, %q)", tt.version, tt.target) + } +} diff --git a/pkg/dockerinstall/validator_test.go b/pkg/dockerinstall/validator_test.go index 724470f6c..0297ca5d3 100644 --- a/pkg/dockerinstall/validator_test.go +++ b/pkg/dockerinstall/validator_test.go @@ -66,7 +66,7 @@ func (s *ValidatorTestSuite) TestCheckWSL_IsWSL_DryRun() { func (s *ValidatorTestSuite) TestCheckDeprecation_NotDeprecated() { validator := NewValidator(s.opts) - distro := &Distro{ID: "ubuntu", Version: "22.04"} + distro := &Distro{ID: DistroUbuntu, Version: ubuntuRelease2204} validator.CheckDeprecation(distro) s.Empty(s.stdout.String()) } @@ -74,7 +74,7 @@ func (s *ValidatorTestSuite) TestCheckDeprecation_NotDeprecated() { func (s *ValidatorTestSuite) TestCheckDeprecation_DeprecatedUbuntu_DryRun() { s.opts.dryRun = true validator := NewValidator(s.opts) - distro := &Distro{ID: "ubuntu", Version: "xenial"} + distro := &Distro{ID: DistroUbuntu, Version: "xenial"} validator.CheckDeprecation(distro) s.Contains(s.stdout.String(), "DEPRECATION WARNING") s.Contains(s.stdout.String(), "ubuntu xenial") @@ -83,7 +83,7 @@ func (s *ValidatorTestSuite) TestCheckDeprecation_DeprecatedUbuntu_DryRun() { func (s *ValidatorTestSuite) TestCheckDeprecation_DeprecatedDebian() { s.opts.dryRun = true validator := NewValidator(s.opts) - distro := &Distro{ID: "debian", Version: CodenameStretch} + distro := &Distro{ID: DistroDebian, Version: CodenameStretch} validator.CheckDeprecation(distro) s.Contains(s.stdout.String(), "DEPRECATION WARNING") s.Contains(s.stdout.String(), "debian stretch") @@ -92,28 +92,36 @@ func (s *ValidatorTestSuite) TestCheckDeprecation_DeprecatedDebian() { func (s *ValidatorTestSuite) TestCheckDeprecation_DeprecatedFedora() { s.opts.dryRun = true validator := NewValidator(s.opts) - distro := &Distro{ID: "fedora", Version: "32"} + distro := &Distro{ID: DistroFedora, Version: "32"} validator.CheckDeprecation(distro) s.Contains(s.stdout.String(), "DEPRECATION WARNING") } func (s *ValidatorTestSuite) TestCheckDeprecation_CurrentFedora() { validator := NewValidator(s.opts) - distro := &Distro{ID: "fedora", Version: "39"} + distro := &Distro{ID: DistroFedora, Version: "39"} validator.CheckDeprecation(distro) s.Empty(s.stdout.String()) } func (s *ValidatorTestSuite) TestValidateDistro_EmptyID() { validator := NewValidator(s.opts) - distro := &Distro{ID: "", Version: "22.04"} + distro := &Distro{ID: "", Version: ubuntuRelease2204} err := validator.ValidateDistro(distro) s.Error(err) s.Contains(s.stderr.String(), "Unable to detect distribution") } func (s *ValidatorTestSuite) TestValidateDistro_Supported() { - supported := []string{"ubuntu", "debian", "raspbian", "centos", "fedora", "rhel", "sles"} + supported := []string{ + DistroUbuntu, + DistroDebian, + DistroRaspbian, + DistroCentOS, + DistroFedora, + DistroRHEL, + DistroSLES, + } for _, id := range supported { s.SetupTest() validator := NewValidator(s.opts)