Skip to content

Commit 988f2ff

Browse files
committed
[FAB-3922] Write unit tests for core/scc
The package core/scc has no unit tests. I wrote some UTs and now its CC is 87%. Change-Id: Iafd146e306385da55d842f57e81309be1878cdeb Signed-off-by: Yacov Manevich <[email protected]>
1 parent 2470d35 commit 988f2ff

File tree

1 file changed

+117
-0
lines changed

1 file changed

+117
-0
lines changed

core/scc/scc_test.go

+117
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/*
2+
Copyright IBM Corp. 2017 All Rights Reserved.
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 scc
18+
19+
import (
20+
"fmt"
21+
"testing"
22+
23+
"github.com/hyperledger/fabric/core/common/ccprovider"
24+
"github.com/hyperledger/fabric/core/ledger/ledgermgmt"
25+
ccprovider2 "github.com/hyperledger/fabric/core/mocks/ccprovider"
26+
"github.com/hyperledger/fabric/core/peer"
27+
"github.com/spf13/viper"
28+
"github.com/stretchr/testify/assert"
29+
)
30+
31+
func init() {
32+
viper.Set("chaincode.system", map[string]string{"lscc": "enable", "a": "enable"})
33+
ccprovider.RegisterChaincodeProviderFactory(&ccprovider2.MockCcProviderFactory{})
34+
RegisterSysCCs()
35+
}
36+
37+
func TestDeploy(t *testing.T) {
38+
DeploySysCCs("")
39+
f := func() {
40+
DeploySysCCs("a")
41+
}
42+
assert.Panics(t, f)
43+
ledgermgmt.InitializeTestEnv()
44+
defer ledgermgmt.CleanupTestEnv()
45+
err := peer.MockCreateChain("a")
46+
fmt.Println(err)
47+
deploySysCC("a", &SystemChaincode{
48+
Enabled: true,
49+
Name: "lscc",
50+
})
51+
}
52+
53+
func TestDeDeploySysCC(t *testing.T) {
54+
DeDeploySysCCs("")
55+
f := func() {
56+
DeDeploySysCCs("a")
57+
}
58+
assert.NotPanics(t, f)
59+
}
60+
61+
func TestSCCProvider(t *testing.T) {
62+
assert.NotNil(t, (&sccProviderFactory{}).NewSystemChaincodeProvider())
63+
}
64+
65+
func TestIsSysCC(t *testing.T) {
66+
assert.True(t, IsSysCC("lscc"))
67+
assert.False(t, IsSysCC("noSCC"))
68+
assert.True(t, (&sccProviderImpl{}).IsSysCC("lscc"))
69+
assert.False(t, (&sccProviderImpl{}).IsSysCC("noSCC"))
70+
}
71+
72+
func TestIsSysCCAndNotInvokableCC2CC(t *testing.T) {
73+
assert.False(t, IsSysCCAndNotInvokableCC2CC("lscc"))
74+
assert.True(t, IsSysCC("cscc"))
75+
assert.True(t, IsSysCCAndNotInvokableCC2CC("cscc"))
76+
assert.True(t, (&sccProviderImpl{}).IsSysCC("cscc"))
77+
assert.False(t, (&sccProviderImpl{}).IsSysCCAndNotInvokableCC2CC("lscc"))
78+
assert.True(t, (&sccProviderImpl{}).IsSysCCAndNotInvokableCC2CC("cscc"))
79+
}
80+
81+
func TestIsSysCCAndNotInvokableExternal(t *testing.T) {
82+
assert.False(t, IsSysCCAndNotInvokableExternal("cscc"))
83+
assert.True(t, IsSysCC("cscc"))
84+
assert.True(t, IsSysCCAndNotInvokableExternal("vscc"))
85+
assert.False(t, (&sccProviderImpl{}).IsSysCCAndNotInvokableExternal("cscc"))
86+
assert.True(t, (&sccProviderImpl{}).IsSysCC("cscc"))
87+
assert.True(t, (&sccProviderImpl{}).IsSysCCAndNotInvokableExternal("vscc"))
88+
}
89+
90+
func TestSccProviderImpl_GetQueryExecutorForLedger(t *testing.T) {
91+
qe, err := (&sccProviderImpl{}).GetQueryExecutorForLedger("")
92+
assert.Nil(t, qe)
93+
assert.Error(t, err)
94+
}
95+
96+
func TestMockRegisterAndResetSysCCs(t *testing.T) {
97+
orig := MockRegisterSysCCs([]*SystemChaincode{})
98+
assert.NotEmpty(t, orig)
99+
MockResetSysCCs(orig)
100+
assert.Equal(t, len(orig), len(systemChaincodes))
101+
}
102+
103+
func TestRegisterSysCC(t *testing.T) {
104+
err := RegisterSysCC(&SystemChaincode{
105+
Name: "lscc",
106+
Path: "path",
107+
Enabled: true,
108+
})
109+
assert.NoError(t, err)
110+
err = RegisterSysCC(&SystemChaincode{
111+
Name: "lscc",
112+
Path: "path",
113+
Enabled: true,
114+
})
115+
assert.Error(t, err)
116+
assert.Contains(t, "path already registered", err)
117+
}

0 commit comments

Comments
 (0)