Skip to content

Commit

Permalink
Merge "Generate block fixtures in orderer/common/server UT"
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason Yellick authored and Gerrit Code Review committed Nov 11, 2019
2 parents 03710df + dfd4309 commit 5758c93
Show file tree
Hide file tree
Showing 18 changed files with 327 additions and 137 deletions.
63 changes: 28 additions & 35 deletions orderer/common/server/etcdraft_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,8 @@ func nextPort() int32 {
}

func TestSpawnEtcdRaft(t *testing.T) {

gt := NewGomegaWithT(t)

cwd, err := filepath.Abs(".")
gt.Expect(err).NotTo(HaveOccurred())

// Set the fabric root folder for easy navigation to sampleconfig folder
fabricRootDir, err := filepath.Abs(filepath.Join("..", "..", ".."))
gt.Expect(err).NotTo(HaveOccurred())
Expand All @@ -55,7 +51,7 @@ func TestSpawnEtcdRaft(t *testing.T) {
defer os.RemoveAll(tempDir)

t.Run("Invalid bootstrap block", func(t *testing.T) {
testEtcdRaftOSNFailureInvalidBootstrapBlock(NewGomegaWithT(t), tempDir, orderer, fabricRootDir)
testEtcdRaftOSNFailureInvalidBootstrapBlock(NewGomegaWithT(t), tempDir, orderer, fabricRootDir, configtxgen)
})

t.Run("TLS disabled single listener", func(t *testing.T) {
Expand All @@ -81,17 +77,17 @@ func TestSpawnEtcdRaft(t *testing.T) {
gt.Expect(err).NotTo(HaveOccurred())
defer os.RemoveAll(tempDir)

testEtcdRaftOSNSuccess(gt, tempDir, configtxgen, cwd, orderer, fabricRootDir)
testEtcdRaftOSNSuccess(gt, tempDir, configtxgen, orderer, fabricRootDir)
})
})
}

