github.com/extrame/fabric-ca@v2.0.0-alpha+incompatible/lib/server/db/sqlite/migrator_test.go (about)

     1  /*
     2  Copyright IBM Corp. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package sqlite_test
     8  
     9  import (
    10  	"errors"
    11  
    12  	"github.com/hyperledger/fabric-ca/lib/server/db/sqlite"
    13  	"github.com/hyperledger/fabric-ca/lib/server/db/sqlite/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 *sqlite.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 = sqlite.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("Error creating users table: 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("Error creating certificates table: 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 modifying name and prekey column fails", func() {
   158  			mockTx.ExecReturnsOnCall(1, nil, errors.New("failed to modify columns"))
   159  			migrator.Tx = mockTx
   160  
   161  			err := migrator.MigrateAffiliationsTable()
   162  			Expect(err).To(HaveOccurred())
   163  			Expect(err.Error()).To(Equal("Error creating affiliations table: failed to modify columns"))
   164  		})
   165  
   166  		It("returns error if updating properties to new affiliations table level fails", func() {
   167  			mockTx.ExecReturnsOnCall(2, nil, errors.New("failed to update properties table"))
   168  			migrator.Tx = mockTx
   169  
   170  			err := migrator.MigrateAffiliationsTable()
   171  			Expect(err).To(HaveOccurred())
   172  			Expect(err.Error()).To(Equal("failed to update properties table"))
   173  		})
   174  
   175  		It("migrates successfully", func() {
   176  			err := migrator.MigrateAffiliationsTable()
   177  			Expect(err).NotTo(HaveOccurred())
   178  		})
   179  	})
   180  
   181  	It("migrates credentials table", func() {
   182  		err := migrator.MigrateCredentialsTable()
   183  		Expect(err).NotTo(HaveOccurred())
   184  	})
   185  
   186  	It("migrates rainfo table", func() {
   187  		err := migrator.MigrateRAInfoTable()
   188  		Expect(err).NotTo(HaveOccurred())
   189  	})
   190  
   191  	It("migrates nonces table", func() {
   192  		err := migrator.MigrateNoncesTable()
   193  		Expect(err).NotTo(HaveOccurred())
   194  	})
   195  
   196  	Context("rollback", func() {
   197  		It("returns an error if it fails", func() {
   198  			mockTx.RollbackReturns(errors.New("failed to rollback"))
   199  			migrator.Tx = mockTx
   200  
   201  			err := migrator.Rollback()
   202  			Expect(err).To(HaveOccurred())
   203  			Expect(err.Error()).To(Equal("failed to rollback"))
   204  		})
   205  
   206  		It("completes with no error on success", func() {
   207  			err := migrator.Rollback()
   208  			Expect(err).NotTo(HaveOccurred())
   209  		})
   210  	})
   211  
   212  	Context("commit", func() {
   213  		It("returns an error if it fails", func() {
   214  			mockTx.CommitReturns(errors.New("failed to commit"))
   215  			migrator.Tx = mockTx
   216  
   217  			err := migrator.Commit()
   218  			Expect(err).To(HaveOccurred())
   219  			Expect(err.Error()).To(Equal("Error encountered while committing database migration changes: failed to commit"))
   220  		})
   221  
   222  		It("completes with no error on success", func() {
   223  			err := migrator.Commit()
   224  			Expect(err).NotTo(HaveOccurred())
   225  		})
   226  	})
   227  })