github.com/hyperledger-gerrit-archive/fabric-ca@v2.0.0-alpha.0.20190916143245-4cd4192f0366+incompatible/lib/server/db/mysql/mysql_test.go (about) 1 /* 2 Copyright IBM Corp. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package mysql_test 8 9 import ( 10 "context" 11 "errors" 12 "path/filepath" 13 14 "github.com/hyperledger/fabric-ca/lib/server/db/mysql" 15 "github.com/hyperledger/fabric-ca/lib/server/db/mysql/mocks" 16 "github.com/hyperledger/fabric-ca/lib/tls" 17 . "github.com/onsi/ginkgo" 18 . "github.com/onsi/gomega" 19 ) 20 21 const ( 22 testdataDir = "../../../../../testdata" 23 ) 24 25 var _ = Describe("Mysql", func() { 26 var ( 27 db *mysql.Mysql 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 = mysql.NewDB( 37 "root:rootpw@tcp(localhost:3306)/fabric_ca_db", 38 "", 39 tls, 40 nil, 41 nil, 42 ) 43 mockDB = &mocks.FabricCADB{} 44 }) 45 46 Context("open connection to database", func() { 47 It("fails to open database connection of root cert files missing from tls config", func() { 48 db.TLS.CertFiles = nil 49 err := db.Connect() 50 Expect(err).To(HaveOccurred()) 51 Expect(err.Error()).Should( 52 ContainSubstring( 53 "Failed to get client TLS for MySQL: No trusted root certificates for TLS were provided", 54 ), 55 ) 56 Expect(db.SqlxDB).To(BeNil()) 57 }) 58 59 It("fails to open database connection if unable to ping database", func() { 60 err := db.Connect() 61 Expect(err).To(HaveOccurred()) 62 Expect(db.SqlxDB).To(BeNil()) 63 }) 64 }) 65 66 Context("pinging database", func() { 67 It("returns an error if unable to ping database", func() { 68 mockDB.PingContextReturns(errors.New("ping error")) 69 db.SqlxDB = mockDB 70 71 err := db.PingContext(context.Background()) 72 Expect(err).To(HaveOccurred()) 73 Expect(err.Error()).To(Equal("Failed to ping to MySQL database: ping error")) 74 }) 75 76 It("returns no error if able to ping database", func() { 77 db.SqlxDB = mockDB 78 79 err := db.PingContext(context.Background()) 80 Expect(err).NotTo(HaveOccurred()) 81 }) 82 }) 83 84 Context("creating fabric ca database", func() { 85 It("returns an error if unable execute create fabric ca database sql", func() { 86 mockDB.ExecReturns(nil, errors.New("error creating database")) 87 db.SqlxDB = mockDB 88 _, err := db.CreateDatabase() 89 Expect(err).To(HaveOccurred()) 90 Expect(err.Error()).Should(ContainSubstring("Failed to create MySQL database: Failed to execute create database query: error creating database")) 91 }) 92 93 It("creates the fabric ca database", func() { 94 db.SqlxDB = mockDB 95 96 _, err := db.CreateDatabase() 97 Expect(err).NotTo(HaveOccurred()) 98 }) 99 }) 100 101 Context("creating tables", func() { 102 It("returns an error if unable to create users table", func() { 103 mockDB.ExecReturnsOnCall(0, nil, errors.New("unable to create table")) 104 105 db.SqlxDB = mockDB 106 err := db.CreateTables() 107 Expect(err).To(HaveOccurred()) 108 Expect(err.Error()).Should(ContainSubstring("Failed to create MySQL tables: Error creating users table: unable to create table")) 109 }) 110 111 It("returns an error if unable to create affiliations table", func() { 112 mockDB.ExecReturnsOnCall(1, nil, errors.New("unable to create table")) 113 114 db.SqlxDB = mockDB 115 err := db.CreateTables() 116 Expect(err).To(HaveOccurred()) 117 Expect(err.Error()).Should(ContainSubstring("Failed to create MySQL tables: Error creating affiliations table: unable to create table")) 118 }) 119 120 It("returns an error if unable to create index on affiliations table", func() { 121 mockDB.ExecReturnsOnCall(2, nil, errors.New("unable to create table")) 122 123 db.SqlxDB = mockDB 124 err := db.CreateTables() 125 Expect(err).To(HaveOccurred()) 126 Expect(err.Error()).Should(ContainSubstring("Failed to create MySQL tables: Error creating index on affiliations table: unable to create table")) 127 }) 128 129 It("returns an error if unable to create certificates table", func() { 130 mockDB.ExecReturnsOnCall(3, nil, errors.New("unable to create table")) 131 132 db.SqlxDB = mockDB 133 err := db.CreateTables() 134 Expect(err).To(HaveOccurred()) 135 Expect(err.Error()).Should(ContainSubstring("Failed to create MySQL tables: Error creating certificates table: unable to create table")) 136 }) 137 138 It("returns an error if unable to create credentials table", func() { 139 mockDB.ExecReturnsOnCall(4, nil, errors.New("unable to create table")) 140 141 db.SqlxDB = mockDB 142 err := db.CreateTables() 143 Expect(err).To(HaveOccurred()) 144 Expect(err.Error()).Should(ContainSubstring("Failed to create MySQL tables: Error creating credentials table: unable to create table")) 145 }) 146 147 It("returns an error if unable to create revocation_authority_info table", func() { 148 mockDB.ExecReturnsOnCall(5, 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 MySQL tables: Error creating revocation_authority_info table: unable to create table")) 154 }) 155 156 It("returns an error if unable to create nonces table", func() { 157 mockDB.ExecReturnsOnCall(6, 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 MySQL tables: Error creating nonces table: unable to create table")) 163 }) 164 165 It("returns an error if unable to create properties table", func() { 166 mockDB.ExecReturnsOnCall(7, 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 MySQL tables: Error creating properties table: unable to create table")) 172 }) 173 174 It("returns an error if unable to insert default value in properties table", func() { 175 mockDB.ExecReturnsOnCall(8, nil, errors.New("unable to insert default values")) 176 177 db.SqlxDB = mockDB 178 err := db.CreateTables() 179 Expect(err).To(HaveOccurred()) 180 Expect(err.Error()).Should(ContainSubstring("Failed to create MySQL tables: unable to insert default values")) 181 }) 182 183 It("creates the fabric ca tables", func() { 184 db.SqlxDB = mockDB 185 186 err := db.CreateTables() 187 Expect(err).NotTo(HaveOccurred()) 188 }) 189 190 }) 191 })