github.com/IBM-Blockchain/fabric-operator@v1.0.4/pkg/initializer/ca/config/db_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("DB config", func() {
    33  	const (
    34  		homeDir = "homedir"
    35  	)
    36  
    37  	BeforeEach(func() {
    38  		os.Mkdir(homeDir, 0777)
    39  	})
    40  
    41  	AfterEach(func() {
    42  		err := os.RemoveAll(homeDir)
    43  		Expect(err).NotTo(HaveOccurred())
    44  	})
    45  
    46  	var cfg *config.Config
    47  
    48  	Context("parses DB configuration", func() {
    49  		BeforeEach(func() {
    50  			cfg = &config.Config{
    51  				ServerConfig: &v1.ServerConfig{
    52  					CAConfig: v1.CAConfig{
    53  						DB: &v1.CAConfigDB{
    54  							Type:       string(config.Postgres),
    55  							Datasource: "host=0.0.0.0 port=8080 user=db password=db dbname=fabric sslmode=true",
    56  							TLS: v1.ClientTLSConfig{
    57  								Enabled:   pointer.True(),
    58  								CertFiles: []string{"../../../../testdata/tls/tls.crt"},
    59  								Client: v1.KeyCertFiles{
    60  									CertFile: certFile,
    61  									KeyFile:  keyFile,
    62  								},
    63  							},
    64  						},
    65  					},
    66  				},
    67  				HomeDir:    homeDir,
    68  				SqlitePath: "/tmp/ca.db",
    69  			}
    70  		})
    71  
    72  		It("returns an error if invalid database type specified", func() {
    73  			cfg.ServerConfig.CAConfig.DB.Type = "couchdb"
    74  			_, err := cfg.ParseDBBlock()
    75  			Expect(err).To(HaveOccurred())
    76  			Expect(err.Error()).To(Equal("database type 'couchdb' is not supported"))
    77  		})
    78  
    79  		It("returns an error if mysql database type specified", func() {
    80  			cfg.ServerConfig.CAConfig.DB.Type = string(config.MySQL)
    81  			_, err := cfg.ParseDBBlock()
    82  			Expect(err).To(HaveOccurred())
    83  			Expect(err.Error()).To(Equal("MySQL is not supported"))
    84  		})
    85  
    86  		It("returns no error and an empty map if TLS disabled", func() {
    87  			cfg.ServerConfig.CAConfig.DB.TLS.Enabled = pointer.False()
    88  			crypto, err := cfg.ParseDBBlock()
    89  			Expect(err).NotTo(HaveOccurred())
    90  			Expect(crypto).To(BeNil())
    91  		})
    92  
    93  		It("returns an error if missing datasource", func() {
    94  			cfg.ServerConfig.CAConfig.DB.Datasource = ""
    95  			_, err := cfg.ParseDBBlock()
    96  			Expect(err).To(HaveOccurred())
    97  			Expect(err.Error()).To(Equal("no datasource string specified for postgres"))
    98  		})
    99  
   100  		It("returns an error if datasource is unexpected format", func() {
   101  			cfg.ServerConfig.CAConfig.DB.Datasource = "dbname=testdb"
   102  			_, err := cfg.ParseDBBlock()
   103  			Expect(err).To(HaveOccurred())
   104  			Expect(err.Error()).To(Equal("datasource for postgres is not valid"))
   105  		})
   106  
   107  		It("parses config and returns a map containing all db crypto and updated paths to crypto material", func() {
   108  			crypto, err := cfg.ParseDBBlock()
   109  			Expect(err).NotTo(HaveOccurred())
   110  
   111  			certData, certKeyExists := crypto["db-cert.pem"]
   112  			Expect(certKeyExists).To(Equal(true))
   113  			Expect(certData).NotTo(BeNil())
   114  			Expect(cfg.ServerConfig.CAConfig.DB.TLS.Client.CertFile).To(Equal(filepath.Join(cfg.HomeDir, "db-cert.pem")))
   115  
   116  			keyData, keyKeyExists := crypto["db-key.pem"]
   117  			Expect(keyKeyExists).To(Equal(true))
   118  			Expect(keyData).NotTo(BeNil())
   119  			Expect(cfg.ServerConfig.CAConfig.DB.TLS.Client.KeyFile).To(Equal(filepath.Join(cfg.HomeDir, "db-key.pem")))
   120  
   121  			clientAuthData, clientAuthCertKeyExists := crypto["db-certfile0.pem"]
   122  			Expect(clientAuthCertKeyExists).To(Equal(true))
   123  			Expect(clientAuthData).NotTo(BeNil())
   124  			Expect(cfg.ServerConfig.CAConfig.DB.TLS.CertFiles[0]).To(Equal(filepath.Join(cfg.HomeDir, "db-certfile0.pem")))
   125  		})
   126  
   127  		It("creates SQLLite database and returns empty crypto map", func() {
   128  			cfg.ServerConfig.CAConfig.DB.Type = string(config.SQLLite)
   129  			crypto, err := cfg.ParseDBBlock()
   130  			Expect(err).NotTo(HaveOccurred())
   131  			Expect(crypto).To(BeNil())
   132  
   133  			os.RemoveAll("dbconfigtest")
   134  		})
   135  	})
   136  })