Skip to content

Commit

Permalink
Fix more tests
Browse files Browse the repository at this point in the history
Signed-off-by: May Rosenbaum <[email protected]>
  • Loading branch information
MayRosenbaum committed Apr 14, 2024
1 parent 5c1219a commit 9f6ed31
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 77 deletions.
2 changes: 1 addition & 1 deletion common/configtx/test/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func MakeChannelConfig(channelID string) (*cb.Config, error) {
// MakeGenesisBlockFromMSPs creates a genesis block using the MSPs provided for the given channelID
func MakeGenesisBlockFromMSPs(channelID string, appMSPConf, ordererMSPConf *mspproto.MSPConfig, appOrgID, ordererOrgID string) (*cb.Block, error) {
profile := genesisconfig.Load(genesisconfig.SampleDevModeSoloProfile, configtest.GetDevConfigDir())
profile.Orderer.Organizations = nil

channelGroup, err := encoder.NewChannelGroup(profile)
if err != nil {
logger.Panicf("Error creating channel config: %s", err)
Expand Down
70 changes: 1 addition & 69 deletions integration/lifecycle/lifecycle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ package lifecycle

import (
"bytes"
"fmt"
"os"
"path/filepath"
"syscall"
Expand All @@ -23,8 +22,6 @@ import (
"github.com/hyperledger/fabric/integration/nwo/commands"
"github.com/hyperledger/fabric/integration/nwo/fabricconfig"
"github.com/hyperledger/fabric/integration/nwo/runner"
"github.com/hyperledger/fabric/internal/configtxlator/update"
"github.com/hyperledger/fabric/protoutil"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gbytes"
Expand Down Expand Up @@ -313,20 +310,7 @@ var _ = Describe("Lifecycle", func() {

// update the channel config to include org3
updatedConfig.ChannelGroup.Groups["Application"].Groups["Org3"] = org3Group.ConfigGroup
ordererOrg3Endpoint := &common.OrdererAddresses{
Addresses: []string{"127.0.0.1:7050"},
}
updatedConfig.ChannelGroup.Groups["Orderer"].Groups["Org3"] = &common.ConfigGroup{
Values: map[string]*common.ConfigValue{
"Endpoints": {
Value: protoutil.MarshalOrPanic(ordererOrg3Endpoint),
ModPolicy: "/Channel/Application/Writers",
},
},
ModPolicy: "/Channel/Application/Writers",
}
fmt.Printf("Lifecycle: org3 info: %v", updatedConfig.ChannelGroup.Groups["Orderer"].Groups["Org3"])
updateConfigSucceeds(network, orderer, "testchannel", currentConfig, updatedConfig, testPeers[0], testPeers...)
nwo.UpdateConfig(network, orderer, "testchannel", currentConfig, updatedConfig, true, testPeers[0], testPeers...)

By("joining the org3 peers to the channel")
network.JoinChannel("testchannel", orderer, org3peer0)
Expand Down Expand Up @@ -420,55 +404,3 @@ var _ = Describe("Lifecycle", func() {
Expect(sess.Err).To(gbytes.Say(`Chaincode invoke successful. result: status:200`))
})
})

func updateConfigSucceeds(n *nwo.Network, orderer *nwo.Orderer, channel string, current, updated *common.Config, peer *nwo.Peer, additionalSigners ...*nwo.Peer) {
tempDir, err := os.MkdirTemp("", "updateConfig")
Expect(err).NotTo(HaveOccurred())
defer os.RemoveAll(tempDir)

// compute update
configUpdate, err := update.Compute(current, updated)
Expect(err).NotTo(HaveOccurred())
configUpdate.ChannelId = channel

signedEnvelope, err := protoutil.CreateSignedEnvelope(
common.HeaderType_CONFIG_UPDATE,
channel,
nil, // local signer
&common.ConfigUpdateEnvelope{ConfigUpdate: protoutil.MarshalOrPanic(configUpdate)},
0, // message version
0, // epoch
)
Expect(err).NotTo(HaveOccurred())
Expect(signedEnvelope).NotTo(BeNil())

updateFile := filepath.Join(tempDir, "update.pb")
err = os.WriteFile(updateFile, protoutil.MarshalOrPanic(signedEnvelope), 0o600)
Expect(err).NotTo(HaveOccurred())

for _, signer := range additionalSigners {
sess, err := n.PeerAdminSession(signer, commands.SignConfigTx{
File: updateFile,
ClientAuth: n.ClientAuthRequired,
})
Expect(err).NotTo(HaveOccurred())
Eventually(sess, n.EventuallyTimeout).Should(gexec.Exit(0))
}

sess, err := n.OrdererAdminSession(orderer, peer, commands.SignConfigTx{
File: updateFile,
ClientAuth: n.ClientAuthRequired,
})
Expect(err).NotTo(HaveOccurred())
Eventually(sess, n.EventuallyTimeout).Should(gexec.Exit(0))

sess, err = n.OrdererAdminSession(orderer, peer, commands.ChannelUpdate{
ChannelID: channel,
Orderer: n.OrdererAddress(orderer, nwo.ListenPort),
File: updateFile,
ClientAuth: n.ClientAuthRequired,
})
Expect(err).NotTo(HaveOccurred())
Eventually(sess, n.EventuallyTimeout).Should(gexec.Exit(0))
Expect(sess.Err).To(gbytes.Say("Successfully submitted channel update"))
}
1 change: 1 addition & 0 deletions internal/peer/common/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ func TestGetOrdererEndpointFromConfigTx(t *testing.T) {

t.Run("green-path", func(t *testing.T) {
profile := genesisconfig.Load(genesisconfig.SampleInsecureSoloProfile, configtest.GetDevConfigDir())
profile.Capabilities = map[string]bool{"V_2": true}
channelGroup, err := encoder.NewChannelGroup(profile)
require.NoError(t, err)
channelConfig := &cb.Config{ChannelGroup: channelGroup}
Expand Down
77 changes: 73 additions & 4 deletions orderer/common/cluster/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ import (
"errors"
"fmt"
"math"
"os"
"os/exec"
"path"
"path/filepath"
"strings"
"sync"
"testing"
Expand All @@ -40,6 +44,7 @@ import (
"github.com/hyperledger/fabric/orderer/common/cluster"
"github.com/hyperledger/fabric/orderer/common/cluster/mocks"
"github.com/hyperledger/fabric/protoutil"
"github.com/onsi/gomega/gexec"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -351,13 +356,13 @@ func injectGlobalOrdererEndpoint(t *testing.T, block *common.Block, endpoint str
Value: protoutil.MarshalOrPanic(ordererAddresses.Value()),
ModPolicy: "/Channel/Orderer/Admins",
}
// Remove the per org addresses, if applicable
// Update the per org addresses
ordererGrps := confEnv.Config.ChannelGroup.Groups[channelconfig.OrdererGroupKey].Groups
for _, grp := range ordererGrps {
if grp.Values[channelconfig.EndpointsKey] == nil {
continue
}
grp.Values[channelconfig.EndpointsKey].Value = nil
grp.Values[channelconfig.EndpointsKey].Value = protoutil.MarshalOrPanic(ordererAddresses.Value())
}
// And put it back into the block
payload.Data = protoutil.MarshalOrPanic(confEnv)
Expand Down Expand Up @@ -470,7 +475,32 @@ func TestEndpointconfigFromConfigBlockFailures(t *testing.T) {
}

func TestBlockValidationPolicyVerifier(t *testing.T) {
dir := filepath.Join(os.TempDir())
os.Mkdir(dir, 0o700)
defer os.RemoveAll(dir)

cryptogen, err := gexec.Build("github.com/hyperledger/fabric/cmd/cryptogen")
require.NoError(t, err)
defer os.Remove(cryptogen)

cryptoConfigDir := filepath.Join(dir, "crypto-config")
b, err := exec.Command(cryptogen, "generate", fmt.Sprintf("--output=%s", cryptoConfigDir)).CombinedOutput()
require.NoError(t, err, string(b))

config := genesisconfig.Load(genesisconfig.SampleInsecureSoloProfile, configtest.GetDevConfigDir())
config.Orderer.Organizations = append(config.Orderer.Organizations, &genesisconfig.Organization{
MSPDir: filepath.Join(cryptoConfigDir, "ordererOrganizations", "example.com", "msp"),
OrdererEndpoints: []string{"foo:7050", "bar:8050"},
MSPType: "bccsp",
ID: "SampleMSP",
Name: "SampleOrg",
Policies: map[string]*genesisconfig.Policy{
"Admins": {Type: "ImplicitMeta", Rule: "ANY Admins"},
"Readers": {Type: "ImplicitMeta", Rule: "ANY Readers"},
"Writers": {Type: "ImplicitMeta", Rule: "ANY Writers"},
},
})

group, err := encoder.NewChannelGroup(config)
require.NoError(t, err)
require.NotNil(t, group)
Expand Down Expand Up @@ -758,7 +788,8 @@ func TestBlockVerifierBuilderNoConfigBlock(t *testing.T) {
}

func TestBlockVerifierFunc(t *testing.T) {
block := sampleConfigBlock()
block := sampleConfigBlock(t)
require.NotNil(t, block)
bvfunc := cluster.BlockVerifierBuilder(&mocks.BCCSP{})

verifier := bvfunc(block)
Expand All @@ -779,7 +810,21 @@ func TestBlockVerifierFunc(t *testing.T) {
require.NoError(t, err)
}

func sampleConfigBlock() *common.Block {
func sampleConfigBlock(t *testing.T) *common.Block {
certDir := t.TempDir()
tlsCA, err := tlsgen.NewCA()
require.NoError(t, err)
config := genesisconfig.Load(genesisconfig.SampleAppChannelSmartBftProfile, configtest.GetDevConfigDir())
generateCertificatesSmartBFT(t, config, tlsCA, certDir)
config.Orderer.Organizations[0].OrdererEndpoints = []string{"127.0.0.1:7050"}

group, err := encoder.NewChannelGroup(config)
if err != nil {
return nil
}

sampleOrg := group.Groups["Orderer"].Groups["SampleOrg"]

return &common.Block{
Header: &common.BlockHeader{
PreviousHash: []byte("foo"),
Expand Down Expand Up @@ -819,6 +864,9 @@ func sampleConfigBlock() *common.Block {
},
},
},
Groups: map[string]*common.ConfigGroup{
"SampleOrg": sampleOrg,
},
Values: map[string]*common.ConfigValue{
"BatchSize": {
Value: protoutil.MarshalOrPanic(&orderer.BatchSize{
Expand Down Expand Up @@ -867,6 +915,27 @@ func sampleConfigBlock() *common.Block {
}
}

func generateCertificatesSmartBFT(t *testing.T, confAppSmartBFT *genesisconfig.Profile, tlsCA tlsgen.CA, certDir string) {
for i, c := range confAppSmartBFT.Orderer.ConsenterMapping {
t.Logf("BFT Consenter: %+v", c)
srvC, err := tlsCA.NewServerCertKeyPair(c.Host)
require.NoError(t, err)
srvP := path.Join(certDir, fmt.Sprintf("server%d.crt", i))
err = os.WriteFile(srvP, srvC.Cert, 0o644)
require.NoError(t, err)

clnC, err := tlsCA.NewClientCertKeyPair()
require.NoError(t, err)
clnP := path.Join(certDir, fmt.Sprintf("client%d.crt", i))
err = os.WriteFile(clnP, clnC.Cert, 0o644)
require.NoError(t, err)

c.Identity = srvP
c.ServerTLSCert = srvP
c.ClientTLSCert = clnP
}
}

func TestGetTLSSessionBinding(t *testing.T) {
serverCert, err := ca.NewServerCertKeyPair("127.0.0.1")
require.NoError(t, err)
Expand Down
6 changes: 6 additions & 0 deletions orderer/common/multichannel/chainsupport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ func TestConsensusMetadataValidation(t *testing.T) {
mockResources := &mocks.Resources{}
mockResources.ConfigtxValidatorReturns(mockValidator)
mockResources.OrdererConfigReturns(mockOrderer, true)
mockChannelConfig := &mocks.ChannelConfig{}
mockChannelConfig.OrdererAddressesReturns([]string{"127.0.0.1"})
mockChannelCapabilities := &mocks.ChannelCapabilities{}
mockChannelCapabilities.ConsensusTypeBFTReturns(true)
mockChannelConfig.CapabilitiesReturns(mockChannelCapabilities)
mockResources.ChannelConfigReturns(mockChannelConfig)

ms := &mutableResourcesMock{
Resources: mockResources,
Expand Down
3 changes: 2 additions & 1 deletion orderer/common/multichannel/ledger_resources_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ func (mrm *mutableResourcesMock) CreateBundle(channelID string, c *common.Config
}

func testConfigEnvelope(t *testing.T) *common.ConfigEnvelope {
conf := genesisconfig.Load(genesisconfig.SampleInsecureSoloProfile, configtest.GetDevConfigDir())
conf := genesisconfig.Load(genesisconfig.SampleDevModeSoloProfile, configtest.GetDevConfigDir())
conf.Orderer.Organizations[0].OrdererEndpoints = []string{"127.0.0.1:7050"}
group, err := encoder.NewChannelGroup(conf)
require.NoError(t, err)
group.Groups["Orderer"].Values["ConsensusType"].Value, err = proto.Marshal(&orderer.ConsensusType{
Expand Down
6 changes: 4 additions & 2 deletions orderer/common/multichannel/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,11 @@ func makeConfigTx(chainID string, i int) *cb.Envelope {
}

func makeConfigTxFull(chainID string, i int) *cb.Envelope {
gConf := genesisconfig.Load(genesisconfig.SampleInsecureSoloProfile, configtest.GetDevConfigDir())
gConf := genesisconfig.Load(genesisconfig.SampleDevModeSoloProfile, configtest.GetDevConfigDir())
gConf.Orderer.Capabilities = map[string]bool{
capabilities.OrdererV2_0: true,
}
gConf.Orderer.Organizations[0].OrdererEndpoints = []string{"127.0.0.1:7050"}
gConf.Orderer.MaxChannels = 10
channelGroup, err := encoder.NewChannelGroup(gConf)
if err != nil {
Expand All @@ -138,10 +139,11 @@ func makeConfigTxFull(chainID string, i int) *cb.Envelope {
}

func makeConfigTxMig(chainID string, i int) *cb.Envelope {
gConf := genesisconfig.Load(genesisconfig.SampleInsecureSoloProfile, configtest.GetDevConfigDir())
gConf := genesisconfig.Load(genesisconfig.SampleDevModeSoloProfile, configtest.GetDevConfigDir())
gConf.Orderer.Capabilities = map[string]bool{
capabilities.OrdererV2_0: true,
}
gConf.Orderer.Organizations[0].OrdererEndpoints = []string{"127.0.0.1:7050"}
gConf.Orderer.OrdererType = "solo"
channelGroup, err := encoder.NewChannelGroup(gConf)
if err != nil {
Expand Down

0 comments on commit 9f6ed31

Please sign in to comment.