Skip to content

Commit

Permalink
[FAB-5426] Fix unit-tests on vagrant/windows
Browse files Browse the repository at this point in the history
This change adds several error checks on removal of temporary files
it copies files instead of creating symlinks that don't work on
Windows, it uses files in local volumes when chmod is involved
and adds stopping of the server and/or closing of the DB to
avoid leaving the database dangling in the ether.

Patch-set #2: minor simplification
Patch-set #3: limits changes to test files leaving to FAB-6085 the
changes related to the lib code.

Before the change:

fabric-ca/lib $ go test
--- FAIL: TestCAInit (0.09s)
	ca_test.go:200: ====== orgwd /opt/gopath/src/github.com/hyperl
edger/fabric-ca/lib
	ca_test.go:211: confDir: TestCAInit514958420
	ca_test.go:216: Working dir /opt/gopath/src/github.com/hyperle
dger/fabric-ca/testdata/TestCAInit514958420
	ca_test.go:236: ca.init error: Failed to get BCCSP with opts:
Could not initialize BCCSP PKCS11 [Failed initializing configuration [
Hash Family not supported []]]
	ca_test.go:253: changed to  /opt/gopath/src/github.com/hyperle
dger/fabric-ca/testdata/TestCaInit209323427
	ca_test.go:272: symlink error:  link ../ec256-1-key.pem ca-key
.pem: operation not permitted
	ca_test.go:259: Removed all: /opt/gopath/src/github.com/hyperl
edger/fabric-ca/testdata/TestCaInit209323427
	ca_test.go:222: Removed all: /opt/gopath/src/github.com/hyperl
