github.com/hechain20/hechain@v0.0.0-20220316014945-b544036ba106/integration/pluggable/pluggable_test.go (about)

     1  /*
     2  Copyright hechain All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package pluggable
     8  
     9  import (
    10  	"fmt"
    11  	"io/ioutil"
    12  	"os"
    13  	"os/exec"
    14  	"path/filepath"
    15  	"syscall"
    16  	"time"
    17  
    18  	docker "github.com/fsouza/go-dockerclient"
    19  	"github.com/hechain20/hechain/integration/nwo"
    20  	"github.com/hechain20/hechain/integration/nwo/commands"
    21  	"github.com/hechain20/hechain/integration/nwo/fabricconfig"
    22  	. "github.com/onsi/ginkgo"
    23  	. "github.com/onsi/gomega"
    24  	"github.com/onsi/gomega/gbytes"
    25  	"github.com/onsi/gomega/gexec"
    26  	"github.com/tedsuo/ifrit"
    27  )
    28  
    29  var _ = Describe("EndToEnd", func() {
    30  	var (
    31  		testDir   string
    32  		client    *docker.Client
    33  		network   *nwo.Network
    34  		chaincode nwo.Chaincode
    35  		process   ifrit.Process
    36  
    37  		endorsementPluginPath string
    38  		validationPluginPath  string
    39  	)
    40  
    41  	BeforeEach(func() {
    42  		var err error
    43  		testDir, err = ioutil.TempDir("", "pluggable-suite")
    44  		Expect(err).NotTo(HaveOccurred())
    45  
    46  		// Compile plugins
    47  		endorsementPluginPath = compilePlugin("endorsement")
    48  		validationPluginPath = compilePlugin("validation")
    49  
    50  		// Create directories for endorsement and validation activation
    51  		dir := filepath.Join(testDir, "endorsement")
    52  		err = os.Mkdir(dir, 0o700)
    53  		Expect(err).NotTo(HaveOccurred())
    54  		SetEndorsementPluginActivationFolder(dir)
    55  
    56  		dir = filepath.Join(testDir, "validation")
    57  		err = os.Mkdir(dir, 0o700)
    58  		Expect(err).NotTo(HaveOccurred())
    59  		SetValidationPluginActivationFolder(dir)
    60  
    61  		// Speed up test by reducing the number of peers we bring up
    62  		soloConfig := nwo.BasicSolo()
    63  		soloConfig.RemovePeer("Org1", "peer1")
    64  		soloConfig.RemovePeer("Org2", "peer1")
    65  		Expect(soloConfig.Peers).To(HaveLen(2))
    66  
    67  		// docker client
    68  		client, err = docker.NewClientFromEnv()
    69  		Expect(err).NotTo(HaveOccurred())
    70  
    71  		network = nwo.New(soloConfig, testDir, client, StartPort(), components)
    72  		network.GenerateConfigTree()
    73  
    74  		// modify config
    75  		configurePlugins(network, endorsementPluginPath, validationPluginPath)
    76  
    77  		// generate network config
    78  		network.Bootstrap()
    79  
    80  		networkRunner := network.NetworkGroupRunner()
    81  		process = ifrit.Invoke(networkRunner)
    82  		Eventually(process.Ready(), network.EventuallyTimeout).Should(BeClosed())
    83  
    84  		chaincode = nwo.Chaincode{
    85  			Name:            "mycc",
    86  			Version:         "0.0",
    87  			Path:            components.Build("github.com/hechain20/hechain/integration/chaincode/simple/cmd"),
    88  			Lang:            "binary",
    89  			PackageFile:     filepath.Join(testDir, "simplecc.tar.gz"),
    90  			Ctor:            `{"Args":["init","a","100","b","200"]}`,
    91  			SignaturePolicy: `OR ('Org1MSP.member','Org2MSP.member')`,
    92  			Sequence:        "1",
    93  			InitRequired:    true,
    94  			Label:           "my_prebuilt_chaincode",
    95  		}
    96  		orderer := network.Orderer("orderer")
    97  		network.CreateAndJoinChannel(orderer, "testchannel")
    98  		nwo.EnableCapabilities(network, "testchannel", "Application", "V2_0", orderer, network.Peer("Org1", "peer0"), network.Peer("Org2", "peer0"))
    99  		nwo.DeployChaincode(network, "testchannel", orderer, chaincode)
   100  	})
   101  
   102  	AfterEach(func() {
   103  		// stop the network
   104  		process.Signal(syscall.SIGTERM)
   105  		Eventually(process.Wait(), network.EventuallyTimeout).Should(Receive())
   106  
   107  		// cleanup the network artifacts
   108  		network.Cleanup()
   109  		os.RemoveAll(testDir)
   110  
   111  		// cleanup the compiled plugins
   112  		os.Remove(endorsementPluginPath)
   113  		os.Remove(validationPluginPath)
   114  	})
   115  
   116  	It("executes a basic solo network with specified plugins", func() {
   117  		// Make sure plugins activated
   118  		peerCount := len(network.Peers)
   119  		activations := CountEndorsementPluginActivations()
   120  		Expect(activations).To(Equal(peerCount))
   121  		activations = CountValidationPluginActivations()
   122  		Expect(activations).To(Equal(peerCount))
   123  
   124  		RunQueryInvokeQuery(network, network.Orderer("orderer"), network.Peer("Org1", "peer0"))
   125  	})
   126  })
   127  
   128  // compilePlugin compiles the plugin of the given type and returns the path for
   129  // the plugin file.
   130  func compilePlugin(pluginType string) string {
   131  	// Building plugins can take quite a while as the build caches won't be
   132  	// populated for the 'dynlink' install suffix. While a minute or two is
   133  	// usually enough, timeouts have occurred at 2 minutes on slow machines.
   134  	const buildTimeout = 5 * time.Minute
   135  
   136  	pluginFilePath := filepath.Join("testdata", "plugins", pluginType, "plugin.so")
   137  	cmd := exec.Command(
   138  		"go", "build",
   139  		"-x", // print build commands while running
   140  		"-buildmode=plugin",
   141  		"-o", pluginFilePath,
   142  		fmt.Sprintf("github.com/hechain20/hechain/integration/pluggable/testdata/plugins/%s", pluginType),
   143  	)
   144  	pw := gexec.NewPrefixedWriter(fmt.Sprintf("[build-plugin-%s] ", pluginType), GinkgoWriter)
   145  	sess, err := gexec.Start(cmd, pw, pw)
   146  	Expect(err).NotTo(HaveOccurred())
   147  	Eventually(sess, buildTimeout).Should(gexec.Exit(0))
   148  
   149  	Expect(pluginFilePath).To(BeARegularFile())
   150  	return pluginFilePath
   151  }
   152  
   153  func configurePlugins(network *nwo.Network, endorsement, validation string) {
   154  	for _, p := range network.Peers {
   155  		core := network.ReadPeerConfig(p)
   156  		core.Peer.Handlers.Endorsers = fabricconfig.HandlerMap{
   157  			"escc": fabricconfig.Handler{Name: "plugin-escc", Library: endorsement},
   158  		}
   159  		core.Peer.Handlers.Validators = fabricconfig.HandlerMap{
   160  			"vscc": fabricconfig.Handler{Name: "plugin-vscc", Library: validation},
   161  		}
   162  		network.WritePeerConfig(p, core)
   163  	}
   164  }
   165  
   166  func RunQueryInvokeQuery(n *nwo.Network, orderer *nwo.Orderer, peer *nwo.Peer) {
   167  	By("querying the chaincode")
   168  	sess, err := n.PeerUserSession(peer, "User1", commands.ChaincodeQuery{
   169  		ChannelID: "testchannel",
   170  		Name:      "mycc",
   171  		Ctor:      `{"Args":["query","a"]}`,
   172  	})
   173  	Expect(err).NotTo(HaveOccurred())
   174  	Eventually(sess, n.EventuallyTimeout).Should(gexec.Exit(0))
   175  	Expect(sess).To(gbytes.Say("100"))
   176  
   177  	sess, err = n.PeerUserSession(peer, "User1", commands.ChaincodeInvoke{
   178  		ChannelID: "testchannel",
   179  		Orderer:   n.OrdererAddress(orderer, nwo.ListenPort),
   180  		Name:      "mycc",
   181  		Ctor:      `{"Args":["invoke","a","b","10"]}`,
   182  		PeerAddresses: []string{
   183  			n.PeerAddress(n.Peer("Org1", "peer0"), nwo.ListenPort),
   184  			n.PeerAddress(n.Peer("Org2", "peer0"), nwo.ListenPort),
   185  		},
   186  		WaitForEvent: true,
   187  	})
   188  	Expect(err).NotTo(HaveOccurred())
   189  	Eventually(sess, n.EventuallyTimeout).Should(gexec.Exit(0))
   190  	Expect(sess.Err).To(gbytes.Say("Chaincode invoke successful. result: status:200"))
   191  
   192  	sess, err = n.PeerUserSession(peer, "User1", commands.ChaincodeQuery{
   193  		ChannelID: "testchannel",
   194  		Name:      "mycc",
   195  		Ctor:      `{"Args":["query","a"]}`,
   196  	})
   197  	Expect(err).NotTo(HaveOccurred())
   198  	Eventually(sess, n.EventuallyTimeout).Should(gexec.Exit(0))
   199  	Expect(sess).To(gbytes.Say("90"))
   200  }