github.com/redhat-appstudio/release-service@v0.0.0-20240507045911-a8558ef3422a/metadata/metadata_test.go (about) 1 /* 2 Copyright 2022. 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 metadata 18 19 import ( 20 "testing" 21 22 . "github.com/onsi/ginkgo/v2" 23 . "github.com/onsi/gomega" 24 tektonv1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1" 25 corev1 "k8s.io/api/core/v1" 26 v1 "k8s.io/apimachinery/pkg/apis/meta/v1" 27 ) 28 29 func TestMetadata(t *testing.T) { 30 RegisterFailHandler(Fail) 31 RunSpecs(t, "Metadata Test Suite") 32 } 33 34 var _ = Describe("Metadata", func() { 35 36 Context("addEntries function", func() { 37 When("called with an empty destination map", func() { 38 src := map[string]string{ 39 "pet/dog": "bark", 40 "pet/cat": "meow", 41 "pond/frog": "ribit", 42 } 43 dst := map[string]string{} 44 addEntries(src, dst) 45 46 It("should copy all src key/value pairs unconditionally", func() { 47 Expect(dst["pet/dog"]).To(Equal("bark")) 48 Expect(dst["pet/cat"]).To(Equal("meow")) 49 Expect(dst["pond/frog"]).To(Equal("ribit")) 50 }) 51 It("source and destination maps should have the same length", func() { 52 Expect(len(dst)).To(Equal(len(src))) 53 }) 54 }) 55 When("called with a non-empty destination map containing duplicate keys", func() { 56 src := map[string]string{ 57 "pet/dog": "bark", 58 "pet/cat": "meow", 59 "pond/frog": "ribit", 60 } 61 dst := map[string]string{ 62 "pet/dog": "howl", 63 } 64 addEntries(src, dst) 65 It("should only copy source key/value pairs that do not already exist in the destination map", func() { 66 Expect(dst["pet/dog"]).To(Equal("howl")) 67 Expect(dst["pet/cat"]).To(Equal("meow")) 68 Expect(dst["pond/frog"]).To(Equal("ribit")) 69 }) 70 It("should have increased the length of the destination map from 1 to 3", func() { 71 Expect(len(dst)).To(Equal(3)) 72 }) 73 }) 74 }) 75 76 Context("filterByPrefix function", func() { 77 When("called with an empty prefix string", func() { 78 src := map[string]string{ 79 "pet/dog": "bark", 80 "pet/cat": "meow", 81 "pond/frog": "ribit", 82 } 83 dst := filterByPrefix(src, "") 84 It("should return a map identical to the map given", func() { 85 Expect(dst["pet/dog"]).To(Equal("bark")) 86 Expect(dst["pet/cat"]).To(Equal("meow")) 87 Expect(dst["pond/frog"]).To(Equal("ribit")) 88 }) 89 It("input and returned maps should have the same length", func() { 90 Expect(len(dst)).To(Equal(len(src))) 91 }) 92 }) 93 When("called with an non-empty prefix string", func() { 94 src := map[string]string{ 95 "pet/dog": "bark", 96 "pet/cat": "meow", 97 "pond/frog": "ribit", 98 } 99 dst := filterByPrefix(src, "pond") 100 It("should return a new map of key/value pairs where the key(s) matched the given prefix", func() { 101 Expect(dst["pond/frog"]).To(Equal("ribit")) 102 }) 103 It("returned map length should equal exactly 1.", func() { 104 Expect(len(dst)).To(Equal(1)) 105 }) 106 }) 107 }) 108 109 Context("safeCopy function", func() { 110 When("called with a key not already present in the map", func() { 111 dst := map[string]string{"foo/dog": "bark"} 112 safeCopy(dst, "foo/cat", "meow") 113 It("should set the given key/value in the map", func() { 114 Expect(dst["foo/dog"]).To(Equal("bark")) 115 Expect(dst["foo/cat"]).To(Equal("meow")) 116 }) 117 It("should have increased the map length from 1 to 2", func() { 118 Expect(len(dst)).To(Equal(2)) 119 }) 120 }) 121 When("called with a key already present in dst map", func() { 122 dst := map[string]string{"foo/dog": "bark"} 123 safeCopy(dst, "foo/dog", "meow") 124 It("should not set the given key/value pair into the map", func() { 125 Expect(dst["foo/dog"]).To(Equal("bark")) 126 }) 127 It("should not increase the map length", func() { 128 Expect(len(dst)).To(Equal(1)) 129 }) 130 }) 131 }) 132 133 Context("AddAnnotations function", func() { 134 When("called with a pipelineRun containing nil Annotations", func() { 135 pipelineRun := &tektonv1.PipelineRun{ 136 ObjectMeta: v1.ObjectMeta{ 137 Annotations: nil, 138 Labels: nil, 139 }, 140 } 141 AddAnnotations(pipelineRun, map[string]string{}) 142 It("should create the Annotations map", func() { 143 Expect(pipelineRun.GetAnnotations()).ToNot(BeNil()) 144 }) 145 It("should do nothing with the Labels map", func() { 146 Expect(pipelineRun.GetLabels()).To(BeNil()) 147 }) 148 }) 149 }) 150 151 Context("AddLabels function", func() { 152 When("called with an object containing nil Labels", func() { 153 pod := &corev1.Pod{ 154 ObjectMeta: v1.ObjectMeta{ 155 Annotations: nil, 156 Labels: nil, 157 }, 158 } 159 AddLabels(pod, map[string]string{}) 160 It("should create the Labels map", func() { 161 Expect(pod.GetLabels()).ToNot(BeNil()) 162 }) 163 It("should do nothing with the Annotations map", func() { 164 Expect(pod.GetAnnotations()).To(BeNil()) 165 }) 166 }) 167 }) 168 169 Context("GetAnnotationsWithPrefix function", func() { 170 When("calling filterByPrefix with GetAnnotations()", func() { 171 pod := &corev1.Pod{ 172 ObjectMeta: v1.ObjectMeta{ 173 Annotations: map[string]string{ 174 "pet/dog": "bark", 175 }, 176 Labels: map[string]string{ 177 "foo": "bar", 178 }, 179 }, 180 } 181 dst := GetAnnotationsWithPrefix(pod, "pet/") 182 It("should only fetch Annotations", func() { 183 Expect(dst["pet/dog"]).To(Equal("bark")) 184 }) 185 It("should not fetch Labels", func() { 186 Expect(dst["foo"]).To(BeEmpty()) 187 }) 188 }) 189 }) 190 191 Context("GetLabelsWithPrefix function", func() { 192 When("calling filterByPrefix with GetLabels()", func() { 193 pod := &corev1.Pod{ 194 ObjectMeta: v1.ObjectMeta{ 195 Annotations: map[string]string{ 196 "foo": "bar", 197 }, 198 Labels: map[string]string{ 199 "pet/dog": "bark", 200 }, 201 }, 202 } 203 dst := GetLabelsWithPrefix(pod, "pet/") 204 It("should only fetch Labels", func() { 205 Expect(dst["pet/dog"]).To(Equal("bark")) 206 }) 207 It("should not fetch Annotations", func() { 208 Expect(dst["foo"]).To(BeEmpty()) 209 }) 210 }) 211 }) 212 })