Skip to content

Commit

Permalink
[FAB-5935] Fixes TestSRVServerInit and more on vagrant
Browse files Browse the repository at this point in the history
This change simply uses a temp dir on a local volume before
trying to change its access mode so that it can effectively
be set to 000.

With this changes the test passes:

$ go test -v -run TestSRVServerInit
=== RUN   TestSRVServerInit
2017/08/28 13:14:50 [DEBUG] CA Home Directory: /opt/gopath/src/github.com/hyperledger/fabric-ca/lib/rootDir
...
--- PASS: TestSRVServerInit (0.30s)
        server_test.go:111: Current dir: .
        server_test.go:112: Current curd: /tmp/ServerInitStat202077287
        server_test.go:119: Server.Init error: Failed to get server's home directory: stat .: permission denied
PASS

Patch-set #2: fixes similar problem with TestCAInit, TestCLIClientConfigStat, and TestCWBCAConfigStat
Patch-set #3: makes sure to delete all temp files that were created by tests
Patch-set #4: fixes file format
Patch-set #5: improves error handling
Patch-set #6: fixes error message that referenced wrong dir
Patch-set #7: fixes yet another error cleaning up tmp file

Change-Id: If2aa88d683df7997159442f1d0d9d678f21496f3
Signed-off-by: Arnaud J Le Hors <[email protected]>
Signed-off-by: Allen Bailey <[email protected]>
  • Loading branch information
