sigs.k8s.io/kubebuilder/v3@v3.14.0/pkg/internal/validation/dns_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 validation 18 19 import ( 20 "fmt" 21 "strings" 22 "testing" 23 24 . "github.com/onsi/ginkgo/v2" 25 . "github.com/onsi/gomega" 26 ) 27 28 func TestValidation(t *testing.T) { 29 RegisterFailHandler(Fail) 30 RunSpecs(t, "Validation Suite") 31 } 32 33 var _ = Describe("IsDNS1123Label", func() { 34 It("should return no error", func() { 35 for _, value := range []string{ 36 "a", "ab", "abc", "a1", "a-1", "a--1--2--b", 37 "0", "01", "012", "1a", "1-a", "1--a--b--2", 38 strings.Repeat("a", 56), 39 } { 40 By(fmt.Sprintf("for %s", value)) 41 Expect(len(IsDNS1123Label(value))).To(Equal(0)) 42 } 43 }) 44 45 It("should return at least one error", func() { 46 for _, value := range []string{ 47 "", "A", "ABC", "aBc", "A1", "A-1", "1-A", 48 "-", "a-", "-a", "1-", "-1", 49 "_", "a_", "_a", "a_b", "1_", "_1", "1_2", 50 ".", "a.", ".a", "a.b", "1.", ".1", "1.2", 51 " ", "a ", " a", "a b", "1 ", " 1", "1 2", 52 strings.Repeat("a", 57), 53 } { 54 By(fmt.Sprintf("for %s", value)) 55 Expect(len(IsDNS1123Label(value))).NotTo(Equal(0)) 56 } 57 }) 58 }) 59 60 var _ = Describe("IsDNS1123Subdomain", func() { 61 It("should return no error", func() { 62 for _, value := range []string{ 63 "a", "ab", "abc", "a1", "a-1", "a--1--2--b", 64 "0", "01", "012", "1a", "1-a", "1--a--b--2", 65 "a.a", "ab.a", "abc.a", "a1.a", "a-1.a", "a--1--2--b.a", 66 "a.1", "ab.1", "abc.1", "a1.1", "a-1.1", "a--1--2--b.1", 67 "0.a", "01.a", "012.a", "1a.a", "1-a.a", "1--a--b--2", 68 "0.1", "01.1", "012.1", "1a.1", "1-a.1", "1--a--b--2.1", 69 "a.b.c.d.e", "aa.bb.cc.dd.ee", "1.2.3.4.5", "11.22.33.44.55", 70 strings.Repeat("a", 253), 71 } { 72 By(fmt.Sprintf("for %s", value)) 73 Expect(len(IsDNS1123Subdomain(value))).To(Equal(0)) 74 } 75 }) 76 77 It("should return at least one error", func() { 78 for _, value := range []string{ 79 "", "A", "ABC", "aBc", "A1", "A-1", "1-A", 80 "-", "a-", "-a", "1-", "-1", 81 "_", "a_", "_a", "a_b", "1_", "_1", "1_2", 82 ".", "a.", ".a", "a..b", "1.", ".1", "1..2", 83 " ", "a ", " a", "a b", "1 ", " 1", "1 2", 84 "A.a", "aB.a", "ab.A", "A1.a", "a1.A", 85 "A.1", "aB.1", "A1.1", "1A.1", 86 "0.A", "01.A", "012.A", "1A.a", "1a.A", 87 "A.B.C.D.E", "AA.BB.CC.DD.EE", "a.B.c.d.e", "aa.bB.cc.dd.ee", 88 "a@b", "a,b", "a_b", "a;b", 89 "a:b", "a%b", "a?b", "a$b", 90 strings.Repeat("a", 254), 91 } { 92 By(fmt.Sprintf("for %s", value)) 93 Expect(len(IsDNS1123Subdomain(value))).NotTo(Equal(0)) 94 } 95 }) 96 }) 97 98 var _ = Describe("IsDNS1035Label", func() { 99 It("should return no error", func() { 100 for _, value := range []string{ 101 "a", "ab", "abc", "a1", "a-1", "a--1--2--b", 102 strings.Repeat("a", 63), 103 } { 104 By(fmt.Sprintf("for %s", value)) 105 Expect(len(IsDNS1035Label(value))).To(Equal(0)) 106 } 107 }) 108 109 It("should return at least one error", func() { 110 for _, value := range []string{ 111 "0", "01", "012", "1a", "1-a", "1--a--b--2", 112 "", "A", "ABC", "aBc", "A1", "A-1", "1-A", 113 "-", "a-", "-a", "1-", "-1", 114 "_", "a_", "_a", "a_b", "1_", "_1", "1_2", 115 ".", "a.", ".a", "a.b", "1.", ".1", "1.2", 116 " ", "a ", " a", "a b", "1 ", " 1", "1 2", 117 strings.Repeat("a", 64), 118 } { 119 By(fmt.Sprintf("for %s", value)) 120 Expect(len(IsDNS1035Label(value))).NotTo(Equal(0)) 121 } 122 }) 123 }) 124 125 // This test provides coverage to the conditional that handles no examples 126 // as they are not being used in any check 127 var _ = Describe("regexError", func() { 128 It("should work without examples", func() { 129 Expect(regexError("$", "_")).To(Equal("$ (regex used for validation is '_')")) 130 }) 131 })