github.com/IBM-Blockchain/fabric-operator@v1.0.4/pkg/initializer/ca/config/operations_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 config_test
    20  
    21  import (
    22  	"os"
    23  	"path/filepath"
    24  
    25  	v1 "github.com/IBM-Blockchain/fabric-operator/pkg/apis/ca/v1"
    26  	"github.com/IBM-Blockchain/fabric-operator/pkg/initializer/ca/config"
    27  	"github.com/IBM-Blockchain/fabric-operator/pkg/util/pointer"
    28  	. "github.com/onsi/ginkgo/v2"
    29  	. "github.com/onsi/gomega"
    30  )
    31  
    32  var _ = Describe("Operations config", func() {
    33  	var (
    34  		cfg     *config.Config
    35  		homeDir = "operationsconfigtest"
    36  	)
    37  
    38  	BeforeEach(func() {
    39  		os.Mkdir(homeDir, 0777)
    40  	})
    41  
    42  	AfterEach(func() {
    43  		err := os.RemoveAll(homeDir)
    44  		Expect(err).NotTo(HaveOccurred())
    45  	})
    46  
    47  	Context("parses Operations configuration", func() {
    48  		BeforeEach(func() {
    49  			cfg = &config.Config{
    50  				ServerConfig: &v1.ServerConfig{
    51  					Operations: v1.Options{
    52  						TLS: v1.TLS{
    53  							Enabled:           pointer.True(),
    54  							CertFile:          certFile,
    55  							KeyFile:           keyFile,
    56  							ClientCACertFiles: []string{certFile},
    57  						},
    58  					},
    59  				},
    60  				HomeDir: homeDir,
    61  			}
    62  		})
    63  
    64  		It("returns no error and an empty map if TLS disabled", func() {
    65  			cfg.ServerConfig.Operations.TLS.Enabled = pointer.False()
    66  			crypto, err := cfg.ParseOperationsBlock()
    67  			Expect(err).NotTo(HaveOccurred())
    68  			Expect(crypto).To(BeNil())
    69  		})
    70  
    71  		It("parses config and returns a map containing all crypto and updated paths to crypto material", func() {
    72  			crypto, err := cfg.ParseOperationsBlock()
    73  			Expect(err).NotTo(HaveOccurred())
    74  
    75  			certData, certKeyExists := crypto["operations-cert.pem"]
    76  			Expect(certKeyExists).To(Equal(true))
    77  			Expect(certData).NotTo(BeNil())
    78  			Expect(cfg.ServerConfig.Operations.TLS.CertFile).To(Equal(filepath.Join(cfg.HomeDir, "operations-cert.pem")))
    79  
    80  			keyData, keyKeyExists := crypto["operations-key.pem"]
    81  			Expect(keyKeyExists).To(Equal(true))
    82  			Expect(keyData).NotTo(BeNil())
    83  			Expect(cfg.ServerConfig.Operations.TLS.KeyFile).To(Equal(filepath.Join(cfg.HomeDir, "operations-key.pem")))
    84  
    85  			chainData, chainKeyExists := crypto["operations-certfile0.pem"]
    86  			Expect(chainKeyExists).To(Equal(true))
    87  			Expect(chainData).NotTo(BeNil())
    88  			Expect(cfg.ServerConfig.Operations.TLS.ClientCACertFiles[0]).To(Equal(filepath.Join(cfg.HomeDir, "operations-certfile0.pem")))
    89  		})
    90  	})
    91  })