sigs.k8s.io/kubebuilder/v3@v3.14.0/pkg/config/version_test.go (about) 1 /* 2 Copyright 2022 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package config 18 19 import ( 20 "sort" 21 22 . "github.com/onsi/ginkgo/v2" 23 . "github.com/onsi/gomega" 24 25 "sigs.k8s.io/kubebuilder/v3/pkg/model/stage" 26 ) 27 28 var _ = Describe("Version", func() { 29 // Parse, String and Validate are tested by MarshalJSON and UnmarshalJSON 30 31 Context("Compare", func() { 32 // Test Compare() by sorting a list. 33 var ( 34 versions = []Version{ 35 {Number: 2, Stage: stage.Alpha}, 36 {Number: 44, Stage: stage.Alpha}, 37 {Number: 1}, 38 {Number: 2, Stage: stage.Beta}, 39 {Number: 4, Stage: stage.Beta}, 40 {Number: 1, Stage: stage.Alpha}, 41 {Number: 4}, 42 {Number: 44, Stage: stage.Alpha}, 43 {Number: 30}, 44 {Number: 4, Stage: stage.Alpha}, 45 } 46 47 sortedVersions = []Version{ 48 {Number: 1, Stage: stage.Alpha}, 49 {Number: 1}, 50 {Number: 2, Stage: stage.Alpha}, 51 {Number: 2, Stage: stage.Beta}, 52 {Number: 4, Stage: stage.Alpha}, 53 {Number: 4, Stage: stage.Beta}, 54 {Number: 4}, 55 {Number: 30}, 56 {Number: 44, Stage: stage.Alpha}, 57 {Number: 44, Stage: stage.Alpha}, 58 } 59 ) 60 61 It("sorts a valid list of versions correctly", func() { 62 sort.Slice(versions, func(i int, j int) bool { 63 return versions[i].Compare(versions[j]) == -1 64 }) 65 Expect(versions).To(Equal(sortedVersions)) 66 }) 67 }) 68 69 Context("IsStable", func() { 70 DescribeTable("should return true for stable versions", 71 func(version Version) { Expect(version.IsStable()).To(BeTrue()) }, 72 Entry("for version 1", Version{Number: 1}), 73 Entry("for version 1 (stable)", Version{Number: 1, Stage: stage.Stable}), 74 Entry("for version 22", Version{Number: 22}), 75 Entry("for version 22 (stable)", Version{Number: 22, Stage: stage.Stable}), 76 ) 77 78 DescribeTable("should return false for unstable versions", 79 func(version Version) { Expect(version.IsStable()).To(BeFalse()) }, 80 Entry("for version 1 (alpha)", Version{Number: 1, Stage: stage.Alpha}), 81 Entry("for version 1 (beta)", Version{Number: 1, Stage: stage.Beta}), 82 Entry("for version 22 (alpha)", Version{Number: 22, Stage: stage.Alpha}), 83 Entry("for version 22 (beta)", Version{Number: 22, Stage: stage.Beta}), 84 ) 85 }) 86 87 Context("MarshalJSON", func() { 88 DescribeTable("should be marshalled appropriately", 89 func(version Version, str string) { 90 b, err := version.MarshalJSON() 91 Expect(err).NotTo(HaveOccurred()) 92 Expect(string(b)).To(Equal(str)) 93 }, 94 Entry("for version 1", Version{Number: 1}, `"1"`), 95 Entry("for version 1 (stable)", Version{Number: 1, Stage: stage.Stable}, `"1"`), 96 Entry("for version 1 (alpha)", Version{Number: 1, Stage: stage.Alpha}, `"1-alpha"`), 97 Entry("for version 1 (beta)", Version{Number: 1, Stage: stage.Beta}, `"1-beta"`), 98 Entry("for version 22", Version{Number: 22}, `"22"`), 99 Entry("for version 22 (stable)", Version{Number: 22, Stage: stage.Stable}, `"22"`), 100 Entry("for version 22 (alpha)", Version{Number: 22, Stage: stage.Alpha}, `"22-alpha"`), 101 Entry("for version 22 (beta)", Version{Number: 22, Stage: stage.Beta}, `"22-beta"`), 102 ) 103 104 DescribeTable("should fail to be marshalled", 105 func(version Version) { 106 _, err := version.MarshalJSON() 107 Expect(err).To(HaveOccurred()) 108 }, 109 Entry("for version 0", Version{Number: 0}), 110 Entry("for version 0 (stable)", Version{Number: 0, Stage: stage.Stable}), 111 Entry("for version 0 (alpha)", Version{Number: 0, Stage: stage.Alpha}), 112 Entry("for version 0 (beta)", Version{Number: 0, Stage: stage.Beta}), 113 Entry("for version 0 (implicit)", Version{}), 114 Entry("for version 0 (stable) (implicit)", Version{Stage: stage.Stable}), 115 Entry("for version 0 (alpha) (implicit)", Version{Stage: stage.Alpha}), 116 Entry("for version 0 (beta) (implicit)", Version{Stage: stage.Beta}), 117 Entry("for version -1", Version{Number: -1}), 118 Entry("for version -1 (stable)", Version{Number: -1, Stage: stage.Stable}), 119 Entry("for version -1 (alpha)", Version{Number: -1, Stage: stage.Alpha}), 120 Entry("for version -1 (beta)", Version{Number: -1, Stage: stage.Beta}), 121 Entry("for invalid stage", Version{Stage: stage.Stage(34)}), 122 ) 123 }) 124 125 Context("UnmarshalJSON", func() { 126 DescribeTable("should be unmarshalled appropriately", 127 func(str string, number int, s stage.Stage) { 128 var v Version 129 err := v.UnmarshalJSON([]byte(str)) 130 Expect(err).NotTo(HaveOccurred()) 131 Expect(v.Number).To(Equal(number)) 132 Expect(v.Stage).To(Equal(s)) 133 }, 134 Entry("for version string `1`", `"1"`, 1, stage.Stable), 135 Entry("for version string `1-alpha`", `"1-alpha"`, 1, stage.Alpha), 136 Entry("for version string `1-beta`", `"1-beta"`, 1, stage.Beta), 137 Entry("for version string `22`", `"22"`, 22, stage.Stable), 138 Entry("for version string `22-alpha`", `"22-alpha"`, 22, stage.Alpha), 139 Entry("for version string `22-beta`", `"22-beta"`, 22, stage.Beta), 140 ) 141 142 DescribeTable("should fail to be unmarshalled", 143 func(str string) { 144 var v Version 145 err := v.UnmarshalJSON([]byte(str)) 146 Expect(err).To(HaveOccurred()) 147 }, 148 Entry("for empty version string", ``), 149 Entry("for version string ``", `""`), 150 Entry("for version string `0`", `"0"`), 151 Entry("for version string `0-alpha`", `"0-alpha"`), 152 Entry("for version string `0-beta`", `"0-beta"`), 153 Entry("for version string `-1`", `"-1"`), 154 Entry("for version string `-1-alpha`", `"-1-alpha"`), 155 Entry("for version string `-1-beta`", `"-1-beta"`), 156 Entry("for version string `v1`", `"v1"`), 157 Entry("for version string `v1-alpha`", `"v1-alpha"`), 158 Entry("for version string `v1-beta`", `"v1-beta"`), 159 Entry("for version string `1.0`", `"1.0"`), 160 Entry("for version string `v1.0`", `"v1.0"`), 161 Entry("for version string `v1.0-alpha`", `"v1.0-alpha"`), 162 Entry("for version string `1.0.0`", `"1.0.0"`), 163 Entry("for version string `1-a`", `"1-a"`), 164 ) 165 }) 166 })