From 343920cfe385a25063a17cf1a7636a4fe0bc7d54 Mon Sep 17 00:00:00 2001 From: "J.C. Jones" Date: Wed, 27 May 2015 13:36:04 -0700 Subject: [PATCH] Fix integration test while running with MySQL - Add SQL configuration options - Increase the width of the authz and pending_authz tables' challenges field - Make it configurable whether CREATE TABLE commands should run --- ca/certificate-authority-data.go | 6 ++---- ca/certificate-authority-data_test.go | 9 +++++++++ ca/certificate-authority_test.go | 6 +++++- cmd/boulder-ca/main.go | 5 +++++ cmd/boulder-sa/main.go | 7 ++++++- cmd/boulder/main.go | 10 +++++++++- cmd/shell.go | 6 +++++- core/interfaces.go | 2 +- ra/registration-authority_test.go | 6 +++++- sa/storage-authority.go | 22 ++++++++++++++-------- sa/storage-authority_test.go | 2 +- test/boulder-config.json | 8 ++++++-- test/boulder-test-config.json | 8 ++++++-- 13 files changed, 74 insertions(+), 23 deletions(-) diff --git a/ca/certificate-authority-data.go b/ca/certificate-authority-data.go index 33d0c17f9..0ab8645b7 100644 --- a/ca/certificate-authority-data.go +++ b/ca/certificate-authority-data.go @@ -39,16 +39,14 @@ func NewCertificateAuthorityDatabaseImpl(driver string, name string) (cadb core. db: db, log: logger, } - - err = createTablesIfNotExist(db) return } // createTablesIfNotExist builds the database tables and inserts the initial // state, if the tables do not already exist. It is not an error for the tables // to already exist. -func createTablesIfNotExist(db *sql.DB) (err error) { - tx, err := db.Begin() +func (cadb *CertificateAuthorityDatabaseImpl) CreateTablesIfNotExists() (err error) { + tx, err := cadb.db.Begin() if err != nil { return } diff --git a/ca/certificate-authority-data_test.go b/ca/certificate-authority-data_test.go index 39f13acc4..d88323912 100644 --- a/ca/certificate-authority-data_test.go +++ b/ca/certificate-authority-data_test.go @@ -35,6 +35,9 @@ func TestBeginCommit(t *testing.T) { cadb, err := NewCertificateAuthorityDatabaseImpl(sqliteDriver, sqliteName) test.AssertNotError(t, err, "Could not construct CA DB") + err = cadb.CreateTablesIfNotExists() + test.AssertNotError(t, err, "Could not construct tables") + err = cadb.Begin() test.AssertNotError(t, err, "Could not begin") @@ -53,6 +56,9 @@ func TestGetSetSequenceOutsideTx(t *testing.T) { cadb, err := NewCertificateAuthorityDatabaseImpl(sqliteDriver, sqliteName) test.AssertNotError(t, err, "Could not construct CA DB") + err = cadb.CreateTablesIfNotExists() + test.AssertNotError(t, err, "Could not construct tables") + _, err = cadb.IncrementAndGetSerial() test.AssertError(t, err, "Not permitted") } @@ -61,6 +67,9 @@ func TestGetSetSequenceNumber(t *testing.T) { cadb, err := NewCertificateAuthorityDatabaseImpl(sqliteDriver, sqliteName) test.AssertNotError(t, err, "Could not construct CA DB") + err = cadb.CreateTablesIfNotExists() + test.AssertNotError(t, err, "Could not construct tables") + err = cadb.Begin() test.AssertNotError(t, err, "Could not begin") diff --git a/ca/certificate-authority_test.go b/ca/certificate-authority_test.go index 0bf0ab1a9..2afeef5a0 100644 --- a/ca/certificate-authority_test.go +++ b/ca/certificate-authority_test.go @@ -322,11 +322,15 @@ func (cadb *MockCADatabase) IncrementAndGetSerial() (int, error) { return 1, nil } +func (cadb *MockCADatabase) CreateTablesIfNotExists() error { + return nil +} + func setup(t *testing.T) (cadb core.CertificateAuthorityDatabase, storageAuthority core.StorageAuthority, caConfig Config) { // Create an SA ssa, err := sa.NewSQLStorageAuthority("sqlite3", ":memory:") test.AssertNotError(t, err, "Failed to create SA") - ssa.InitTables() + ssa.CreateTablesIfNotExists() storageAuthority = ssa cadb, _ = NewMockCertificateAuthorityDatabase() diff --git a/cmd/boulder-ca/main.go b/cmd/boulder-ca/main.go index bb3ba69e3..d9d7ac1f4 100644 --- a/cmd/boulder-ca/main.go +++ b/cmd/boulder-ca/main.go @@ -36,6 +36,11 @@ func main() { cadb, err := ca.NewCertificateAuthorityDatabaseImpl(c.CA.DBDriver, c.CA.DBName) cmd.FailOnError(err, "Failed to create CA database") + if c.SQL.CreateTables { + err = cadb.CreateTablesIfNotExists() + cmd.FailOnError(err, "Failed to create CA tables") + } + cai, err := ca.NewCertificateAuthorityImpl(cadb, c.CA) cmd.FailOnError(err, "Failed to create CA impl") diff --git a/cmd/boulder-sa/main.go b/cmd/boulder-sa/main.go index aed735605..4276d55e4 100644 --- a/cmd/boulder-sa/main.go +++ b/cmd/boulder-sa/main.go @@ -35,7 +35,12 @@ func main() { sai, err := sa.NewSQLStorageAuthority(c.SA.DBDriver, c.SA.DBName) cmd.FailOnError(err, "Failed to create SA impl") - sai.SetSQLDebug(c.SA.SQLDebug) + sai.SetSQLDebug(c.SQL.SQLDebug) + + if c.SQL.CreateTables { + err = sai.CreateTablesIfNotExists() + cmd.FailOnError(err, "Failed to create tables") + } go cmd.ProfileCmd("SA", stats) diff --git a/cmd/boulder/main.go b/cmd/boulder/main.go index 3f9b0efb0..e47d8c4cf 100644 --- a/cmd/boulder/main.go +++ b/cmd/boulder/main.go @@ -77,7 +77,7 @@ func main() { wfe := wfe.NewWebFrontEndImpl() sa, err := sa.NewSQLStorageAuthority(c.SA.DBDriver, c.SA.DBName) cmd.FailOnError(err, "Unable to create SA") - sa.SetSQLDebug(c.SA.SQLDebug) + sa.SetSQLDebug(c.SQL.SQLDebug) ra := ra.NewRegistrationAuthorityImpl() va := va.NewValidationAuthorityImpl(c.CA.TestMode) @@ -88,6 +88,14 @@ func main() { ca, err := ca.NewCertificateAuthorityImpl(cadb, c.CA) cmd.FailOnError(err, "Unable to create CA") + if c.SQL.CreateTables { + err = sa.CreateTablesIfNotExists() + cmd.FailOnError(err, "Failed to create SA tables") + + err = cadb.CreateTablesIfNotExists() + cmd.FailOnError(err, "Failed to create CA tables") + } + // Wire them up wfe.RA = &ra wfe.SA = sa diff --git a/cmd/shell.go b/cmd/shell.go index 6ededbe27..bc38fa9e8 100644 --- a/cmd/shell.go +++ b/cmd/shell.go @@ -67,7 +67,11 @@ type Config struct { SA struct { DBDriver string DBName string - SQLDebug bool + } + + SQL struct { + CreateTables bool + SQLDebug bool } Statsd struct { diff --git a/core/interfaces.go b/core/interfaces.go index 548920040..106671b63 100644 --- a/core/interfaces.go +++ b/core/interfaces.go @@ -119,9 +119,9 @@ type StorageAuthority interface { // CertificateAuthorityDatabase represents an atomic sequence source type CertificateAuthorityDatabase interface { + CreateTablesIfNotExists() error Begin() error Commit() error Rollback() error - IncrementAndGetSerial() (int, error) } diff --git a/ra/registration-authority_test.go b/ra/registration-authority_test.go index 4a635f9fe..c4d976bc3 100644 --- a/ra/registration-authority_test.go +++ b/ra/registration-authority_test.go @@ -62,6 +62,10 @@ func (cadb *MockCADatabase) IncrementAndGetSerial() (int, error) { return 1, nil } +func (cadb *MockCADatabase) CreateTablesIfNotExists() error { + return nil +} + var ( // These values we simulate from the client AccountKeyJSON = []byte(`{ @@ -116,7 +120,7 @@ func initAuthorities(t *testing.T) (core.CertificateAuthority, *DummyValidationA sa, err := sa.NewSQLStorageAuthority("sqlite3", ":memory:") test.AssertNotError(t, err, "Failed to create SA") - sa.InitTables() + sa.CreateTablesIfNotExists() va := &DummyValidationAuthority{} diff --git a/sa/storage-authority.go b/sa/storage-authority.go index a70e3ba4f..a0bd5e0bb 100644 --- a/sa/storage-authority.go +++ b/sa/storage-authority.go @@ -185,11 +185,7 @@ func NewSQLStorageAuthority(driver string, name string) (ssa *SQLStorageAuthorit bucket: make(map[string]interface{}), } - err = ssa.InitTables() - if err != nil { - return - } - + ssa.initTables() return } @@ -203,19 +199,29 @@ func (ssa *SQLStorageAuthority) SetSQLDebug(state bool) { } } -func (ssa *SQLStorageAuthority) InitTables() (err error) { +// initTables constructs the table map for the ORM. If you want to also create +// the tables, call CreateTablesIfNotExists. +func (ssa *SQLStorageAuthority) initTables() { regTable := ssa.dbMap.AddTableWithName(core.Registration{}, "registrations").SetKeys(true, "ID") regTable.SetVersionCol("LockCol") regTable.ColMap("Key").SetMaxSize(512).SetNotNull(true) - ssa.dbMap.AddTableWithName(pendingauthzModel{}, "pending_authz").SetKeys(false, "ID").SetVersionCol("LockCol") - ssa.dbMap.AddTableWithName(authzModel{}, "authz").SetKeys(false, "ID") + pendingAuthzTable := ssa.dbMap.AddTableWithName(pendingauthzModel{}, "pending_authz").SetKeys(false, "ID") + pendingAuthzTable.SetVersionCol("LockCol") + pendingAuthzTable.ColMap("Challenges").SetMaxSize(1536) + + authzTable := ssa.dbMap.AddTableWithName(authzModel{}, "authz").SetKeys(false, "ID") + authzTable.ColMap("Challenges").SetMaxSize(1536) + ssa.dbMap.AddTableWithName(core.Certificate{}, "certificates").SetKeys(false, "Serial") ssa.dbMap.AddTableWithName(core.CertificateStatus{}, "certificateStatus").SetKeys(false, "Serial").SetVersionCol("LockCol") ssa.dbMap.AddTableWithName(core.OcspResponse{}, "ocspResponses").SetKeys(true, "ID") ssa.dbMap.AddTableWithName(core.Crl{}, "crls").SetKeys(false, "Serial") ssa.dbMap.AddTableWithName(core.DeniedCsr{}, "deniedCsrs").SetKeys(true, "ID") +} +// CreateTablesIfNotExists instructs the ORM to create any missing tables. +func (ssa *SQLStorageAuthority) CreateTablesIfNotExists() (err error) { err = ssa.dbMap.CreateTablesIfNotExists() return } diff --git a/sa/storage-authority_test.go b/sa/storage-authority_test.go index 35a820b95..1f31e80bf 100644 --- a/sa/storage-authority_test.go +++ b/sa/storage-authority_test.go @@ -28,7 +28,7 @@ func initSA(t *testing.T) *SQLStorageAuthority { if err != nil { t.Fatalf("Failed to create SA") } - if err = sa.InitTables(); err != nil { + if err = sa.CreateTablesIfNotExists(); err != nil { t.Fatalf("Failed to create SA") } return sa diff --git a/test/boulder-config.json b/test/boulder-config.json index d659fb525..6b6a63197 100644 --- a/test/boulder-config.json +++ b/test/boulder-config.json @@ -51,8 +51,12 @@ "sa": { "dbDriver": "sqlite3", - "dbName": ":memory:", - "SQLDebug": false + "dbName": ":memory:" + }, + + "sql": { + "SQLDebug": true, + "CreateTables": false }, "revoker": { diff --git a/test/boulder-test-config.json b/test/boulder-test-config.json index 5e52230ae..188ce031f 100644 --- a/test/boulder-test-config.json +++ b/test/boulder-test-config.json @@ -51,8 +51,12 @@ "sa": { "dbDriver": "sqlite3", - "dbName": ":memory:", - "SQLDebug": false + "dbName": ":memory:" + }, + + "sql": { + "SQLDebug": false, + "CreateTables": true }, "mail": {