Skip to content

Commit

Permalink
[FAB-4068] UT improvements in peer/node
Browse files Browse the repository at this point in the history
This patch adds UT test for peer/node. The coverage is 69.8%.

Change-Id: I27e432ca7d93785aec9ad949024ed76235f22b54
Signed-off-by: Nao Nishijima <Nao.nishijima@hal.hitachi.com>
  • Loading branch information
Nao Nishijima authored and Nao Nishijima committed Jun 2, 2017
1 parent a01b2f9 commit cbefc95
Show file tree
Hide file tree
Showing 2 changed files with 227 additions and 0 deletions.
117 changes: 117 additions & 0 deletions peer/node/start_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*
Copyright 2017 Hitachi America, Ltd.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package node

import (
"io/ioutil"
"os"
"strconv"
"syscall"
"testing"
"time"

"github.com/hyperledger/fabric/msp/mgmt/testtools"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
)

func TestStartCmd(t *testing.T) {
viper.Set("peer.address", "0.0.0.0:6051")
viper.Set("peer.listenAddress", "0.0.0.0:6051")
viper.Set("peer.fileSystemPath", "/tmp/hyperledger/test")
viper.Set("chaincode.executetimeout", "30s")
overrideLogModules := []string{"msp", "gossip", "ledger", "cauthdsl", "policies", "grpc"}
for _, module := range overrideLogModules {
viper.Set("logging."+module, "INFO")
}

msptesttools.LoadMSPSetupForTesting()

go func() {
cmd := startCmd()
assert.NoError(t, cmd.Execute(), "expected to successfully start command")
}()

timer := time.NewTimer(time.Second * 3)
defer timer.Stop()

// waiting for pid file will be created
loop:
for {
select {
case <-timer.C:
t.Errorf("timeout waiting for start command")
default:
_, err := os.Stat("/tmp/hyperledger/test/peer.pid")
if err != nil {
time.Sleep(200 * time.Millisecond)
} else {
break loop
}
}
}

pidFile, err := ioutil.ReadFile("/tmp/hyperledger/test/peer.pid")
if err != nil {
t.Fail()
t.Errorf("can't delete pid file")
}
pid, err := strconv.Atoi(string(pidFile))
killerr := syscall.Kill(pid, syscall.SIGTERM)
if killerr != nil {
t.Errorf("Error trying to kill -15 pid %d: %s", pid, killerr)
}

os.RemoveAll("/tmp/hyperledger/test")
}

func TestWritePid(t *testing.T) {
var tests = []struct {
name string
fileName string
pid int
expected bool
}{
{
name: "readPid success",
fileName: "/tmp/hyperledger/test/peer.pid",
pid: os.Getpid(),
expected: true,
},
{
name: "readPid error",
fileName: "",
pid: os.Getpid(),
expected: false,
},
}

for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
t.Logf("Running test %s", test.name)
if test.expected {
err := writePid(test.fileName, test.pid)
os.Remove(test.fileName)
assert.NoError(t, err, "expected to successfully write pid file")
} else {
err := writePid(test.fileName, test.pid)
assert.Error(t, err, "addition of empty pid filename should fail")
}
})
}
}
110 changes: 110 additions & 0 deletions peer/node/status_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
Copyright 2017 Hitachi America, Ltd.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package node

import (
"testing"

"github.com/hyperledger/fabric/core"
"github.com/hyperledger/fabric/core/comm"
testpb "github.com/hyperledger/fabric/core/comm/testdata/grpc"
"github.com/hyperledger/fabric/core/peer"
pb "github.com/hyperledger/fabric/protos/peer"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
"golang.org/x/net/context"
)

type testServiceServer struct{}

func (tss *testServiceServer) EmptyCall(context.Context, *testpb.Empty) (*testpb.Empty, error) {
return new(testpb.Empty), nil
}

func TestStatusCmd(t *testing.T) {

viper.Set("peer.address", "localhost:7070")
peerServer, err := peer.CreatePeerServer("localhost:7070", comm.SecureServerConfig{})
if err != nil {
t.Fatalf("Failed to create peer server (%s)", err)
} else {
pb.RegisterAdminServer(peerServer.Server(), core.NewAdminServer())
go peerServer.Start()
defer peerServer.Stop()

cmd := statusCmd()
if err := cmd.Execute(); err != nil {
t.Fail()
t.Errorf("expected status command to succeed")
}
}
}

func TestStatus(t *testing.T) {
var tests = []struct {
name string
peerAddress string
listenAddress string
expected bool
}{
{
name: "status function to success",
peerAddress: "localhost:7071",
listenAddress: "localhost:7071",
expected: true,
},
{
name: "admin client error",
peerAddress: "",
listenAddress: "localhost:7072",
expected: false,
},
}

for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
t.Logf("Running test: %s", test.name)
viper.Set("peer.address", test.peerAddress)
peerServer, err := peer.CreatePeerServer(test.listenAddress, comm.SecureServerConfig{})
if err != nil {
t.Fatalf("Failed to create peer server (%s)", err)
} else {
pb.RegisterAdminServer(peerServer.Server(), core.NewAdminServer())
go peerServer.Start()
defer peerServer.Stop()
if test.expected {
assert.NoError(t, status())
} else {
assert.Error(t, status())
}
}
})
}
}

func TestStatusWithGetStatusError(t *testing.T) {
viper.Set("peer.address", "localhost:7073")
peerServer, err := peer.CreatePeerServer(":7073", comm.SecureServerConfig{})
if err != nil {
t.Fatalf("Failed to create peer server (%s)", err)
}
testpb.RegisterTestServiceServer(peerServer.Server(), &testServiceServer{})
go peerServer.Start()
defer peerServer.Stop()
assert.Error(t, status())
}

0 comments on commit cbefc95

Please sign in to comment.