edger/fabric-ca/testdata/TestCAInit514958420
--- FAIL: TestSRVMultiCAConfigs (0.95s)
	server_test.go:785: TestMultiCA...
	server_test.go:791: Server configuration: &{Port:7075 Address:
...
	server_test.go:810: Starting 3 CAs with a duplicated CN name:
Please modify CSR in /opt/gopath/src/github.com/hyperledger/fabric-ca/
testdata/ca/rootca/ca3/ca-cert.pem and try adding CA again: Both issue
r and subject distinguished name are already in use
	server_test.go:817: Create err: <nil>
	server_test.go:822: Create err: <nil>
	server_test.go:828: srv.Start ERROR Validation of certificate
and key failed: Invalid certificate in file '/opt/gopath/src/github.co
m/hyperledger/fabric-ca/testdata/ca/rootca/ca1/ca-cert.pem': Failed to
 PEM decode certificate
	server_test.go:842: srv.Start ERROR Validation of certificate a
nd key failed: Invalid certificate in file '/opt/gopath/src/github.com/
hyperledger/fabric-ca/testdata/ca/rootca/ca1/ca-cert.pem': Failed to PE
M decode certificate
	server_test.go:2236: symlink expired cert to ../testdata/expire
dcert.pem failed:  link ../testdata/expiredcert.pem ../testdata/ca/root
ca/ca1/ca-cert.pem: file exists
	server_test.go:2240: symlink key to ../testdata/tls_client-key.
pem failed:  link ../testdata/tls_client-key.pem ../testdata/ca/rootca/
ca1/ca-key.pem: file exists
	server_test.go:2244: srvStart ERROR Validation of certificate a
nd key failed: Invalid certificate in file '/opt/gopath/src/github.com/
hyperledger/fabric-ca/testdata/ca/rootca/ca1/ca-cert.pem': Failed to PE
M decode certificate
	server_test.go:2236: symlink expired cert to ../testdata/noKeyU
sage.cert.pem failed:  link ../testdata/noKeyUsage.cert.pem ../testdata
/ca/rootca/ca1/ca-cert.pem: file exists
	server_test.go:2240: symlink key to ../testdata/noKeyUsage.key.
pem failed:  link ../testdata/noKeyUsage.key.pem ../testdata/ca/rootca/
ca1/ca-key.pem: file exists
	server_test.go:2244: srvStart ERROR Validation of certificate a
nd key failed: Invalid certificate in file '/opt/gopath/src/github.com/
hyperledger/fabric-ca/testdata/ca/rootca/ca1/ca-cert.pem': Failed to PE
M decode certificate
	server_test.go:2236: symlink expired cert to ../testdata/caFals
e.cert.pem failed:  link ../testdata/caFalse.cert.pem ../testdata/ca/ro
otca/ca1/ca-cert.pem: file exists
	server_test.go:2240: symlink key to ../testdata/caFalse.key.pem
 failed:  link ../testdata/caFalse.key.pem ../testdata/ca/rootca/ca1/ca
-key.pem: file exists
	server_test.go:2244: srvStart ERROR Validation of certificate a
nd key failed: Invalid certificate in file '/opt/gopath/src/github.com/
hyperledger/fabric-ca/testdata/ca/rootca/ca1/ca-cert.pem': Failed to PE
M decode certificate
	server_test.go:2236: symlink expired cert to ../testdata/dsaCa-
cert.pem failed:  link ../testdata/dsaCa-cert.pem ../testdata/ca/rootca
/ca1/ca-cert.pem: file exists
	server_test.go:2240: symlink key to ../testdata/dsaCa-key.pem f
ailed:  link ../testdata/dsaCa-key.pem ../testdata/ca/rootca/ca1/ca-key
.pem: file exists
	server_test.go:2244: srvStart ERROR Validation of certificate a
nd key failed: Invalid certificate in file '/opt/gopath/src/github.com/
hyperledger/fabric-ca/testdata/ca/rootca/ca1/ca-cert.pem': Failed to PE
M decode certificate
	server_test.go:2236: symlink expired cert to ../testdata/dsaCa-
cert.pem failed:  link ../testdata/dsaCa-cert.pem ../testdata/ca/rootca
/ca1/ca-cert.pem: file exists
	server_test.go:2240: symlink key to ../testdata/dsaCa-key.pem f
ailed:  link ../testdata/dsaCa-key.pem ../testdata/ca/rootca/ca1/ca-key
.pem: file exists
	server_test.go:2244: srvStart ERROR Validation of certificate a
nd key failed: Invalid certificate in file '/opt/gopath/src/github.com/
hyperledger/fabric-ca/testdata/ca/rootca/ca1/ca-cert.pem': Failed to PE
M decode certificate
	server_test.go:2236: symlink expired cert to ../testdata/rsa512
-cert.pem failed:  link ../testdata/rsa512-cert.pem ../testdata/ca/root
ca/ca1/ca-cert.pem: file exists
	server_test.go:2240: symlink key to ../testdata/rsa512-key.pem
failed:  link ../testdata/rsa512-key.pem ../testdata/ca/rootca/ca1/ca-k
ey.pem: file exists
	server_test.go:2244: srvStart ERROR Validation of certificate a
nd key failed: Invalid certificate in file '/opt/gopath/src/github.com/
hyperledger/fabric-ca/testdata/ca/rootca/ca1/ca-cert.pem': Failed to PE
M decode certificate
	server_test.go:2236: symlink expired cert to ../testdata/ec256-
1-cert.pem failed:  link ../testdata/ec256-1-cert.pem ../testdata/ca/ro
otca/ca1/ca-cert.pem: file exists
	server_test.go:2240: symlink key to ../testdata/ec256-2-key.pem
 failed:  link ../testdata/ec256-2-key.pem ../testdata/ca/rootca/ca1/ca
-key.pem: file exists
	server_test.go:2244: srvStart ERROR Validation of certificate a
nd key failed: Invalid certificate in file '/opt/gopath/src/github.com/
hyperledger/fabric-ca/testdata/ca/rootca/ca1/ca-cert.pem': Failed to PE
M decode certificate
	server_test.go:2236: symlink expired cert to ../testdata/rsa204
8-1-cert.pem failed:  link ../testdata/rsa2048-1-cert.pem ../testdata/c
a/rootca/ca1/ca-cert.pem: file exists
	server_test.go:2240: symlink key to ../testdata/rsa2048-2-key.p
em failed:  link ../testdata/rsa2048-2-key.pem ../testdata/ca/rootca/ca
1/ca-key.pem: file exists
	server_test.go:2244: srvStart ERROR Validation of certificate a
nd key failed: Invalid certificate in file '/opt/gopath/src/github.com/
hyperledger/fabric-ca/testdata/ca/rootca/ca1/ca-cert.pem': Failed to PE
M decode certificate
	server_test.go:2236: symlink expired cert to ../testdata/ec256-
1-cert.pem failed:  link ../testdata/ec256-1-cert.pem ../testdata/ca/ro
otca/ca1/ca-cert.pem: file exists
	server_test.go:2240: symlink key to ../testdata/rsa2048-1-cert.
pem failed:  link ../testdata/rsa2048-1-cert.pem ../testdata/ca/rootca/
ca1/ca-key.pem: file exists
	server_test.go:2244: srvStart ERROR Validation of certificate a
...
--- FAIL: TestSRVMultiCAWithIntermediate (0.03s)
	server_test.go:997: Server configuration: &{Port:7075 Address:
...
	server_test.go:1002: Failed to start server: Validation of certi
ficate and key failed: Invalid certificate in file '/opt/gopath/src/gith
ub.com/hyperledger/fabric-ca/testdata/ca/rootca/ca1/ca-cert.pem': Failed
 to PEM decode certificate
--- FAIL: TestSRVMaxEnrollmentInfinite (1.76s)
	server_test.go:1154: RemoveAll failed: remove rootDir/fabric_ca.
db: text file busy
	server_test.go:1156: Test max enrollment infinite
	server_test.go:1224: RemoveAll failed: remove rootDir/fabric_ca.
db: text file busy
--- FAIL: TestSRVMaxEnrollmentDisabled (1.38s)
	server_test.go:1231: RemoveAll failed: remove rootDir/fabric_ca.
db: text file busy
	server_test.go:1233: Test max enrollment disabled
	server_test.go:1272: RemoveAll failed: remove rootDir/fabric_ca.
db: text file busy
--- FAIL: TestSRVMaxEnrollmentLimited (1.65s)
	server_test.go:1279: RemoveAll failed: remove rootDir/fabric_ca.
db: text file busy
	server_test.go:1281: Test max enrollment limited
	server_test.go:1358: RemoveAll failed: remove rootDir/fabric_ca.
db: text file busy
FAIL
exit status 1
FAIL	github.com/hyperledger/fabric-ca/lib	111.445s

After the change:

fabric-ca/lib$ go test
PASS
ok      github.com/hyperledger/fabric-ca/lib    128.416s

Change-Id: I6e037cf65df6b61ad556824d72c6e7c7ba64f815
Signed-off-by: Arnaud J Le Hors <[email protected]>
  • Loading branch information
lehors committed Sep 27, 2017
1 parent b21aa3b commit 2780ccb
Show file tree
Hide file tree
Showing 4 changed files with 448 additions and 156 deletions.
16 changes: 8 additions & 8 deletions lib/ca_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,13 +267,13 @@ func TestCAInit(t *testing.T) {
}
ca.Config.CA.Keyfile = caKey
ca.Config.CA.Certfile = caCert
err = os.Link("../ec256-1-key.pem", caKey)
err = CopyFile("../ec256-1-key.pem", caKey)
if err != nil {
t.Fatal("symlink error: ", err)
t.Fatal("Failed to copy file: ", err)
}
err = os.Link("../ec256-2-cert.pem", caCert)
err = CopyFile("../ec256-2-cert.pem", caCert)
if err != nil {
t.Fatal("symlink error: ", err)
t.Fatal("Failed to copy file: ", err)
}
err = ca.init(false)
t.Logf("init err: %v", err)
Expand Down Expand Up @@ -317,13 +317,13 @@ func TestCAInit(t *testing.T) {
if err != nil {
t.Fatalf("os.Remove failed: %v", err)
}
err = os.Link("../rsa2048-1-key.pem", caKey)
err = CopyFile("../rsa2048-1-key.pem", caKey)
if err != nil {
t.Fatal("symlink error: ", err)
t.Fatal("Failed to copy file: ", err)
}
err = os.Link("../rsa2048-1-cert.pem", caCert)
err = CopyFile("../rsa2048-1-cert.pem", caCert)
if err != nil {
t.Fatal("symlink error: ", err)
t.Fatal("Failed to copy file: ", err)
}
ca.Config.CA.Keyfile = caKey
ca.Config.CA.Certfile = caCert
Expand Down
13 changes: 11 additions & 2 deletions lib/dasqlite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,10 @@ func TestDBCreation(t *testing.T) {
testWithExistingDbAndTable(t)
testWithExistingDb(t)

os.Remove(rootDB)
err := os.Remove(rootDB)
if err != nil {
t.Errorf("Remove failed: %s", err)
}
}

func createSQLiteDB(path string, t *testing.T) (*sqlx.DB, *TestAccessor) {
Expand Down Expand Up @@ -159,6 +162,8 @@ func testWithExistingDbAndTablesAndUser(t *testing.T) {
err = srv.Stop()
assert.NoError(t, err, "Failed to stop server")

err = db.Close()
assert.NoError(t, err, "Failed to close DB")
}

// Test starting a server with an already existing database and tables, but not bootstrapped
Expand All @@ -183,14 +188,16 @@ func testWithExistingDbAndTable(t *testing.T) {
err = srv.Stop()
assert.NoError(t, err, "Failed to stop server")

err = db.Close()
assert.NoError(t, err, "Failed to close DB")
}

// Test starting a server with an already existing database, but no tables or users
func testWithExistingDb(t *testing.T) {
var err error

os.Remove(rootDB)
_, acc := createSQLiteDB(rootDB, t)
db, acc := createSQLiteDB(rootDB, t)

srv := TestGetServer2(false, rootPort, rootDir, "", -1, t)
srv.CA.Config.DB.Datasource = "fabric_ca.db"
Expand All @@ -204,6 +211,8 @@ func testWithExistingDb(t *testing.T) {
err = srv.Stop()
assert.NoError(t, err, "Failed to stop server")

err = db.Close()
assert.NoError(t, err, "Failed to close DB")
}

func removeDatabase() {
Expand Down
Loading

0 comments on commit 2780ccb

Please sign in to comment.