X Tutup
Skip to content

Commit 5e97aa2

Browse files
committed
use MariaDB in the unit tests
And delete the uses of sqlite3
1 parent 3363646 commit 5e97aa2

File tree

22 files changed

+290
-242
lines changed

22 files changed

+290
-242
lines changed

Makefile

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@ build: $(OBJECTS)
3939

4040
pre:
4141
@mkdir -p $(OBJDIR)
42-
@echo [go] lib/github.com/mattn/go-sqlite3
43-
@go install ./Godeps/_workspace/src/github.com/mattn/go-sqlite3
4442

4543
# Compile each of the binaries
4644
$(OBJECTS): pre

ca/certificate-authority-data.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111

1212
"github.com/letsencrypt/boulder/core"
1313
blog "github.com/letsencrypt/boulder/log"
14-
"github.com/letsencrypt/boulder/sa"
1514

1615
gorp "github.com/letsencrypt/boulder/Godeps/_workspace/src/gopkg.in/gorp.v1"
1716
)
@@ -32,14 +31,9 @@ type SerialNumber struct {
3231

3332
// NewCertificateAuthorityDatabaseImpl constructs a Database for the
3433
// Certificate Authority.
35-
func NewCertificateAuthorityDatabaseImpl(driver string, name string) (cadb core.CertificateAuthorityDatabase, err error) {
34+
func NewCertificateAuthorityDatabaseImpl(dbMap *gorp.DbMap) (cadb core.CertificateAuthorityDatabase, err error) {
3635
logger := blog.GetAuditLogger()
3736

38-
dbMap, err := sa.NewDbMap(driver, name)
39-
if err != nil {
40-
return nil, err
41-
}
42-
4337
dbMap.AddTableWithName(SerialNumber{}, "serialNumber").SetKeys(true, "ID")
4438

4539
cadb = &CertificateAuthorityDatabaseImpl{

ca/certificate-authority-data_test.go

Lines changed: 42 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -8,39 +8,14 @@ package ca
88
import (
99
"testing"
1010

11-
_ "github.com/letsencrypt/boulder/Godeps/_workspace/src/github.com/mattn/go-sqlite3"
11+
"github.com/letsencrypt/boulder/core"
12+
"github.com/letsencrypt/boulder/sa"
1213
"github.com/letsencrypt/boulder/test"
1314
)
1415

15-
const badDriver = "nothing"
16-
const badFilename = "/doesnotexist/nofile"
17-
const sqliteDriver = "sqlite3"
18-
const sqliteName = ":memory:"
19-
20-
func TestConstruction(t *testing.T) {
21-
// Successful case
22-
_, err := NewCertificateAuthorityDatabaseImpl(sqliteDriver, sqliteName)
23-
test.AssertNotError(t, err, "Could not construct CA DB")
24-
25-
// Covers "sql.Open" error
26-
_, err = NewCertificateAuthorityDatabaseImpl(badDriver, sqliteName)
27-
test.AssertError(t, err, "Should have failed construction")
28-
29-
// Covers "db.Ping" error
30-
_, err = NewCertificateAuthorityDatabaseImpl(sqliteDriver, badFilename)
31-
test.AssertError(t, err, "Should have failed construction")
32-
}
33-
3416
func TestGetSetSequenceOutsideTx(t *testing.T) {
35-
cadb, err := NewCertificateAuthorityDatabaseImpl(sqliteDriver, sqliteName)
36-
test.AssertNotError(t, err, "Could not construct CA DB")
37-
38-
err = cadb.CreateTablesIfNotExists()
39-
test.AssertNotError(t, err, "Could not construct tables")
40-
41-
_, err = cadb.IncrementAndGetSerial(nil)
42-
test.AssertError(t, err, "Not permitted")
43-
17+
cadb, cleanUp := caDBImpl(t)
18+
defer cleanUp()
4419
tx, err := cadb.Begin()
4520
test.AssertNotError(t, err, "Could not begin")
4621
tx.Commit()
@@ -55,12 +30,8 @@ func TestGetSetSequenceOutsideTx(t *testing.T) {
5530
}
5631

5732
func TestGetSetSequenceNumber(t *testing.T) {
58-
cadb, err := NewCertificateAuthorityDatabaseImpl(sqliteDriver, sqliteName)
59-
test.AssertNotError(t, err, "Could not construct CA DB")
60-
61-
err = cadb.CreateTablesIfNotExists()
62-
test.AssertNotError(t, err, "Could not construct tables")
63-
33+
cadb, cleanUp := caDBImpl(t)
34+
defer cleanUp()
6435
tx, err := cadb.Begin()
6536
test.AssertNotError(t, err, "Could not begin")
6637

@@ -74,3 +45,39 @@ func TestGetSetSequenceNumber(t *testing.T) {
7445
err = tx.Commit()
7546
test.AssertNotError(t, err, "Could not commit")
7647
}
48+
49+
func caDBImpl(t *testing.T) (core.CertificateAuthorityDatabase, func()) {
50+
dbMap, err := sa.NewDbMap(dbConnStr)
51+
if err != nil {
52+
t.Fatalf("Could not construct dbMap: %s", err)
53+
}
54+
55+
cadb, err := NewCertificateAuthorityDatabaseImpl(dbMap)
56+
if err != nil {
57+
t.Fatalf("Could not construct CA DB: %s", err)
58+
}
59+
60+
// We intentionally call CreateTablesIfNotExists twice before returning
61+
// because of the weird insert inside it.
62+
63+
err = cadb.CreateTablesIfNotExists()
64+
if err != nil {
65+
t.Fatalf("Could not construct tables: %s", err)
66+
}
67+
err = dbMap.TruncateTables()
68+
if err != nil {
69+
t.Fatalf("Could not truncate tables: %s", err)
70+
}
71+
err = cadb.CreateTablesIfNotExists()
72+
if err != nil {
73+
t.Fatalf("Could not construct tables: %s", err)
74+
}
75+
cleanUp := func() {
76+
if err := dbMap.TruncateTables(); err != nil {
77+
t.Fatalf("Could not truncate tables after the test: %s", err)
78+
}
79+
dbMap.Db.Close()
80+
}
81+
82+
return cadb, cleanUp
83+
}

ca/certificate-authority_test.go

Lines changed: 46 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,11 @@ import (
1111
"encoding/asn1"
1212
"encoding/hex"
1313
"fmt"
14-
"os"
1514
"testing"
1615
"time"
1716

1817
cfsslConfig "github.com/letsencrypt/boulder/Godeps/_workspace/src/github.com/cloudflare/cfssl/config"
1918
ocspConfig "github.com/letsencrypt/boulder/Godeps/_workspace/src/github.com/cloudflare/cfssl/ocsp/config"
20-
_ "github.com/letsencrypt/boulder/Godeps/_workspace/src/github.com/mattn/go-sqlite3"
2119
"github.com/letsencrypt/boulder/mocks"
2220

2321
"github.com/letsencrypt/boulder/core"
@@ -339,22 +337,37 @@ const profileName = "ee"
339337
const caKeyFile = "../test/test-ca.key"
340338
const caCertFile = "../test/test-ca.pem"
341339

342-
func TestMain(m *testing.M) {
340+
// TODO(jmhodges): change this to boulder_ca_test database
341+
var dbConnStr = "mysql+tcp://boulder@localhost:3306/boulder_test"
343342

344-
os.Exit(m.Run())
345-
}
346-
347-
func setup(t *testing.T) (cadb core.CertificateAuthorityDatabase, storageAuthority core.StorageAuthority, caConfig Config) {
343+
func setup(t *testing.T) (core.CertificateAuthorityDatabase, core.StorageAuthority, Config, func()) {
348344
// Create an SA
349-
ssa, err := sa.NewSQLStorageAuthority("sqlite3", ":memory:")
350-
test.AssertNotError(t, err, "Failed to create SA")
351-
ssa.CreateTablesIfNotExists()
352-
storageAuthority = ssa
345+
dbMap, err := sa.NewDbMap(dbConnStr)
346+
if err != nil {
347+
t.Fatalf("Failed to create dbMap: %s", err)
348+
}
349+
ssa, err := sa.NewSQLStorageAuthority(dbMap)
350+
if err != nil {
351+
t.Fatalf("Failed to create SA: %s", err)
352+
}
353+
if err = ssa.CreateTablesIfNotExists(); err != nil {
354+
t.Fatalf("Failed to create tables: %s", err)
355+
}
356+
if err = dbMap.TruncateTables(); err != nil {
357+
t.Fatalf("Failed to truncate tables: %s", err)
358+
}
353359

354-
cadb, _ = mocks.NewMockCertificateAuthorityDatabase()
360+
cadb, caDBCleanUp := caDBImpl(t)
361+
cleanUp := func() {
362+
if err = dbMap.TruncateTables(); err != nil {
363+
t.Fatalf("Failed to truncate tables after the test: %s", err)
364+
}
365+
dbMap.Db.Close()
366+
caDBCleanUp()
367+
}
355368

356369
// Create a CA
357-
caConfig = Config{
370+
caConfig := Config{
358371
Profile: profileName,
359372
SerialPrefix: 17,
360373
Key: KeyConfig{
@@ -399,18 +412,21 @@ func setup(t *testing.T) (cadb core.CertificateAuthorityDatabase, storageAuthori
399412
},
400413
},
401414
}
402-
return cadb, storageAuthority, caConfig
415+
return cadb, ssa, caConfig, cleanUp
403416
}
404417

405418
func TestFailNoSerial(t *testing.T) {
406-
cadb, _, caConfig := setup(t)
419+
cadb, _, caConfig, cleanUp := setup(t)
420+
defer cleanUp()
421+
407422
caConfig.SerialPrefix = 0
408423
_, err := NewCertificateAuthorityImpl(cadb, caConfig, caCertFile)
409424
test.AssertError(t, err, "CA should have failed with no SerialPrefix")
410425
}
411426

412427
func TestRevoke(t *testing.T) {
413-
cadb, storageAuthority, caConfig := setup(t)
428+
cadb, storageAuthority, caConfig, cleanUp := setup(t)
429+
defer cleanUp()
414430
ca, err := NewCertificateAuthorityImpl(cadb, caConfig, caCertFile)
415431
test.AssertNotError(t, err, "Failed to create CA")
416432
if err != nil {
@@ -442,7 +458,8 @@ func TestRevoke(t *testing.T) {
442458
}
443459

444460
func TestIssueCertificate(t *testing.T) {
445-
cadb, storageAuthority, caConfig := setup(t)
461+
cadb, storageAuthority, caConfig, cleanUp := setup(t)
462+
defer cleanUp()
446463
ca, err := NewCertificateAuthorityImpl(cadb, caConfig, caCertFile)
447464
test.AssertNotError(t, err, "Failed to create CA")
448465
ca.SA = storageAuthority
@@ -518,7 +535,8 @@ func TestIssueCertificate(t *testing.T) {
518535
}
519536

520537
func TestRejectNoName(t *testing.T) {
521-
cadb, storageAuthority, caConfig := setup(t)
538+
cadb, storageAuthority, caConfig, cleanUp := setup(t)
539+
defer cleanUp()
522540
ca, err := NewCertificateAuthorityImpl(cadb, caConfig, caCertFile)
523541
test.AssertNotError(t, err, "Failed to create CA")
524542
ca.SA = storageAuthority
@@ -534,7 +552,8 @@ func TestRejectNoName(t *testing.T) {
534552
}
535553

536554
func TestRejectTooManyNames(t *testing.T) {
537-
cadb, storageAuthority, caConfig := setup(t)
555+
cadb, storageAuthority, caConfig, cleanUp := setup(t)
556+
defer cleanUp()
538557
ca, err := NewCertificateAuthorityImpl(cadb, caConfig, caCertFile)
539558
test.AssertNotError(t, err, "Failed to create CA")
540559
ca.SA = storageAuthority
@@ -547,7 +566,8 @@ func TestRejectTooManyNames(t *testing.T) {
547566
}
548567

549568
func TestDeduplication(t *testing.T) {
550-
cadb, storageAuthority, caConfig := setup(t)
569+
cadb, storageAuthority, caConfig, cleanUp := setup(t)
570+
defer cleanUp()
551571
ca, err := NewCertificateAuthorityImpl(cadb, caConfig, caCertFile)
552572
test.AssertNotError(t, err, "Failed to create CA")
553573
ca.SA = storageAuthority
@@ -576,7 +596,8 @@ func TestDeduplication(t *testing.T) {
576596
}
577597

578598
func TestRejectValidityTooLong(t *testing.T) {
579-
cadb, storageAuthority, caConfig := setup(t)
599+
cadb, storageAuthority, caConfig, cleanUp := setup(t)
600+
defer cleanUp()
580601
ca, err := NewCertificateAuthorityImpl(cadb, caConfig, caCertFile)
581602
test.AssertNotError(t, err, "Failed to create CA")
582603
ca.SA = storageAuthority
@@ -597,7 +618,8 @@ func TestRejectValidityTooLong(t *testing.T) {
597618
}
598619

599620
func TestShortKey(t *testing.T) {
600-
cadb, storageAuthority, caConfig := setup(t)
621+
cadb, storageAuthority, caConfig, cleanUp := setup(t)
622+
defer cleanUp()
601623
ca, err := NewCertificateAuthorityImpl(cadb, caConfig, caCertFile)
602624
ca.SA = storageAuthority
603625
ca.MaxKeySize = 4096
@@ -610,7 +632,8 @@ func TestShortKey(t *testing.T) {
610632
}
611633

612634
func TestRejectBadAlgorithm(t *testing.T) {
613-
cadb, storageAuthority, caConfig := setup(t)
635+
cadb, storageAuthority, caConfig, cleanUp := setup(t)
636+
defer cleanUp()
614637
ca, err := NewCertificateAuthorityImpl(cadb, caConfig, caCertFile)
615638
ca.SA = storageAuthority
616639
ca.MaxKeySize = 4096

cmd/admin-revoker/main.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,12 @@ import (
1717

1818
"github.com/letsencrypt/boulder/Godeps/_workspace/src/github.com/cactus/go-statsd-client/statsd"
1919
"github.com/letsencrypt/boulder/Godeps/_workspace/src/github.com/codegangsta/cli"
20-
21-
// Load both drivers to allow configuring either
22-
_ "github.com/letsencrypt/boulder/Godeps/_workspace/src/github.com/go-sql-driver/mysql"
23-
_ "github.com/letsencrypt/boulder/Godeps/_workspace/src/github.com/mattn/go-sqlite3"
24-
20+
gorp "github.com/letsencrypt/boulder/Godeps/_workspace/src/gopkg.in/gorp.v1"
2521
"github.com/letsencrypt/boulder/cmd"
2622
"github.com/letsencrypt/boulder/core"
2723
blog "github.com/letsencrypt/boulder/log"
2824
"github.com/letsencrypt/boulder/rpc"
2925
"github.com/letsencrypt/boulder/sa"
30-
31-
gorp "github.com/letsencrypt/boulder/Godeps/_workspace/src/gopkg.in/gorp.v1"
3226
)
3327

3428
var reasons = map[int]string{
@@ -76,7 +70,7 @@ func setupContext(context *cli.Context) (rpc.CertificateAuthorityClient, *blog.A
7670
cac, err := rpc.NewCertificateAuthorityClient(caRPC)
7771
cmd.FailOnError(err, "Unable to create CA client")
7872

79-
dbMap, err := sa.NewDbMap(c.Revoker.DBDriver, c.Revoker.DBConnect)
73+
dbMap, err := sa.NewDbMap(c.Revoker.DBConnect)
8074
cmd.FailOnError(err, "Couldn't setup database connection")
8175

8276
saRPC, err := rpc.NewAmqpRPCClient("AdminRevoker->SA", c.AMQP.SA.Server, ch)

cmd/boulder-ca/main.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ package main
77

88
import (
99
"github.com/letsencrypt/boulder/Godeps/_workspace/src/github.com/cactus/go-statsd-client/statsd"
10-
1110
"github.com/letsencrypt/boulder/ca"
1211
"github.com/letsencrypt/boulder/cmd"
1312
blog "github.com/letsencrypt/boulder/log"
1413
"github.com/letsencrypt/boulder/rpc"
14+
"github.com/letsencrypt/boulder/sa"
1515
)
1616

1717
func main() {
@@ -31,8 +31,10 @@ func main() {
3131

3232
go cmd.DebugServer(c.CA.DebugAddr)
3333

34-
cadb, err := ca.NewCertificateAuthorityDatabaseImpl(c.CA.DBDriver, c.CA.DBConnect)
34+
dbMap, err := sa.NewDbMap(c.CA.DBConnect)
35+
cmd.FailOnError(err, "Couldn't connect to CA database")
3536

37+
cadb, err := ca.NewCertificateAuthorityDatabaseImpl(dbMap)
3638
cmd.FailOnError(err, "Failed to create CA database")
3739

3840
if c.SQL.CreateTables {

cmd/boulder-sa/main.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,10 @@ func main() {
3131

3232
go cmd.DebugServer(c.SA.DebugAddr)
3333

34-
sai, err := sa.NewSQLStorageAuthority(c.SA.DBDriver, c.SA.DBConnect)
34+
dbMap, err := sa.NewDbMap(c.SA.DBConnect)
35+
cmd.FailOnError(err, "Couldn't connect to SA database")
3536

37+
sai, err := sa.NewSQLStorageAuthority(dbMap)
3638
cmd.FailOnError(err, "Failed to create SA impl")
3739
sai.SetSQLDebug(c.SQL.SQLDebug)
3840

cmd/expiration-mailer/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ func main() {
240240
go cmd.DebugServer(c.Mailer.DebugAddr)
241241

242242
// Configure DB
243-
dbMap, err := sa.NewDbMap(c.Mailer.DBDriver, c.Mailer.DBConnect)
243+
dbMap, err := sa.NewDbMap(c.Mailer.DBConnect)
244244
cmd.FailOnError(err, "Could not connect to database")
245245

246246
ch, err := rpc.AmqpChannel(c)

cmd/expiration-mailer/main_test.go

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,30 @@ var testKey = rsa.PrivateKey{
140140
Primes: []*big.Int{p, q},
141141
}
142142

143+
// TODO(jmhodges): Turn this into boulder_sa_test
144+
var dbConnStr = "mysql+tcp://boulder@localhost:3306/boulder_test"
145+
143146
func TestFindExpiringCertificates(t *testing.T) {
144-
dbMap, err := sa.NewDbMap("sqlite3", ":memory:")
145-
test.AssertNotError(t, err, "Couldn't connect to SQLite")
147+
dbMap, err := sa.NewDbMap(dbConnStr)
148+
if err != nil {
149+
t.Fatalf("Couldn't connect the database: %s", err)
150+
}
146151
err = dbMap.CreateTablesIfNotExists()
147-
test.AssertNotError(t, err, "Couldn't create tables")
152+
if err != nil {
153+
t.Fatalf("Couldn't create tables: %s", err)
154+
}
155+
err = dbMap.TruncateTables()
156+
if err != nil {
157+
t.Fatalf("Couldn't truncate tables: %s", err)
158+
}
159+
defer func() {
160+
err = dbMap.TruncateTables()
161+
if err != nil {
162+
t.Fatalf("Couldn't truncate tables after the test: %s", err)
163+
}
164+
dbMap.Db.Close()
165+
}()
166+
148167
tmpl, err := template.New("expiry-email").Parse(testTmpl)
149168
test.AssertNotError(t, err, "Couldn't parse test email template")
150169
stats, _ := statsd.NewNoopClient(nil)

0 commit comments

Comments
 (0)
X Tutup