github.com/adecaro/fabric-ca@v2.0.0-alpha+incompatible/lib/server/db/mysql/migrator_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 "errors" 11 12 "github.com/hyperledger/fabric-ca/lib/server/db/mysql" 13 "github.com/hyperledger/fabric-ca/lib/server/db/mysql/mocks" 14 "github.com/hyperledger/fabric-ca/lib/server/db/util" 15 . "github.com/onsi/ginkgo" 16 . "github.com/onsi/gomega" 17 ) 18 19 var _ = Describe("Migrator", func() { 20 var ( 21 migrator *mysql.Migrator 22 mockTx *mocks.FabricCATx 23 ) 24 25 BeforeEach(func() { 26 mockTx = &mocks.FabricCATx{} 27 curLevels := &util.Levels{ 28 Identity: 0, 29 Affiliation: 0, 30 Certificate: 0, 31 Credential: 0, 32 RAInfo: 0, 33 Nonce: 0, 34 } 35 serverLevels := &util.Levels{ 36 Identity: 2, 37 Affiliation: 2, 38 Certificate: 2, 39 Credential: 2, 40 RAInfo: 2, 41 Nonce: 2, 42 } 43 migrator = mysql.NewMigrator(mockTx, curLevels, serverLevels) 44 }) 45 46 Context("users table", func() { 47 It("returns error if modifying columns id, type, and affilaition fails", func() { 48 mockTx.ExecReturnsOnCall(0, nil, errors.New("failed to modify id, type, and affilaition columns")) 49 migrator.Tx = mockTx 50 51 err := migrator.MigrateUsersTable() 52 Expect(err).To(HaveOccurred()) 53 Expect(err.Error()).To(Equal("failed to modify id, type, and affilaition columns")) 54 }) 55 56 It("returns error if modifying attributes column fails", func() { 57 mockTx.ExecReturnsOnCall(1, nil, errors.New("failed to modify attributes column")) 58 migrator.Tx = mockTx 59 60 err := migrator.MigrateUsersTable() 61 Expect(err).To(HaveOccurred()) 62 Expect(err.Error()).To(Equal("failed to modify attributes column")) 63 }) 64 65 It("returns error if adding level column fails", func() { 66 mockTx.QueryxReturns(nil, nil) 67 mockTx.ExecReturnsOnCall(2, nil, errors.New("failed to add level columns")) 68 migrator.Tx = mockTx 69 70 err := migrator.MigrateUsersTable() 71 Expect(err).To(HaveOccurred()) 72 Expect(err.Error()).To(Equal("failed to add level columns")) 73 }) 74 75 It("returns error if adding incorrect_password_attempts column fails", func() { 76 mockTx.QueryxReturns(nil, nil) 77 mockTx.ExecReturnsOnCall(3, nil, errors.New("failed to add incorrect_password_attempts columns")) 78 migrator.Tx = mockTx 79 80 err := migrator.MigrateUsersTable() 81 Expect(err).To(HaveOccurred()) 82 Expect(err.Error()).To(Equal("failed to add incorrect_password_attempts columns")) 83 }) 84 85 It("returns error if updating properties to new users table level fails", func() { 86 mockTx.QueryxReturns(nil, nil) 87 mockTx.ExecReturnsOnCall(4, nil, errors.New("failed to update properties table")) 88 migrator.Tx = mockTx 89 90 err := migrator.MigrateUsersTable() 91 Expect(err).To(HaveOccurred()) 92 Expect(err.Error()).To(Equal("failed to update properties table")) 93 }) 94 95 It("returns error if migrating a user in the users table fails", func() { 96 mockTx.QueryxReturns(nil, errors.New("failed to query users")) 97 migrator.Tx = mockTx 98 99 err := migrator.MigrateUsersTable() 100 Expect(err).To(HaveOccurred()) 101 Expect(err.Error()).To(Equal("Failed to get identities that need to be updated: failed to query users")) 102 }) 103 104 It("migrates successfully", func() { 105 mockTx.QueryxReturns(nil, nil) 106 migrator.Tx = mockTx 107 108 err := migrator.MigrateUsersTable() 109 Expect(err).NotTo(HaveOccurred()) 110 }) 111 }) 112 113 Context("certificates table", func() { 114 It("returns error if adding level column fails", func() { 115 mockTx.ExecReturnsOnCall(0, nil, errors.New("failed to add level columns")) 116 migrator.Tx = mockTx 117 118 err := migrator.MigrateCertificatesTable() 119 Expect(err).To(HaveOccurred()) 120 Expect(err.Error()).To(Equal("failed to add level columns")) 121 }) 122 123 It("returns error if modifying id column fails", func() { 124 mockTx.ExecReturnsOnCall(1, nil, errors.New("failed to modify id column")) 125 migrator.Tx = mockTx 126 127 err := migrator.MigrateCertificatesTable() 128 Expect(err).To(HaveOccurred()) 129 Expect(err.Error()).To(Equal("failed to modify id column")) 130 }) 131 132 It("returns error if updating properties to new certificate table level fails", func() { 133 mockTx.ExecReturnsOnCall(2, nil, errors.New("failed to update properties table")) 134 migrator.Tx = mockTx 135 136 err := migrator.MigrateCertificatesTable() 137 Expect(err).To(HaveOccurred()) 138 Expect(err.Error()).To(Equal("failed to update properties table")) 139 }) 140 141 It("migrates successfully", func() { 142 err := migrator.MigrateCertificatesTable() 143 Expect(err).NotTo(HaveOccurred()) 144 }) 145 }) 146 147 Context("affiliations table", func() { 148 It("returns error if adding level column fails", func() { 149 mockTx.ExecReturnsOnCall(0, nil, errors.New("failed to add level columns")) 150 migrator.Tx = mockTx 151 152 err := migrator.MigrateAffiliationsTable() 153 Expect(err).To(HaveOccurred()) 154 Expect(err.Error()).To(Equal("failed to add level columns")) 155 }) 156 157 It("returns error if dropping index fails", func() { 158 mockTx.ExecReturnsOnCall(1, nil, errors.New("failed to drop index")) 159 migrator.Tx = mockTx 160 161 err := migrator.MigrateAffiliationsTable() 162 Expect(err).To(HaveOccurred()) 163 Expect(err.Error()).To(Equal("failed to drop index")) 164 }) 165 166 It("returns error if adding id column fails", func() { 167 mockTx.ExecReturnsOnCall(2, nil, errors.New("failed to add id column")) 168 migrator.Tx = mockTx 169 170 err := migrator.MigrateAffiliationsTable() 171 Expect(err).To(HaveOccurred()) 172 Expect(err.Error()).To(Equal("failed to add id column")) 173 }) 174 175 It("returns error if modifying name and prekey column fails", func() { 176 mockTx.ExecReturnsOnCall(3, nil, errors.New("failed to modify columns")) 177 migrator.Tx = mockTx 178 179 err := migrator.MigrateAffiliationsTable() 180 Expect(err).To(HaveOccurred()) 181 Expect(err.Error()).To(Equal("failed to modify columns")) 182 }) 183 184 It("returns error if adding index fails", func() { 185 mockTx.ExecReturnsOnCall(4, nil, errors.New("failed to add index")) 186 migrator.Tx = mockTx 187 188 err := migrator.MigrateAffiliationsTable() 189 Expect(err).To(HaveOccurred()) 190 Expect(err.Error()).To(Equal("failed to add index")) 191 }) 192 193 It("returns error if updating properties to new affiliations table level fails", func() { 194 mockTx.ExecReturnsOnCall(5, nil, errors.New("failed to update properties table")) 195 migrator.Tx = mockTx 196 197 err := migrator.MigrateAffiliationsTable() 198 Expect(err).To(HaveOccurred()) 199 Expect(err.Error()).To(Equal("failed to update properties table")) 200 }) 201 202 It("migrates successfully", func() { 203 err := migrator.MigrateAffiliationsTable() 204 Expect(err).NotTo(HaveOccurred()) 205 }) 206 }) 207 208 It("migrates credentials table", func() { 209 err := migrator.MigrateCredentialsTable() 210 Expect(err).NotTo(HaveOccurred()) 211 }) 212 213 It("migrates rainfo table", func() { 214 err := migrator.MigrateRAInfoTable() 215 Expect(err).NotTo(HaveOccurred()) 216 }) 217 218 It("migrates nonces table", func() { 219 err := migrator.MigrateNoncesTable() 220 Expect(err).NotTo(HaveOccurred()) 221 }) 222 223 Context("rollback", func() { 224 It("returns an error if it fails", func() { 225 mockTx.RollbackReturns(errors.New("failed to rollback")) 226 migrator.Tx = mockTx 227 228 err := migrator.Rollback() 229 Expect(err).To(HaveOccurred()) 230 Expect(err.Error()).To(Equal("failed to rollback")) 231 }) 232 233 It("completes with no error on success", func() { 234 err := migrator.Rollback() 235 Expect(err).NotTo(HaveOccurred()) 236 }) 237 }) 238 239 Context("commit", func() { 240 It("returns an error if it fails", func() { 241 mockTx.CommitReturns(errors.New("failed to commit")) 242 migrator.Tx = mockTx 243 244 err := migrator.Commit() 245 Expect(err).To(HaveOccurred()) 246 Expect(err.Error()).To(Equal("Error encountered while committing database migration changes: failed to commit")) 247 }) 248 249 It("completes with no error on success", func() { 250 err := migrator.Commit() 251 Expect(err).NotTo(HaveOccurred()) 252 }) 253 }) 254 })