diff --git a/common/checks.go b/common/checks.go index 58af96c2..3f1fc162 100644 --- a/common/checks.go +++ b/common/checks.go @@ -577,38 +577,24 @@ func IsIPV4(ip string) bool { return true } -// Gets three integers for a version string -// Converts "1.2.3" into []int{1, 2, 3} +var fullVersionPattern = regexp.MustCompile(`^(?:[^.0-9-]+)?(\d+\.\d+\.\d+(?:\.\d+)?)$`) + +// VersionToList parses a version with an optional prefix and fourth component. func VersionToList(version string) ([]int, error) { - // A valid version must be made of 3 integers - re1 := regexp.MustCompile(`^(\d+)\.(\d+)\.(\d+)$`) - // Also valid version is 3 numbers with a prefix - re2 := regexp.MustCompile(`^[^.0-9-]+(\d+)\.(\d+)\.(\d+)$`) - verList1 := re1.FindAllStringSubmatch(version, -1) - verList2 := re2.FindAllStringSubmatch(version, -1) - verList := verList1 - //CondPrintf("%#v\n", verList) - if verList == nil { - verList = verList2 - } - if verList == nil { - return []int{-1}, fmt.Errorf("required version format: x.x.xx - Got '%s'", version) - } - - var intList = make([]int, 3) - newCount := 0 - for N, item := range verList[0] { - if N == 0 { - continue - } - intVal, err := strconv.Atoi(item) + match := fullVersionPattern.FindStringSubmatch(version) + if match == nil { + return []int{-1}, fmt.Errorf("required version format: x.y.z or x.y.z.w - Got '%s'", version) + } + parts := strings.Split(match[1], ".") + numbers := make([]int, len(parts)) + for i, part := range parts { + value, err := strconv.Atoi(part) if err != nil { - return []int{-1}, fmt.Errorf("(%d) error converting %s (list: %+v) [%s] ", N, version, verList, err) + return []int{-1}, fmt.Errorf("invalid version component %q in %q: %w", part, version, err) } - intList[newCount] = intVal - newCount++ + numbers[i] = value } - return intList, nil + return numbers, nil } // Converts a version string into a name. @@ -620,7 +606,7 @@ func VersionToName(version string) string { } // VersionToPort converts a version string into a port number -// e.g. "5.6.33" -> 5633 +// e.g. "5.6.33" -> 5633. A fourth component does not change the base port. // Note that if the conversion exceeds the maximum port (65,535), such as 10.10.x, // the actual number will be reduced by 60,000 func VersionToPort(version string) (int, error) { diff --git a/common/strutils.go b/common/strutils.go index fd505f99..e3df8816 100644 --- a/common/strutils.go +++ b/common/strutils.go @@ -219,15 +219,13 @@ func SortVersionsSubset(versions []string, wanted string) (sorted []string) { } } sort.Slice(vlist, func(a, b int) bool { - majA := vlist[a].majMinRev[0] - minA := vlist[a].majMinRev[1] - revA := vlist[a].majMinRev[2] - majB := vlist[b].majMinRev[0] - minB := vlist[b].majMinRev[1] - revB := vlist[b].majMinRev[2] - return majA < majB || - (majA == majB && minA < minB) || - (majA == majB && minA == minB && revA < revB) + left, right := vlist[a].majMinRev, vlist[b].majMinRev + for i := 0; i < len(left) && i < len(right); i++ { + if left[i] != right[i] { + return left[i] < right[i] + } + } + return len(left) < len(right) }) for _, v := range vlist { sorted = append(sorted, v.text) @@ -366,11 +364,11 @@ func LatestVersion(searchDir, pattern string) string { files, err := os.ReadDir(searchDir) ErrCheckExitf(err, 1, "ERROR reading directory %s: %s", searchDir, err) var matchingList []string - validPattern := regexp.MustCompile(`\d+\.\d+$`) + validPattern := regexp.MustCompile(`^\d+\.\d+$`) if !validPattern.MatchString(pattern) { Exit(1, "Invalid pattern. Must be '#.#'") } - re := regexp.MustCompile(fmt.Sprintf(`^%s\.\d+$`, pattern)) + re := regexp.MustCompile(fmt.Sprintf(`^%s\.\d+(?:\.\d+)?$`, regexp.QuoteMeta(pattern))) for _, f := range files { if f.IsDir() && re.MatchString(f.Name()) { matchingList = append(matchingList, f.Name()) diff --git a/common/version_test.go b/common/version_test.go index 01cb8b27..266f6f9c 100644 --- a/common/version_test.go +++ b/common/version_test.go @@ -54,7 +54,7 @@ func TestVersionToPort(t *testing.T) { {"5096", -1}, // FAIL: No separators {"50.96", -1}, // FAIL: Not enough separators {"dummy", -1}, // FAIL: Not numbers - {"5.0.96.2", -1}, // FAIL: Too many components + {"5.0.96.2", 5096}, // OK: fourth component leaves base port unchanged {"5.0.96", 5096}, // OK: 5.0 {"5.1.72", 5172}, // OK: 5.1 {"5.5.55", 5555}, // OK: 5.5 @@ -111,7 +111,7 @@ func TestVersionToList(t *testing.T) { {"5096", []int{-1}}, // FAIL: No separators {"50.96", []int{-1}}, // FAIL: Not enough separators {"dummy", []int{-1}}, // FAIL: Not numbers - {"5.0.96.2", []int{-1}}, // FAIL: Too many components + {"5.0.96.2.3", []int{-1}}, // FAIL: Too many components {"5.0.96", []int{5, 0, 96}}, // OK: 5.0 {"5.1.72", []int{5, 1, 72}}, // OK: 5.1 {"5.5.55", []int{5, 5, 55}}, // OK: 5.5 @@ -124,6 +124,8 @@ func TestVersionToList(t *testing.T) { {"ma10.10.1", []int{10, 10, 1}}, // OK: 10.10 with prefix {"ma15.15.1", []int{15, 15, 1}}, // OK: 15.15 with prefix {"22.12.2", []int{22, 12, 2}}, // OK: 12.12 + + {"8.4.13.1", []int{8, 4, 13, 1}}, // OK: fourth component } for _, vl := range versions { version := vl.version @@ -271,3 +273,10 @@ func TestSortVersions(t *testing.T) { checkSortVersion(t, sd) } } + +func TestSortFourComponentVersions(t *testing.T) { + checkSortVersion(t, sortVersionData{ + data: []string{"8.4.13.10", "8.4.14", "8.4.13.2", "8.4.13"}, + expected: []expectedData{{0, "8.4.13"}, {1, "8.4.13.2"}, {2, "8.4.13.10"}, {3, "8.4.14"}}, + }) +} diff --git a/ops/unpack.go b/ops/unpack.go index f834cd36..7cc7557c 100644 --- a/ops/unpack.go +++ b/ops/unpack.go @@ -47,14 +47,9 @@ func UnpackTarball(options UnpackOptions) error { return fmt.Errorf(globals.ErrDirectoryNotFound, Basedir) } tarball := options.TarballName - reVersion := regexp.MustCompile(`(\d+\.\d+\.\d+)`) - verList := reVersion.FindAllStringSubmatch(tarball, -1) - - detectedVersion := "" - if verList != nil { - detectedVersion = verList[0][0] - } - // common.CondPrintf(">> %#v %s\n",verList, detected_version) + // Versioned download directories are unrelated to the archive's version. + reVersion := regexp.MustCompile(`\d+\.\d+\.\d+(?:\.\d+)*`) + detectedVersion := reVersion.FindString(common.BaseName(tarball)) isShell := options.IsShell target := options.TargetServer @@ -80,8 +75,7 @@ func UnpackTarball(options UnpackOptions) error { return fmt.Errorf("unpack: No version was detected from tarball name. " + "Flag --unpack-version becomes mandatory") } - // This call used to ensure that the port provided is in the right format - _, err := common.VersionToPort(Version) + _, err := common.VersionToList(Version) if err != nil { return fmt.Errorf("version %s not in the required format", Version) } diff --git a/ops/unpack_test.go b/ops/unpack_test.go new file mode 100644 index 00000000..8af82b6f --- /dev/null +++ b/ops/unpack_test.go @@ -0,0 +1,84 @@ +// DBDeployer - The MySQL Sandbox +// Copyright © 2006-2022 Giuseppe Maxia +// +// 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 ops + +import ( + "archive/tar" + "compress/gzip" + "os" + "path/filepath" + "testing" + + "github.com/ProxySQL/dbdeployer/common" +) + +func TestUnpackDistinctMySQLVersions(t *testing.T) { + root := t.TempDir() + archives := filepath.Join(root, "downloads") + versionedPath := filepath.Join(archives, "1.2.3") + binaries := filepath.Join(root, "binaries") + for _, dir := range []string{archives, versionedPath, binaries} { + if err := os.Mkdir(dir, 0755); err != nil { + t.Fatal(err) + } + } + for _, version := range []string{"8.4.13", "8.4.13.1", "9.7.4.1", "26.10.0.1"} { + name := "mysql-" + version + "-linux-glibc2.28-x86_64-minimal" + filename := filepath.Join(archives, name+".tar.gz") + if version == "26.10.0.1" { + filename = filepath.Join(versionedPath, name+".tar.gz") + } + writeVersionArchive(t, filename, name, version) + if err := UnpackTarball(UnpackOptions{SandboxBinary: binaries, TarballName: filename}); err != nil { + t.Fatalf("unpack MySQL %s: %v", version, err) + } + } + for _, version := range []string{"8.4.13", "8.4.13.1", "9.7.4.1", "26.10.0.1"} { + data, err := os.ReadFile(filepath.Join(binaries, version, "VERSION")) + if err != nil || string(data) != version { + t.Errorf("unpacked MySQL %s: got %q, err %v", version, data, err) + } + } + if got := common.LatestVersion(binaries, "8.4"); got != "8.4.13.1" { + t.Errorf("latest installed MySQL 8.4 = %q, want 8.4.13.1", got) + } +} + +func writeVersionArchive(t *testing.T, filename, name, version string) { + t.Helper() + file, err := os.Create(filename) + if err != nil { + t.Fatal(err) + } + defer file.Close() + gz := gzip.NewWriter(file) + tw := tar.NewWriter(gz) + if err := tw.WriteHeader(&tar.Header{Name: name + "/", Typeflag: tar.TypeDir, Mode: 0755}); err != nil { + t.Fatal(err) + } + if err := tw.WriteHeader(&tar.Header{Name: name + "/VERSION", Mode: 0644, Size: int64(len(version))}); err != nil { + t.Fatal(err) + } + if _, err := tw.Write([]byte(version)); err != nil { + t.Fatal(err) + } + if err := tw.Close(); err != nil { + t.Fatal(err) + } + if err := gz.Close(); err != nil { + t.Fatal(err) + } +}