Skip to content

Commit

Permalink
[FAB-10377] Extract copyFile to helpers.CopyFile
Browse files Browse the repository at this point in the history
Some additional minor cleanup in some areas as well.

Change-Id: I75c961c411529aa92b563963012908c0aed1c389
Signed-off-by: Matthew Sykes <sykesmat@us.ibm.com>
  • Loading branch information
sykesm authored and mastersingh24 committed Jun 1, 2018
1 parent 4bc5e66 commit aa8875d
Show file tree
Hide file tree
Showing 13 changed files with 124 additions and 189 deletions.
27 changes: 17 additions & 10 deletions integration/e2e/acl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/gogo/protobuf/proto"
"github.com/hyperledger/fabric/common/tools/configtxlator/update"
"github.com/hyperledger/fabric/core/aclmgmt/resources"
"github.com/hyperledger/fabric/integration/helpers"
"github.com/hyperledger/fabric/integration/runner"
"github.com/hyperledger/fabric/integration/world"
"github.com/hyperledger/fabric/protos/common"
Expand All @@ -26,12 +27,18 @@ import (

var _ = Describe("EndToEndACL", func() {
var (
testDir string
w *world.World
deployment world.Deployment
org1Peer0 *runner.Peer

org1Peer0 *runner.Peer
)

BeforeEach(func() {
var err error
testDir, err = ioutil.TempDir("", "acl-e2e")
Expect(err).NotTo(HaveOccurred())

w = world.GenerateBasicConfig("solo", 2, 2, testDir, components)

// sets up the world for all tests
Expand All @@ -48,11 +55,10 @@ var _ = Describe("EndToEndACL", func() {
Orderer: "127.0.0.1:7050",
}
w.BootstrapNetwork(deployment.Channel)
copyFile(filepath.Join("testdata", "orderer.yaml"), filepath.Join(testDir, "orderer.yaml"))
copyPeerConfigs(w.PeerOrgs, w.Rootpath)
helpers.CopyFile(filepath.Join("testdata", "orderer.yaml"), filepath.Join(testDir, "orderer.yaml"))
w.CopyPeerConfigs("testdata")
w.BuildNetwork()
err := w.SetupChannel(deployment, []string{"peer0.org1.example.com", "peer0.org2.example.com"})
Expect(err).NotTo(HaveOccurred())
w.SetupChannel(deployment, []string{"peer0.org1.example.com", "peer0.org2.example.com"})

org1Peer0 = components.Peer()
org1Peer0.ConfigDir = filepath.Join(w.Rootpath, "peer0.org1.example.com")
Expand All @@ -61,6 +67,7 @@ var _ = Describe("EndToEndACL", func() {

AfterEach(func() {
w.Close(deployment)
os.RemoveAll(testDir)
})

It("enforces access control list policies", func() {
Expand Down Expand Up @@ -137,24 +144,24 @@ var _ = Describe("EndToEndACL", func() {
// signs the configuration with Org2's signer, and then submits the config update
// using Org1
func SetACLPolicy(w *world.World, deployment world.Deployment, policyName, policy string) {
outputFile := filepath.Join(testDir, "updated_config.pb")
outputFile := filepath.Join(w.Rootpath, "updated_config.pb")
GenerateACLConfigUpdate(w, deployment, policyName, policy, outputFile)

signConfigDir := filepath.Join(testDir, "peer0.org2.example.com")
signConfigDir := filepath.Join(w.Rootpath, "peer0.org2.example.com")
signMSPConfigPath := filepath.Join(w.Rootpath, "crypto", "peerOrganizations", "org2.example.com", "users", "Admin@org2.example.com", "msp")
SignConfigUpdate(w, outputFile, signConfigDir, signMSPConfigPath)

sendConfigDir := filepath.Join(testDir, "peer0.org1.example.com")
sendConfigDir := filepath.Join(w.Rootpath, "peer0.org1.example.com")
sendMSPConfigPath := filepath.Join(w.Rootpath, "crypto", "peerOrganizations", "org1.example.com", "users", "Admin@org1.example.com", "msp")
SendConfigUpdate(deployment, outputFile, sendConfigDir, sendMSPConfigPath)
}

func GenerateACLConfigUpdate(w *world.World, deployment world.Deployment, policyName, policy, outputFile string) {
// fetch the config block
fetchRun := components.Peer()
fetchRun.ConfigDir = filepath.Join(testDir, "peer0.org1.example.com")
fetchRun.ConfigDir = filepath.Join(w.Rootpath, "peer0.org1.example.com")
fetchRun.MSPConfigPath = filepath.Join(w.Rootpath, "crypto", "peerOrganizations", "org1.example.com", "users", "Admin@org1.example.com", "msp")
output := filepath.Join(testDir, "config_block.pb")
output := filepath.Join(w.Rootpath, "config_block.pb")
fRunner := fetchRun.FetchChannel(deployment.Channel, output, "config", deployment.Orderer)
execute(fRunner)
Expect(fRunner.Err()).To(gbytes.Say("Received block: "))
Expand Down
17 changes: 1 addition & 16 deletions integration/e2e/e2e_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ package e2e

import (
"encoding/json"
"io/ioutil"
"os"

"github.com/hyperledger/fabric/integration/world"
. "github.com/onsi/ginkgo"
Expand All @@ -23,10 +21,7 @@ func TestEndToEnd(t *testing.T) {
RunSpecs(t, "EndToEnd Suite")
}

var (
components *world.Components
testDir string
)
var components *world.Components

var _ = SynchronizedBeforeSuite(func() []byte {
components = &world.Components{}
Expand All @@ -41,16 +36,6 @@ var _ = SynchronizedBeforeSuite(func() []byte {
Expect(err).NotTo(HaveOccurred())
})

var _ = BeforeEach(func() {
var err error
testDir, err = ioutil.TempDir("", "e2e-suite")
Expect(err).NotTo(HaveOccurred())
})

var _ = AfterEach(func() {
os.RemoveAll(testDir)
})

var _ = SynchronizedAfterSuite(func() {
}, func() {
components.Cleanup()
Expand Down
44 changes: 14 additions & 30 deletions integration/e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ SPDX-License-Identifier: Apache-2.0
package e2e

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
Expand All @@ -17,15 +16,23 @@ import (
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gbytes"

"github.com/hyperledger/fabric/integration/helpers"
"github.com/hyperledger/fabric/integration/world"
"github.com/tedsuo/ifrit"
)

var _ = Describe("EndToEnd", func() {
var w *world.World
var deployment world.Deployment
var (
testDir string
w *world.World
deployment world.Deployment
)

BeforeEach(func() {
var err error
testDir, err = ioutil.TempDir("", "e2e")
Expect(err).NotTo(HaveOccurred())

deployment = world.Deployment{
Channel: "testchannel",
Chaincode: world.Chaincode{
Expand All @@ -44,6 +51,7 @@ var _ = Describe("EndToEnd", func() {
if w != nil {
w.Close(deployment)
}
os.RemoveAll(testDir)
})

Describe("basic solo network with 2 orgs", func() {
Expand All @@ -64,15 +72,14 @@ var _ = Describe("EndToEnd", func() {
Expect(filepath.Join(testDir, "Org2_anchors_update_tx.pb")).To(BeARegularFile())

By("setting up directories for the network")
copyFile(filepath.Join("testdata", "orderer.yaml"), filepath.Join(testDir, "orderer.yaml"))
copyPeerConfigs(w.PeerOrgs, w.Rootpath)
helpers.CopyFile(filepath.Join("testdata", "orderer.yaml"), filepath.Join(testDir, "orderer.yaml"))
w.CopyPeerConfigs("testdata")

By("building the network")
w.BuildNetwork()

By("setting up the channel")
err := w.SetupChannel(deployment, []string{"peer0.org1.example.com", "peer0.org2.example.com"})
Expect(err).NotTo(HaveOccurred())
w.SetupChannel(deployment, []string{"peer0.org1.example.com", "peer0.org2.example.com"})

RunQueryInvokeQuery(w, deployment)
})
Expand Down Expand Up @@ -125,26 +132,3 @@ func execute(r ifrit.Runner) (err error) {
Eventually(p.Wait(), 30*time.Second).Should(Receive(&err))
return err
}

func copyFile(src, dest string) {
data, err := ioutil.ReadFile(src)
Expect(err).NotTo(HaveOccurred())
err = ioutil.WriteFile(dest, data, 0775)
Expect(err).NotTo(HaveOccurred())
}

func copyPeerConfigs(peerOrgs []world.PeerOrgConfig, rootPath string) {
for _, peerOrg := range peerOrgs {
for peer := 0; peer < peerOrg.PeerCount; peer++ {
peerDir := fmt.Sprintf("peer%d.%s", peer, peerOrg.Domain)
if _, err := os.Stat(filepath.Join(rootPath, peerDir)); os.IsNotExist(err) {
err := os.Mkdir(filepath.Join(rootPath, peerDir), 0755)
Expect(err).NotTo(HaveOccurred())
}
copyFile(
filepath.Join("testdata", fmt.Sprintf("%s_%d-core.yaml", peerOrg.Domain, peer)),
filepath.Join(rootPath, peerDir, "core.yaml"),
)
}
}
}
20 changes: 20 additions & 0 deletions integration/helpers/files.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/

package helpers

import (
"io/ioutil"

. "github.com/onsi/gomega"
)

func CopyFile(src, dest string) {
data, err := ioutil.ReadFile(src)
ExpectWithOffset(1, err).NotTo(HaveOccurred())
err = ioutil.WriteFile(dest, data, 0775)
ExpectWithOffset(1, err).NotTo(HaveOccurred())
}
22 changes: 0 additions & 22 deletions integration/pluggable/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ package e2e

import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -90,34 +89,13 @@ var _ = Describe("EndToEnd", func() {
})
})

func copyFile(src, dest string) {
data, err := ioutil.ReadFile(src)
Expect(err).NotTo(HaveOccurred())
err = ioutil.WriteFile(dest, data, 0775)
Expect(err).NotTo(HaveOccurred())
}

func execute(r ifrit.Runner) (err error) {
p := ifrit.Invoke(r)
Eventually(p.Ready()).Should(BeClosed())
Eventually(p.Wait(), 10*time.Second).Should(Receive(&err))
return err
}

func copyPeerConfigs(peerOrgs []world.PeerOrgConfig, rootPath string) {
for _, peerOrg := range peerOrgs {
for peer := 0; peer < peerOrg.PeerCount; peer++ {
peerDir := fmt.Sprintf("%s_%d", peerOrg.Domain, peer)
if _, err := os.Stat(filepath.Join(rootPath, peerDir)); os.IsNotExist(err) {
err := os.Mkdir(filepath.Join(rootPath, peerDir), 0755)
Expect(err).NotTo(HaveOccurred())
}
copyFile(filepath.Join("testdata", fmt.Sprintf("%s-core.yaml", peerDir)),
filepath.Join(rootPath, peerDir, "core.yaml"))
}
}
}

// compilePlugin compiles the plugin of the given type and returns the path for the plugin file
func compilePlugin(pluginType string) string {
pluginFilePath := filepath.Join("testdata", "plugins", pluginType, "plugin.so")
Expand Down
3 changes: 2 additions & 1 deletion integration/runner/configtxgen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/onsi/gomega/gbytes"
"github.com/onsi/gomega/gexec"

"github.com/hyperledger/fabric/integration/helpers"
"github.com/hyperledger/fabric/integration/runner"
"github.com/tedsuo/ifrit"
)
Expand All @@ -40,7 +41,7 @@ var _ = Describe("Configtxgen", func() {
Expect(filepath.Join(tempDir, "crypto-config", "peerOrganizations")).To(BeADirectory())
Expect(filepath.Join(tempDir, "crypto-config", "ordererOrganizations")).To(BeADirectory())

copyFile(filepath.Join("testdata", "configtx.yaml"), filepath.Join(tempDir, "configtx.yaml"))
helpers.CopyFile(filepath.Join("testdata", "configtx.yaml"), filepath.Join(tempDir, "configtx.yaml"))
})

AfterEach(func() {
Expand Down
7 changes: 4 additions & 3 deletions integration/runner/orderer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"path/filepath"
"syscall"

"github.com/hyperledger/fabric/integration/helpers"
"github.com/hyperledger/fabric/integration/runner"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
Expand All @@ -30,15 +31,15 @@ var _ = Describe("Orderer", func() {
tempDir, err = ioutil.TempDir("", "orderer")
Expect(err).NotTo(HaveOccurred())

copyFile(filepath.Join("testdata", "cryptogen-config.yaml"), filepath.Join(tempDir, "cryptogen-config.yaml"))
helpers.CopyFile(filepath.Join("testdata", "cryptogen-config.yaml"), filepath.Join(tempDir, "cryptogen-config.yaml"))
cryptogen := components.Cryptogen()
cryptogen.Config = filepath.Join(tempDir, "cryptogen-config.yaml")
cryptogen.Output = filepath.Join(tempDir, "crypto-config")

crypto := cryptogen.Generate()
Expect(execute(crypto)).To(Succeed())

copyFile(filepath.Join("testdata", "configtx.yaml"), filepath.Join(tempDir, "configtx.yaml"))
helpers.CopyFile(filepath.Join("testdata", "configtx.yaml"), filepath.Join(tempDir, "configtx.yaml"))
configtxgen := components.Configtxgen()
configtxgen.ChannelID = "mychannel"
configtxgen.Profile = "TwoOrgsOrdererGenesis"
Expand All @@ -49,7 +50,7 @@ var _ = Describe("Orderer", func() {
err = execute(r)
Expect(err).NotTo(HaveOccurred())

copyFile(filepath.Join("testdata", "orderer.yaml"), filepath.Join(tempDir, "orderer.yaml"))
helpers.CopyFile(filepath.Join("testdata", "orderer.yaml"), filepath.Join(tempDir, "orderer.yaml"))
orderer = components.Orderer()
orderer.ConfigDir = tempDir
orderer.LedgerLocation = tempDir
Expand Down
9 changes: 5 additions & 4 deletions integration/runner/peer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"time"

docker "github.com/fsouza/go-dockerclient"
"github.com/hyperledger/fabric/integration/helpers"
"github.com/hyperledger/fabric/integration/runner"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
Expand Down Expand Up @@ -45,7 +46,7 @@ var _ = Describe("Peer", func() {
cryptoDir = filepath.Join(tempDir, "crypto-config")
peer = components.Peer()

copyFile(filepath.Join("testdata", "cryptogen-config.yaml"), filepath.Join(tempDir, "cryptogen-config.yaml"))
helpers.CopyFile(filepath.Join("testdata", "cryptogen-config.yaml"), filepath.Join(tempDir, "cryptogen-config.yaml"))
cryptogen := components.Cryptogen()
cryptogen.Config = filepath.Join(tempDir, "cryptogen-config.yaml")
cryptogen.Output = cryptoDir
Expand All @@ -54,7 +55,7 @@ var _ = Describe("Peer", func() {
Expect(execute(crypto)).To(Succeed())

// Generate orderer config block
copyFile(filepath.Join("testdata", "configtx.yaml"), filepath.Join(tempDir, "configtx.yaml"))
helpers.CopyFile(filepath.Join("testdata", "configtx.yaml"), filepath.Join(tempDir, "configtx.yaml"))
configtxgen := components.Configtxgen()
configtxgen.ChannelID = "mychannel"
configtxgen.Profile = "TwoOrgsOrdererGenesis"
Expand All @@ -75,7 +76,7 @@ var _ = Describe("Peer", func() {
Expect(err).NotTo(HaveOccurred())

// Start the orderer
copyFile(filepath.Join("testdata", "orderer.yaml"), filepath.Join(tempDir, "orderer.yaml"))
helpers.CopyFile(filepath.Join("testdata", "orderer.yaml"), filepath.Join(tempDir, "orderer.yaml"))
orderer = components.Orderer()
orderer.ConfigDir = tempDir
orderer.LedgerLocation = tempDir
Expand All @@ -86,7 +87,7 @@ var _ = Describe("Peer", func() {
Eventually(ordererProcess.Ready()).Should(BeClosed())
Consistently(ordererProcess.Wait()).ShouldNot(Receive())

copyFile(filepath.Join("testdata", "core.yaml"), filepath.Join(tempDir, "core.yaml"))
helpers.CopyFile(filepath.Join("testdata", "core.yaml"), filepath.Join(tempDir, "core.yaml"))
peer.ConfigDir = tempDir

client, err = docker.NewClientFromEnv()
Expand Down
8 changes: 0 additions & 8 deletions integration/runner/runner_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ package runner_test

import (
"encoding/json"
"io/ioutil"
"time"

"github.com/hyperledger/fabric/integration/world"
Expand Down Expand Up @@ -46,13 +45,6 @@ var _ = SynchronizedAfterSuite(func() {
components.Cleanup()
})

func copyFile(src, dest string) {
data, err := ioutil.ReadFile(src)
Expect(err).NotTo(HaveOccurred())
err = ioutil.WriteFile(dest, data, 0775)
Expect(err).NotTo(HaveOccurred())
}

func execute(r ifrit.Runner) error {
var err error
p := ifrit.Invoke(r)
Expand Down
Loading

0 comments on commit aa8875d

Please sign in to comment.