|
| 1 | +/* |
| 2 | +Copyright IBM Corp. All Rights Reserved. |
| 3 | +
|
| 4 | +SPDX-License-Identifier: Apache-2.0 |
| 5 | +*/ |
| 6 | + |
| 7 | +package server_test |
| 8 | + |
| 9 | +import ( |
| 10 | + "fmt" |
| 11 | + "io/ioutil" |
| 12 | + "os" |
| 13 | + "os/exec" |
| 14 | + "path/filepath" |
| 15 | + "testing" |
| 16 | + "time" |
| 17 | + |
| 18 | + . "github.com/onsi/gomega" |
| 19 | + "github.com/onsi/gomega/gbytes" |
| 20 | + "github.com/onsi/gomega/gexec" |
| 21 | +) |
| 22 | + |
| 23 | +func TestSpawnEtcdRaft(t *testing.T) { |
| 24 | + gt := NewGomegaWithT(t) |
| 25 | + |
| 26 | + // Create tempdir to be used to store the genesis block for the system channel |
| 27 | + tempDir, err := ioutil.TempDir("", "etcdraft-orderer-launch") |
| 28 | + gt.Expect(err).NotTo(HaveOccurred()) |
| 29 | + defer os.RemoveAll(tempDir) |
| 30 | + |
| 31 | + // Set the fabric root folder for easy navigation to sampleconfig folder |
| 32 | + fabricRootDir, err := filepath.Abs(filepath.Join("..", "..", "..")) |
| 33 | + gt.Expect(err).NotTo(HaveOccurred()) |
| 34 | + |
| 35 | + // Build the configtxgen binary |
| 36 | + configtxgen, err := gexec.Build("github.com/hyperledger/fabric/common/tools/configtxgen") |
| 37 | + gt.Expect(err).NotTo(HaveOccurred()) |
| 38 | + |
| 39 | + // Build the orderer binary |
| 40 | + orderer, err := gexec.Build("github.com/hyperledger/fabric/orderer") |
| 41 | + gt.Expect(err).NotTo(HaveOccurred()) |
| 42 | + |
| 43 | + defer gexec.CleanupBuildArtifacts() |
| 44 | + |
| 45 | + // Create the genesis block for the system channel |
| 46 | + genesisBlockPath := filepath.Join(tempDir, "genesis.block") |
| 47 | + cmd := exec.Command(configtxgen, "-channelID", "system", "-profile", "SampleDevModeEtcdRaft", |
| 48 | + "-outputBlock", genesisBlockPath) |
| 49 | + cmd.Env = append(cmd.Env, fmt.Sprintf("FABRIC_CFG_PATH=%s", filepath.Join(fabricRootDir, "sampleconfig"))) |
| 50 | + configtxgenProcess, err := gexec.Start(cmd, nil, nil) |
| 51 | + gt.Expect(err).NotTo(HaveOccurred()) |
| 52 | + |
| 53 | + gt.Eventually(configtxgenProcess, time.Minute).Should(gexec.Exit(0)) |
| 54 | + gt.Expect(configtxgenProcess.Err).To(gbytes.Say("Writing genesis block")) |
| 55 | + |
| 56 | + // Launch the OSN |
| 57 | + ordererProcess := launchOrderer(gt, cmd, orderer, tempDir, genesisBlockPath, fabricRootDir) |
| 58 | + gt.Eventually(ordererProcess.Err, time.Minute).Should(gbytes.Say("Beginning to serve requests")) |
| 59 | + gt.Eventually(ordererProcess.Err, time.Minute).Should(gbytes.Say("becomeLeader")) |
| 60 | + ordererProcess.Kill() |
| 61 | +} |
| 62 | + |
| 63 | +func launchOrderer(gt *GomegaWithT, cmd *exec.Cmd, orderer, tempDir, genesisBlockPath, fabricRootDir string) *gexec.Session { |
| 64 | + cwd, err := filepath.Abs(".") |
| 65 | + gt.Expect(err).NotTo(HaveOccurred()) |
| 66 | + |
| 67 | + // Launch the orderer process |
| 68 | + cmd = exec.Command(orderer) |
| 69 | + cmd.Env = []string{ |
| 70 | + "ORDERER_GENERAL_LISTENPORT=5611", |
| 71 | + "ORDERER_GENERAL_GENESISPROFILE=SampleDevModeEtcdRaft", |
| 72 | + "ORDERER_GENERAL_GENESISMETHOD=file", |
| 73 | + "ORDERER_GENERAL_SYSTEMCHANNEL=system", |
| 74 | + "ORDERER_GENERAL_TLS_CLIENTAUTHREQUIRED=true", |
| 75 | + "ORDERER_GENERAL_TLS_ENABLED=true", |
| 76 | + fmt.Sprintf("ORDERER_FILELEDGER_LOCATION=%s", filepath.Join(tempDir, "ledger")), |
| 77 | + fmt.Sprintf("ORDERER_GENERAL_GENESISFILE=%s", genesisBlockPath), |
| 78 | + fmt.Sprintf("ORDERER_GENERAL_CLUSTER_CLIENTCERTIFICATE=%s", filepath.Join(cwd, "testdata", "tls", "server.crt")), |
| 79 | + fmt.Sprintf("ORDERER_GENERAL_CLUSTER_CLIENTPRIVATEKEY=%s", filepath.Join(cwd, "testdata", "tls", "server.key")), |
| 80 | + fmt.Sprintf("ORDERER_GENERAL_CLUSTER_ROOTCAS=[%s]", filepath.Join(cwd, "testdata", "tls", "ca.crt")), |
| 81 | + fmt.Sprintf("ORDERER_GENERAL_TLS_ROOTCAS=[%s]", filepath.Join(cwd, "testdata", "tls", "ca.crt")), |
| 82 | + fmt.Sprintf("ORDERER_GENERAL_TLS_CERTIFICATE=%s", filepath.Join(cwd, "testdata", "tls", "server.crt")), |
| 83 | + fmt.Sprintf("ORDERER_GENERAL_TLS_PRIVATEKEY=%s", filepath.Join(cwd, "testdata", "tls", "server.key")), |
| 84 | + fmt.Sprintf("FABRIC_CFG_PATH=%s", filepath.Join(fabricRootDir, "sampleconfig")), |
| 85 | + } |
| 86 | + sess, err := gexec.Start(cmd, nil, nil) |
| 87 | + gt.Expect(err).NotTo(HaveOccurred()) |
| 88 | + return sess |
| 89 | +} |
0 commit comments