sigs.k8s.io/kubebuilder/v3@v3.14.0/pkg/model/stage/stage_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 stage
    18  
    19  import (
    20  	"sort"
    21  	"testing"
    22  
    23  	g "github.com/onsi/ginkgo/v2" // An alias is required because Context is defined elsewhere in this package.
    24  	. "github.com/onsi/gomega"
    25  )
    26  
    27  func TestStage(t *testing.T) {
    28  	RegisterFailHandler(g.Fail)
    29  	g.RunSpecs(t, "Stage Suite")
    30  }
    31  
    32  var _ = g.Describe("ParseStage", func() {
    33  	g.DescribeTable("should be correctly parsed for valid stage strings",
    34  		func(str string, stage Stage) {
    35  			s, err := ParseStage(str)
    36  			Expect(err).NotTo(HaveOccurred())
    37  			Expect(s).To(Equal(stage))
    38  		},
    39  		g.Entry("for alpha stage", "alpha", Alpha),
    40  		g.Entry("for beta stage", "beta", Beta),
    41  		g.Entry("for stable stage", "", Stable),
    42  	)
    43  
    44  	g.DescribeTable("should error when parsing invalid stage strings",
    45  		func(str string) {
    46  			_, err := ParseStage(str)
    47  			Expect(err).To(HaveOccurred())
    48  		},
    49  		g.Entry("passing a number as the stage string", "1"),
    50  		g.Entry("passing `gamma` as the stage string", "gamma"),
    51  		g.Entry("passing a dash-prefixed stage string", "-alpha"),
    52  	)
    53  })
    54  
    55  var _ = g.Describe("Stage", func() {
    56  	g.Context("String", func() {
    57  		g.DescribeTable("should return the correct string value",
    58  			func(stage Stage, str string) { Expect(stage.String()).To(Equal(str)) },
    59  			g.Entry("for alpha stage", Alpha, "alpha"),
    60  			g.Entry("for beta stage", Beta, "beta"),
    61  			g.Entry("for stable stage", Stable, ""),
    62  		)
    63  
    64  		g.DescribeTable("should panic",
    65  			func(stage Stage) { Expect(func() { _ = stage.String() }).To(Panic()) },
    66  			g.Entry("for stage 34", Stage(34)),
    67  			g.Entry("for stage 75", Stage(75)),
    68  			g.Entry("for stage 123", Stage(123)),
    69  			g.Entry("for stage 255", Stage(255)),
    70  		)
    71  	})
    72  
    73  	g.Context("Validate", func() {
    74  		g.DescribeTable("should validate existing stages",
    75  			func(stage Stage) { Expect(stage.Validate()).To(Succeed()) },
    76  			g.Entry("for alpha stage", Alpha),
    77  			g.Entry("for beta stage", Beta),
    78  			g.Entry("for stable stage", Stable),
    79  		)
    80  
    81  		g.DescribeTable("should fail for non-existing stages",
    82  			func(stage Stage) { Expect(stage.Validate()).NotTo(Succeed()) },
    83  			g.Entry("for stage 34", Stage(34)),
    84  			g.Entry("for stage 75", Stage(75)),
    85  			g.Entry("for stage 123", Stage(123)),
    86  			g.Entry("for stage 255", Stage(255)),
    87  		)
    88  	})
    89  
    90  	g.Context("Compare", func() {
    91  		// Test Stage.Compare by sorting a list
    92  		var (
    93  			stages = []Stage{
    94  				Stable,
    95  				Alpha,
    96  				Stable,
    97  				Beta,
    98  				Beta,
    99  				Alpha,
   100  			}
   101  
   102  			sortedStages = []Stage{
   103  				Alpha,
   104  				Alpha,
   105  				Beta,
   106  				Beta,
   107  				Stable,
   108  				Stable,
   109  			}
   110  		)
   111  
   112  		g.It("sorts stages correctly", func() {
   113  			sort.Slice(stages, func(i int, j int) bool {
   114  				return stages[i].Compare(stages[j]) == -1
   115  			})
   116  			Expect(stages).To(Equal(sortedStages))
   117  		})
   118  	})
   119  
   120  	g.Context("IsStable", func() {
   121  		g.It("should return true for stable stage", func() {
   122  			Expect(Stable.IsStable()).To(BeTrue())
   123  		})
   124  
   125  		g.DescribeTable("should return false for any unstable stage",
   126  			func(stage Stage) { Expect(stage.IsStable()).To(BeFalse()) },
   127  			g.Entry("for alpha stage", Alpha),
   128  			g.Entry("for beta stage", Beta),
   129  		)
   130  	})
   131  })