Skip to content

Commit

Permalink
add CodeCoverage Test script (#872)
Browse files Browse the repository at this point in the history
* fix log/ledger usages in all testcases

* update coverage test script, fix p2p testcases

* change back package name of test files

* fix nbr_peer_test
  • Loading branch information
Honglei-Cong authored and laizy committed May 20, 2019
1 parent 041caf6 commit 6686600
Show file tree
Hide file tree
Showing 20 changed files with 139 additions and 80 deletions.
27 changes: 27 additions & 0 deletions .go.coverage.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/bin/bash

unset dirs files
dirs=$(go list ./... | grep -v vendor/ | grep -v ontology$)

mkdir coverage
tmpFile=./coverage/coverage.out.tmp
outFile=./coverage/coverage.out
testMissedFile=./coverage/coverage_missed.out
rm -f $tmpFile $testMissedFile
echo 'mode: count' > $outFile

for d in $dirs
do
go test -v $d -coverprofile=$tmpFile
if [ -f $tmpFile ]
then
cat $tmpFile | tail -n +2 >> $outFile
rm $tmpFile
else
echo -e "[no test files] \t" $d >> $testMissedFile
fi
done

go tool cover -html=$outFile -o ./coverage/coverage.out.html
go tool cover -func=$outFile -o ./coverage/coverage_func.out.txt

2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,6 @@ docker: Makefile docker/payload docker/Dockerfile
@touch $@

clean:
rm -rf *.8 *.o *.out *.6 *exe
rm -rf *.8 *.o *.out *.6 *exe coverage
rm -rf ontology ontology-* tools docker/payload docker/build

6 changes: 3 additions & 3 deletions common/log/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func TestLog(t *testing.T) {
os.RemoveAll("Log/")
}()

Init(PATH, Stdout)
InitLog(InfoLog, PATH, Stdout)
Log.SetDebugLevel(DebugLog)
logPrint()

Expand All @@ -64,7 +64,7 @@ func TestNewLogFile(t *testing.T) {
defer func() {
os.RemoveAll("Log/")
}()
Init(PATH, Stdout)
InitLog(InfoLog, PATH, Stdout)
logfileNum1, err1 := ioutil.ReadDir("Log/")
if err1 != nil {
fmt.Println(err1)
Expand All @@ -75,7 +75,7 @@ func TestNewLogFile(t *testing.T) {
assert.NotEqual(t, isNeedNewFile, true)
ClosePrintLog()
time.Sleep(time.Second * 2)
Init(PATH, Stdout)
InitLog(InfoLog, PATH, Stdout)
logfileNum2, err2 := ioutil.ReadDir("Log/")
if err2 != nil {
fmt.Println(err2)
Expand Down
28 changes: 14 additions & 14 deletions consensus/vbft/chain_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
package vbft

import (
"fmt"
"os"
"testing"

Expand All @@ -31,19 +30,17 @@ import (
"github.com/ontio/ontology/core/ledger"
)

func newChainStore() *ChainStore {
log.Init(log.PATH, log.Stdout)
func newTestChainStore(t *testing.T) *ChainStore {
log.InitLog(log.InfoLog, log.Stdout)
var err error
acct := account.NewAccount("SHA256withECDSA")
if acct == nil {
fmt.Println("GetDefaultAccount error: acc is nil")
os.Exit(1)
t.Fatalf("GetDefaultAccount error: acc is nil")
}

db, err := ledger.NewLedger(config.DEFAULT_DATA_DIR, 0)
if err != nil {
log.Fatalf("NewLedger error %s", err)
os.Exit(1)
t.Fatalf("NewLedger error %s", err)
}
acc1 := account.NewAccount("")
acc2 := account.NewAccount("")
Expand All @@ -57,28 +54,31 @@ func newChainStore() *ChainStore {
genesisConfig := config.DefConfig.Genesis
block, err := genesis.BuildGenesisBlock(bookkeepers, genesisConfig)
if err != nil {
log.Errorf("BuildGenesisBlock error %s", err)
os.Exit(1)
t.Fatalf("BuildGenesisBlock error %s", err)
}
err = db.Init(bookkeepers, block)
if err != nil {
log.Errorf("InitLedgerStoreWithGenesisBlock error %s", err)
os.Exit(1)
t.Fatalf("InitLedgerStoreWithGenesisBlock error %s", err)
}
chainstore, err := OpenBlockStore(db, nil)
if err != nil {
log.Errorf("openblockstore failed: %v\n", err)
return nil
t.Fatalf("openblockstore failed: %v\n", err)
}
return chainstore
}

func cleanTestChainStore() {
os.RemoveAll(config.DEFAULT_DATA_DIR)
}

func TestGetChainedBlockNum(t *testing.T) {
chainstore := newChainStore()
chainstore := newTestChainStore(t)
if chainstore == nil {
t.Error("newChainStrore error")
return
}
defer cleanTestChainStore()

blocknum := chainstore.GetChainedBlockNum()
t.Logf("TestGetChainedBlockNum :%d", blocknum)
}
4 changes: 2 additions & 2 deletions consensus/vbft/config/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func constructConfig() (*config.VBFTConfig, error) {
}

func TestGenConsensusPayload(t *testing.T) {
log.Init(log.PATH, log.Stdout)
log.InitLog(log.InfoLog, log.Stdout)
config, err := constructConfig()
if err != nil {
t.Errorf("constructConfig failed:%s", err)
Expand All @@ -96,7 +96,7 @@ func TestGenConsensusPayload(t *testing.T) {
}

func TestGenesisChainConfig(t *testing.T) {
log.Init(log.PATH, log.Stdout)
log.InitLog(log.InfoLog, log.Stdout)
config, err := constructConfig()
if err != nil {
t.Errorf("constructConfig failed:%s", err)
Expand Down
2 changes: 1 addition & 1 deletion consensus/vbft/node_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func TestGetProposerRankLocked(t *testing.T) {
}

func TestGetHighestRankProposal(t *testing.T) {
log.Init(log.PATH, log.Stdout)
log.InitLog(log.InfoLog, log.Stdout)
server := constructServer()
server.peerPool = peerPool()
block, err := constructBlock()
Expand Down
6 changes: 3 additions & 3 deletions consensus/vbft/state_mgmt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func TestStateMgr_getState(t *testing.T) {
}

func TestStateMgr_onPeerUpdate(t *testing.T) {
log.Init(log.PATH, log.Stdout)
log.InitLog(log.InfoLog, log.Stdout)
sev := constructServer()
peerstate := &PeerState{
peerIdx: 1,
Expand Down Expand Up @@ -223,15 +223,15 @@ func TestStateMgr_isSyncedReady(t *testing.T) {
}

func TestStateMgr_checkStartSyncing(t *testing.T) {
log.Init(log.PATH, log.Stdout)
log.InitLog(log.InfoLog, log.Stdout)
statemgr := constructPeerState()
statemgr.server.syncer = newSyncer(statemgr.server)
statemgr.checkStartSyncing(1, true)
t.Log("TestcheckStartSyncing")
}

func TestStateMgr_getConsensusedCommittedBlockNum(t *testing.T) {
log.Init(log.PATH, log.Stdout)
log.InitLog(log.InfoLog, log.Stdout)
statemgr := constructPeerState()
maxcomit, flag := statemgr.getConsensusedCommittedBlockNum()
t.Logf("TestgetConsensusedCommittedBlockNum maxcommitted:%v, consensused:%v", maxcomit, flag)
Expand Down
2 changes: 1 addition & 1 deletion p2pserver/actor/server/actor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
)

func TestP2PActorServer(t *testing.T) {
log.Init(log.Stdout)
log.InitLog(log.InfoLog, log.Stdout)
fmt.Println("Start test the p2pserver by actor...")

p2p := p2pserver.NewServer()
Expand Down
16 changes: 8 additions & 8 deletions p2pserver/link/link_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,16 @@ var (
)

func init() {
log.Init(log.Stdout)
log.InitLog(log.InfoLog, log.Stdout)

cliLink = NewLink()
serverLink = NewLink()

cliLink.id = 0x733936
serverLink.id = 0x8274950
cliLink.SetID(0x733936)
serverLink.SetID(0x8274950)

cliLink.port = 50338
serverLink.port = 50339
cliLink.SetPort(50338)
serverLink.SetPort(50339)

cliChan = make(chan *mt.MsgPayload, 100)
serverChan = make(chan *mt.MsgPayload, 100)
Expand Down Expand Up @@ -90,8 +90,8 @@ func TestNewLink(t *testing.T) {
cliLink.UpdateRXTime(time.Now())

msg := &mt.MsgPayload{
Id: cliLink.id,
Addr: cliLink.addr,
Id: cliLink.GetID(),
Addr: cliLink.GetAddr(),
Payload: &mt.NotFound{comm.UINT256_EMPTY},
}
go func() {
Expand All @@ -101,7 +101,7 @@ func TestNewLink(t *testing.T) {

timeout := time.NewTimer(time.Second)
select {
case <-cliLink.recvChan:
case <-cliChan:
t.Log("read data from channel")
case <-timeout.C:
timeout.Stop()
Expand Down
10 changes: 8 additions & 2 deletions p2pserver/message/utils/msg_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"bytes"
"encoding/binary"
"encoding/hex"
"os"
"testing"
"time"

Expand All @@ -47,8 +48,8 @@ var (
network p2p.P2P
)

func init() {
log.Init(log.Stdout)
func TestMain(m *testing.M) {
log.InitLog(log.InfoLog, log.Stdout)
// Start local network server and create message router
network = netserver.NewNetServer()

Expand Down Expand Up @@ -76,6 +77,11 @@ func init() {
if err != nil {
log.Fatalf("DefLedger.Init error %s", err)
}

m.Run()

ledger.DefLedger.Close()
os.RemoveAll(config.DEFAULT_DATA_DIR)
}

// TestVersionHandle tests Function VersionHandle handling a version message
Expand Down
3 changes: 2 additions & 1 deletion p2pserver/message/utils/msg_router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@
package utils

import (
"testing"

"github.com/ontio/ontology-eventbus/actor"
"github.com/ontio/ontology/common/log"
"github.com/ontio/ontology/p2pserver/message/types"
"github.com/ontio/ontology/p2pserver/net/netserver"
"github.com/ontio/ontology/p2pserver/net/protocol"
"github.com/stretchr/testify/assert"
"testing"
)

func testHandler(data *types.MsgPayload, p2p p2p.P2P, pid *actor.PID, args ...interface{}) {
Expand Down
4 changes: 1 addition & 3 deletions p2pserver/net/netserver/netserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
)

func init() {
log.Init(log.Stdout)
log.InitLog(log.InfoLog, log.Stdout)
fmt.Println("Start test the netserver...")
}

Expand Down Expand Up @@ -76,11 +76,9 @@ func TestNewNetServer(t *testing.T) {
}

fmt.Printf("lastest server time is %s\n", time.Unix(server.GetTime()/1e9, 0).String())

}

func TestNetServerNbrPeer(t *testing.T) {
log.Init(log.Stdout)
server := NewNetServer()
server.Start()
defer server.Halt()
Expand Down
3 changes: 1 addition & 2 deletions p2pserver/p2pserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,11 @@ import (
)

func init() {
log.InitLog(log.InfoLog)
log.InitLog(log.InfoLog, log.Stdout)
fmt.Println("Start test the netserver...")

}
func TestNewP2PServer(t *testing.T) {
log.Init(log.Stdout)
fmt.Println("Start test new p2pserver...")

p2p := NewServer()
Expand Down
Loading

0 comments on commit 6686600

Please sign in to comment.