github.com/adecaro/fabric-ca@v2.0.0-alpha+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/hyperledger/fabric/common/metrics/disabled"
    18  	. "github.com/onsi/ginkgo"
    19  	. "github.com/onsi/gomega"
    20  )
    21  
    22  const (
    23  	testdataDir = "../../../../../testdata"
    24  )
    25  
    26  var _ = Describe("Mysql", func() {
    27  	var (
    28  		db     *mysql.Mysql
    29  		mockDB *mocks.FabricCADB
    30  	)
    31  
    32  	BeforeEach(func() {
    33  		tls := &tls.ClientTLSConfig{
    34  			Enabled:   true,
    35  			CertFiles: []string{filepath.Join(testdataDir, "root.pem")},
    36  		}
    37  		db = mysql.NewDB("root:rootpw@tcp(localhost:3306)/fabric_ca_db", "", tls, nil, &disabled.Provider{})
    38  		mockDB = &mocks.FabricCADB{}
    39  	})
    40  
    41  	Context("open connection to database", func() {
    42  		It("fails to open database connection of root cert files missing from tls config", func() {
    43  			db.TLS.CertFiles = nil
    44  			err := db.Connect()
    45  			Expect(err).To(HaveOccurred())
    46  			Expect(err.Error()).Should(ContainSubstring("Failed to get client TLS for MySQL: No trusted root certificates for TLS were provided"))
    47  			Expect(db.SqlxDB).To(BeNil())
    48  		})
    49  
    50  		It("fails to open database connection if unable to ping database", func() {
    51  			err := db.Connect()
    52  			Expect(err).To(HaveOccurred())
    53  			Expect(db.SqlxDB).To(BeNil())
    54  		})
    55  	})
    56  
    57  	Context("pinging database", func() {
    58  		It("returns an error if unable to ping database", func() {
    59  			mockDB.PingContextReturns(errors.New("ping error"))
    60  			db.SqlxDB = mockDB
    61  
    62  			err := db.PingContext(context.Background())
    63  			Expect(err).To(HaveOccurred())
    64  			Expect(err.Error()).To(Equal("Failed to ping to MySQL database: ping error"))
    65  		})
    66  
    67  		It("returns no error if able to ping database", func() {
    68  			db.SqlxDB = mockDB
    69  
    70  			err := db.PingContext(context.Background())
    71  			Expect(err).NotTo(HaveOccurred())
    72  		})
    73  	})
    74  
    75  	Context("creating fabric ca database", func() {
    76  		It("returns an error if unable execute create fabric ca database sql", func() {
    77  			mockDB.ExecReturns(nil, errors.New("error creating database"))
    78  			db.SqlxDB = mockDB
    79  			_, err := db.CreateDatabase()
    80  			Expect(err).To(HaveOccurred())
    81  			Expect(err.Error()).Should(ContainSubstring("Failed to create MySQL database: Failed to execute create database query: error creating database"))
    82  		})
    83  
    84  		It("creates the fabric ca database", func() {
    85  			db.SqlxDB = mockDB
    86  
    87  			_, err := db.CreateDatabase()
    88  			Expect(err).NotTo(HaveOccurred())
    89  		})
    90  	})
    91  
    92  	Context("creating tables", func() {
    93  		It("returns an error if unable to create users table", func() {
    94  			mockDB.ExecReturnsOnCall(0, nil, errors.New("unable to create table"))
    95  
    96  			db.SqlxDB = mockDB
    97  			err := db.CreateTables()
    98  			Expect(err).To(HaveOccurred())
    99  			Expect(err.Error()).Should(ContainSubstring("Failed to create MySQL tables: Error creating users table: unable to create table"))
   100  		})
   101  
   102  		It("returns an error if unable to create affiliations table", func() {
   103  			mockDB.ExecReturnsOnCall(1, 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 affiliations table: unable to create table"))
   109  		})
   110  
   111  		It("returns an error if unable to create index on affiliations table", func() {
   112  			mockDB.ExecReturnsOnCall(2, 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 index on affiliations table: unable to create table"))
   118  		})
   119  
   120  		It("returns an error if unable to create certificates table", func() {
   121  			mockDB.ExecReturnsOnCall(3, 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 certificates table: unable to create table"))
   127  		})
   128  
   129  		It("returns an error if unable to create credentials table", func() {
   130  			mockDB.ExecReturnsOnCall(4, 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 credentials table: unable to create table"))
   136  		})
   137  
   138  		It("returns an error if unable to create revocation_authority_info table", func() {
   139  			mockDB.ExecReturnsOnCall(5, 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 revocation_authority_info table: unable to create table"))
   145  		})
   146  
   147  		It("returns an error if unable to create nonces table", func() {
   148  			mockDB.ExecReturnsOnCall(6, 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 nonces table: unable to create table"))
   154  		})
   155  
   156  		It("returns an error if unable to create properties table", func() {
   157  			mockDB.ExecReturnsOnCall(7, 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 properties table: unable to create table"))
   163  		})
   164  
   165  		It("returns an error if unable to insert default value in properties table", func() {
   166  			mockDB.ExecReturnsOnCall(8, nil, errors.New("unable to insert default values"))
   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: unable to insert default values"))
   172  		})
   173  
   174  		It("creates the fabric ca tables", func() {
   175  			db.SqlxDB = mockDB
   176  
   177  			err := db.CreateTables()
   178  			Expect(err).NotTo(HaveOccurred())
   179  		})
   180  
   181  	})
   182  })