Skip to content

Commit 844eb7b

Browse files
committed
Improved test coverage for msp/mgmt
This change-set improves the test coverage of the msp/mgmt package Change-Id: Iee1a5b57ce5175adee315d9c7a6041cb1cb33aac Signed-off-by: Angelo De Caro <[email protected]>
1 parent d661d11 commit 844eb7b

File tree

3 files changed

+141
-1
lines changed

3 files changed

+141
-1
lines changed

msp/mgmt/deserializer_test.go

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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 mgmt
18+
19+
import (
20+
"fmt"
21+
"os"
22+
"testing"
23+
24+
msp2 "github.com/hyperledger/fabric/common/config/msp"
25+
"github.com/hyperledger/fabric/msp"
26+
"github.com/stretchr/testify/assert"
27+
)
28+
29+
func TestNewDeserializersManager(t *testing.T) {
30+
assert.NotNil(t, NewDeserializersManager())
31+
}
32+
33+
func TestMspDeserializersManager_Deserialize(t *testing.T) {
34+
m := NewDeserializersManager()
35+
36+
i, err := GetLocalMSP().GetDefaultSigningIdentity()
37+
assert.NoError(t, err)
38+
raw, err := i.Serialize()
39+
assert.NoError(t, err)
40+
41+
i2, err := m.Deserialize(raw)
42+
assert.NoError(t, err)
43+
assert.NotNil(t, i2)
44+
assert.NotNil(t, i2.IdBytes)
45+
assert.Equal(t, m.GetLocalMSPIdentifier(), i2.Mspid)
46+
}
47+
48+
func TestMspDeserializersManager_GetChannelDeserializers(t *testing.T) {
49+
m := NewDeserializersManager()
50+
51+
deserializers := m.GetChannelDeserializers()
52+
assert.NotNil(t, deserializers)
53+
}
54+
55+
func TestMspDeserializersManager_GetLocalDeserializer(t *testing.T) {
56+
m := NewDeserializersManager()
57+
58+
i, err := GetLocalMSP().GetDefaultSigningIdentity()
59+
assert.NoError(t, err)
60+
raw, err := i.Serialize()
61+
assert.NoError(t, err)
62+
63+
i2, err := m.GetLocalDeserializer().DeserializeIdentity(raw)
64+
assert.NoError(t, err)
65+
assert.NotNil(t, i2)
66+
assert.Equal(t, m.GetLocalMSPIdentifier(), i2.GetMSPIdentifier())
67+
}
68+
69+
func TestMain(m *testing.M) {
70+
var err error
71+
testConf, err := msp.GetLocalMspConfig("../sampleconfig/", nil, "DEFAULT")
72+
if err != nil {
73+
fmt.Printf("Setup should have succeeded, got err %s instead", err)
74+
os.Exit(-1)
75+
}
76+
77+
err = GetLocalMSP().Setup(testConf)
78+
if err != nil {
79+
fmt.Printf("Setup for msp should have succeeded, got err %s instead", err)
80+
os.Exit(-1)
81+
}
82+
83+
XXXSetMSPManager("foo", &msp2.MSPConfigHandler{MSPManager: msp.NewMSPManager()})
84+
retVal := m.Run()
85+
os.Exit(retVal)
86+
}

msp/mgmt/principal.go

-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ func (m *localMSPPrincipalGetter) Get(role string) (*msp.MSPPrincipal, error) {
4848
return nil, fmt.Errorf("Could not extract local msp identifier [%s]", err)
4949
}
5050

51-
// TODO: put the constants in some more appropriate place
5251
switch role {
5352
case Admins:
5453
principalBytes, err := proto.Marshal(&msp.MSPRole{Role: msp.MSPRole_ADMIN, MspIdentifier: mspid})

msp/mgmt/principal_test.go

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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 mgmt
18+
19+
import (
20+
"testing"
21+
22+
"github.com/golang/protobuf/proto"
23+
"github.com/hyperledger/fabric/protos/msp"
24+
"github.com/stretchr/testify/assert"
25+
)
26+
27+
func TestNewLocalMSPPrincipalGetter(t *testing.T) {
28+
assert.NotNil(t, NewLocalMSPPrincipalGetter())
29+
}
30+
31+
func TestLocalMSPPrincipalGetter_Get(t *testing.T) {
32+
m := NewDeserializersManager()
33+
g := NewLocalMSPPrincipalGetter()
34+
35+
_, err := g.Get("")
36+
assert.Error(t, err)
37+
38+
p, err := g.Get(Admins)
39+
assert.NoError(t, err)
40+
assert.NotNil(t, p)
41+
assert.Equal(t, msp.MSPPrincipal_ROLE, p.PrincipalClassification)
42+
role := &msp.MSPRole{}
43+
proto.Unmarshal(p.Principal, role)
44+
assert.Equal(t, m.GetLocalMSPIdentifier(), role.MspIdentifier)
45+
assert.Equal(t, msp.MSPRole_ADMIN, role.Role)
46+
47+
p, err = g.Get(Members)
48+
assert.NoError(t, err)
49+
assert.NotNil(t, p)
50+
assert.Equal(t, msp.MSPPrincipal_ROLE, p.PrincipalClassification)
51+
role = &msp.MSPRole{}
52+
proto.Unmarshal(p.Principal, role)
53+
assert.Equal(t, m.GetLocalMSPIdentifier(), role.MspIdentifier)
54+
assert.Equal(t, msp.MSPRole_MEMBER, role.Role)
55+
}

0 commit comments

Comments
 (0)