github.com/adecaro/fabric-ca@v2.0.0-alpha+incompatible/lib/server/db/postgres/postgres_test.go (about)

     1  /*
     2  Copyright IBM Corp. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package postgres_test
     8  
     9  import (
    10  	"context"
    11  	"path/filepath"
    12  
    13  	"github.com/hyperledger/fabric-ca/lib/server/db/postgres"
    14  	"github.com/hyperledger/fabric-ca/lib/server/db/postgres/mocks"
    15  	"github.com/hyperledger/fabric-ca/lib/tls"
    16  	"github.com/hyperledger/fabric/common/metrics/disabled"
    17  	. "github.com/onsi/ginkgo"
    18  	. "github.com/onsi/gomega"
    19  	"github.com/pkg/errors"
    20  )
    21  
    22  const (
    23  	testdataDir = "../../../../../testdata"
    24  )
    25  
    26  var _ = Describe("Postgres", func() {
    27  	var (
    28  		db     *postgres.Postgres
    29  		mockDB *mocks.FabricCADB
    30  	)
    31  
    32  	BeforeEach(func() {
    33  		tls := &tls.ClientTLSConfig{
    34  			Enabled:   true,
    35  			CertFiles: []string{filepath.Join(testdataDir, "root.pem")},
    36  		}
    37  		db = postgres.NewDB("host=localhost port=5432 user=root password=rootpw dbname=fabric_ca", "", tls, &disabled.Provider{})
    38  		mockDB = &mocks.FabricCADB{}
    39  	})
    40  
    41  	Context("open connection to database", func() {
    42  		It("fails to connect if the contains incorrect syntax", func() {
    43  			db = postgres.NewDB("hos) (t=localhost port=5432 user=root password=rootpw dbname=fabric-ca", "", nil, &disabled.Provider{})
    44  			err := db.Connect()
    45  			Expect(err).To(HaveOccurred())
    46  			Expect(err.Error()).Should(Equal("Database name 'fabric-ca' cannot contain any '-' or end with '.db'"))
    47  
    48  			db = postgres.NewDB("host=localhost port=5432 user=root password=rootpw dbname=fabric_ca.db", "", nil, &disabled.Provider{})
    49  			err = db.Connect()
    50  			Expect(err).To(HaveOccurred())
    51  			Expect(err.Error()).Should(Equal("Database name 'fabric_ca.db' cannot contain any '-' or end with '.db'"))
    52  		})
    53  
    54  		It("fails to open database connection of root cert files missing from tls config", func() {
    55  			db.TLS.CertFiles = nil
    56  			err := db.Connect()
    57  			Expect(err).To(HaveOccurred())
    58  			Expect(err.Error()).Should(Equal("No trusted root certificates for TLS were provided"))
    59  			Expect(db.SqlxDB).To(BeNil())
    60  		})
    61  
    62  		It("fail to open database connection if unable to ping database", func() {
    63  			err := db.Connect()
    64  			Expect(err).To(HaveOccurred())
    65  			Expect(err.Error()).Should(ContainSubstring("Failed to connect to Postgres database. Postgres requires connecting to a specific database, the following databases were tried: [fabric_ca postgres template1]"))
    66  		})
    67  	})
    68  
    69  	Context("pinging database", func() {
    70  		It("returns an error if unable to ping database", func() {
    71  			mockDB.PingContextReturns(errors.New("ping error"))
    72  			db.SqlxDB = mockDB
    73  
    74  			err := db.PingContext(context.Background())
    75  			Expect(err).To(HaveOccurred())
    76  			Expect(err.Error()).To(Equal("Failed to ping to Postgres database: ping error"))
    77  		})
    78  
    79  		It("returns no error if able to ping database", func() {
    80  			db.SqlxDB = mockDB
    81  
    82  			err := db.PingContext(context.Background())
    83  			Expect(err).NotTo(HaveOccurred())
    84  		})
    85  	})
    86  
    87  	Context("creating fabric ca database", func() {
    88  		It("returns an error if unable execute create fabric ca database sql", func() {
    89  			mockDB.ExecReturns(nil, errors.New("error creating database"))
    90  			db.SqlxDB = mockDB
    91  			_, err := db.CreateDatabase()
    92  			Expect(err).To(HaveOccurred())
    93  			Expect(err.Error()).Should(ContainSubstring("Failed to create Postgres database: Failed to execute create database query: error creating database"))
    94  		})
    95  
    96  		It("creates the fabric ca database", func() {
    97  			db.SqlxDB = mockDB
    98  
    99  			_, err := db.CreateDatabase()
   100  			Expect(err).NotTo(HaveOccurred())
   101  		})
   102  	})
   103  
   104  	Context("creating tables", func() {
   105  		It("returns an error if unable to create users table", func() {
   106  			mockDB.ExecReturnsOnCall(0, nil, errors.New("unable to create table"))
   107  
   108  			db.SqlxDB = mockDB
   109  			err := db.CreateTables()
   110  			Expect(err).To(HaveOccurred())
   111  			Expect(err.Error()).Should(ContainSubstring("Failed to create Postgres tables: Error creating users table: unable to create table"))
   112  		})
   113  
   114  		It("returns an error if unable to create affiliations table", func() {
   115  			mockDB.ExecReturnsOnCall(1, nil, errors.New("unable to create table"))
   116  
   117  			db.SqlxDB = mockDB
   118  			err := db.CreateTables()
   119  			Expect(err).To(HaveOccurred())
   120  			Expect(err.Error()).Should(ContainSubstring("Failed to create Postgres tables: Error creating affiliations table: unable to create table"))
   121  		})
   122  
   123  		It("returns an error if unable to create certificates table", func() {
   124  			mockDB.ExecReturnsOnCall(2, nil, errors.New("unable to create table"))
   125  
   126  			db.SqlxDB = mockDB
   127  			err := db.CreateTables()
   128  			Expect(err).To(HaveOccurred())
   129  			Expect(err.Error()).Should(ContainSubstring("Failed to create Postgres tables: Error creating certificates table: unable to create table"))
   130  		})
   131  
   132  		It("returns an error if unable to create credentails table", func() {
   133  			mockDB.ExecReturnsOnCall(3, nil, errors.New("unable to create table"))
   134  
   135  			db.SqlxDB = mockDB
   136  			err := db.CreateTables()
   137  			Expect(err).To(HaveOccurred())
   138  			Expect(err.Error()).Should(ContainSubstring("Failed to create Postgres tables: Error creating credentials table: unable to create table"))
   139  		})
   140  
   141  		It("returns an error if unable to create revocation_authority_info table", func() {
   142  			mockDB.ExecReturnsOnCall(4, nil, errors.New("unable to create table"))
   143  
   144  			db.SqlxDB = mockDB
   145  			err := db.CreateTables()
   146  			Expect(err).To(HaveOccurred())
   147  			Expect(err.Error()).Should(ContainSubstring("Failed to create Postgres tables: Error creating revocation_authority_info table: unable to create table"))
   148  		})
   149  
   150  		It("returns an error if unable to create nonces table", func() {
   151  			mockDB.ExecReturnsOnCall(5, nil, errors.New("unable to create table"))
   152  
   153  			db.SqlxDB = mockDB
   154  			err := db.CreateTables()
   155  			Expect(err).To(HaveOccurred())
   156  			Expect(err.Error()).Should(ContainSubstring("Failed to create Postgres tables: Error creating nonces table: unable to create table"))
   157  		})
   158  
   159  		It("returns an error if unable to create properties table", func() {
   160  			mockDB.ExecReturnsOnCall(6, nil, errors.New("unable to create table"))
   161  
   162  			db.SqlxDB = mockDB
   163  			err := db.CreateTables()
   164  			Expect(err).To(HaveOccurred())
   165  			Expect(err.Error()).Should(ContainSubstring("Failed to create Postgres tables: Error creating properties table: unable to create table"))
   166  		})
   167  
   168  		It("returns an error if unable to insert default value in properties table", func() {
   169  			mockDB.ExecReturnsOnCall(7, nil, errors.New("unable to insert default values"))
   170  
   171  			db.SqlxDB = mockDB
   172  			err := db.CreateTables()
   173  			Expect(err).To(HaveOccurred())
   174  			Expect(err.Error()).Should(ContainSubstring("Failed to create Postgres tables: unable to insert default values"))
   175  		})
   176  
   177  		It("creates the fabric ca tables", func() {
   178  			db.SqlxDB = mockDB
   179  
   180  			err := db.CreateTables()
   181  			Expect(err).NotTo(HaveOccurred())
   182  		})
   183  	})
   184  })