Skip to content
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
44 changes: 15 additions & 29 deletions common/checks.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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) {
Expand Down
20 changes: 9 additions & 11 deletions common/strutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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())
Expand Down
13 changes: 11 additions & 2 deletions common/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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"}},
})
}
14 changes: 4 additions & 10 deletions ops/unpack.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}
Expand Down
84 changes: 84 additions & 0 deletions ops/unpack_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
Loading