github.com/hyperledger/fabric-ca@v2.0.0-alpha.0.20201120210307-7b4f34729db1+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 modifying pem column fails", func() { 133 mockTx.ExecReturnsOnCall(2, nil, errors.New("failed to modify pem column")) 134 migrator.Tx = mockTx 135 136 err := migrator.MigrateCertificatesTable() 137 Expect(err).To(HaveOccurred()) 138 Expect(err.Error()).To(Equal("failed to modify pem column")) 139 }) 140 141 It("returns error if updating properties to new certificate table level fails", func() { 142 mockTx.ExecReturnsOnCall(3, nil, errors.New("failed to update properties table")) 143 migrator.Tx = mockTx 144 145 err := migrator.MigrateCertificatesTable() 146 Expect(err).To(HaveOccurred()) 147 Expect(err.Error()).To(Equal("failed to update properties table")) 148 }) 149 150 It("migrates successfully", func() { 151 err := migrator.MigrateCertificatesTable() 152 Expect(err).NotTo(HaveOccurred()) 153 }) 154 }) 155 156 Context("affiliations table", func() { 157 It("returns error if adding level column fails", func() { 158 mockTx.ExecReturnsOnCall(0, nil, errors.New("failed to add level columns")) 159 migrator.Tx = mockTx 160 161 err := migrator.MigrateAffiliationsTable() 162 Expect(err).To(HaveOccurred()) 163 Expect(err.Error()).To(Equal("failed to add level columns")) 164 }) 165 166 It("returns error if dropping index fails", func() { 167 mockTx.ExecReturnsOnCall(1, nil, errors.New("failed to drop index")) 168 migrator.Tx = mockTx 169 170 err := migrator.MigrateAffiliationsTable() 171 Expect(err).To(HaveOccurred()) 172 Expect(err.Error()).To(Equal("failed to drop index")) 173 }) 174 175 It("returns error if adding id column fails", func() { 176 mockTx.ExecReturnsOnCall(2, nil, errors.New("failed to add id column")) 177 migrator.Tx = mockTx 178 179 err := migrator.MigrateAffiliationsTable() 180 Expect(err).To(HaveOccurred()) 181 Expect(err.Error()).To(Equal("failed to add id column")) 182 }) 183 184 It("returns error if modifying name and prekey column fails", func() { 185 mockTx.ExecReturnsOnCall(3, nil, errors.New("failed to modify columns")) 186 migrator.Tx = mockTx 187 188 err := migrator.MigrateAffiliationsTable() 189 Expect(err).To(HaveOccurred()) 190 Expect(err.Error()).To(Equal("failed to modify columns")) 191 }) 192 193 It("returns error if adding index fails", func() { 194 mockTx.ExecReturnsOnCall(4, nil, errors.New("failed to add index")) 195 migrator.Tx = mockTx 196 197 err := migrator.MigrateAffiliationsTable() 198 Expect(err).To(HaveOccurred()) 199 Expect(err.Error()).To(Equal("failed to add index")) 200 }) 201 202 It("returns error if updating properties to new affiliations table level fails", func() { 203 mockTx.ExecReturnsOnCall(5, nil, errors.New("failed to update properties table")) 204 migrator.Tx = mockTx 205 206 err := migrator.MigrateAffiliationsTable() 207 Expect(err).To(HaveOccurred()) 208 Expect(err.Error()).To(Equal("failed to update properties table")) 209 }) 210 211 It("migrates successfully", func() { 212 err := migrator.MigrateAffiliationsTable() 213 Expect(err).NotTo(HaveOccurred()) 214 }) 215 }) 216 217 It("migrates credentials table", func() { 218 err := migrator.MigrateCredentialsTable() 219 Expect(err).NotTo(HaveOccurred()) 220 }) 221 222 It("migrates rainfo table", func() { 223 err := migrator.MigrateRAInfoTable() 224 Expect(err).NotTo(HaveOccurred()) 225 }) 226 227 It("migrates nonces table", func() { 228 err := migrator.MigrateNoncesTable() 229 Expect(err).NotTo(HaveOccurred()) 230 }) 231 232 Context("rollback", func() { 233 It("returns an error if it fails", func() { 234 mockTx.RollbackReturns(errors.New("failed to rollback")) 235 migrator.Tx = mockTx 236 237 err := migrator.Rollback() 238 Expect(err).To(HaveOccurred()) 239 Expect(err.Error()).To(Equal("failed to rollback")) 240 }) 241 242 It("completes with no error on success", func() { 243 err := migrator.Rollback() 244 Expect(err).NotTo(HaveOccurred()) 245 }) 246 }) 247 248 Context("commit", func() { 249 It("returns an error if it fails", func() { 250 mockTx.CommitReturns(errors.New("failed to commit")) 251 migrator.Tx = mockTx 252 253 err := migrator.Commit() 254 Expect(err).To(HaveOccurred()) 255 Expect(err.Error()).To(Equal("Error encountered while committing database migration changes: failed to commit")) 256 }) 257 258 It("completes with no error on success", func() { 259 err := migrator.Commit() 260 Expect(err).NotTo(HaveOccurred()) 261 }) 262 }) 263 })