lehors committed Sep 14, 2017
1 parent 27ebf39 commit 02c8f4e
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 66 deletions.
73 changes: 39 additions & 34 deletions lib/ca_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,17 +192,35 @@ func TestCAInit(t *testing.T) {
var caCert = "ca-cert.pem"
var caKey = "ca-key.pem"

wd, err := os.Getwd()
orgwd, err := os.Getwd()
if err != nil {
t.Fatalf("failed to get cwd")
t.Fatalf("failed to get cwd: %s", err)
}
t.Logf("====== wd %v", wd)
t.Logf("====== orgwd %v", orgwd)
confDir, err := cdTmpTestDir("TestCAInit")
if err != nil {
t.Fatalf("failed to cd to tmp dir")
t.Fatalf("failed to cd to tmp dir: %s", err)
}
defer func() {
err = os.Chdir(orgwd)
if err != nil {
t.Fatalf("failed to cd to %v: %s", orgwd, err)
}
}()
t.Logf("confDir: %v", confDir)

wd, err := os.Getwd()
if err != nil {
t.Fatalf("failed to get cwd: %s", err)
}
t.Log("Working dir", wd)
defer func() {
err = os.RemoveAll(wd)
if err != nil {
t.Fatalf("RemoveAll failed: %s", err)
} else {
t.Logf("Removed all: %s", wd)
}
}()
ca, err := NewCA(confDir, &cfg, &srv, false)
if err != nil {
t.Fatal("NewCA FAILED")
Expand All @@ -222,12 +240,24 @@ func TestCAInit(t *testing.T) {
// delete everything and start over
// initKeyMaterial error
os.Chdir("..")
confDir1 := confDir
confDir, err = cdTmpTestDir("TestCAInit")

confDir, err = cdTmpTestDir("TestCaInit")
if err != nil {
t.Fatalf("failed to cd to tmp dir")
t.Fatalf("failed to cd to tmp dir: %s", err)
}
t.Logf("confDir: %v", confDir)
wd2, err := os.Getwd()
if err != nil {
t.Fatalf("failed to get cwd: %s", err)
}
t.Log("changed to ", wd2)
defer func() {
err = os.RemoveAll(wd2)
if err != nil {
t.Fatalf("RemoveAll failed: %s", err)
} else {
t.Logf("Removed all: %s", wd2)
}
}()

ca.Config.CSP = &factory.FactoryOpts{ProviderName: "SW", SwOpts: swo, Pkcs11Opts: pko}
ca, err = NewCA(confDir, &cfg, &srv, true)
Expand Down Expand Up @@ -302,31 +332,6 @@ func TestCAInit(t *testing.T) {
if err == nil {
t.Fatal("Should have failed")
}

os.Chdir("..")
wd, err = os.Getwd()
if err != nil {
t.Fatalf("failed to get cwd")
}
t.Logf("changed to ====== wd %v", wd)
t.Logf("Removing %s", confDir)
err = os.RemoveAll(confDir)
if err != nil {
t.Fatalf("os.RemoveAll failed: %v", err)
}
t.Logf("Removing %s", confDir1)
err = os.RemoveAll(confDir1)
if err != nil {
t.Fatalf("os.RemoveAll failed: %v", err)
}

t.Logf(" changing to ====== wd %v", wd)
os.Chdir(wd)
wd, err = os.Getwd()
if err != nil {
t.Fatalf("failed to get cwd")
}
t.Logf("changed to ====== wd %v", wd)
}

func getTestDir(d string) (string, error) {
Expand Down
38 changes: 27 additions & 11 deletions lib/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,34 +55,50 @@ const (
func TestCLIClientConfigStat(t *testing.T) {
wd, err := os.Getwd()
if err != nil {
t.Fatalf("failed to get cwd")
t.Fatalf("failed to get cwd: %s", err)
}
td, err := ioutil.TempDir(tdDir, "ClientConfigStat")
td, err := ioutil.TempDir("", "ClientConfigStat")
if err != nil {
t.Fatalf("failed to get tmp dir")
t.Fatalf("failed to get tmp dir: %s", err)
}
os.Chdir(td)
defer func() {
err = os.RemoveAll(td)
if err != nil {
t.Fatalf("RemoveAll failed: %s", err)
}
}()
err = os.Chdir(td)
if err != nil {
t.Fatalf("failed to cd to %v: %s", td, err)
}
defer func() {
err = os.Chdir(wd)
if err != nil {
t.Fatalf("failed to cd to %v: %s", wd, err)
}
}()
fileInfo, err := os.Stat(".")
if err != nil {
t.Fatalf("os.Stat failed on current dir")
t.Fatalf("os.Stat failed on current dir: %s", err)
}
oldmode := fileInfo.Mode()
err = os.Chmod(".", 0000)
if err != nil {
t.Fatalf("Chmod on %s failed", tdDir)
t.Fatalf("Chmod on %s failed: %s", td, err)
}

defer func() {
err = os.Chmod(td, oldmode)
if err != nil {
t.Fatalf("Chmod on %s failed: %s", td, err)
}
}()
c := new(Client)
c.Config = new(ClientConfig)
err = c.Init()
t.Logf("initDB err: %v", err)
if err == nil {
t.Errorf("initDB should have failed (getcwd failure)")
}
_ = os.Chmod(".", oldmode)

os.RemoveAll(td)
os.Chdir(wd)
}

func TestCLIClientInit(t *testing.T) {
Expand Down
37 changes: 27 additions & 10 deletions lib/client_whitebox_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -546,39 +546,56 @@ func TestCWBNewCertificateRequest(t *testing.T) {
func TestCWBCAConfigStat(t *testing.T) {
wd, err := os.Getwd()
if err != nil {
t.Fatalf("failed to get cwd")
t.Fatalf("failed to get cwd: %s", err)
}
td, err := ioutil.TempDir(testdataDir, "CAConfigStat")
td, err := ioutil.TempDir("", "CAConfigStat")
if err != nil {
t.Fatalf("failed to get tmp dir")
t.Fatalf("failed to get tmp dir: %s", err)
}
os.Chdir(td)
defer func() {
err = os.RemoveAll(td)
if err != nil {
t.Fatalf("RemoveAll failed: %s", err)
}
}()
err = os.Chdir(td)
if err != nil {
t.Fatalf("failed to cd to %v: %s", td, err)
}
defer func() {
err = os.Chdir(wd)
if err != nil {
t.Fatalf("failed to cd to %v: %s", wd, err)
}
}()

ca := &CA{}
ca.Config = &CAConfig{}
ca.HomeDir = "."
fileInfo, err := os.Stat(".")
if err != nil {
t.Fatalf("os.Stat failed on current dir")
t.Fatalf("os.Stat failed on current dir: %s", err)
}
oldmode := fileInfo.Mode()
err = os.Chmod(".", 0000)
if err != nil {
t.Fatalf("Chmod on %s failed", testdataDir)
t.Fatalf("Chmod on %s failed: %s", fileInfo.Name(), err)
}
defer func() {
err = os.Chmod(td, oldmode)
if err != nil {
t.Fatalf("Chmod on %s failed: %s", td, err)
}
}()

ca.Config.DB.Type = ""
err = ca.initDB()
t.Logf("initDB err: %v", err)
if err == nil {
t.Errorf("initDB should have failed (getcwd failure)")
}
_ = os.Chmod(".", oldmode)
ca.Config.DB.Datasource = ""
ca.HomeDir = ""

defer os.RemoveAll(td)
os.Chdir(wd)
}

func TestCLIClientClean(t *testing.T) {
Expand Down
38 changes: 27 additions & 11 deletions lib/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,39 +93,55 @@ func TestSRVServerInit(t *testing.T) {
// Fail case - cannot get home directory
wd, err := os.Getwd()
if err != nil {
t.Fatalf("failed to get cwd")
t.Fatalf("failed to get cwd: %s", err)
}
td, err := ioutil.TempDir(testdataDir, "ServerInitStat")
td, err := ioutil.TempDir("", "ServerInitStat")
if err != nil {
t.Fatalf("failed to get tmp dir")
t.Fatalf("failed to get tmp dir: %s", err)
}
defer func() {
err = os.RemoveAll(td)
if err != nil {
t.Fatalf("RemoveAll failed: %s", err)
}
}()
server.HomeDir = ""
os.Chdir(td)

err = os.Chdir(td)
if err != nil {
t.Fatalf("failed to cd to %v: %s", td, err)
}
defer func() {
err = os.Chdir(wd)
if err != nil {
t.Fatalf("failed to cd to %v: %s", wd, err)
}
}()
fileInfo, err := os.Stat(".")
if err != nil {
t.Fatalf("os.Stat failed on current dir")
t.Fatalf("os.Stat failed on current dir: %s", err)
}
oldmode := fileInfo.Mode()
curd, err := os.Getwd()
t.Logf("Current dir: %s", fileInfo.Name())
t.Logf("Current curd: %v", curd)
err = os.Chmod(".", 0000)
if err != nil {
t.Fatalf("Chmod on %s failed", fileInfo.Name())
t.Fatalf("Chmod on %s failed: %s", fileInfo.Name(), err)
}
defer func() {
err = os.Chmod(td, oldmode)
if err != nil {
t.Fatalf("Chmod on %s failed: %s", td, err)
}
}()

err = server.Init(false)
t.Logf("Server.Init error: %v", err)
if err == nil {
t.Errorf("Server init should have failed (permission error)")
}

os.Chdir("..")
_ = os.Chmod(td, oldmode)
server.HomeDir = ""
defer os.RemoveAll(td)
os.Chdir(wd)
}

func TestSRVRootServer(t *testing.T) {
Expand Down

0 comments on commit 02c8f4e

Please sign in to comment.