gitee.com/zhaochuninhefei/fabric-ca-gm@v0.0.2/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 "gitee.com/zhaochuninhefei/fabric-ca-gm/lib/server/db/mysql" 15 "gitee.com/zhaochuninhefei/fabric-ca-gm/lib/server/db/mysql/mocks" 16 "gitee.com/zhaochuninhefei/fabric-ca-gm/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 When("creating the Fabric CA database fails", func() { 86 It("returns an error", func() { 87 mockDB.ExecReturns(nil, errors.New("error creating database")) 88 db.SqlxDB = mockDB 89 _, err := db.CreateDatabase() 90 Expect(err).To(HaveOccurred()) 91 Expect(err.Error()).To(Equal("Failed to create MySQL database: Failed to execute create database query: error creating database")) 92 }) 93 }) 94 95 When("the database does not exist", func() { 96 It("creates the fabric ca database", func() { 97 db.SqlxDB = mockDB 98 sqlxDB, err := db.CreateDatabase() 99 Expect(err).NotTo(HaveOccurred()) 100 Expect(db.SqlxDB).To(Equal(sqlxDB)) 101 Expect(mockDB.ExecCallCount()).To(Equal(1)) 102 }) 103 }) 104 105 When("the database already exists", func() { 106 It("does not attempt to create the database", func() { 107 mockDB.GetStub = func(s string, i interface{}, s2 string, i2 ...interface{}) error { 108 *(i.(*bool)) = true 109 return nil 110 } 111 db.SqlxDB = mockDB 112 sqlxDB, err := db.CreateDatabase() 113 Expect(err).NotTo(HaveOccurred()) 114 Expect(db.SqlxDB).To(Equal(sqlxDB)) 115 Expect(mockDB.ExecCallCount()).To(Equal(0)) 116 }) 117 }) 118 }) 119 120 Context("creating tables", func() { 121 It("returns an error if unable to create users table", func() { 122 mockDB.ExecReturnsOnCall(0, nil, errors.New("unable to create table")) 123 124 db.SqlxDB = mockDB 125 err := db.CreateTables() 126 Expect(err).To(HaveOccurred()) 127 Expect(err.Error()).Should(ContainSubstring("Failed to create MySQL tables: Error creating users table: unable to create table")) 128 }) 129 130 It("returns an error if unable to create affiliations table", func() { 131 mockDB.ExecReturnsOnCall(1, nil, errors.New("unable to create table")) 132 133 db.SqlxDB = mockDB 134 err := db.CreateTables() 135 Expect(err).To(HaveOccurred()) 136 Expect(err.Error()).Should(ContainSubstring("Failed to create MySQL tables: Error creating affiliations table: unable to create table")) 137 }) 138 139 It("returns an error if unable to create index on affiliations table", func() { 140 mockDB.ExecReturnsOnCall(2, nil, errors.New("unable to create table")) 141 142 db.SqlxDB = mockDB 143 err := db.CreateTables() 144 Expect(err).To(HaveOccurred()) 145 Expect(err.Error()).Should(ContainSubstring("Failed to create MySQL tables: Error creating index on affiliations table: unable to create table")) 146 }) 147 148 It("returns an error if unable to create certificates table", func() { 149 mockDB.ExecReturnsOnCall(3, nil, errors.New("unable to create table")) 150 151 db.SqlxDB = mockDB 152 err := db.CreateTables() 153 Expect(err).To(HaveOccurred()) 154 Expect(err.Error()).Should(ContainSubstring("Failed to create MySQL tables: Error creating certificates table: unable to create table")) 155 }) 156 157 It("returns an error if unable to create credentials table", func() { 158 mockDB.ExecReturnsOnCall(4, nil, errors.New("unable to create table")) 159 160 db.SqlxDB = mockDB 161 err := db.CreateTables() 162 Expect(err).To(HaveOccurred()) 163 Expect(err.Error()).Should(ContainSubstring("Failed to create MySQL tables: Error creating credentials table: unable to create table")) 164 }) 165 166 It("returns an error if unable to create revocation_authority_info table", func() { 167 mockDB.ExecReturnsOnCall(5, 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 MySQL tables: Error creating revocation_authority_info table: unable to create table")) 173 }) 174 175 It("returns an error if unable to create nonces table", func() { 176 mockDB.ExecReturnsOnCall(6, 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 MySQL tables: Error creating nonces table: unable to create table")) 182 }) 183 184 It("returns an error if unable to create properties table", func() { 185 mockDB.ExecReturnsOnCall(7, 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 MySQL tables: Error creating properties table: unable to create table")) 191 }) 192 193 It("returns an error if unable to insert default value in properties table", func() { 194 mockDB.ExecReturnsOnCall(8, nil, errors.New("unable to insert default values")) 195 196 db.SqlxDB = mockDB 197 err := db.CreateTables() 198 Expect(err).To(HaveOccurred()) 199 Expect(err.Error()).Should(ContainSubstring("Failed to create MySQL tables: unable to insert default values")) 200 }) 201 202 It("creates the fabric ca tables", func() { 203 db.SqlxDB = mockDB 204 205 err := db.CreateTables() 206 Expect(err).NotTo(HaveOccurred()) 207 }) 208 209 }) 210 })