From 70566f4bd42964315e7afd0cd1e6820ef9fd4564 Mon Sep 17 00:00:00 2001 From: David Gageot Date: Fri, 5 Oct 2018 14:52:59 +0200 Subject: [PATCH] Add a test to check version upgrades Each version should upgrade to the next version. This test is an additional safety net when adding a new version. Signed-off-by: David Gageot --- pkg/skaffold/schema/versions.go | 38 ++++++++++++++++++++-------- pkg/skaffold/schema/versions_test.go | 23 +++++++++++++++++ 2 files changed, 51 insertions(+), 10 deletions(-) diff --git a/pkg/skaffold/schema/versions.go b/pkg/skaffold/schema/versions.go index 0a414c49be0..4ab44a79028 100644 --- a/pkg/skaffold/schema/versions.go +++ b/pkg/skaffold/schema/versions.go @@ -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" @@ -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. @@ -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) } @@ -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") } diff --git a/pkg/skaffold/schema/versions_test.go b/pkg/skaffold/schema/versions_test.go index ccfefd3bae6..cdaedf399a3 100644 --- a/pkg/skaffold/schema/versions_test.go +++ b/pkg/skaffold/schema/versions_test.go @@ -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) +}