func createBootstrapBlock(gt *GomegaWithT, tempDir, configtxgen, cwd string) string {
// Create the genesis block for the system channel
func createBootstrapBlock(gt *GomegaWithT, tempDir, configtxgen, channel, profile string) string {
// create a genesis block for the specified channel and profile
genesisBlockPath := filepath.Join(tempDir, "genesis.block")
cmd := exec.Command(configtxgen, "-channelID", "system", "-profile", "SampleDevModeEtcdRaft",
cmd := exec.Command(configtxgen, "-channelID", channel, "-profile", profile,
"-outputBlock", genesisBlockPath)
cmd.Env = append(cmd.Env, fmt.Sprintf("FABRIC_CFG_PATH=%s", filepath.Join(cwd, "testdata")))
cmd.Env = append(cmd.Env, "FABRIC_CFG_PATH=testdata")
configtxgenProcess, err := gexec.Start(cmd, nil, nil)
gt.Expect(err).NotTo(HaveOccurred())
gt.Eventually(configtxgenProcess, time.Minute).Should(gexec.Exit(0))
Expand All @@ -100,8 +96,8 @@ func createBootstrapBlock(gt *GomegaWithT, tempDir, configtxgen, cwd string) str
return genesisBlockPath
}

func testEtcdRaftOSNSuccess(gt *GomegaWithT, tempDir, configtxgen, cwd, orderer, fabricRootDir string) {
genesisBlockPath := createBootstrapBlock(gt, tempDir, configtxgen, cwd)
func testEtcdRaftOSNSuccess(gt *GomegaWithT, tempDir, configtxgen, orderer, fabricRootDir string) {
genesisBlockPath := createBootstrapBlock(gt, tempDir, configtxgen, "system", "SampleEtcdRaftSystemChannel")

// Launch the OSN
ordererProcess := launchOrderer(gt, orderer, tempDir, genesisBlockPath, fabricRootDir)
Expand All @@ -126,9 +122,9 @@ func testEtcdRaftOSNSuccess(gt *GomegaWithT, tempDir, configtxgen, cwd, orderer,
gt.Eventually(ordererProcess.Err, time.Minute).Should(gbytes.Say("becomeLeader"))
}

func testEtcdRaftOSNFailureInvalidBootstrapBlock(gt *GomegaWithT, tempDir, orderer, fabricRootDir string) {
// Grab an application channel genesis block
genesisBlockPath := filepath.Join(filepath.Join("testdata", "mychannel.block"))
func testEtcdRaftOSNFailureInvalidBootstrapBlock(gt *GomegaWithT, tempDir, orderer, fabricRootDir, configtxgen string) {
// create an application channel genesis block
genesisBlockPath := createBootstrapBlock(gt, tempDir, configtxgen, "mychannel", "SampleOrgChannel")
genesisBlockBytes, err := ioutil.ReadFile(genesisBlockPath)
gt.Expect(err).NotTo(HaveOccurred())

Expand All @@ -146,10 +142,7 @@ func testEtcdRaftOSNFailureInvalidBootstrapBlock(gt *GomegaWithT, tempDir, order
}

func testEtcdRaftOSNNoTLSSingleListener(gt *GomegaWithT, tempDir, orderer, fabricRootDir string, configtxgen string) {
cwd, err := filepath.Abs(".")
gt.Expect(err).NotTo(HaveOccurred())

genesisBlockPath := createBootstrapBlock(gt, tempDir, configtxgen, cwd)
genesisBlockPath := createBootstrapBlock(gt, tempDir, configtxgen, "system", "SampleEtcdRaftSystemChannel")

cmd := exec.Command(orderer)
cmd.Env = []string{
Expand All @@ -169,10 +162,10 @@ func testEtcdRaftOSNNoTLSSingleListener(gt *GomegaWithT, tempDir, orderer, fabri
}

func testEtcdRaftOSNNoTLSDualListener(gt *GomegaWithT, tempDir, orderer, fabricRootDir string, configtxgen string) {
cwd, err := filepath.Abs(".")
cwd, err := os.Getwd()
gt.Expect(err).NotTo(HaveOccurred())

genesisBlockPath := createBootstrapBlock(gt, tempDir, configtxgen, cwd)
genesisBlockPath := createBootstrapBlock(gt, tempDir, configtxgen, "system", "SampleEtcdRaftSystemChannel")

cmd := exec.Command(orderer)
cmd.Env = []string{
Expand All @@ -185,11 +178,11 @@ func testEtcdRaftOSNNoTLSDualListener(gt *GomegaWithT, tempDir, orderer, fabricR
fmt.Sprintf("ORDERER_GENERAL_GENESISFILE=%s", genesisBlockPath),
fmt.Sprintf("ORDERER_GENERAL_CLUSTER_LISTENPORT=%d", nextPort()),
"ORDERER_GENERAL_CLUSTER_LISTENADDRESS=127.0.0.1",
fmt.Sprintf("ORDERER_GENERAL_CLUSTER_SERVERCERTIFICATE=%s", filepath.Join(cwd, "testdata", "tls", "server.crt")),
fmt.Sprintf("ORDERER_GENERAL_CLUSTER_SERVERPRIVATEKEY=%s", filepath.Join(cwd, "testdata", "tls", "server.key")),
fmt.Sprintf("ORDERER_GENERAL_CLUSTER_CLIENTCERTIFICATE=%s", filepath.Join(cwd, "testdata", "tls", "server.crt")),
fmt.Sprintf("ORDERER_GENERAL_CLUSTER_CLIENTPRIVATEKEY=%s", filepath.Join(cwd, "testdata", "tls", "server.key")),
fmt.Sprintf("ORDERER_GENERAL_CLUSTER_ROOTCAS=[%s]", filepath.Join(cwd, "testdata", "tls", "ca.crt")),
fmt.Sprintf("ORDERER_GENERAL_CLUSTER_SERVERCERTIFICATE=%s", filepath.Join(cwd, "testdata", "example.com", "tls", "server.crt")),
fmt.Sprintf("ORDERER_GENERAL_CLUSTER_SERVERPRIVATEKEY=%s", filepath.Join(cwd, "testdata", "example.com", "tls", "server.key")),
fmt.Sprintf("ORDERER_GENERAL_CLUSTER_CLIENTCERTIFICATE=%s", filepath.Join(cwd, "testdata", "example.com", "tls", "server.crt")),
fmt.Sprintf("ORDERER_GENERAL_CLUSTER_CLIENTPRIVATEKEY=%s", filepath.Join(cwd, "testdata", "example.com", "tls", "server.key")),
fmt.Sprintf("ORDERER_GENERAL_CLUSTER_ROOTCAS=[%s]", filepath.Join(cwd, "testdata", "example.com", "tls", "ca.crt")),
fmt.Sprintf("ORDERER_CONSENSUS_WALDIR=%s", filepath.Join(tempDir, "wal")),
fmt.Sprintf("ORDERER_CONSENSUS_SNAPDIR=%s", filepath.Join(tempDir, "snapshot")),
fmt.Sprintf("FABRIC_CFG_PATH=%s", filepath.Join(fabricRootDir, "sampleconfig")),
Expand All @@ -204,7 +197,7 @@ func testEtcdRaftOSNNoTLSDualListener(gt *GomegaWithT, tempDir, orderer, fabricR
}

func launchOrderer(gt *GomegaWithT, orderer, tempDir, genesisBlockPath, fabricRootDir string) *gexec.Session {
cwd, err := filepath.Abs(".")
cwd, err := os.Getwd()
gt.Expect(err).NotTo(HaveOccurred())

// Launch the orderer process
Expand All @@ -220,14 +213,14 @@ func launchOrderer(gt *GomegaWithT, orderer, tempDir, genesisBlockPath, fabricRo
fmt.Sprintf("ORDERER_GENERAL_GENESISFILE=%s", genesisBlockPath),
fmt.Sprintf("ORDERER_GENERAL_CLUSTER_LISTENPORT=%d", nextPort()),
"ORDERER_GENERAL_CLUSTER_LISTENADDRESS=127.0.0.1",
fmt.Sprintf("ORDERER_GENERAL_CLUSTER_SERVERCERTIFICATE=%s", filepath.Join(cwd, "testdata", "tls", "server.crt")),
fmt.Sprintf("ORDERER_GENERAL_CLUSTER_SERVERPRIVATEKEY=%s", filepath.Join(cwd, "testdata", "tls", "server.key")),
fmt.Sprintf("ORDERER_GENERAL_CLUSTER_CLIENTCERTIFICATE=%s", filepath.Join(cwd, "testdata", "tls", "server.crt")),
fmt.Sprintf("ORDERER_GENERAL_CLUSTER_CLIENTPRIVATEKEY=%s", filepath.Join(cwd, "testdata", "tls", "server.key")),
fmt.Sprintf("ORDERER_GENERAL_CLUSTER_ROOTCAS=[%s]", filepath.Join(cwd, "testdata", "tls", "ca.crt")),
fmt.Sprintf("ORDERER_GENERAL_TLS_ROOTCAS=[%s]", filepath.Join(cwd, "testdata", "tls", "ca.crt")),
fmt.Sprintf("ORDERER_GENERAL_TLS_CERTIFICATE=%s", filepath.Join(cwd, "testdata", "tls", "server.crt")),
fmt.Sprintf("ORDERER_GENERAL_TLS_PRIVATEKEY=%s", filepath.Join(cwd, "testdata", "tls", "server.key")),
fmt.Sprintf("ORDERER_GENERAL_CLUSTER_SERVERCERTIFICATE=%s", filepath.Join(cwd, "testdata", "example.com", "tls", "server.crt")),
fmt.Sprintf("ORDERER_GENERAL_CLUSTER_SERVERPRIVATEKEY=%s", filepath.Join(cwd, "testdata", "example.com", "tls", "server.key")),
fmt.Sprintf("ORDERER_GENERAL_CLUSTER_CLIENTCERTIFICATE=%s", filepath.Join(cwd, "testdata", "example.com", "tls", "server.crt")),
fmt.Sprintf("ORDERER_GENERAL_CLUSTER_CLIENTPRIVATEKEY=%s", filepath.Join(cwd, "testdata", "example.com", "tls", "server.key")),
fmt.Sprintf("ORDERER_GENERAL_CLUSTER_ROOTCAS=[%s]", filepath.Join(cwd, "testdata", "example.com", "tls", "ca.crt")),
fmt.Sprintf("ORDERER_GENERAL_TLS_ROOTCAS=[%s]", filepath.Join(cwd, "testdata", "example.com", "tls", "ca.crt")),
fmt.Sprintf("ORDERER_GENERAL_TLS_CERTIFICATE=%s", filepath.Join(cwd, "testdata", "example.com", "tls", "server.crt")),
fmt.Sprintf("ORDERER_GENERAL_TLS_PRIVATEKEY=%s", filepath.Join(cwd, "testdata", "example.com", "tls", "server.key")),
fmt.Sprintf("ORDERER_CONSENSUS_WALDIR=%s", filepath.Join(tempDir, "wal")),
fmt.Sprintf("ORDERER_CONSENSUS_SNAPDIR=%s", filepath.Join(tempDir, "snapshot")),
fmt.Sprintf("FABRIC_CFG_PATH=%s", filepath.Join(fabricRootDir, "sampleconfig")),
Expand Down
4 changes: 2 additions & 2 deletions orderer/common/server/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -498,8 +498,8 @@ func TestUpdateTrustedRoots(t *testing.T) {
TLS: localconfig.TLS{
Enabled: true,
ClientAuthRequired: true,
PrivateKey: filepath.Join(".", "testdata", "tls", "server.key"),
Certificate: filepath.Join(".", "testdata", "tls", "server.crt"),
PrivateKey: filepath.Join(".", "testdata", "example.com", "tls", "server.key"),
Certificate: filepath.Join(".", "testdata", "example.com", "tls", "server.crt"),
},
},
}
Expand Down
Loading

0 comments on commit 5758c93

Please sign in to comment.