gitee.com/zhaochuninhefei/fabric-ca-gm@v0.0.2/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 "gitee.com/zhaochuninhefei/fabric-ca-gm/lib/server/db/postgres" 14 "gitee.com/zhaochuninhefei/fabric-ca-gm/lib/server/db/postgres/mocks" 15 "gitee.com/zhaochuninhefei/fabric-ca-gm/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 When("creating the Fabric CA database fails", func() { 131 It("returns an error", func() { 132 mockDB.ExecReturns(nil, errors.New("error creating database")) 133 db.SqlxDB = mockDB 134 _, err := db.CreateDatabase() 135 Expect(err).To(HaveOccurred()) 136 Expect(err.Error()).To(Equal("Failed to create Postgres database: Failed to execute create database query: error creating database")) 137 }) 138 }) 139 140 When("the database does not exist", func() { 141 It("creates the fabric ca database", func() { 142 db.SqlxDB = mockDB 143 sqlxDB, err := db.CreateDatabase() 144 Expect(err).NotTo(HaveOccurred()) 145 Expect(db.SqlxDB).To(Equal(sqlxDB)) 146 Expect(mockDB.ExecCallCount()).To(Equal(1)) 147 }) 148 }) 149 150 When("the database already exists", func() { 151 It("does not attempt to create the database", func() { 152 mockDB.GetStub = func(s string, i interface{}, s2 string, i2 ...interface{}) error { 153 *(i.(*bool)) = true 154 return nil 155 } 156 db.SqlxDB = mockDB 157 sqlxDB, err := db.CreateDatabase() 158 Expect(err).NotTo(HaveOccurred()) 159 Expect(db.SqlxDB).To(Equal(sqlxDB)) 160 Expect(mockDB.ExecCallCount()).To(Equal(0)) 161 }) 162 }) 163 }) 164 165 Context("creating tables", func() { 166 It("returns an error if unable to create users table", func() { 167 mockDB.ExecReturnsOnCall(0, nil, errors.New("unable to create table")) 168 169 db.SqlxDB = mockDB 170 err := db.CreateTables() 171 Expect(err).To(HaveOccurred()) 172 Expect(err.Error()).Should(ContainSubstring("Failed to create Postgres tables: Error creating users table: unable to create table")) 173 }) 174 175 It("returns an error if unable to create users index", func() { 176 mockDB.ExecReturnsOnCall(1, nil, errors.New("unable to create table")) 177 178 db.SqlxDB = mockDB 179 err := db.CreateTables() 180 Expect(err).To(HaveOccurred()) 181 Expect(err.Error()).Should(ContainSubstring("Failed to create Postgres tables: Error creating users id index: unable to create table")) 182 }) 183 184 It("returns an error if unable to create affiliations table", func() { 185 mockDB.ExecReturnsOnCall(2, nil, errors.New("unable to create table")) 186 187 db.SqlxDB = mockDB 188 err := db.CreateTables() 189 Expect(err).To(HaveOccurred()) 190 Expect(err.Error()).Should(ContainSubstring("Failed to create Postgres tables: Error creating affiliations table: unable to create table")) 191 }) 192 193 It("returns an error if unable to create certificates table", func() { 194 mockDB.ExecReturnsOnCall(3, nil, errors.New("unable to create table")) 195 196 db.SqlxDB = mockDB 197 err := db.CreateTables() 198 Expect(err).To(HaveOccurred()) 199 Expect(err.Error()).Should(ContainSubstring("Failed to create Postgres tables: Error creating certificates table: unable to create table")) 200 }) 201 202 It("returns an error if unable to create credentails table", func() { 203 mockDB.ExecReturnsOnCall(4, nil, errors.New("unable to create table")) 204 205 db.SqlxDB = mockDB 206 err := db.CreateTables() 207 Expect(err).To(HaveOccurred()) 208 Expect(err.Error()).Should(ContainSubstring("Failed to create Postgres tables: Error creating credentials table: unable to create table")) 209 }) 210 211 It("returns an error if unable to create revocation_authority_info table", func() { 212 mockDB.ExecReturnsOnCall(5, nil, errors.New("unable to create table")) 213 214 db.SqlxDB = mockDB 215 err := db.CreateTables() 216 Expect(err).To(HaveOccurred()) 217 Expect(err.Error()).Should(ContainSubstring("Failed to create Postgres tables: Error creating revocation_authority_info table: unable to create table")) 218 }) 219 220 It("returns an error if unable to create nonces table", func() { 221 mockDB.ExecReturnsOnCall(6, nil, errors.New("unable to create table")) 222 223 db.SqlxDB = mockDB 224 err := db.CreateTables() 225 Expect(err).To(HaveOccurred()) 226 Expect(err.Error()).Should(ContainSubstring("Failed to create Postgres tables: Error creating nonces table: unable to create table")) 227 }) 228 229 It("returns an error if unable to create properties table", func() { 230 mockDB.ExecReturnsOnCall(7, nil, errors.New("unable to create table")) 231 232 db.SqlxDB = mockDB 233 err := db.CreateTables() 234 Expect(err).To(HaveOccurred()) 235 Expect(err.Error()).Should(ContainSubstring("Failed to create Postgres tables: Error creating properties table: unable to create table")) 236 }) 237 238 It("returns an error if unable to insert default value in properties table", func() { 239 mockDB.ExecReturnsOnCall(8, nil, errors.New("unable to insert default values")) 240 241 db.SqlxDB = mockDB 242 err := db.CreateTables() 243 Expect(err).To(HaveOccurred()) 244 Expect(err.Error()).Should(ContainSubstring("Failed to create Postgres tables: unable to insert default values")) 245 }) 246 247 It("creates the fabric ca tables", func() { 248 db.SqlxDB = mockDB 249 250 err := db.CreateTables() 251 Expect(err).NotTo(HaveOccurred()) 252 }) 253 }) 254 })