Skip to content

Commit bb866f7

Browse files
authored
Add tests for configmap.go (#3990)
* added tests for configmap.go * update the Describes messages to match method names * added tests for addObjectToBinaryData * added tests for getRegistryConfigMaps * add license header
1 parent fc038e3 commit bb866f7

File tree

3 files changed

+319
-1
lines changed

3 files changed

+319
-1
lines changed

internal/olm/operator/registry/configmap/configmap.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,9 @@ func (rr *RegistryResources) getRegistryConfigMaps(ctx context.Context, namespac
4949
// for a given PackageManifest and Bundles. Each ConfigMaps's binary data is
5050
// indexed by the ConfigMap's name.
5151
func makeConfigMapsForPackageManifests(pkg *apimanifests.PackageManifest,
52-
bundles []*apimanifests.Bundle) (_ map[string]map[string][]byte, err error) {
52+
bundles []*apimanifests.Bundle) (map[string]map[string][]byte, error) {
5353

54+
var err error
5455
binaryDataByConfigMap := make(map[string]map[string][]byte)
5556
// Create a PackageManifest ConfigMap.
5657
cmName := getRegistryConfigMapName(pkg.PackageName) + "-package"
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 2019 The Operator-SDK 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 configmap_test
16+
17+
import (
18+
"testing"
19+
20+
. "github.com/onsi/ginkgo"
21+
. "github.com/onsi/gomega"
22+
)
23+
24+
func TestConfigmap(t *testing.T) {
25+
RegisterFailHandler(Fail)
26+
RunSpecs(t, "Configmap Suite")
27+
}
Lines changed: 290 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,290 @@
1+
// Copyright 2019 The Operator-SDK 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 configmap
16+
17+
import (
18+
"context"
19+
"crypto/sha256"
20+
"encoding/base32"
21+
"fmt"
22+
"strings"
23+
24+
"github.com/blang/semver"
25+
. "github.com/onsi/ginkgo"
26+
. "github.com/onsi/gomega"
27+
"github.com/operator-framework/api/pkg/lib/version"
28+
apimanifests "github.com/operator-framework/api/pkg/manifests"
29+
"github.com/operator-framework/api/pkg/operators/v1alpha1"
30+
"github.com/operator-framework/operator-sdk/internal/olm/client"
31+
"github.com/operator-framework/operator-sdk/internal/util/k8sutil"
32+
corev1 "k8s.io/api/core/v1"
33+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
34+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
35+
client_cr "sigs.k8s.io/controller-runtime/pkg/client"
36+
"sigs.k8s.io/controller-runtime/pkg/client/fake"
37+
"sigs.k8s.io/yaml"
38+
)
39+
40+
var _ = Describe("ConfigMap", func() {
41+
42+
Describe("hashContents", func() {
43+
It("should return the hash of the byte array without any error", func() {
44+
val := []byte("Hello")
45+
h := sha256.New()
46+
_, _ = h.Write(val)
47+
enc := base32.StdEncoding.WithPadding(base32.NoPadding)
48+
ans := enc.EncodeToString(h.Sum(nil))
49+
50+
Expect(hashContents(val)).Should(Equal(ans))
51+
})
52+
})
53+
54+
Describe("getRegistryConfigMapName", func() {
55+
It("should return the registry configmap name", func() {
56+
val := "Test"
57+
name := k8sutil.FormatOperatorNameDNS1123(val)
58+
ans := fmt.Sprintf("%s-registry-manifests", name)
59+
60+
Expect(getRegistryConfigMapName(val)).Should(Equal(ans))
61+
})
62+
})
63+
64+
Describe("makeObjectFileName", func() {
65+
var (
66+
fileName string
67+
testVal []byte
68+
userInput []string
69+
)
70+
BeforeEach(func() {
71+
testVal = []byte("test")
72+
userInput = []string{"userInput", "userInput2"}
73+
fileName = hashContents(testVal) + "."
74+
})
75+
It("returns the filename with extra user given string", func() {
76+
for _, name := range userInput {
77+
fileName += strings.ToLower(name) + "."
78+
}
79+
fileName = fileName + "yaml"
80+
Expect(makeObjectFileName(testVal, userInput...)).Should(Equal(fileName))
81+
})
82+
It("returns the filename without extra user given string", func() {
83+
fileName = fileName + "yaml"
84+
Expect(makeObjectFileName(testVal)).Should(Equal(fileName))
85+
})
86+
})
87+
88+
Describe("addObjectToBinaryData ", func() {
89+
It("should add given object to the given binaryData", func() {
90+
userInput := []string{"userInput", "userInput2"}
91+
92+
b := make(map[string][]byte)
93+
obj := struct {
94+
val1 string
95+
val2 string
96+
}{
97+
val1: "val1",
98+
val2: "val2",
99+
}
100+
101+
binaryData := make(map[string][]byte)
102+
expected, err := yaml.Marshal(obj)
103+
Expect(err).Should(BeNil())
104+
binaryData[makeObjectFileName(expected, userInput...)] = expected
105+
// Test and verify function
106+
Expect(addObjectToBinaryData(b, obj, userInput...)).Should(BeNil())
107+
Expect(b).Should(Equal(binaryData))
108+
})
109+
110+
})
111+
112+
Describe("makeObjectBinaryData", func() {
113+
It("creates the binary data", func() {
114+
binaryData := make(map[string][]byte)
115+
obj := struct {
116+
val1 string
117+
val2 string
118+
}{
119+
val1: "val1",
120+
val2: "val2",
121+
}
122+
123+
userInput := []string{"userInput", "userInput2"}
124+
b, e := makeObjectBinaryData(obj, userInput...)
125+
Expect(e).Should(BeNil())
126+
// Test and verify function
127+
e = addObjectToBinaryData(binaryData, obj, userInput...)
128+
Expect(e).Should(BeNil())
129+
Expect(b).Should(Equal(binaryData))
130+
131+
})
132+
})
133+
134+
Describe("makeBundleBinaryData", func() {
135+
It("should serialize bundle to binary data", func() {
136+
var e error
137+
b := apimanifests.Bundle{
138+
Name: "testbundle",
139+
Objects: []*unstructured.Unstructured{
140+
{
141+
Object: map[string]interface{}{"val1": "val1"},
142+
},
143+
{
144+
Object: map[string]interface{}{"val2": "va2"},
145+
},
146+
},
147+
}
148+
149+
binaryData, err := makeBundleBinaryData(&b)
150+
Expect(err).Should(BeNil())
151+
val := make(map[string][]byte)
152+
for _, obj := range b.Objects {
153+
e = addObjectToBinaryData(val, obj, obj.GetName(), obj.GetKind())
154+
Expect(e).Should(BeNil())
155+
}
156+
157+
Expect(binaryData).Should(Equal(val))
158+
})
159+
})
160+
161+
Describe("makeConfigMapsForPackageManifests", func() {
162+
var (
163+
p apimanifests.PackageManifest
164+
e error
165+
b []*apimanifests.Bundle
166+
)
167+
BeforeEach(func() {
168+
b = []*apimanifests.Bundle{
169+
{
170+
Name: "testbundle",
171+
Objects: []*unstructured.Unstructured{
172+
{
173+
Object: map[string]interface{}{"val1": "val1"},
174+
},
175+
{
176+
Object: map[string]interface{}{"val2": "va2"},
177+
},
178+
},
179+
CSV: &v1alpha1.ClusterServiceVersion{
180+
Spec: v1alpha1.ClusterServiceVersionSpec{
181+
Version: version.OperatorVersion{
182+
Version: semver.SpecVersion,
183+
},
184+
},
185+
},
186+
},
187+
{
188+
Name: "testbundle_2",
189+
Objects: []*unstructured.Unstructured{
190+
{
191+
Object: map[string]interface{}{"val1": "val1"},
192+
},
193+
{
194+
Object: map[string]interface{}{"val2": "va2"},
195+
},
196+
},
197+
CSV: &v1alpha1.ClusterServiceVersion{
198+
Spec: v1alpha1.ClusterServiceVersionSpec{
199+
Version: version.OperatorVersion{
200+
Version: semver.SpecVersion,
201+
},
202+
},
203+
},
204+
},
205+
}
206+
p = apimanifests.PackageManifest{
207+
PackageName: "test_package_manifest",
208+
Channels: []apimanifests.PackageChannel{
209+
{Name: "test_1",
210+
CurrentCSVName: "test_csv_1",
211+
},
212+
{Name: "test_2",
213+
CurrentCSVName: "test_csv_2",
214+
},
215+
},
216+
DefaultChannelName: "test_channel_name",
217+
}
218+
})
219+
It("should serialize packagemanifest to binary data", func() {
220+
binaryDataByConfigMap, err := makeConfigMapsForPackageManifests(&p, b)
221+
Expect(err).Should(BeNil())
222+
223+
val := make(map[string]map[string][]byte)
224+
cmName := getRegistryConfigMapName(p.PackageName) + "-package"
225+
val[cmName], err = makeObjectBinaryData(p)
226+
Expect(err).Should(BeNil())
227+
for _, bundle := range b {
228+
version := bundle.CSV.Spec.Version.String()
229+
cmName := getRegistryConfigMapName(p.PackageName) + "-" + k8sutil.FormatOperatorNameDNS1123(version)
230+
val[cmName], e = makeBundleBinaryData(bundle)
231+
Expect(e).Should(BeNil())
232+
}
233+
234+
Expect(binaryDataByConfigMap).Should(Equal(val))
235+
})
236+
237+
})
238+
239+
Describe("getRegistryConfigMaps", func() {
240+
var (
241+
rr RegistryResources
242+
list corev1.ConfigMapList
243+
e error
244+
)
245+
BeforeEach(func() {
246+
fakeclient := fake.NewFakeClient(
247+
&corev1.ConfigMapList{
248+
Items: []corev1.ConfigMap{
249+
{
250+
ObjectMeta: metav1.ObjectMeta{
251+
Namespace: "testns",
252+
Labels: makeRegistryLabels("test"),
253+
},
254+
},
255+
{
256+
ObjectMeta: metav1.ObjectMeta{
257+
Namespace: "testns2",
258+
Labels: makeRegistryLabels("test"),
259+
},
260+
},
261+
},
262+
},
263+
)
264+
rr = RegistryResources{
265+
Client: &client.Client{
266+
KubeClient: fakeclient,
267+
},
268+
Pkg: &apimanifests.PackageManifest{
269+
PackageName: "test",
270+
},
271+
Bundles: rr.Bundles,
272+
}
273+
274+
list = corev1.ConfigMapList{}
275+
})
276+
It("performs operations and returns all the configmaps", func() {
277+
opts := []client_cr.ListOption{
278+
client_cr.MatchingLabels(makeRegistryLabels(rr.Pkg.PackageName)),
279+
client_cr.InNamespace("testns"),
280+
}
281+
e = rr.Client.KubeClient.List(context.TODO(), &list, opts...)
282+
Expect(e).Should(BeNil())
283+
configmaps, err := rr.getRegistryConfigMaps(context.TODO(), "testns")
284+
Expect(err).Should(BeNil())
285+
286+
Expect(configmaps).Should(Equal(list.Items))
287+
})
288+
289+
})
290+
})

0 commit comments

Comments
 (0)