Skip to content

Commit

Permalink
Refactor API chaining to remove UpdatedConfig and related types
Browse files Browse the repository at this point in the history
FAB-17744

Signed-off-by: Will Lahti <[email protected]>
  • Loading branch information
wlahti committed May 6, 2020
1 parent 2607873 commit 63b156d
Show file tree
Hide file tree
Showing 14 changed files with 372 additions and 613 deletions.
174 changes: 54 additions & 120 deletions configtx/application.go

Large diffs are not rendered by default.

60 changes: 27 additions & 33 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.UpdatedConfig().Application().Organization("Org1").AddAnchorPeer(newOrg1AnchorPeer)
err = c.Application().Organization("Org1").AddAnchorPeer(newOrg1AnchorPeer)
gt.Expect(err).NotTo(HaveOccurred())

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

gt.Expect(proto.Equal(c.UpdatedConfig().Config, 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.UpdatedConfig().Application().Organization("Org1").AddAnchorPeer(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.UpdatedConfig().Application().Organization("Org1").RemoveAnchorPeer(anchorPeer1)
err = applicationOrg1.RemoveAnchorPeer(anchorPeer1)
gt.Expect(err).NotTo(HaveOccurred())

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

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

c := New(config)

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

c := New(config)

anchorPeers, err := c.UpdatedConfig().Application().Organization("Org1").AnchorPeers()
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.UpdatedConfig().Application().Organization("Org1").AddAnchorPeer(expectedAnchorPeer)
err = c.Application().Organization("Org1").AddAnchorPeer(expectedAnchorPeer)
gt.Expect(err).NotTo(HaveOccurred())

anchorPeers, err = c.UpdatedConfig().Application().Organization("Org1").AnchorPeers()
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.UpdatedConfig().Application().Organization("Org1").RemoveAnchorPeer(expectedAnchorPeer)
err = c.Application().Organization("Org1").RemoveAnchorPeer(expectedAnchorPeer)
gt.Expect(err).NotTo(HaveOccurred())

anchorPeers, err = c.UpdatedConfig().Application().Organization("Org1").AnchorPeers()
anchorPeers, err = c.Application().Organization("Org1").AnchorPeers()
gt.Expect(err).NotTo(HaveOccurred())
gt.Expect(anchorPeers).To(BeNil())
gt.Expect(anchorPeers).To(HaveLen(0))
Expand Down Expand Up @@ -630,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.UpdatedConfig().Application().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 := c.UpdatedConfig().Application().ACLs()
acls, err := c.Application().ACLs()
gt.Expect(err).NotTo(HaveOccurred())
gt.Expect(acls).To(Equal(tt.expectedACL))

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

c := New(config)

err = c.UpdatedConfig().Application().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 := c.UpdatedConfig().Application().ACLs()
acls, err := c.Application().ACLs()
gt.Expect(err).NotTo(HaveOccurred())
gt.Expect(acls).To(Equal(tt.expectedACL))
}
Expand Down Expand Up @@ -888,10 +884,10 @@ func TestSetApplicationOrg(t *testing.T) {
}
`, certBase64, crlBase64)

err = c.UpdatedConfig().Application().SetOrganization(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 @@ -921,7 +917,7 @@ func TestSetApplicationOrgFailures(t *testing.T) {
Name: "Org3",
}

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

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

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

c = New(c.UpdatedConfig().Config)

applicationConfig, err := c.OriginalConfig().Application().Configuration()
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 @@ -971,7 +965,7 @@ func TestApplicationConfigurationFailure(t *testing.T) {
configMod: func(c ConfigTx, appOrg Application, gt *GomegaWithT) {
for _, org := range appOrg.Organizations {
if org.Name == "Org2" {
err := c.UpdatedConfig().Application().SetOrganization(org)
err := c.Application().SetOrganization(org)
gt.Expect(err).NotTo(HaveOccurred())
}
}
Expand Down Expand Up @@ -1004,9 +998,9 @@ func TestApplicationConfigurationFailure(t *testing.T) {
tt.configMod(c, baseApplicationConf, gt)
}

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

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

c := New(config)

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

c := New(config)

applicationACLs, err := c.UpdatedConfig().Application().ACLs()
applicationACLs, err := c.Application().ACLs()
gt.Expect(err).To(MatchError("unmarshaling ACLs: unexpected EOF"))
gt.Expect(applicationACLs).To(BeNil())
}
Expand Down
58 changes: 29 additions & 29 deletions configtx/capabilities_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,18 @@ func TestChannelCapabilities(t *testing.T) {
},
}

c := New(config)

err := setValue(config.ChannelGroup, capabilitiesValue(expectedCapabilities), AdminsPolicyKey)
gt.Expect(err).NotTo(HaveOccurred())

channelCapabilities, err := c.OriginalConfig().Channel().Capabilities()
c := New(config)

channelCapabilities, err := c.Channel().Capabilities()
gt.Expect(err).NotTo(HaveOccurred())
gt.Expect(channelCapabilities).To(Equal(expectedCapabilities))

// Delete the capabilities key and assert retrieval to return nil
delete(config.ChannelGroup.Values, CapabilitiesKey)
channelCapabilities, err = c.OriginalConfig().Channel().Capabilities()
delete(c.Channel().channelGroup.Values, CapabilitiesKey)
channelCapabilities, err = c.Channel().Capabilities()
gt.Expect(err).NotTo(HaveOccurred())
gt.Expect(channelCapabilities).To(BeNil())
}
Expand All @@ -67,13 +67,13 @@ func TestOrdererCapabilities(t *testing.T) {

c := New(config)

ordererCapabilities, err := c.OriginalConfig().Orderer().Capabilities()
ordererCapabilities, err := c.Orderer().Capabilities()
gt.Expect(err).NotTo(HaveOccurred())
gt.Expect(ordererCapabilities).To(Equal(baseOrdererConf.Capabilities))

// Delete the capabilities key and assert retrieval to return nil
delete(c.OriginalConfig().Orderer().ordererGroup.Values, CapabilitiesKey)
ordererCapabilities, err = c.OriginalConfig().Orderer().Capabilities()
delete(c.Orderer().ordererGroup.Values, CapabilitiesKey)
ordererCapabilities, err = c.Orderer().Capabilities()
gt.Expect(err).NotTo(HaveOccurred())
gt.Expect(ordererCapabilities).To(BeNil())
}
Expand All @@ -97,13 +97,13 @@ func TestApplicationCapabilities(t *testing.T) {

c := New(config)

applicationCapabilities, err := c.OriginalConfig().Application().Capabilities()
applicationCapabilities, err := c.Application().Capabilities()
gt.Expect(err).NotTo(HaveOccurred())
gt.Expect(applicationCapabilities).To(Equal(baseApplicationConf.Capabilities))

// Delete the capabilities key and assert retrieval to return nil
delete(config.ChannelGroup.Groups[ApplicationGroupKey].Values, CapabilitiesKey)
applicationCapabilities, err = c.OriginalConfig().Application().Capabilities()
delete(c.Application().applicationGroup.Values, CapabilitiesKey)
applicationCapabilities, err = c.Application().Capabilities()
gt.Expect(err).NotTo(HaveOccurred())
gt.Expect(applicationCapabilities).To(BeNil())
}
Expand Down Expand Up @@ -142,11 +142,11 @@ func TestSetChannelCapability(t *testing.T) {
}
`

err := c.UpdatedConfig().Channel().AddCapability("V3_0")
err := c.Channel().AddCapability("V3_0")
gt.Expect(err).NotTo(HaveOccurred())

buf := bytes.Buffer{}
err = protolator.DeepMarshalJSON(&buf, &commonext.DynamicChannelGroup{ConfigGroup: c.UpdatedConfig().ChannelGroup})
err = protolator.DeepMarshalJSON(&buf, &commonext.DynamicChannelGroup{ConfigGroup: c.Channel().channelGroup})
gt.Expect(err).NotTo(HaveOccurred())

gt.Expect(buf.String()).To(Equal(expectedConfigGroupJSON))
Expand Down Expand Up @@ -186,7 +186,7 @@ func TestSetChannelCapabilityFailures(t *testing.T) {

c := New(tt.config)

err := c.UpdatedConfig().Channel().AddCapability(tt.capability)
err := c.Channel().AddCapability(tt.capability)
gt.Expect(err).To(MatchError(tt.expectedErr))
})
}
Expand Down Expand Up @@ -431,11 +431,11 @@ func TestAddOrdererCapability(t *testing.T) {
`, orgCertBase64, orgCRLBase64)

capability := "V3_0"
err = c.UpdatedConfig().Orderer().AddCapability(capability)
err = c.Orderer().AddCapability(capability)
gt.Expect(err).NotTo(HaveOccurred())

buf := bytes.Buffer{}
err = protolator.DeepMarshalJSON(&buf, &ordererext.DynamicOrdererGroup{ConfigGroup: c.UpdatedConfig().Orderer().ordererGroup})
err = protolator.DeepMarshalJSON(&buf, &ordererext.DynamicOrdererGroup{ConfigGroup: c.Orderer().ordererGroup})
gt.Expect(err).NotTo(HaveOccurred())

gt.Expect(buf.String()).To(Equal(expectedConfigGroupJSON))
Expand Down Expand Up @@ -486,7 +486,7 @@ func TestAddOrdererCapabilityFailures(t *testing.T) {

c := New(config)

err = c.UpdatedConfig().Orderer().AddCapability(tt.capability)
err = c.Orderer().AddCapability(tt.capability)
gt.Expect(err).To(MatchError(tt.expectedErr))
})
}
Expand Down Expand Up @@ -690,14 +690,14 @@ func TestAddApplicationCapability(t *testing.T) {

c := New(config)

err = c.UpdatedConfig().Application().AddCapability(tt.capability)
err = c.Application().AddCapability(tt.capability)
gt.Expect(err).NotTo(HaveOccurred())

updatedApplicationGroupJSON := bytes.Buffer{}
err = protolator.DeepMarshalJSON(&updatedApplicationGroupJSON, &peerext.DynamicApplicationGroup{ConfigGroup: c.UpdatedConfig().ChannelGroup.Groups[ApplicationGroupKey]})
err = protolator.DeepMarshalJSON(&updatedApplicationGroupJSON, &peerext.DynamicApplicationGroup{ConfigGroup: c.Application().applicationGroup})
gt.Expect(err).NotTo(HaveOccurred())
originalApplicationGroupJSON := bytes.Buffer{}
err = protolator.DeepMarshalJSON(&originalApplicationGroupJSON, &peerext.DynamicApplicationGroup{ConfigGroup: c.OriginalConfig().ChannelGroup.Groups[ApplicationGroupKey]})
err = protolator.DeepMarshalJSON(&originalApplicationGroupJSON, &peerext.DynamicApplicationGroup{ConfigGroup: c.original.ChannelGroup.Groups[ApplicationGroupKey]})
gt.Expect(err).NotTo(HaveOccurred())

gt.Expect(updatedApplicationGroupJSON.String()).To(Equal(tt.expectedConfigGroupJSON))
Expand Down Expand Up @@ -755,7 +755,7 @@ func TestAddApplicationCapabilityFailures(t *testing.T) {

c := New(config)

err = c.UpdatedConfig().Application().AddCapability(tt.capability)
err = c.Application().AddCapability(tt.capability)
gt.Expect(err).To(MatchError(tt.expectedErr))
})
}
Expand Down Expand Up @@ -798,11 +798,11 @@ func TestRemoveChannelCapability(t *testing.T) {
}
`

err := c.UpdatedConfig().Channel().RemoveCapability("V3_0")
err := c.Channel().RemoveCapability("V3_0")
gt.Expect(err).NotTo(HaveOccurred())

buf := bytes.Buffer{}
err = protolator.DeepMarshalJSON(&buf, &commonext.DynamicChannelGroup{ConfigGroup: c.UpdatedConfig().ChannelGroup})
err = protolator.DeepMarshalJSON(&buf, &commonext.DynamicChannelGroup{ConfigGroup: c.Channel().channelGroup})
gt.Expect(err).NotTo(HaveOccurred())

gt.Expect(buf.String()).To(Equal(expectedConfigGroupJSON))
Expand Down Expand Up @@ -856,7 +856,7 @@ func TestRemoveChannelCapabilityFailures(t *testing.T) {

c := New(tt.config)

err := c.UpdatedConfig().Channel().RemoveCapability(tt.capability)
err := c.Channel().RemoveCapability(tt.capability)
gt.Expect(err).To(MatchError(tt.expectedErr))
})
}
Expand Down Expand Up @@ -1098,11 +1098,11 @@ func TestRemoveOrdererCapability(t *testing.T) {
`, orgCertBase64, orgCRLBase64)

capability := "V1_3"
err = c.UpdatedConfig().Orderer().RemoveCapability(capability)
err = c.Orderer().RemoveCapability(capability)
gt.Expect(err).NotTo(HaveOccurred())

buf := bytes.Buffer{}
err = protolator.DeepMarshalJSON(&buf, &ordererext.DynamicOrdererGroup{ConfigGroup: c.UpdatedConfig().Orderer().ordererGroup})
err = protolator.DeepMarshalJSON(&buf, &ordererext.DynamicOrdererGroup{ConfigGroup: c.Orderer().ordererGroup})
gt.Expect(err).NotTo(HaveOccurred())

gt.Expect(buf.String()).To(Equal(expectedConfigGroupJSON))
Expand Down Expand Up @@ -1160,7 +1160,7 @@ func TestRemoveOrdererCapabilityFailures(t *testing.T) {

c := New(config)

err = c.UpdatedConfig().Orderer().RemoveCapability(tt.capability)
err = c.Orderer().RemoveCapability(tt.capability)
gt.Expect(err).To(MatchError(tt.expectedErr))
})
}
Expand Down Expand Up @@ -1262,7 +1262,7 @@ func TestRemoveApplicationCapability(t *testing.T) {
}
`
capability := "V1_3"
err = c.UpdatedConfig().Application().RemoveCapability(capability)
err = c.Application().RemoveCapability(capability)
gt.Expect(err).NotTo(HaveOccurred())

buf := bytes.Buffer{}
Expand Down Expand Up @@ -1324,7 +1324,7 @@ func TestRemoveApplicationCapabilityFailures(t *testing.T) {

c := New(config)

err = c.UpdatedConfig().Application().RemoveCapability(tt.capability)
err = c.Application().RemoveCapability(tt.capability)
gt.Expect(err).To(MatchError(tt.expectedErr))
})
}
Expand Down
Loading

0 comments on commit 63b156d

Please sign in to comment.