2018-09-14 13:53:56 -04:00
|
|
|
package versions
|
2018-03-19 18:56:51 -04:00
|
|
|
|
|
|
|
import (
|
2018-09-28 17:06:28 -04:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2018-03-19 18:56:51 -04:00
|
|
|
"testing"
|
|
|
|
|
2018-09-28 17:06:28 -04:00
|
|
|
clitypes "github.com/docker/cli/types"
|
2020-02-22 12:12:14 -05:00
|
|
|
"gotest.tools/v3/assert"
|
2018-03-19 18:56:51 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestParseTagsSimple(t *testing.T) {
|
|
|
|
tags := []string{"1.0.0", "1.1.2", "1.1.1", "1.2.2"}
|
|
|
|
currentVersion := "1.1.0"
|
|
|
|
res, err := parseTags(tags, currentVersion)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
assert.Assert(t, err, "already present")
|
|
|
|
assert.Assert(t, len(res.Downgrades) == 1 && res.Downgrades[0].Tag == "1.0.0")
|
|
|
|
assert.Assert(t, len(res.Patches) == 2 && res.Patches[0].Tag == "1.1.1" && res.Patches[1].Tag == "1.1.2")
|
|
|
|
assert.Assert(t, len(res.Upgrades) == 1 && res.Upgrades[0].Tag == "1.2.2")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseConfirmMinSegments(t *testing.T) {
|
|
|
|
tags := []string{"1", "1.1.1", "2"}
|
|
|
|
currentVersion := "1.1"
|
|
|
|
res, err := parseTags(tags, currentVersion)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
assert.Assert(t, err, "already present")
|
|
|
|
assert.Assert(t, len(res.Downgrades) == 1 && res.Downgrades[0].Tag == "1")
|
|
|
|
assert.Assert(t, len(res.Patches) == 1 && res.Patches[0].Tag == "1.1.1")
|
|
|
|
assert.Assert(t, len(res.Upgrades) == 1 && res.Upgrades[0].Tag == "2")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseTagsFilterPrerelease(t *testing.T) {
|
|
|
|
tags := []string{"1.0.0", "1.1.1", "1.2.2", "1.1.0-beta1"}
|
|
|
|
currentVersion := "1.1.0"
|
|
|
|
res, err := parseTags(tags, currentVersion)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
assert.Assert(t, err, "already present")
|
|
|
|
assert.Assert(t, len(res.Downgrades) == 2 && res.Downgrades[0].Tag == "1.0.0")
|
|
|
|
assert.Assert(t, len(res.Patches) == 1 && res.Patches[0].Tag == "1.1.1")
|
|
|
|
assert.Assert(t, len(res.Upgrades) == 1 && res.Upgrades[0].Tag == "1.2.2")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseTagsBadTag(t *testing.T) {
|
|
|
|
tags := []string{"1.0.0", "1.1.1", "1.2.2", "notasemanticversion"}
|
|
|
|
currentVersion := "1.1.0"
|
|
|
|
res, err := parseTags(tags, currentVersion)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
assert.Assert(t, err, "already present")
|
|
|
|
assert.Assert(t, len(res.Downgrades) == 1 && res.Downgrades[0].Tag == "1.0.0")
|
|
|
|
assert.Assert(t, len(res.Patches) == 1 && res.Patches[0].Tag == "1.1.1")
|
|
|
|
assert.Assert(t, len(res.Upgrades) == 1 && res.Upgrades[0].Tag == "1.2.2")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseBadCurrent(t *testing.T) {
|
|
|
|
tags := []string{"1.0.0", "1.1.2", "1.1.1", "1.2.2"}
|
|
|
|
currentVersion := "notasemanticversion"
|
|
|
|
_, err := parseTags(tags, currentVersion)
|
|
|
|
assert.ErrorContains(t, err, "failed to parse existing")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseBadCurrent2(t *testing.T) {
|
|
|
|
tags := []string{"1.0.0", "1.1.2", "1.1.1", "1.2.2"}
|
|
|
|
currentVersion := ""
|
|
|
|
_, err := parseTags(tags, currentVersion)
|
|
|
|
assert.ErrorContains(t, err, "failed to parse existing")
|
|
|
|
}
|
2018-09-28 17:06:28 -04:00
|
|
|
|
|
|
|
func TestGetCurrentRuntimeMetadataNotPresent(t *testing.T) {
|
|
|
|
tmpdir, err := ioutil.TempDir("", "docker-root")
|
|
|
|
assert.NilError(t, err)
|
|
|
|
defer os.RemoveAll(tmpdir)
|
|
|
|
_, err = GetCurrentRuntimeMetadata(tmpdir)
|
|
|
|
assert.ErrorType(t, err, os.IsNotExist)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetCurrentRuntimeMetadataBadJson(t *testing.T) {
|
|
|
|
tmpdir, err := ioutil.TempDir("", "docker-root")
|
|
|
|
assert.NilError(t, err)
|
|
|
|
defer os.RemoveAll(tmpdir)
|
|
|
|
filename := filepath.Join(tmpdir, clitypes.RuntimeMetadataName+".json")
|
|
|
|
err = ioutil.WriteFile(filename, []byte("not json"), 0644)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
_, err = GetCurrentRuntimeMetadata(tmpdir)
|
|
|
|
assert.ErrorContains(t, err, "malformed runtime metadata file")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetCurrentRuntimeMetadataHappyPath(t *testing.T) {
|
|
|
|
tmpdir, err := ioutil.TempDir("", "docker-root")
|
|
|
|
assert.NilError(t, err)
|
|
|
|
defer os.RemoveAll(tmpdir)
|
|
|
|
metadata := clitypes.RuntimeMetadata{Platform: "platformgoeshere"}
|
|
|
|
err = WriteRuntimeMetadata(tmpdir, &metadata)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
res, err := GetCurrentRuntimeMetadata(tmpdir)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Equal(t, res.Platform, "platformgoeshere")
|
|
|
|
}
|