sigs.k8s.io/kubebuilder/v3@v3.14.0/pkg/plugin/version_test.go (about)

     1  /*
     2  Copyright 2020 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 plugin
    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  	Context("Parse", func() {
    30  		DescribeTable("should be correctly parsed for valid version strings",
    31  			func(str string, number int, s stage.Stage) {
    32  				var v Version
    33  				Expect(v.Parse(str)).To(Succeed())
    34  				Expect(v.Number).To(Equal(number))
    35  				Expect(v.Stage).To(Equal(s))
    36  			},
    37  			Entry("for version string `0`", "0", 0, stage.Stable),
    38  			Entry("for version string `0-alpha`", "0-alpha", 0, stage.Alpha),
    39  			Entry("for version string `0-beta`", "0-beta", 0, stage.Beta),
    40  			Entry("for version string `1`", "1", 1, stage.Stable),
    41  			Entry("for version string `1-alpha`", "1-alpha", 1, stage.Alpha),
    42  			Entry("for version string `1-beta`", "1-beta", 1, stage.Beta),
    43  			Entry("for version string `v1`", "v1", 1, stage.Stable),
    44  			Entry("for version string `v1-alpha`", "v1-alpha", 1, stage.Alpha),
    45  			Entry("for version string `v1-beta`", "v1-beta", 1, stage.Beta),
    46  			Entry("for version string `22`", "22", 22, stage.Stable),
    47  			Entry("for version string `22-alpha`", "22-alpha", 22, stage.Alpha),
    48  			Entry("for version string `22-beta`", "22-beta", 22, stage.Beta),
    49  		)
    50  
    51  		DescribeTable("should error when parsing an invalid version string",
    52  			func(str string) {
    53  				var v Version
    54  				Expect(v.Parse(str)).NotTo(Succeed())
    55  			},
    56  			Entry("for version string ``", ""),
    57  			Entry("for version string `-1`", "-1"),
    58  			Entry("for version string `-1-alpha`", "-1-alpha"),
    59  			Entry("for version string `-1-beta`", "-1-beta"),
    60  			Entry("for version string `1.0`", "1.0"),
    61  			Entry("for version string `v1.0`", "v1.0"),
    62  			Entry("for version string `v1.0-alpha`", "v1.0-alpha"),
    63  			Entry("for version string `1.0.0`", "1.0.0"),
    64  			Entry("for version string `1-a`", "1-a"),
    65  		)
    66  	})
    67  
    68  	Context("String", func() {
    69  		DescribeTable("should return the correct string value",
    70  			func(version Version, str string) { Expect(version.String()).To(Equal(str)) },
    71  			Entry("for version 0", Version{Number: 0}, "v0"),
    72  			Entry("for version 0 (stable)", Version{Number: 0, Stage: stage.Stable}, "v0"),
    73  			Entry("for version 0 (alpha)", Version{Number: 0, Stage: stage.Alpha}, "v0-alpha"),
    74  			Entry("for version 0 (beta)", Version{Number: 0, Stage: stage.Beta}, "v0-beta"),
    75  			Entry("for version 0 (implicit)", Version{}, "v0"),
    76  			Entry("for version 0 (stable) (implicit)", Version{Stage: stage.Stable}, "v0"),
    77  			Entry("for version 0 (alpha) (implicit)", Version{Stage: stage.Alpha}, "v0-alpha"),
    78  			Entry("for version 0 (beta) (implicit)", Version{Stage: stage.Beta}, "v0-beta"),
    79  			Entry("for version 1", Version{Number: 1}, "v1"),
    80  			Entry("for version 1 (stable)", Version{Number: 1, Stage: stage.Stable}, "v1"),
    81  			Entry("for version 1 (alpha)", Version{Number: 1, Stage: stage.Alpha}, "v1-alpha"),
    82  			Entry("for version 1 (beta)", Version{Number: 1, Stage: stage.Beta}, "v1-beta"),
    83  			Entry("for version 22", Version{Number: 22}, "v22"),
    84  			Entry("for version 22 (stable)", Version{Number: 22, Stage: stage.Stable}, "v22"),
    85  			Entry("for version 22 (alpha)", Version{Number: 22, Stage: stage.Alpha}, "v22-alpha"),
    86  			Entry("for version 22 (beta)", Version{Number: 22, Stage: stage.Beta}, "v22-beta"),
    87  		)
    88  	})
    89  
    90  	Context("Validate", func() {
    91  		DescribeTable("should validate valid versions",
    92  			func(version Version) { Expect(version.Validate()).To(Succeed()) },
    93  			Entry("for version 0", Version{Number: 0}),
    94  			Entry("for version 0 (stable)", Version{Number: 0, Stage: stage.Stable}),
    95  			Entry("for version 0 (alpha)", Version{Number: 0, Stage: stage.Alpha}),
    96  			Entry("for version 0 (beta)", Version{Number: 0, Stage: stage.Beta}),
    97  			Entry("for version 0 (implicit)", Version{}),
    98  			Entry("for version 0 (stable) (implicit)", Version{Stage: stage.Stable}),
    99  			Entry("for version 0 (alpha) (implicit)", Version{Stage: stage.Alpha}),
   100  			Entry("for version 0 (beta) (implicit)", Version{Stage: stage.Beta}),
   101  			Entry("for version 1", Version{Number: 1}),
   102  			Entry("for version 1 (stable)", Version{Number: 1, Stage: stage.Stable}),
   103  			Entry("for version 1 (alpha)", Version{Number: 1, Stage: stage.Alpha}),
   104  			Entry("for version 1 (beta)", Version{Number: 1, Stage: stage.Beta}),
   105  			Entry("for version 22", Version{Number: 22}),
   106  			Entry("for version 22 (stable)", Version{Number: 22, Stage: stage.Stable}),
   107  			Entry("for version 22 (alpha)", Version{Number: 22, Stage: stage.Alpha}),
   108  			Entry("for version 22 (beta)", Version{Number: 22, Stage: stage.Beta}),
   109  		)
   110  
   111  		DescribeTable("should fail for invalid versions",
   112  			func(version Version) { Expect(version.Validate()).NotTo(Succeed()) },
   113  			Entry("for version -1", Version{Number: -1}),
   114  			Entry("for version -1 (stable)", Version{Number: -1, Stage: stage.Stable}),
   115  			Entry("for version -1 (alpha)", Version{Number: -1, Stage: stage.Alpha}),
   116  			Entry("for version -1 (beta)", Version{Number: -1, Stage: stage.Beta}),
   117  			Entry("for invalid stage", Version{Stage: stage.Stage(34)}),
   118  		)
   119  	})
   120  
   121  	Context("Compare", func() {
   122  		// Test Compare() by sorting a list.
   123  		var (
   124  			versions = []Version{
   125  				{Number: 2, Stage: stage.Alpha},
   126  				{Number: 44, Stage: stage.Alpha},
   127  				{Number: 1},
   128  				{Number: 2, Stage: stage.Beta},
   129  				{Number: 4, Stage: stage.Beta},
   130  				{Number: 1, Stage: stage.Alpha},
   131  				{Number: 4},
   132  				{Number: 44, Stage: stage.Alpha},
   133  				{Number: 30},
   134  				{Number: 4, Stage: stage.Alpha},
   135  			}
   136  
   137  			sortedVersions = []Version{
   138  				{Number: 1, Stage: stage.Alpha},
   139  				{Number: 1},
   140  				{Number: 2, Stage: stage.Alpha},
   141  				{Number: 2, Stage: stage.Beta},
   142  				{Number: 4, Stage: stage.Alpha},
   143  				{Number: 4, Stage: stage.Beta},
   144  				{Number: 4},
   145  				{Number: 30},
   146  				{Number: 44, Stage: stage.Alpha},
   147  				{Number: 44, Stage: stage.Alpha},
   148  			}
   149  		)
   150  
   151  		It("sorts a valid list of versions correctly", func() {
   152  			sort.Slice(versions, func(i int, j int) bool {
   153  				return versions[i].Compare(versions[j]) == -1
   154  			})
   155  			Expect(versions).To(Equal(sortedVersions))
   156  		})
   157  	})
   158  
   159  	Context("IsStable", func() {
   160  		DescribeTable("should return true for stable versions",
   161  			func(version Version) { Expect(version.IsStable()).To(BeTrue()) },
   162  			Entry("for version 1", Version{Number: 1}),
   163  			Entry("for version 1 (stable)", Version{Number: 1, Stage: stage.Stable}),
   164  			Entry("for version 22", Version{Number: 22}),
   165  			Entry("for version 22 (stable)", Version{Number: 22, Stage: stage.Stable}),
   166  		)
   167  
   168  		DescribeTable("should return false for unstable versions",
   169  			func(version Version) { Expect(version.IsStable()).To(BeFalse()) },
   170  			Entry("for version 0", Version{Number: 0}),
   171  			Entry("for version 0 (stable)", Version{Number: 0, Stage: stage.Stable}),
   172  			Entry("for version 0 (alpha)", Version{Number: 0, Stage: stage.Alpha}),
   173  			Entry("for version 0 (beta)", Version{Number: 0, Stage: stage.Beta}),
   174  			Entry("for version 0 (implicit)", Version{}),
   175  			Entry("for version 0 (stable) (implicit)", Version{Stage: stage.Stable}),
   176  			Entry("for version 0 (alpha) (implicit)", Version{Stage: stage.Alpha}),
   177  			Entry("for version 0 (beta) (implicit)", Version{Stage: stage.Beta}),
   178  			Entry("for version 1 (alpha)", Version{Number: 1, Stage: stage.Alpha}),
   179  			Entry("for version 1 (beta)", Version{Number: 1, Stage: stage.Beta}),
   180  			Entry("for version 22 (alpha)", Version{Number: 22, Stage: stage.Alpha}),
   181  			Entry("for version 22 (beta)", Version{Number: 22, Stage: stage.Beta}),
   182  		)
   183  	})
   184  })