Skip to content

Commit

Permalink
Update pkg/configtx tests to use New to create ConfigTx (hyperledger#…
Browse files Browse the repository at this point in the history
…1143)

And some miscellaneous cleanup elsewhere.

FAB-17775 #done

Signed-off-by: Will Lahti <[email protected]>
  • Loading branch information
wlahti authored Apr 22, 2020
1 parent 27f9ff2 commit fe7cbc6
Show file tree
Hide file tree
Showing 10 changed files with 249 additions and 391 deletions.
4 changes: 2 additions & 2 deletions pkg/configtx/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (c *ConfigTx) AddAnchorPeer(orgName string, newAnchorPeer Address) error {
// Unmarshal existing anchor peers if the config value exists
err := proto.Unmarshal(anchorPeerConfigValue.Value, anchorPeersProto)
if err != nil {
return fmt.Errorf("failed unmarshaling %s's anchor peer endpoints: %v", orgName, err)
return fmt.Errorf("failed unmarshaling anchor peer endpoints for org %s: %v", orgName, err)
}
}

Expand Down Expand Up @@ -118,7 +118,7 @@ func (c *ConfigTx) RemoveAnchorPeer(orgName string, anchorPeerToRemove Address)
// Unmarshal existing anchor peers if the config value exists
err := proto.Unmarshal(anchorPeerConfigValue.Value, anchorPeersProto)
if err != nil {
return fmt.Errorf("failed unmarshaling %s's anchor peer endpoints: %v", orgName, err)
return fmt.Errorf("failed unmarshaling anchor peer endpoints for org %s: %v", orgName, err)
}
}

Expand Down
97 changes: 37 additions & 60 deletions pkg/configtx/application_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"fmt"
"testing"

"github.com/golang/protobuf/proto"
cb "github.com/hyperledger/fabric-protos-go/common"
"github.com/hyperledger/fabric/common/tools/protolator"
"github.com/hyperledger/fabric/common/tools/protolator/protoext/peerext"
Expand Down Expand Up @@ -91,7 +92,8 @@ func TestNewApplicationGroup(t *testing.T) {
}
},
"version": "0"
}`
}
`

applicationGroup, err := newApplicationGroup(application)
gt.Expect(err).NotTo(HaveOccurred())
Expand Down Expand Up @@ -219,10 +221,7 @@ func TestAddAnchorPeer(t *testing.T) {
},
}

c := ConfigTx{
original: config,
updated: config,
}
c := New(config)

newOrg1AnchorPeer := Address{
Host: "host3",
Expand Down Expand Up @@ -349,7 +348,7 @@ func TestAddAnchorPeer(t *testing.T) {
},
"sequence": "0"
}
`
`

expectedUpdatedConfig := &cb.Config{}

Expand All @@ -362,7 +361,7 @@ func TestAddAnchorPeer(t *testing.T) {
err = c.AddAnchorPeer("Org2", newOrg2AnchorPeer)
gt.Expect(err).NotTo(HaveOccurred())

gt.Expect(config).To(Equal(expectedUpdatedConfig))
gt.Expect(proto.Equal(c.UpdatedConfig(), expectedUpdatedConfig)).To(BeTrue())
}

func TestRemoveAnchorPeer(t *testing.T) {
Expand All @@ -385,10 +384,7 @@ func TestRemoveAnchorPeer(t *testing.T) {
},
}

c := ConfigTx{
original: config,
updated: config,
}
c := New(config)

