github.com/true-sqn/fabric@v2.1.1+incompatible/integration/pluggable/pluggable_test.go (about)

     1  /*
     2  Copyright IBM Corp 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/hyperledger/fabric/integration/nwo"
    20  	"github.com/hyperledger/fabric/integration/nwo/commands"
    21  	"github.com/hyperledger/fabric/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, 0700)
    53  		Expect(err).NotTo(HaveOccurred())
    54  		SetEndorsementPluginActivationFolder(dir)
    55  
    56  		dir = filepath.Join(testDir, "validation")
    57  		err = os.Mkdir(dir, 0700)
    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/hyperledger/fabric/integration/chaincode/module"),
    88  			Lang:            "binary",
    89  			PackageFile:     filepath.Join(testDir, "modulecc.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 the plugin file
   129  func compilePlugin(pluginType string) string {
   130  	pluginFilePath := filepath.Join("testdata", "plugins", pluginType, "plugin.so")
   131  	cmd := exec.Command(
   132  		"go", "build",
   133  		"-x", // print build commands while running
   134  		"-buildmode=plugin",
   135  		"-o", pluginFilePath,
   136  		fmt.Sprintf("github.com/hyperledger/fabric/integration/pluggable/testdata/plugins/%s", pluginType),
   137  	)
   138  	pw := gexec.NewPrefixedWriter(fmt.Sprintf("[build-plugin-%s] ", pluginType), GinkgoWriter)
   139  	sess, err := gexec.Start(cmd, pw, pw)
   140  	Expect(err).NotTo(HaveOccurred())
   141  	Eventually(sess, 2*time.Minute).Should(gexec.Exit(0))
   142  
   143  	Expect(pluginFilePath).To(BeARegularFile())
   144  	return pluginFilePath
   145  }
   146  
   147  func configurePlugins(network *nwo.Network, endorsement, validation string) {
   148  	for _, p := range network.Peers {
   149  		core := network.ReadPeerConfig(p)
   150  		core.Peer.Handlers.Endorsers = fabricconfig.HandlerMap{
   151  			"escc": fabricconfig.Handler{Name: "plugin-escc", Library: endorsement},
   152  		}
   153  		core.Peer.Handlers.Validators = fabricconfig.HandlerMap{
   154  			"vscc": fabricconfig.Handler{Name: "plugin-vscc", Library: validation},
   155  		}
   156  		network.WritePeerConfig(p, core)
   157  	}
   158  }
   159  
   160  func RunQueryInvokeQuery(n *nwo.Network, orderer *nwo.Orderer, peer *nwo.Peer) {
   161  	By("querying the chaincode")
   162  	sess, err := n.PeerUserSession(peer, "User1", commands.ChaincodeQuery{
   163  		ChannelID: "testchannel",
   164  		Name:      "mycc",
   165  		Ctor:      `{"Args":["query","a"]}`,
   166  	})
   167  	Expect(err).NotTo(HaveOccurred())
   168  	Eventually(sess, n.EventuallyTimeout).Should(gexec.Exit(0))
   169  	Expect(sess).To(gbytes.Say("100"))
   170  
   171  	sess, err = n.PeerUserSession(peer, "User1", commands.ChaincodeInvoke{
   172  		ChannelID: "testchannel",
   173  		Orderer:   n.OrdererAddress(orderer, nwo.ListenPort),
   174  		Name:      "mycc",
   175  		Ctor:      `{"Args":["invoke","a","b","10"]}`,
   176  		PeerAddresses: []string{
   177  			n.PeerAddress(n.Peer("Org1", "peer0"), nwo.ListenPort),
   178  			n.PeerAddress(n.Peer("Org2", "peer0"), nwo.ListenPort),
   179  		},
   180  		WaitForEvent: true,
   181  	})
   182  	Expect(err).NotTo(HaveOccurred())
   183  	Eventually(sess, n.EventuallyTimeout).Should(gexec.Exit(0))
   184  	Expect(sess.Err).To(gbytes.Say("Chaincode invoke successful. result: status:200"))
   185  
   186  	sess, err = n.PeerUserSession(peer, "User1", commands.ChaincodeQuery{
   187  		ChannelID: "testchannel",
   188  		Name:      "mycc",
   189  		Ctor:      `{"Args":["query","a"]}`,
   190  	})
   191  	Expect(err).NotTo(HaveOccurred())
   192  	Eventually(sess, n.EventuallyTimeout).Should(gexec.Exit(0))
   193  	Expect(sess).To(gbytes.Say("90"))
   194  }