Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chain APIs for orderer, application, consortium, and channel operations #11

Merged
merged 6 commits into from
May 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
364 changes: 269 additions & 95 deletions configtx/application.go

Large diffs are not rendered by default.

124 changes: 28 additions & 96 deletions configtx/application_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,13 +356,13 @@ func TestAddAnchorPeer(t *testing.T) {
err = protolator.DeepUnmarshalJSON(bytes.NewBufferString(expectedUpdatedConfigJSON), expectedUpdatedConfig)
gt.Expect(err).ToNot(HaveOccurred())

err = c.AddAnchorPeer("Org1", newOrg1AnchorPeer)
err = c.Application().Organization("Org1").AddAnchorPeer(newOrg1AnchorPeer)
gt.Expect(err).NotTo(HaveOccurred())

err = c.AddAnchorPeer("Org2", newOrg2AnchorPeer)
err = c.Application().Organization("Org2").AddAnchorPeer(newOrg2AnchorPeer)
gt.Expect(err).NotTo(HaveOccurred())

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

func TestRemoveAnchorPeer(t *testing.T) {
Expand Down Expand Up @@ -485,17 +485,18 @@ func TestRemoveAnchorPeer(t *testing.T) {
`

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

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

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

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

func TestRemoveAnchorPeerFailure(t *testing.T) {
Expand All @@ -513,7 +514,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 anchor peer endpoints for org Org1: proto: can't skip unknown wire type 6",
expectedErr: "failed unmarshaling anchor peer endpoints for application org Org1: proto: can't skip unknown wire type 6",
},
}

Expand Down Expand Up @@ -541,7 +542,7 @@ func TestRemoveAnchorPeerFailure(t *testing.T) {

c := New(config)

err = c.RemoveAnchorPeer(tt.orgName, tt.anchorPeerToRemove)
err = c.Application().Organization(tt.orgName).RemoveAnchorPeer(tt.anchorPeerToRemove)
gt.Expect(err).To(MatchError(tt.expectedErr))
})
}
Expand All @@ -565,84 +566,29 @@ func TestAnchorPeers(t *testing.T) {

c := New(config)

anchorPeers, err := c.AnchorPeers("Org1")
anchorPeers, err := c.Application().Organization("Org1").AnchorPeers()
gt.Expect(err).NotTo(HaveOccurred())
gt.Expect(anchorPeers).To(BeNil())
gt.Expect(anchorPeers).To(HaveLen(0))

expectedAnchorPeer := Address{Host: "host1", Port: 123}
err = c.AddAnchorPeer("Org1", expectedAnchorPeer)
err = c.Application().Organization("Org1").AddAnchorPeer(expectedAnchorPeer)
gt.Expect(err).NotTo(HaveOccurred())

c = New(c.UpdatedConfig())

anchorPeers, err = c.AnchorPeers("Org1")
anchorPeers, err = c.Application().Organization("Org1").AnchorPeers()
gt.Expect(err).NotTo(HaveOccurred())
gt.Expect(anchorPeers).To(HaveLen(1))
gt.Expect(anchorPeers[0]).To(Equal(expectedAnchorPeer))

err = c.RemoveAnchorPeer("Org1", expectedAnchorPeer)
err = c.Application().Organization("Org1").RemoveAnchorPeer(expectedAnchorPeer)
gt.Expect(err).NotTo(HaveOccurred())

// create new configtx with the updated config to test final anchor peers
// to ensure a nil slice is returned when all anchor peers are removed
c = New(c.UpdatedConfig())

anchorPeers, err = c.AnchorPeers("Org1")
anchorPeers, err = c.Application().Organization("Org1").AnchorPeers()
gt.Expect(err).NotTo(HaveOccurred())
gt.Expect(anchorPeers).To(BeNil())
gt.Expect(anchorPeers).To(HaveLen(0))
}

func TestAnchorPeerFailures(t *testing.T) {
t.Parallel()

gt := NewGomegaWithT(t)

channelGroup := newConfigGroup()

application, _ := baseApplication(t)
applicationGroup, err := newApplicationGroup(application)
gt.Expect(err).NotTo(HaveOccurred())

baseMSP, _ := baseMSP(t)
orgNoAnchor := Organization{
Name: "Org1",
Policies: applicationOrgStandardPolicies(),
MSP: baseMSP,
}
orgGroup, err := newOrgConfigGroup(orgNoAnchor)
gt.Expect(err).NotTo(HaveOccurred())
applicationGroup.Groups[orgNoAnchor.Name] = orgGroup

channelGroup.Groups[ApplicationGroupKey] = applicationGroup
config := &cb.Config{
ChannelGroup: channelGroup,
}

c := New(config)

for _, test := range []struct {
name string
orgName string
expectedErr string
}{
{
name: "When org does not exist in application channel",
orgName: "bad-org",
expectedErr: "application org bad-org does not exist in channel config",
},
} {
test := test
t.Run(test.name, func(t *testing.T) {
t.Parallel()
gt := NewGomegaWithT(t)
_, err := c.AnchorPeers(test.orgName)
gt.Expect(err).To(MatchError(test.expectedErr))
})
}
}

func TestSetACL(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -685,24 +631,19 @@ func TestSetACL(t *testing.T) {
config := &cb.Config{
ChannelGroup: channelGroup,
}
expectedOriginalACL := map[string]string{"acl1": "hi"}
if tt.configMod != nil {
tt.configMod(config)
}
c := New(config)

err = c.SetACLs(tt.newACL)
err = c.Application().SetACLs(tt.newACL)
if tt.expectedErr != "" {
gt.Expect(err).To(MatchError(tt.expectedErr))
} else {
gt.Expect(err).NotTo(HaveOccurred())
acls, err := getACLs(c.updated)
acls, err := c.Application().ACLs()
gt.Expect(err).NotTo(HaveOccurred())
gt.Expect(acls).To(Equal(tt.expectedACL))

originalACLs, err := getACLs(c.original)
gt.Expect(err).NotTo(HaveOccurred())
gt.Expect(originalACLs).To(Equal(expectedOriginalACL))
}
})
}
Expand Down Expand Up @@ -760,12 +701,12 @@ func TestRemoveACL(t *testing.T) {

c := New(config)

err = c.RemoveACLs(tt.removeACL)
err = c.Application().RemoveACLs(tt.removeACL)
if tt.expectedErr != "" {
gt.Expect(err).To(MatchError(tt.expectedErr))
} else {
gt.Expect(err).NotTo(HaveOccurred())
acls, err := getACLs(c.UpdatedConfig())
acls, err := c.Application().ACLs()
gt.Expect(err).NotTo(HaveOccurred())
gt.Expect(acls).To(Equal(tt.expectedACL))
}
Expand Down Expand Up @@ -943,10 +884,10 @@ func TestSetApplicationOrg(t *testing.T) {
}
`, certBase64, crlBase64)

err = c.SetApplicationOrg(org)
err = c.Application().SetOrganization(org)
gt.Expect(err).NotTo(HaveOccurred())

actualApplicationConfigGroup := c.UpdatedConfig().ChannelGroup.Groups[ApplicationGroupKey].Groups["Org3"]
actualApplicationConfigGroup := c.Application().Organization("Org3").orgGroup
buf := bytes.Buffer{}
err = protolator.DeepMarshalJSON(&buf, &peerext.DynamicApplicationOrgGroup{ConfigGroup: actualApplicationConfigGroup})
gt.Expect(err).NotTo(HaveOccurred())
Expand Down Expand Up @@ -976,7 +917,7 @@ func TestSetApplicationOrgFailures(t *testing.T) {
Name: "Org3",
}

err = c.SetApplicationOrg(org)
err = c.Application().SetOrganization(org)
gt.Expect(err).To(MatchError("failed to create application org Org3: no policies defined"))
}

Expand All @@ -999,13 +940,11 @@ func TestApplicationConfiguration(t *testing.T) {
c := New(config)

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

c = New(c.UpdatedConfig())

applicationConfig, err := c.ApplicationConfiguration()
applicationConfig, err := c.Application().Configuration()
gt.Expect(err).NotTo(HaveOccurred())
gt.Expect(applicationConfig.ACLs).To(Equal(baseApplicationConf.ACLs))
gt.Expect(applicationConfig.Capabilities).To(Equal(baseApplicationConf.Capabilities))
Expand All @@ -1021,19 +960,12 @@ func TestApplicationConfigurationFailure(t *testing.T) {
configMod func(ConfigTx, Application, *GomegaWithT)
expectedErr string
}{
{
testName: "When the application group does not exist",
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(c ConfigTx, appOrg Application, gt *GomegaWithT) {
for _, org := range appOrg.Organizations {
if org.Name == "Org2" {
err := c.SetApplicationOrg(org)
err := c.Application().SetOrganization(org)
gt.Expect(err).NotTo(HaveOccurred())
}
}
Expand Down Expand Up @@ -1066,9 +998,9 @@ func TestApplicationConfigurationFailure(t *testing.T) {
tt.configMod(c, baseApplicationConf, gt)
}

c = New(c.UpdatedConfig())
c = New(c.updated)

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

c := New(config)

applicationACLs, err := c.ApplicationACLs()
applicationACLs, err := c.Application().ACLs()
gt.Expect(err).NotTo(HaveOccurred())
gt.Expect(applicationACLs).To(Equal(baseApplicationConf.ACLs))
}
Expand Down Expand Up @@ -1121,7 +1053,7 @@ func TestApplicationACLsFailure(t *testing.T) {

c := New(config)

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