Skip to content

Commit

Permalink
[FAB-4062] Aded unit tests to /fabric/core
Browse files Browse the repository at this point in the history
Added test to admin_test.go to test StopServer.

Moved os.Exit from the StopServer function into a separate variable to allow unit testing

. Replaced if with assert.Equal

Signed-off-by: Liam Grace <liamgrace.896@gmail.com>
Change-Id: I532336e48eebb92cc25adbe0cec80c888e473387
  • Loading branch information
liam-grace committed May 25, 2017
1 parent 9eb72a7 commit 4a84f9d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
5 changes: 4 additions & 1 deletion core/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ import (

var log = flogging.MustGetLogger("server")

// Here for unit testing purposes
var osExit = os.Exit

// NewAdminServer creates and returns a Admin service instance.
func NewAdminServer() *ServerAdmin {
s := new(ServerAdmin)
Expand Down Expand Up @@ -61,7 +64,7 @@ func (*ServerAdmin) StopServer(context.Context, *empty.Empty) (*pb.ServerStatus,
pidFile := config.GetPath("peer.fileSystemPath") + "/peer.pid"
log.Debugf("Remove pid file %s", pidFile)
os.Remove(pidFile)
defer os.Exit(1)
defer osExit(1)
return status, nil
}

Expand Down
18 changes: 18 additions & 0 deletions core/admin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,24 @@ func TestStartServer(t *testing.T) {
assert.Nil(t, err, "Error should have been nil")
}

func TestStopServer(t *testing.T) {
oldOsExit := osExit

defer func() { osExit = oldOsExit }()

var got int
myExit := func(code int) {
got = code
}

osExit = myExit
response, err := adminServer.StopServer(context.Background(), &empty.Empty{})
assert.NotNil(t, response, "Response should have been set")
assert.Nil(t, err, "Error should have been nil")

assert.Equal(t, 1, got, "Exit code should be 1")
}

func TestLoggingCalls(t *testing.T) {
flogging.MustGetLogger("test")
flogging.SetPeerStartupModulesMap()
Expand Down

0 comments on commit 4a84f9d

Please sign in to comment.