github.com/IBM-Blockchain/fabric-operator@v1.0.4/pkg/offering/k8s/console/override/consolecm_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 override_test
    20  
    21  import (
    22  	. "github.com/onsi/ginkgo/v2"
    23  	. "github.com/onsi/gomega"
    24  	corev1 "k8s.io/api/core/v1"
    25  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    26  	"sigs.k8s.io/yaml"
    27  
    28  	current "github.com/IBM-Blockchain/fabric-operator/api/v1beta1"
    29  	consolev1 "github.com/IBM-Blockchain/fabric-operator/pkg/apis/console/v1"
    30  	v1 "github.com/IBM-Blockchain/fabric-operator/pkg/apis/console/v1"
    31  	"github.com/IBM-Blockchain/fabric-operator/pkg/manager/resources"
    32  	"github.com/IBM-Blockchain/fabric-operator/pkg/offering/k8s/console/override"
    33  	"github.com/IBM-Blockchain/fabric-operator/pkg/util"
    34  )
    35  
    36  var _ = Describe("K8S Console Config Map Overrides", func() {
    37  	var (
    38  		overrider *override.Override
    39  		instance  *current.IBPConsole
    40  		cm        *corev1.ConfigMap
    41  	)
    42  
    43  	BeforeEach(func() {
    44  		var err error
    45  
    46  		cm, err = util.GetConfigMapFromFile("../../../../../definitions/console/console-configmap.yaml")
    47  		Expect(err).NotTo(HaveOccurred())
    48  
    49  		overrider = &override.Override{}
    50  		instance = &current.IBPConsole{
    51  			ObjectMeta: metav1.ObjectMeta{
    52  				Name:      "consolecm",
    53  				Namespace: "consolecmns",
    54  			},
    55  			Spec: current.IBPConsoleSpec{
    56  				Email:            "test@ibm.com",
    57  				AuthScheme:       "scheme1",
    58  				ConfigtxlatorURL: "configtx.ibm.com",
    59  				DeployerURL:      "deployer.ibm.com",
    60  				DeployerTimeout:  5,
    61  				Components:       "component1",
    62  				Sessions:         "session1",
    63  				System:           "system1",
    64  				SystemChannel:    "channel1",
    65  				FeatureFlags: &consolev1.FeatureFlags{
    66  					CreateChannelEnabled: true,
    67  				},
    68  				ClusterData: &consolev1.IBPConsoleClusterData{
    69  					Zones: []string{"zone1"},
    70  					Type:  "type1",
    71  				},
    72  				NetworkInfo: &current.NetworkInfo{
    73  					Domain: "ibm.com",
    74  				},
    75  			},
    76  		}
    77  	})
    78  
    79  	Context("create", func() {
    80  		It("returns an error if domain not provided", func() {
    81  			instance.Spec.NetworkInfo.Domain = ""
    82  			err := overrider.ConsoleCM(instance, cm, resources.Create, nil)
    83  			Expect(err).To(HaveOccurred())
    84  			Expect(err.Error()).To(Equal("domain not provided"))
    85  		})
    86  
    87  		It("overrides values based on spec", func() {
    88  			err := overrider.ConsoleCM(instance, cm, resources.Create, nil)
    89  			Expect(err).NotTo(HaveOccurred())
    90  
    91  			config := &v1.ConsoleSettingsConfig{}
    92  			err = yaml.Unmarshal([]byte(cm.Data["settings.yaml"]), config)
    93  			Expect(err).NotTo(HaveOccurred())
    94  
    95  			CommonConsoleCMOverrides(instance, config)
    96  		})
    97  	})
    98  
    99  	Context("update", func() {
   100  		It("overrides values based on spec", func() {
   101  			err := overrider.ConsoleCM(instance, cm, resources.Update, nil)
   102  			Expect(err).NotTo(HaveOccurred())
   103  
   104  			config := &v1.ConsoleSettingsConfig{}
   105  			err = yaml.Unmarshal([]byte(cm.Data["settings.yaml"]), config)
   106  			Expect(err).NotTo(HaveOccurred())
   107  
   108  			CommonConsoleCMOverrides(instance, config)
   109  		})
   110  	})
   111  })
   112  
   113  func CommonConsoleCMOverrides(instance *current.IBPConsole, config *v1.ConsoleSettingsConfig) {
   114  	By("setting email", func() {
   115  		Expect(config.Email).To(Equal(instance.Spec.Email))
   116  	})
   117  
   118  	By("setting auth scheme", func() {
   119  		Expect(config.AuthScheme).To(Equal(instance.Spec.AuthScheme))
   120  	})
   121  
   122  	By("setting configtxlator URL", func() {
   123  		Expect(config.Configtxlator).To(Equal(instance.Spec.ConfigtxlatorURL))
   124  	})
   125  
   126  	By("setting Deployer URL", func() {
   127  		Expect(config.DeployerURL).To(Equal(instance.Spec.DeployerURL))
   128  	})
   129  
   130  	By("setting Deployer timeout", func() {
   131  		Expect(config.DeployerTimeout).To(Equal(instance.Spec.DeployerTimeout))
   132  	})
   133  
   134  	By("setting components", func() {
   135  		Expect(config.DBCustomNames.Components).To(Equal(instance.Spec.Components))
   136  	})
   137  
   138  	By("setting sessions", func() {
   139  		Expect(config.DBCustomNames.Sessions).To(Equal(instance.Spec.Sessions))
   140  	})
   141  
   142  	By("setting system", func() {
   143  		Expect(config.DBCustomNames.System).To(Equal(instance.Spec.System))
   144  	})
   145  
   146  	By("setting system channel", func() {
   147  		Expect(config.SystemChannelID).To(Equal(instance.Spec.SystemChannel))
   148  	})
   149  
   150  	By("setting Proxy TLS Reqs", func() {
   151  		Expect(config.ProxyTLSReqs).To(Equal("always"))
   152  	})
   153  
   154  	By("settings feature flags", func() {
   155  		Expect(config.Featureflags).To(Equal(instance.Spec.FeatureFlags))
   156  	})
   157  
   158  	By("settings cluster data", func() {
   159  		Expect(config.ClusterData).To(Equal(instance.Spec.ClusterData))
   160  	})
   161  }