-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract Stage to a model package so that it can be shared by plugin a…
…nd project configuration versions Signed-off-by: Adrian Orive <adrian.orive.oneca@gmail.com>
- Loading branch information
Showing
5 changed files
with
424 additions
and
360 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/* | ||
Copyright 2020 The Kubernetes Authors. | ||
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 stage | ||
|
||
import ( | ||
"errors" | ||
) | ||
|
||
var errInvalid = errors.New("invalid version stage") | ||
|
||
// Stage represents the stability of a version | ||
type Stage uint8 | ||
|
||
// Order Stage in decreasing degree of stability for comparison purposes. | ||
// Stable must be 0 so that it is the default Stage | ||
const ( // The order in this const declaration will be used to order version stages except for Stable | ||
// Stable should be used for plugins that are rarely changed in backwards-compatible ways, e.g. bug fixes. | ||
Stable Stage = iota | ||
// Beta should be used for plugins that may be changed in minor ways and are not expected to break between uses. | ||
Beta Stage = iota | ||
// Alpha should be used for plugins that are frequently changed and may break between uses. | ||
Alpha Stage = iota | ||
) | ||
|
||
const ( | ||
alpha = "alpha" | ||
beta = "beta" | ||
stable = "" | ||
) | ||
|
||
// ParseStage parses stage into a Stage, assuming it is one of the valid stages | ||
func ParseStage(stage string) (Stage, error) { | ||
var s Stage | ||
return s, s.Parse(stage) | ||
} | ||
|
||
// Parse parses stage inline, assuming it is one of the valid stages | ||
func (s *Stage) Parse(stage string) error { | ||
switch stage { | ||
case alpha: | ||
*s = Alpha | ||
case beta: | ||
*s = Beta | ||
case stable: | ||
*s = Stable | ||
default: | ||
return errInvalid | ||
} | ||
return nil | ||
} | ||
|
||
// String returns the string representation of s | ||
func (s Stage) String() string { | ||
switch s { | ||
case Alpha: | ||
return alpha | ||
case Beta: | ||
return beta | ||
case Stable: | ||
return stable | ||
default: | ||
panic(errInvalid) | ||
} | ||
} | ||
|
||
// Validate ensures that the stage is one of the valid stages | ||
func (s Stage) Validate() error { | ||
switch s { | ||
case Alpha: | ||
case Beta: | ||
case Stable: | ||
default: | ||
return errInvalid | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// Compare returns -1 if s < other, 0 if s == other, and 1 if s > other. | ||
func (s Stage) Compare(other Stage) int { | ||
if s == other { | ||
return 0 | ||
} | ||
|
||
// Stage are sorted in decreasing order | ||
if s > other { | ||
return -1 | ||
} | ||
return 1 | ||
} | ||
|
||
// IsStable returns whether the stage is stable or not | ||
func (s Stage) IsStable() bool { | ||
return s == Stable | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
/* | ||
Copyright 2020 The Kubernetes Authors. | ||
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 stage | ||
|
||
import ( | ||
"sort" | ||
"testing" | ||
|
||
g "github.com/onsi/ginkgo" // An alias is required because Context is defined elsewhere in this package. | ||
. "github.com/onsi/ginkgo/extensions/table" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func TestStage(t *testing.T) { | ||
RegisterFailHandler(g.Fail) | ||
g.RunSpecs(t, "Stage Suite") | ||
} | ||
|
||
var _ = g.Describe("ParseStage", func() { | ||
DescribeTable("should be correctly parsed for valid stage strings", | ||
func(str string, stage Stage) { | ||
s, err := ParseStage(str) | ||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(s).To(Equal(stage)) | ||
}, | ||
Entry("for alpha stage", "alpha", Alpha), | ||
Entry("for beta stage", "beta", Beta), | ||
Entry("for stable stage", "", Stable), | ||
) | ||
|
||
DescribeTable("should error when parsing invalid stage strings", | ||
func(str string) { | ||
_, err := ParseStage(str) | ||
Expect(err).To(HaveOccurred()) | ||
}, | ||
Entry("passing a number as the stage string", "1"), | ||
Entry("passing `gamma` as the stage string", "gamma"), | ||
Entry("passing a dash-prefixed stage string", "-alpha"), | ||
) | ||
}) | ||
|
||
var _ = g.Describe("Stage", func() { | ||
g.Context("String", func() { | ||
DescribeTable("should return the correct string value", | ||
func(stage Stage, str string) { Expect(stage.String()).To(Equal(str)) }, | ||
Entry("for alpha stage", Alpha, "alpha"), | ||
Entry("for beta stage", Beta, "beta"), | ||
Entry("for stable stage", Stable, ""), | ||
) | ||
|
||
DescribeTable("should panic", | ||
func(stage Stage) { Expect(func() { _ = stage.String() }).To(Panic()) }, | ||
Entry("for stage 34", Stage(34)), | ||
Entry("for stage 75", Stage(75)), | ||
Entry("for stage 123", Stage(123)), | ||
Entry("for stage 255", Stage(255)), | ||
) | ||
}) | ||
|
||
g.Context("Validate", func() { | ||
DescribeTable("should validate existing stages", | ||
func(stage Stage) { Expect(stage.Validate()).To(Succeed()) }, | ||
Entry("for alpha stage", Alpha), | ||
Entry("for beta stage", Beta), | ||
Entry("for stable stage", Stable), | ||
) | ||
|
||
DescribeTable("should fail for non-existing stages", | ||
func(stage Stage) { Expect(stage.Validate()).NotTo(Succeed()) }, | ||
Entry("for stage 34", Stage(34)), | ||
Entry("for stage 75", Stage(75)), | ||
Entry("for stage 123", Stage(123)), | ||
Entry("for stage 255", Stage(255)), | ||
) | ||
}) | ||
|
||
g.Context("Compare", func() { | ||
// Test Stage.Compare by sorting a list | ||
var ( | ||
stages = []Stage{ | ||
Stable, | ||
Alpha, | ||
Stable, | ||
Beta, | ||
Beta, | ||
Alpha, | ||
} | ||
|
||
sortedStages = []Stage{ | ||
Alpha, | ||
Alpha, | ||
Beta, | ||
Beta, | ||
Stable, | ||
Stable, | ||
} | ||
) | ||
|
||
g.It("sorts stages correctly", func() { | ||
sort.Slice(stages, func(i int, j int) bool { | ||
return stages[i].Compare(stages[j]) == -1 | ||
}) | ||
Expect(stages).To(Equal(sortedStages)) | ||
}) | ||
}) | ||
|
||
g.Context("IsStable", func() { | ||
g.It("should return true for stable stage", func() { | ||
Expect(Stable.IsStable()).To(BeTrue()) | ||
}) | ||
|
||
DescribeTable("should return false for any unstable stage", | ||
func(stage Stage) { Expect(stage.IsStable()).To(BeFalse()) }, | ||
Entry("for alpha stage", Alpha), | ||
Entry("for beta stage", Beta), | ||
) | ||
}) | ||
}) |
Oops, something went wrong.