Skip to content

Commit 4a84f9d

Browse files
committed
[FAB-4062] Aded unit tests to /fabric/core
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 <[email protected]> Change-Id: I532336e48eebb92cc25adbe0cec80c888e473387
1 parent 9eb72a7 commit 4a84f9d

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

core/admin.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ import (
2929

3030
var log = flogging.MustGetLogger("server")
3131

32+
// Here for unit testing purposes
33+
var osExit = os.Exit
34+
3235
// NewAdminServer creates and returns a Admin service instance.
3336
func NewAdminServer() *ServerAdmin {
3437
s := new(ServerAdmin)
@@ -61,7 +64,7 @@ func (*ServerAdmin) StopServer(context.Context, *empty.Empty) (*pb.ServerStatus,
6164
pidFile := config.GetPath("peer.fileSystemPath") + "/peer.pid"
6265
log.Debugf("Remove pid file %s", pidFile)
6366
os.Remove(pidFile)
64-
defer os.Exit(1)
67+
defer osExit(1)
6568
return status, nil
6669
}
6770

core/admin_test.go

+18
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,24 @@ func TestStartServer(t *testing.T) {
4646
assert.Nil(t, err, "Error should have been nil")
4747
}
4848

49+
func TestStopServer(t *testing.T) {
50+
oldOsExit := osExit
51+
52+
defer func() { osExit = oldOsExit }()
53+
54+
var got int
55+
myExit := func(code int) {
56+
got = code
57+
}
58+
59+
osExit = myExit
60+
response, err := adminServer.StopServer(context.Background(), &empty.Empty{})
61+
assert.NotNil(t, response, "Response should have been set")
62+
assert.Nil(t, err, "Error should have been nil")
63+
64+
assert.Equal(t, 1, got, "Exit code should be 1")
65+
}
66+
4967
func TestLoggingCalls(t *testing.T) {
5068
flogging.MustGetLogger("test")
5169
flogging.SetPeerStartupModulesMap()

0 commit comments

Comments
 (0)