expectedUpdatedConfigJSON := `
{
Expand Down Expand Up @@ -485,19 +481,20 @@ func TestRemoveAnchorPeer(t *testing.T) {
},
"sequence": "0"
}
`
`

anchorPeer1 := Address{Host: "host1", Port: 123}
err = c.AddAnchorPeer("Org1", anchorPeer1)
gt.Expect(err).NotTo(HaveOccurred())
expectedUpdatedConfig := &cb.Config{}

err = protolator.DeepUnmarshalJSON(bytes.NewBufferString(expectedUpdatedConfigJSON), expectedUpdatedConfig)
gt.Expect(err).ToNot(HaveOccurred())
gt.Expect(err).NotTo(HaveOccurred())

err = c.RemoveAnchorPeer("Org1", anchorPeer1)
gt.Expect(err).NotTo(HaveOccurred())

gt.Expect(config).To(Equal(expectedUpdatedConfig))
gt.Expect(proto.Equal(c.UpdatedConfig(), expectedUpdatedConfig)).To(BeTrue())
}

func TestRemoveAnchorPeerFailure(t *testing.T) {
Expand All @@ -515,7 +512,7 @@ func TestRemoveAnchorPeerFailure(t *testing.T) {
orgName: "Org1",
anchorPeerToRemove: Address{Host: "host1", Port: 123},
configValues: map[string]*cb.ConfigValue{AnchorPeersKey: {Value: []byte("a little fire")}},
expectedErr: "failed unmarshaling Org1's anchor peer endpoints: proto: can't skip unknown wire type 6",
expectedErr: "failed unmarshaling anchor peer endpoints for org Org1: proto: can't skip unknown wire type 6",
},
}

Expand All @@ -541,10 +538,7 @@ func TestRemoveAnchorPeerFailure(t *testing.T) {
},
}

c := ConfigTx{
original: config,
updated: config,
}
c := New(config)

err = c.RemoveAnchorPeer(tt.orgName, tt.anchorPeerToRemove)
gt.Expect(err).To(MatchError(tt.expectedErr))
Expand All @@ -567,10 +561,7 @@ func TestAnchorPeers(t *testing.T) {
ChannelGroup: channelGroup,
}

c := ConfigTx{
original: config,
updated: config,
}
c := New(config)

anchorPeers, err := c.AnchorPeers("Org1")
gt.Expect(err).NotTo(HaveOccurred())
Expand All @@ -581,6 +572,8 @@ func TestAnchorPeers(t *testing.T) {
err = c.AddAnchorPeer("Org1", expectedAnchorPeer)
gt.Expect(err).NotTo(HaveOccurred())

c = New(c.UpdatedConfig())

anchorPeers, err = c.AnchorPeers("Org1")
gt.Expect(err).NotTo(HaveOccurred())
gt.Expect(anchorPeers).To(HaveLen(1))
Expand Down Expand Up @@ -623,10 +616,7 @@ func TestAnchorPeerFailures(t *testing.T) {
ChannelGroup: channelGroup,
}

c := ConfigTx{
original: config,
updated: config,
}
c := New(config)

for _, test := range []struct {
name string
Expand Down Expand Up @@ -763,17 +753,15 @@ func TestRemoveACL(t *testing.T) {
if tt.configMod != nil {
tt.configMod(config)
}
c := &ConfigTx{
original: config,
updated: config,
}

c := New(config)

err = c.RemoveACLs(tt.removeACL)
if tt.expectedErr != "" {
gt.Expect(err).To(MatchError(tt.expectedErr))
} else {
gt.Expect(err).NotTo(HaveOccurred())
acls, err := getACLs(config)
acls, err := getACLs(c.UpdatedConfig())
gt.Expect(err).NotTo(HaveOccurred())
gt.Expect(acls).To(Equal(tt.expectedACL))
}
Expand All @@ -797,10 +785,7 @@ func TestAddApplicationOrg(t *testing.T) {
},
}

c := ConfigTx{
original: config,
updated: config,
}
c := New(config)

org := Organization{
Name: "Org3",
Expand Down Expand Up @@ -961,7 +946,7 @@ func TestAddApplicationOrg(t *testing.T) {
err = c.AddApplicationOrg(org)
gt.Expect(err).NotTo(HaveOccurred())

actualApplicationConfigGroup := config.ChannelGroup.Groups[ApplicationGroupKey].Groups["Org3"]
actualApplicationConfigGroup := c.UpdatedConfig().ChannelGroup.Groups[ApplicationGroupKey].Groups["Org3"]
buf := bytes.Buffer{}
err = protolator.DeepMarshalJSON(&buf, &peerext.DynamicApplicationOrgGroup{ConfigGroup: actualApplicationConfigGroup})
gt.Expect(err).NotTo(HaveOccurred())
Expand All @@ -984,10 +969,7 @@ func TestAddApplicationOrgFailures(t *testing.T) {
},
}

c := ConfigTx{
original: config,
updated: config,
}
c := New(config)

org := Organization{
Name: "Org3",
Expand All @@ -1013,15 +995,15 @@ func TestApplicationConfiguration(t *testing.T) {
},
}

c := &ConfigTx{
original: config,
updated: config,
}
c := New(config)

for _, org := range baseApplicationConf.Organizations {
err = c.AddApplicationOrg(org)
gt.Expect(err).NotTo(HaveOccurred())
}

c = New(c.UpdatedConfig())

applicationConfig, err := c.ApplicationConfiguration()
gt.Expect(err).NotTo(HaveOccurred())
gt.Expect(applicationConfig.ACLs).To(Equal(baseApplicationConf.ACLs))
Expand All @@ -1035,22 +1017,22 @@ func TestApplicationConfigurationFailure(t *testing.T) {

tests := []struct {
testName string
configMod func(*ConfigTx, Application, *GomegaWithT)
configMod func(ConfigTx, Application, *GomegaWithT)
expectedErr string
}{
{
testName: "When the application group does not exist",
configMod: func(ct *ConfigTx, appOrg Application, gt *GomegaWithT) {
delete(ct.original.ChannelGroup.Groups, ApplicationGroupKey)
configMod: func(c ConfigTx, appOrg Application, gt *GomegaWithT) {
delete(c.UpdatedConfig().ChannelGroup.Groups, ApplicationGroupKey)
},
expectedErr: "config does not contain application group",
},
{
testName: "Retrieving application org failed",
configMod: func(ct *ConfigTx, appOrg Application, gt *GomegaWithT) {
configMod: func(c ConfigTx, appOrg Application, gt *GomegaWithT) {
for _, org := range appOrg.Organizations {
if org.Name == "Org2" {
err := ct.AddApplicationOrg(org)
err := c.AddApplicationOrg(org)
gt.Expect(err).NotTo(HaveOccurred())
}
}
Expand Down Expand Up @@ -1078,14 +1060,13 @@ func TestApplicationConfigurationFailure(t *testing.T) {
},
}

c := &ConfigTx{
original: config,
updated: config,
}
c := New(config)
if tt.configMod != nil {
tt.configMod(c, baseApplicationConf, gt)
}

c = New(c.UpdatedConfig())

_, err = c.ApplicationConfiguration()
gt.Expect(err).To(MatchError(tt.expectedErr))
})
Expand All @@ -1109,9 +1090,7 @@ func TestApplicationACLs(t *testing.T) {
},
}

c := &ConfigTx{
original: config,
}
c := New(config)

applicationACLs, err := c.ApplicationACLs()
gt.Expect(err).NotTo(HaveOccurred())
Expand Down Expand Up @@ -1139,9 +1118,7 @@ func TestApplicationACLsFailure(t *testing.T) {
Value: []byte("another little fire"),
}

c := &ConfigTx{
original: config,
}
c := New(config)

applicationACLs, err := c.ApplicationACLs()
gt.Expect(err).To(MatchError("unmarshaling ACLs: unexpected EOF"))
Expand Down
Loading

0 comments on commit fe7cbc6

Please sign in to comment.