get.porter.sh/porter@v1.3.0/pkg/schema/check-strategy_test.go (about) 1 package schema 2 3 import ( 4 "testing" 5 6 "get.porter.sh/porter/tests" 7 "github.com/Masterminds/semver/v3" 8 "github.com/stretchr/testify/assert" 9 "github.com/stretchr/testify/require" 10 ) 11 12 func TestValidateSchemaVersion(t *testing.T) { 13 testcases := []struct { 14 Name string 15 Strategy CheckStrategy 16 Supported string 17 Default string 18 Specified string 19 WantWarning bool 20 WantErr string 21 }{ 22 {Name: "range - in range", Strategy: CheckStrategyExact, Supported: "1.0.0-alpha.1 - 1.0.0-z || 1.0.0 - 1.0.1", Specified: "1.0.0-alpha.2", Default: "1.0.1"}, 23 {Name: "range - out of range", Strategy: CheckStrategyExact, Supported: "1.0.0-alpha.1 - 1.0.1", Specified: "1.1.0", Default: "1.0.1", 24 WantErr: "the schema version is 1.1.0 but the supported schema version is >=1.0.0-alpha.1 <=1.0.1"}, 25 {Name: "exact - exact match", Strategy: CheckStrategyExact, Supported: "=1.0.0", Specified: "1.0.0", Default: "1.0.0"}, 26 {Name: "exact - patch mismatch", Strategy: CheckStrategyExact, Supported: "=1.0.0", Specified: "1.0.1", Default: "1.0.0", 27 WantErr: "the schema version is 1.0.1 but the supported schema version is =1.0.0"}, 28 {Name: "minor - exact match", Strategy: CheckStrategyMinor, Supported: "=1.1.2", Specified: "1.1.2", Default: "1.1.2"}, 29 {Name: "minor - minor match", Strategy: CheckStrategyMinor, Supported: "=1.1.2", Specified: "1.1.1", Default: "1.1.2", 30 WantWarning: true, WantErr: "WARNING: the schema version is 1.1.1 but the supported schema version is =1.1.2"}, 31 {Name: "minor - minor mismatch", Strategy: CheckStrategyMinor, Supported: "=1.2.0", Specified: "1.1.0", Default: "1.2.0", 32 WantErr: "the schema version MAJOR.MINOR values do not match"}, 33 {Name: "major - exact match", Strategy: CheckStrategyMajor, Supported: "=1.1.2", Specified: "1.1.2", Default: "1.2.0"}, 34 {Name: "major - major match", Strategy: CheckStrategyMajor, Supported: "=1.1.2", Specified: "1.1.1", Default: "1.1.2", 35 WantWarning: true, WantErr: "WARNING: the schema version is 1.1.1 but the supported schema version is =1.1.2"}, 36 {Name: "major - major mismatch", Strategy: CheckStrategyMajor, Supported: "=1.1.2", Specified: "2.0.0", Default: "1.1.2", 37 WantErr: "the schema version MAJOR values do not match"}, 38 {Name: "none - mismatch", Strategy: CheckStrategyNone, Supported: "=1.0.0", Specified: "2.0.0", Default: "1.0.0", 39 WantWarning: true, WantErr: "WARNING: the schema version is 2.0.0 but the supported schema version is =1.0.0"}, 40 {Name: "none - invalid version", Strategy: CheckStrategyNone, Supported: "=1.0.0", Specified: "", Default: "1.0.0", 41 WantWarning: true, WantErr: "(none) is not a valid semantic version"}, 42 } 43 for _, tc := range testcases { 44 t.Run(tc.Name, func(t *testing.T) { 45 supportedC, err := semver.NewConstraint(tc.Supported) 46 require.NoError(t, err, "Failed to parse supported constraint") 47 defaultV, err := semver.NewVersion(tc.Default) 48 require.NoError(t, err, "Failed to parse default version") 49 50 gotWarning, err := ValidateSchemaVersion(tc.Strategy, supportedC, tc.Specified, defaultV) 51 if tc.WantErr != "" { 52 tests.RequireErrorContains(t, err, tc.WantErr, "incorrect message returned") 53 } else { 54 require.NoError(t, err, "ValidateSchemaVersion failed") 55 } 56 57 assert.Equal(t, tc.WantWarning, gotWarning, "unexpected warning result") 58 }) 59 } 60 }