Debian Codegraph @

pkg/utils/utils_test.go

raw symbols SCIP
1 // Copyright 2016 CNI authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package utils
16
17 import (
18 "fmt"
19 "strings"
20
21 . "github.com/onsi/ginkgo/v2"
22 . "github.com/onsi/gomega"
23 )
24
25 var _ = Describe("Utils", func() {
26 Describe("FormatChainName", func() {
27 It("must format a short name", func() {
28 chain := FormatChainName("test", "1234")
29 Expect(chain).To(HaveLen(maxChainLength))
30 Expect(chain).To(Equal("CNI-2bbe0c48b91a7d1b8a6753a8"))
31 })
32
33 It("must truncate a long name", func() {
34 chain := FormatChainName("testalongnamethatdoesnotmakesense", "1234")
35 Expect(chain).To(HaveLen(maxChainLength))
36 Expect(chain).To(Equal("CNI-374f33fe84ab0ed84dcdebe3"))
37 })
38
39 It("must be predictable", func() {
40 chain1 := FormatChainName("testalongnamethatdoesnotmakesense", "1234")
41 chain2 := FormatChainName("testalongnamethatdoesnotmakesense", "1234")
42 Expect(chain1).To(HaveLen(maxChainLength))
43 Expect(chain2).To(HaveLen(maxChainLength))
44 Expect(chain1).To(Equal(chain2))
45 })
46
47 It("must change when a character changes", func() {
48 chain1 := FormatChainName("testalongnamethatdoesnotmakesense", "1234")
49 chain2 := FormatChainName("testalongnamethatdoesnotmakesense", "1235")
50 Expect(chain1).To(HaveLen(maxChainLength))
51 Expect(chain2).To(HaveLen(maxChainLength))
52 Expect(chain1).To(Equal("CNI-374f33fe84ab0ed84dcdebe3"))
53 Expect(chain1).NotTo(Equal(chain2))
54 })
55 })
56
57 Describe("MustFormatChainNameWithPrefix", func() {
58 It("generates a chain name with a prefix", func() {
59 chain := MustFormatChainNameWithPrefix("test", "1234", "PREFIX-")
60 Expect(chain).To(HaveLen(maxChainLength))
61 Expect(chain).To(Equal("CNI-PREFIX-2bbe0c48b91a7d1b8"))
62 })
63
64 It("must format a short name", func() {
65 chain := MustFormatChainNameWithPrefix("test", "1234", "PREFIX-")
66 Expect(chain).To(HaveLen(maxChainLength))
67 Expect(chain).To(Equal("CNI-PREFIX-2bbe0c48b91a7d1b8"))
68 })
69
70 It("must truncate a long name", func() {
71 chain := MustFormatChainNameWithPrefix("testalongnamethatdoesnotmakesense", "1234", "PREFIX-")
72 Expect(chain).To(HaveLen(maxChainLength))
73 Expect(chain).To(Equal("CNI-PREFIX-374f33fe84ab0ed84"))
74 })
75
76 It("must be predictable", func() {
77 chain1 := MustFormatChainNameWithPrefix("testalongnamethatdoesnotmakesense", "1234", "PREFIX-")
78 chain2 := MustFormatChainNameWithPrefix("testalongnamethatdoesnotmakesense", "1234", "PREFIX-")
79 Expect(chain1).To(HaveLen(maxChainLength))
80 Expect(chain2).To(HaveLen(maxChainLength))
81 Expect(chain1).To(Equal(chain2))
82 })
83
84 It("must change when a character changes", func() {
85 chain1 := MustFormatChainNameWithPrefix("testalongnamethatdoesnotmakesense", "1234", "PREFIX-")
86 chain2 := MustFormatChainNameWithPrefix("testalongnamethatdoesnotmakesense", "1235", "PREFIX-")
87 Expect(chain1).To(HaveLen(maxChainLength))
88 Expect(chain2).To(HaveLen(maxChainLength))
89 Expect(chain1).To(Equal("CNI-PREFIX-374f33fe84ab0ed84"))
90 Expect(chain1).NotTo(Equal(chain2))
91 })
92
93 It("panics when prefix is too large", func() {
94 longPrefix := strings.Repeat("PREFIX-", 4)
95 Expect(func() {
96 MustFormatChainNameWithPrefix("test", "1234", longPrefix)
97 }).To(Panic())
98 })
99 })
100
101 Describe("MustFormatHashWithPrefix", func() {
102 It("always returns a string with the given prefix", func() {
103 Expect(MustFormatHashWithPrefix(10, "AAA", "some string")).To(HavePrefix("AAA"))
104 Expect(MustFormatHashWithPrefix(10, "foo", "some string")).To(HavePrefix("foo"))
105 Expect(MustFormatHashWithPrefix(10, "bar", "some string")).To(HavePrefix("bar"))
106 })
107
108 It("always returns a string of the given length", func() {
109 Expect(MustFormatHashWithPrefix(10, "AAA", "some string")).To(HaveLen(10))
110 Expect(MustFormatHashWithPrefix(15, "AAA", "some string")).To(HaveLen(15))
111 Expect(MustFormatHashWithPrefix(5, "AAA", "some string")).To(HaveLen(5))
112 })
113
114 It("is deterministic", func() {
115 val1 := MustFormatHashWithPrefix(10, "AAA", "some string")
116 val2 := MustFormatHashWithPrefix(10, "AAA", "some string")
117 val3 := MustFormatHashWithPrefix(10, "AAA", "some string")
118 Expect(val1).To(Equal(val2))
119 Expect(val1).To(Equal(val3))
120 })
121
122 It("is (nearly) perfect (injective function)", func() {
123 hashes := map[string]int{}
124
125 for i := 0; i < 1000; i++ {
126 name := fmt.Sprintf("string %d", i)
127 hashes[MustFormatHashWithPrefix(8, "", name)]++
128 }
129
130 for key, count := range hashes {
131 Expect(count).To(Equal(1), "for key "+key+" got non-unique correspondence")
132 }
133 })
134
135 assertPanicWith := func(f func(), expectedErrorMessage string) {
136 defer func() {
137 Expect(recover()).To(Equal(expectedErrorMessage))
138 }()
139 f()
140 Fail("function should have panicked but did not")
141 }
142
143 It("panics when prefix is longer than the length", func() {
144 assertPanicWith(
145 func() { MustFormatHashWithPrefix(3, "AAA", "some string") },
146 "invalid length",
147 )
148 })
149
150 It("panics when length is not positive", func() {
151 assertPanicWith(
152 func() { MustFormatHashWithPrefix(0, "", "some string") },
153 "invalid length",
154 )
155 })
156
157 It("panics when length is larger than MaxLen", func() {
158 assertPanicWith(
159 func() { MustFormatHashWithPrefix(MaxHashLen+1, "", "some string") },
160 "invalid length",
161 )
162 })
163 })
164 })