github.com/IBM-Blockchain/fabric-operator@v1.0.4/pkg/restart/configmap/configmap_test.go (about) 1 /* 2 * Copyright contributors to the Hyperledger Fabric Operator project 3 * 4 * SPDX-License-Identifier: Apache-2.0 5 * 6 * Licensed under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at: 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 19 package configmap_test 20 21 import ( 22 "context" 23 "encoding/json" 24 "errors" 25 26 . "github.com/onsi/ginkgo/v2" 27 . "github.com/onsi/gomega" 28 "sigs.k8s.io/controller-runtime/pkg/client" 29 30 controllermocks "github.com/IBM-Blockchain/fabric-operator/controllers/mocks" 31 "github.com/IBM-Blockchain/fabric-operator/pkg/restart/configmap" 32 33 corev1 "k8s.io/api/core/v1" 34 "k8s.io/apimachinery/pkg/types" 35 ) 36 37 var _ = Describe("Configmap", func() { 38 39 var ( 40 mockClient *controllermocks.Client 41 manager *configmap.Manager 42 ) 43 44 BeforeEach(func() { 45 mockClient = &controllermocks.Client{} 46 manager = configmap.NewManager(mockClient) 47 }) 48 49 Context("get restart config from", func() { 50 It("returns error if fails to get config map", func() { 51 mockClient.GetReturns(errors.New("fake error")) 52 err := manager.GetRestartConfigFrom("test-config", "namespace", nil) 53 Expect(err).To(HaveOccurred()) 54 Expect(err.Error()).To(ContainSubstring("failed to get test-config config map")) 55 }) 56 57 It("returns error if fails to unmarshal data", func() { 58 into := &TestConfig{} 59 mockClient.GetStub = func(ctx context.Context, ns types.NamespacedName, obj client.Object) error { 60 o := obj.(*corev1.ConfigMap) 61 o.Name = "test-config" 62 o.Namespace = ns.Namespace 63 o.BinaryData = map[string][]byte{ 64 "restart-config.yaml": []byte("test"), 65 } 66 return nil 67 } 68 69 err := manager.GetRestartConfigFrom("test-config", "namespace", into) 70 Expect(err).To(HaveOccurred()) 71 Expect(err.Error()).To(ContainSubstring("failed to unmarshal test-config config map")) 72 }) 73 74 It("unmarshals config map data into struct", func() { 75 into := &TestConfig{} 76 mockClient.GetStub = func(ctx context.Context, ns types.NamespacedName, obj client.Object) error { 77 cfg := &TestConfig{ 78 Field: "test", 79 } 80 bytes, _ := json.Marshal(cfg) 81 82 o := obj.(*corev1.ConfigMap) 83 o.Name = "test-config" 84 o.Namespace = ns.Namespace 85 o.BinaryData = map[string][]byte{ 86 "restart-config.yaml": bytes, 87 } 88 return nil 89 } 90 91 err := manager.GetRestartConfigFrom("test-config", "namespace", into) 92 Expect(err).NotTo(HaveOccurred()) 93 Expect(into.Field).To(Equal("test")) 94 }) 95 }) 96 97 It("update config", func() { 98 mockClient.CreateOrUpdateReturns(errors.New("fake error")) 99 err := manager.UpdateConfig("test-config", "ns", &TestConfig{}) 100 Expect(err).To(HaveOccurred()) 101 Expect(err.Error()).To(ContainSubstring("failed to create or update test-config config map")) 102 }) 103 }) 104 105 type TestConfig struct { 106 Field string 107 }