github.com/hyperledger-gerrit-archive/fabric-ca@v2.0.0-alpha.0.20190916143245-4cd4192f0366+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/onsi/ginkgo"
    17  	. "github.com/onsi/gomega"
    18  	"github.com/pkg/errors"
    19  )
    20  
    21  const (
    22  	testdataDir = "../../../../../testdata"
    23  )
    24  
    25  var _ = Describe("Postgres", func() {
    26  	var (
    27  		db     *postgres.Postgres
    28  		mockDB *mocks.FabricCADB
    29  	)
    30  
    31  	BeforeEach(func() {
    32  		tls := &tls.ClientTLSConfig{
    33  			Enabled:   true,
    34  			CertFiles: []string{filepath.Join(testdataDir, "root.pem")},
    35  		}
    36  		db = postgres.NewDB(
    37  			"host=localhost port=5432 user=root password=rootpw dbname=fabric_ca",
    38  			"",
    39  			tls,
    40  			nil,
    41  		)
    42  		mockDB = &mocks.FabricCADB{}
    43  	})
    44  
    45  	Context("open connection to database", func() {
    46  		It("fails to connect if the contains incorrect syntax", func() {
    47  			db = postgres.NewDB(
    48  				"hos) (t=localhost port=5432 user=root password=rootpw dbname=fabric-ca",
    49  				"",
    50  				nil,
    51  				nil,
    52  			)
    53  			err := db.Connect()
    54  			Expect(err).To(HaveOccurred())
    55  			Expect(err.Error()).Should(Equal("Database name 'fabric-ca' cannot contain any '-' or end with '.db'"))
    56  
    57  			db = postgres.NewDB(
    58  				"host=localhost port=5432 user=root password=rootpw dbname=fabric_ca.db",
    59  				"",
    60  				nil,
    61  				nil,
    62  			)
    63  			err = db.Connect()
    64  			Expect(err).To(HaveOccurred())
    65  			Expect(err.Error()).Should(Equal("Database name 'fabric_ca.db' cannot contain any '-' or end with '.db'"))
    66  		})
    67  
    68  		It("fails to open database connection of root cert files missing from tls config", func() {
    69  			db.TLS.CertFiles = nil
    70  			err := db.Connect()
    71  			Expect(err).To(HaveOccurred())
    72  			Expect(err.Error()).Should(Equal("No trusted root certificates for TLS were provided"))
    73  			Expect(db.SqlxDB).To(BeNil())
    74  		})
    75  
    76  		It("has datasource with TLS connection parameters when TLS is enabled", func() {
    77  			db.TLS = &tls.ClientTLSConfig{
    78  				Enabled:   true,
    79  				CertFiles: []string{"root.pem"},
    80  				Client: tls.KeyCertFiles{
    81  					KeyFile:  "key.pem",
    82  					CertFile: "cert.pem",
    83  				},
    84  			}
    85  			db.Connect()
    86  			Expect(db.Datasource()).To(
    87  				ContainSubstring("sslrootcert=root.pem sslcert=cert.pem sslkey=key.pem"),
    88  			)
    89  		})
    90  
    91  		It("does not have has datasource with TLS connection parameters when TLS is enabled", func() {
    92  			db.TLS = &tls.ClientTLSConfig{
    93  				Enabled: false,
    94  			}
    95  			db.Connect()
    96  			Expect(db.Datasource()).ToNot(ContainSubstring("sslrootcert"))
    97  		})
    98  
    99  		It("fail to open database connection if unable to ping database", func() {
   100  			err := db.Connect()
   101  			Expect(err).To(HaveOccurred())
   102  			Expect(
   103  				err.Error()).Should(
   104  				ContainSubstring(
   105  					"Failed to connect to Postgres database. Postgres requires connecting to a specific database, the following databases were tried: [fabric_ca postgres template1]",
   106  				),
   107  			)
   108  		})
   109  	})
   110  
   111  	Context("pinging database", func() {
   112  		It("returns an error if unable to ping database", func() {
   113  			mockDB.PingContextReturns(errors.New("ping error"))
   114  			db.SqlxDB = mockDB
   115  
   116  			err := db.PingContext(context.Background())
   117  			Expect(err).To(HaveOccurred())
   118  			Expect(err.Error()).To(Equal("Failed to ping to Postgres database: ping error"))
   119  		})
   120  
   121  		It("returns no error if able to ping database", func() {
   122  			db.SqlxDB = mockDB
   123  
   124  			err := db.PingContext(context.Background())
   125  			Expect(err).NotTo(HaveOccurred())
   126  		})
   127  	})
   128  
   129  	Context("creating fabric ca database", func() {
   130  		It("returns an error if unable execute create fabric ca database sql", func() {
   131  			mockDB.ExecReturns(nil, errors.New("error creating database"))
   132  			db.SqlxDB = mockDB
   133  			_, err := db.CreateDatabase()
   134  			Expect(err).To(HaveOccurred())
   135  			Expect(err.Error()).Should(ContainSubstring("Failed to create Postgres database: Failed to execute create database query: error creating database"))
   136  		})
   137  
   138  		It("creates the fabric ca database", func() {
   139  			db.SqlxDB = mockDB
   140  
   141  			_, err := db.CreateDatabase()
   142  			Expect(err).NotTo(HaveOccurred())
   143  		})
   144  	})
   145  
   146  	Context("creating tables", func() {
   147  		It("returns an error if unable to create users table", func() {
   148  			mockDB.ExecReturnsOnCall(0, nil, errors.New("unable to create table"))
   149  
   150  			db.SqlxDB = mockDB
   151  			err := db.CreateTables()
   152  			Expect(err).To(HaveOccurred())
   153  			Expect(err.Error()).Should(ContainSubstring("Failed to create Postgres tables: Error creating users table: unable to create table"))
   154  		})
   155  
   156  		It("returns an error if unable to create affiliations table", func() {
   157  			mockDB.ExecReturnsOnCall(1, nil, errors.New("unable to create table"))
   158  
   159  			db.SqlxDB = mockDB
   160  			err := db.CreateTables()
   161  			Expect(err).To(HaveOccurred())
   162  			Expect(err.Error()).Should(ContainSubstring("Failed to create Postgres tables: Error creating affiliations table: unable to create table"))
   163  		})
   164  
   165  		It("returns an error if unable to create certificates table", func() {
   166  			mockDB.ExecReturnsOnCall(2, nil, errors.New("unable to create table"))
   167  
   168  			db.SqlxDB = mockDB
   169  			err := db.CreateTables()
   170  			Expect(err).To(HaveOccurred())
   171  			Expect(err.Error()).Should(ContainSubstring("Failed to create Postgres tables: Error creating certificates table: unable to create table"))
   172  		})
   173  
   174  		It("returns an error if unable to create credentails table", func() {
   175  			mockDB.ExecReturnsOnCall(3, nil, errors.New("unable to create table"))
   176  
   177  			db.SqlxDB = mockDB
   178  			err := db.CreateTables()
   179  			Expect(err).To(HaveOccurred())
   180  			Expect(err.Error()).Should(ContainSubstring("Failed to create Postgres tables: Error creating credentials table: unable to create table"))
   181  		})
   182  
   183  		It("returns an error if unable to create revocation_authority_info table", func() {
   184  			mockDB.ExecReturnsOnCall(4, nil, errors.New("unable to create table"))
   185  
   186  			db.SqlxDB = mockDB
   187  			err := db.CreateTables()
   188  			Expect(err).To(HaveOccurred())
   189  			Expect(err.Error()).Should(ContainSubstring("Failed to create Postgres tables: Error creating revocation_authority_info table: unable to create table"))
   190  		})
   191  
   192  		It("returns an error if unable to create nonces table", func() {
   193  			mockDB.ExecReturnsOnCall(5, nil, errors.New("unable to create table"))
   194  
   195  			db.SqlxDB = mockDB
   196  			err := db.CreateTables()
   197  			Expect(err).To(HaveOccurred())
   198  			Expect(err.Error()).Should(ContainSubstring("Failed to create Postgres tables: Error creating nonces table: unable to create table"))
   199  		})
   200  
   201  		It("returns an error if unable to create properties table", func() {
   202  			mockDB.ExecReturnsOnCall(6, nil, errors.New("unable to create table"))
   203  
   204  			db.SqlxDB = mockDB
   205  			err := db.CreateTables()
   206  			Expect(err).To(HaveOccurred())
   207  			Expect(err.Error()).Should(ContainSubstring("Failed to create Postgres tables: Error creating properties table: unable to create table"))
   208  		})
   209  
   210  		It("returns an error if unable to insert default value in properties table", func() {
   211  			mockDB.ExecReturnsOnCall(7, nil, errors.New("unable to insert default values"))
   212  
   213  			db.SqlxDB = mockDB
   214  			err := db.CreateTables()
   215  			Expect(err).To(HaveOccurred())
   216  			Expect(err.Error()).Should(ContainSubstring("Failed to create Postgres tables: unable to insert default values"))
   217  		})
   218  
   219  		It("creates the fabric ca tables", func() {
   220  			db.SqlxDB = mockDB
   221  
   222  			err := db.CreateTables()
   223  			Expect(err).NotTo(HaveOccurred())
   224  		})
   225  	})
   226  })