Skip to content

Commit

Permalink
Merge pull request #1103 from dgageot/upgrade-test
Browse files Browse the repository at this point in the history
Add a test to check version upgrades
  • Loading branch information
dgageot authored Oct 10, 2018
2 parents 7407bf1 + 70566f4 commit 017cf58
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 10 deletions.
38 changes: 28 additions & 10 deletions pkg/skaffold/schema/versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package schema
import (
"github.com/pkg/errors"

version "github.com/GoogleContainerTools/skaffold/pkg/skaffold/apiversion"
apiversion "github.com/GoogleContainerTools/skaffold/pkg/skaffold/apiversion"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/util"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/v1alpha1"
Expand All @@ -34,11 +34,29 @@ type APIVersion struct {
Version string `yaml:"apiVersion"`
}

var schemaVersions = map[string]func() util.VersionedConfig{
v1alpha1.Version: v1alpha1.NewSkaffoldPipeline,
v1alpha2.Version: v1alpha2.NewSkaffoldPipeline,
v1alpha3.Version: v1alpha3.NewSkaffoldPipeline,
latest.Version: latest.NewSkaffoldPipeline,
var schemaVersions = versions{
{v1alpha1.Version, v1alpha1.NewSkaffoldPipeline},
{v1alpha2.Version, v1alpha2.NewSkaffoldPipeline},
{v1alpha3.Version, v1alpha3.NewSkaffoldPipeline},
{latest.Version, latest.NewSkaffoldPipeline},
}

type version struct {
apiVersion string
factory func() util.VersionedConfig
}

type versions []version

// Find search the constructor for a given api version.
func (v *versions) Find(apiVersion string) (func() util.VersionedConfig, bool) {
for _, version := range *v {
if version.apiVersion == apiVersion {
return version.factory, true
}
}

return nil, false
}

// ParseConfig reads a configuration file.
Expand All @@ -53,7 +71,7 @@ func ParseConfig(filename string, applyDefaults bool) (util.VersionedConfig, err
return nil, errors.Wrap(err, "parsing api version")
}

factory, present := schemaVersions[apiVersion.Version]
factory, present := schemaVersions.Find(apiVersion.Version)
if !present {
return nil, errors.Wrapf(err, "unknown version: %s", apiVersion.Version)
}
Expand All @@ -72,16 +90,16 @@ func ParseConfig(filename string, applyDefaults bool) (util.VersionedConfig, err

// CheckVersionIsLatest checks that a given version is the most recent.
func CheckVersionIsLatest(apiVersion string) error {
parsedVersion, err := version.Parse(apiVersion)
parsedVersion, err := apiversion.Parse(apiVersion)
if err != nil {
return errors.Wrap(err, "parsing api version")
}

if parsedVersion.LT(version.MustParse(latest.Version)) {
if parsedVersion.LT(apiversion.MustParse(latest.Version)) {
return errors.New("config version out of date: run `skaffold fix`")
}

if parsedVersion.GT(version.MustParse(latest.Version)) {
if parsedVersion.GT(apiversion.MustParse(latest.Version)) {
return errors.New("config version is too new for this version of skaffold: upgrade skaffold")
}

Expand Down
23 changes: 23 additions & 0 deletions pkg/skaffold/schema/versions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,3 +330,26 @@ func TestCheckVersionIsLatest(t *testing.T) {
})
}
}

func TestUpgradeToNextVersion(t *testing.T) {
for i, schemaVersion := range schemaVersions[0 : len(schemaVersions)-2] {
from := schemaVersion
to := schemaVersions[i+1]
description := fmt.Sprintf("Upgrade from %s to %s", from.apiVersion, to.apiVersion)

t.Run(description, func(t *testing.T) {
factory, _ := schemaVersions.Find(from.apiVersion)
newer, err := factory().Upgrade()

testutil.CheckErrorAndDeepEqual(t, false, err, to.apiVersion, newer.GetVersion())
})
}
}

func TestCantUpgradeFromLastestVersion(t *testing.T) {
factory, present := schemaVersions.Find(latest.Version)
testutil.CheckDeepEqual(t, true, present)

_, err := factory().Upgrade()
testutil.CheckError(t, true, err)
}

0 comments on commit 017cf58

Please sign in to comment.