Skip to content

Commit cbefc95

Browse files
Nao NishijimaNao Nishijima
Nao Nishijima
authored and
Nao Nishijima
committed
[FAB-4068] UT improvements in peer/node
This patch adds UT test for peer/node. The coverage is 69.8%. Change-Id: I27e432ca7d93785aec9ad949024ed76235f22b54 Signed-off-by: Nao Nishijima <[email protected]>
1 parent a01b2f9 commit cbefc95

File tree

2 files changed

+227
-0
lines changed

2 files changed

+227
-0
lines changed

peer/node/start_test.go

+117
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/*
2+
Copyright 2017 Hitachi America, Ltd.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package node
18+
19+
import (
20+
"io/ioutil"
21+
"os"
22+
"strconv"
23+
"syscall"
24+
"testing"
25+
"time"
26+
27+
"github.com/hyperledger/fabric/msp/mgmt/testtools"
28+
"github.com/spf13/viper"
29+
"github.com/stretchr/testify/assert"
30+
)
31+
32+
func TestStartCmd(t *testing.T) {
33+
viper.Set("peer.address", "0.0.0.0:6051")
34+
viper.Set("peer.listenAddress", "0.0.0.0:6051")
35+
viper.Set("peer.fileSystemPath", "/tmp/hyperledger/test")
36+
viper.Set("chaincode.executetimeout", "30s")
37+
overrideLogModules := []string{"msp", "gossip", "ledger", "cauthdsl", "policies", "grpc"}
38+
for _, module := range overrideLogModules {
39+
viper.Set("logging."+module, "INFO")
40+
}
41+
42+
msptesttools.LoadMSPSetupForTesting()
43+
44+
go func() {
45+
cmd := startCmd()
46+
assert.NoError(t, cmd.Execute(), "expected to successfully start command")
47+
}()
48+
49+
timer := time.NewTimer(time.Second * 3)
50+
defer timer.Stop()
51+
52+
// waiting for pid file will be created
53+
loop:
54+
for {
55+
select {
56+
case <-timer.C:
57+
t.Errorf("timeout waiting for start command")
58+
default:
59+
_, err := os.Stat("/tmp/hyperledger/test/peer.pid")
60+
if err != nil {
61+
time.Sleep(200 * time.Millisecond)
62+
} else {
63+
break loop
64+
}
65+
}
66+
}
67+
68+
pidFile, err := ioutil.ReadFile("/tmp/hyperledger/test/peer.pid")
69+
if err != nil {
70+
t.Fail()
71+
t.Errorf("can't delete pid file")
72+
}
73+
pid, err := strconv.Atoi(string(pidFile))
74+
killerr := syscall.Kill(pid, syscall.SIGTERM)
75+
if killerr != nil {
76+
t.Errorf("Error trying to kill -15 pid %d: %s", pid, killerr)
77+
}
78+
79+
os.RemoveAll("/tmp/hyperledger/test")
80+
}
81+
82+
func TestWritePid(t *testing.T) {
83+
var tests = []struct {
84+
name string
85+
fileName string
86+
pid int
87+
expected bool
88+
}{
89+
{
90+
name: "readPid success",
91+
fileName: "/tmp/hyperledger/test/peer.pid",
92+
pid: os.Getpid(),
93+
expected: true,
94+
},
95+
{
96+
name: "readPid error",
97+
fileName: "",
98+
pid: os.Getpid(),
99+
expected: false,
100+
},
101+
}
102+
103+
for _, test := range tests {
104+
test := test
105+
t.Run(test.name, func(t *testing.T) {
106+
t.Logf("Running test %s", test.name)
107+
if test.expected {
108+
err := writePid(test.fileName, test.pid)
109+
os.Remove(test.fileName)
110+
assert.NoError(t, err, "expected to successfully write pid file")
111+
} else {
112+
err := writePid(test.fileName, test.pid)
113+
assert.Error(t, err, "addition of empty pid filename should fail")
114+
}
115+
})
116+
}
117+
}

peer/node/status_test.go

+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/*
2+
Copyright 2017 Hitachi America, Ltd.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package node
18+
19+
import (
20+
"testing"
21+
22+
"github.com/hyperledger/fabric/core"
23+
"github.com/hyperledger/fabric/core/comm"
24+
testpb "github.com/hyperledger/fabric/core/comm/testdata/grpc"
25+
"github.com/hyperledger/fabric/core/peer"
26+
pb "github.com/hyperledger/fabric/protos/peer"
27+
"github.com/spf13/viper"
28+
"github.com/stretchr/testify/assert"
29+
"golang.org/x/net/context"
30+
)
31+
32+
type testServiceServer struct{}
33+
34+
func (tss *testServiceServer) EmptyCall(context.Context, *testpb.Empty) (*testpb.Empty, error) {
35+
return new(testpb.Empty), nil
36+
}
37+
38+
func TestStatusCmd(t *testing.T) {
39+
40+
viper.Set("peer.address", "localhost:7070")
41+
peerServer, err := peer.CreatePeerServer("localhost:7070", comm.SecureServerConfig{})
42+
if err != nil {
43+
t.Fatalf("Failed to create peer server (%s)", err)
44+
} else {
45+
pb.RegisterAdminServer(peerServer.Server(), core.NewAdminServer())
46+
go peerServer.Start()
47+
defer peerServer.Stop()
48+
49+
cmd := statusCmd()
50+
if err := cmd.Execute(); err != nil {
51+
t.Fail()
52+
t.Errorf("expected status command to succeed")
53+
}
54+
}
55+
}
56+
57+
func TestStatus(t *testing.T) {
58+
var tests = []struct {
59+
name string
60+
peerAddress string
61+
listenAddress string
62+
expected bool
63+
}{
64+
{
65+
name: "status function to success",
66+
peerAddress: "localhost:7071",
67+
listenAddress: "localhost:7071",
68+
expected: true,
69+
},
70+
{
71+
name: "admin client error",
72+
peerAddress: "",
73+
listenAddress: "localhost:7072",
74+
expected: false,
75+
},
76+
}
77+
78+
for _, test := range tests {
79+
test := test
80+
t.Run(test.name, func(t *testing.T) {
81+
t.Logf("Running test: %s", test.name)
82+
viper.Set("peer.address", test.peerAddress)
83+
peerServer, err := peer.CreatePeerServer(test.listenAddress, comm.SecureServerConfig{})
84+
if err != nil {
85+
t.Fatalf("Failed to create peer server (%s)", err)
86+
} else {
87+
pb.RegisterAdminServer(peerServer.Server(), core.NewAdminServer())
88+
go peerServer.Start()
89+
defer peerServer.Stop()
90+
if test.expected {
91+
assert.NoError(t, status())
92+
} else {
93+
assert.Error(t, status())
94+
}
95+
}
96+
})
97+
}
98+
}
99+
100+
func TestStatusWithGetStatusError(t *testing.T) {
101+
viper.Set("peer.address", "localhost:7073")
102+
peerServer, err := peer.CreatePeerServer(":7073", comm.SecureServerConfig{})
103+
if err != nil {
104+
t.Fatalf("Failed to create peer server (%s)", err)
105+
}
106+
testpb.RegisterTestServiceServer(peerServer.Server(), &testServiceServer{})
107+
go peerServer.Start()
108+
defer peerServer.Stop()
109+
assert.Error(t, status())
110+
}

0 commit comments

Comments
 (0)