github.com/xiaqingdoc/fabric@v2.1.1+incompatible/integration/idemix/idemix_test.go (about)

     1  /*
     2  Copyright IBM Corp All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package idemix
     8  
     9  import (
    10  	"io/ioutil"
    11  	"os"
    12  	"path/filepath"
    13  	"syscall"
    14  
    15  	docker "github.com/fsouza/go-dockerclient"
    16  	"github.com/hyperledger/fabric/integration/nwo"
    17  	"github.com/hyperledger/fabric/integration/nwo/commands"
    18  	. "github.com/onsi/ginkgo"
    19  	. "github.com/onsi/gomega"
    20  	"github.com/onsi/gomega/gbytes"
    21  	"github.com/onsi/gomega/gexec"
    22  	"github.com/tedsuo/ifrit"
    23  )
    24  
    25  var _ = Describe("EndToEnd", func() {
    26  	var (
    27  		testDir   string
    28  		client    *docker.Client
    29  		network   *nwo.Network
    30  		chaincode nwo.Chaincode
    31  		process   ifrit.Process
    32  	)
    33  
    34  	BeforeEach(func() {
    35  		var err error
    36  		testDir, err = ioutil.TempDir("", "idemix_e2e")
    37  		Expect(err).NotTo(HaveOccurred())
    38  
    39  		client, err = docker.NewClientFromEnv()
    40  		Expect(err).NotTo(HaveOccurred())
    41  
    42  		chaincode = nwo.Chaincode{
    43  			Name:            "mycc",
    44  			Version:         "0.0",
    45  			Path:            components.Build("github.com/hyperledger/fabric/integration/chaincode/module"),
    46  			Lang:            "binary",
    47  			PackageFile:     filepath.Join(testDir, "modulecc.tar.gz"),
    48  			Ctor:            `{"Args":["init","a","100","b","200"]}`,
    49  			SignaturePolicy: `AND ('Org1MSP.member','Org2MSP.member')`,
    50  			Sequence:        "1",
    51  			InitRequired:    true,
    52  			Label:           "my_prebuilt_chaincode",
    53  		}
    54  	})
    55  
    56  	AfterEach(func() {
    57  		if process != nil {
    58  			process.Signal(syscall.SIGTERM)
    59  			Eventually(process.Wait(), network.EventuallyTimeout).Should(Receive())
    60  		}
    61  		if network != nil {
    62  			network.Cleanup()
    63  		}
    64  		os.RemoveAll(testDir)
    65  	})
    66  
    67  	Describe("basic solo network with 2 orgs", func() {
    68  		BeforeEach(func() {
    69  			network = nwo.New(nwo.BasicSoloWithIdemix(), testDir, client, StartPort(), components)
    70  			network.GenerateConfigTree()
    71  			network.Bootstrap()
    72  
    73  			networkRunner := network.NetworkGroupRunner()
    74  			process = ifrit.Invoke(networkRunner)
    75  			Eventually(process.Ready(), network.EventuallyTimeout).Should(BeClosed())
    76  		})
    77  
    78  		It("executes a basic solo network with 2 orgs", func() {
    79  			By("getting the orderer by name")
    80  			orderer := network.Orderer("orderer")
    81  
    82  			By("setting up the channel")
    83  			network.CreateAndJoinChannel(orderer, "testchannel")
    84  			nwo.EnableCapabilities(network, "testchannel", "Application", "V2_0", orderer, network.Peer("Org1", "peer0"), network.Peer("Org2", "peer0"))
    85  
    86  			By("deploying the chaincode")
    87  			nwo.DeployChaincode(network, "testchannel", orderer, chaincode)
    88  
    89  			By("getting the client peer by name")
    90  			peer := network.Peer("Org1", "peer0")
    91  
    92  			Query(network, peer, "testchannel", "100")
    93  			Invoke(network, orderer, peer, "testchannel")
    94  			Query(network, peer, "testchannel", "90")
    95  
    96  			By("getting the idemix client peer by name")
    97  			idemixOrg := network.Organization("Org3")
    98  			QueryWithIdemix(network, peer, idemixOrg, "testchannel", "90")
    99  			InvokeWithIdemix(network, orderer, peer, idemixOrg, "testchannel")
   100  			QueryWithIdemix(network, peer, idemixOrg, "testchannel", "80")
   101  			Query(network, peer, "testchannel", "80")
   102  		})
   103  	})
   104  
   105  })
   106  
   107  func Query(n *nwo.Network, peer *nwo.Peer, channel string, expectedOutput string) {
   108  	By("querying the chaincode")
   109  	sess, err := n.PeerUserSession(peer, "User1", commands.ChaincodeQuery{
   110  		ChannelID: channel,
   111  		Name:      "mycc",
   112  		Ctor:      `{"Args":["query","a"]}`,
   113  	})
   114  	Expect(err).NotTo(HaveOccurred())
   115  	Eventually(sess, n.EventuallyTimeout).Should(gexec.Exit(0))
   116  	Expect(sess).To(gbytes.Say(expectedOutput))
   117  }
   118  
   119  func QueryWithIdemix(n *nwo.Network, peer *nwo.Peer, idemixOrg *nwo.Organization, channel string, expectedOutput string) {
   120  	By("querying the chaincode")
   121  	sess, err := n.IdemixUserSession(peer, idemixOrg, "User1", commands.ChaincodeQuery{
   122  		ChannelID: channel,
   123  		Name:      "mycc",
   124  		Ctor:      `{"Args":["query","a"]}`,
   125  	})
   126  	Expect(err).NotTo(HaveOccurred())
   127  	Eventually(sess, n.EventuallyTimeout).Should(gexec.Exit(0))
   128  	Expect(sess).To(gbytes.Say(expectedOutput))
   129  }
   130  
   131  func Invoke(n *nwo.Network, orderer *nwo.Orderer, peer *nwo.Peer, channel string) {
   132  	sess, err := n.PeerUserSession(peer, "User1", commands.ChaincodeInvoke{
   133  		ChannelID: channel,
   134  		Orderer:   n.OrdererAddress(orderer, nwo.ListenPort),
   135  		Name:      "mycc",
   136  		Ctor:      `{"Args":["invoke","a","b","10"]}`,
   137  		PeerAddresses: []string{
   138  			n.PeerAddress(n.Peer("Org1", "peer0"), nwo.ListenPort),
   139  			n.PeerAddress(n.Peer("Org2", "peer0"), nwo.ListenPort),
   140  		},
   141  		WaitForEvent: true,
   142  	})
   143  	Expect(err).NotTo(HaveOccurred())
   144  	Eventually(sess, n.EventuallyTimeout).Should(gexec.Exit(0))
   145  	Expect(sess.Err).To(gbytes.Say("Chaincode invoke successful. result: status:200"))
   146  }
   147  
   148  func InvokeWithIdemix(n *nwo.Network, orderer *nwo.Orderer, peer *nwo.Peer, idemixOrg *nwo.Organization, channel string) {
   149  	sess, err := n.IdemixUserSession(peer, idemixOrg, "User1", commands.ChaincodeInvoke{
   150  		ChannelID: channel,
   151  		Orderer:   n.OrdererAddress(orderer, nwo.ListenPort),
   152  		Name:      "mycc",
   153  		Ctor:      `{"Args":["invoke","a","b","10"]}`,
   154  		PeerAddresses: []string{
   155  			n.PeerAddress(n.Peer("Org1", "peer0"), nwo.ListenPort),
   156  			n.PeerAddress(n.Peer("Org2", "peer0"), nwo.ListenPort),
   157  		},
   158  		WaitForEvent: true,
   159  	})
   160  	Expect(err).NotTo(HaveOccurred())
   161  	Eventually(sess, n.EventuallyTimeout).Should(gexec.Exit(0))
   162  	Expect(sess.Err).To(gbytes.Say("Chaincode invoke successful. result: status:200"))
   163  }