Skip to content

Commit 33036c5

Browse files
authored
Merge pull request #2034 from Adirio/tests/dns-validation
🌱 Add tests to the DNS validation package
2 parents 615f8c2 + 5cc7d55 commit 33036c5

File tree

2 files changed

+139
-7
lines changed

2 files changed

+139
-7
lines changed

pkg/internal/validation/dns.go

+8-7
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import (
2727

2828
const (
2929
dns1123LabelFmt string = "[a-z0-9](?:[-a-z0-9]*[a-z0-9])?"
30-
dns1123SubdomainFmt string = dns1123LabelFmt + "(\\." + dns1123LabelFmt + ")*"
30+
dns1123SubdomainFmt string = dns1123LabelFmt + "(?:\\." + dns1123LabelFmt + ")*"
3131
dns1035LabelFmt string = "[a-z](?:[-a-z0-9]*[a-z0-9])?"
3232
)
3333

@@ -43,7 +43,8 @@ var dns1123LabelConfig = dnsValidationConfig{
4343
format: dns1123LabelFmt,
4444
maxLen: 56, // = 63 - len("-system")
4545
re: regexp.MustCompile("^" + dns1123LabelFmt + "$"),
46-
errMsg: "a DNS-1123 label must consist of lower case alphanumeric characters or '-'",
46+
errMsg: "a DNS-1123 label must consist of lower case alphanumeric characters or '-', " +
47+
"and must start and end with an alphanumeric character",
4748
examples: []string{"example.com"},
4849
}
4950

@@ -75,17 +76,17 @@ func (c dnsValidationConfig) check(value string) (errs []string) {
7576
return errs
7677
}
7778

79+
// IsDNS1123Label tests for a string that conforms to the definition of a label in DNS (RFC 1123).
80+
func IsDNS1123Label(value string) []string {
81+
return dns1123LabelConfig.check(value)
82+
}
83+
7884
// IsDNS1123Subdomain tests for a string that conforms to the definition of a
7985
// subdomain in DNS (RFC 1123).
8086
func IsDNS1123Subdomain(value string) []string {
8187
return dns1123SubdomainConfig.check(value)
8288
}
8389

84-
// IsDNS1123Label tests for a string that conforms to the definition of a label in DNS (RFC 1123).
85-
func IsDNS1123Label(value string) []string {
86-
return dns1123LabelConfig.check(value)
87-
}
88-
8990
// IsDNS1035Label tests for a string that conforms to the definition of a label in DNS (RFC 1035).
9091
func IsDNS1035Label(value string) []string {
9192
return dns1035LabelConfig.check(value)

pkg/internal/validation/dns_test.go

+131
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
/*
2+
Copyright 2021 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"
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+
})

0 commit comments

Comments
 (0)