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

     1  /*
     2  Copyright hechain All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package channelparticipation
     8  
     9  import (
    10  	"bytes"
    11  	"encoding/json"
    12  	"fmt"
    13  	"io/ioutil"
    14  	"mime/multipart"
    15  	"net/http"
    16  
    17  	"github.com/golang/protobuf/proto"
    18  	"github.com/hechain20/hechain/integration/nwo"
    19  	"github.com/hyperledger/fabric-protos-go/common"
    20  	. "github.com/onsi/gomega"
    21  	"github.com/onsi/gomega/gstruct"
    22  	"github.com/onsi/gomega/types"
    23  )
    24  
    25  func Join(n *nwo.Network, o *nwo.Orderer, channel string, block *common.Block, expectedChannelInfo ChannelInfo) {
    26  	blockBytes, err := proto.Marshal(block)
    27  	Expect(err).NotTo(HaveOccurred())
    28  	url := fmt.Sprintf("https://127.0.0.1:%d/participation/v1/channels", n.OrdererPort(o, nwo.AdminPort))
    29  	req := GenerateJoinRequest(url, channel, blockBytes)
    30  	authClient, _ := nwo.OrdererOperationalClients(n, o)
    31  
    32  	body := doBody(authClient, req)
    33  	c := &ChannelInfo{}
    34  	err = json.Unmarshal(body, c)
    35  	Expect(err).NotTo(HaveOccurred())
    36  	Expect(*c).To(Equal(expectedChannelInfo))
    37  }
    38  
    39  func GenerateJoinRequest(url, channel string, blockBytes []byte) *http.Request {
    40  	joinBody := new(bytes.Buffer)
    41  	writer := multipart.NewWriter(joinBody)
    42  	part, err := writer.CreateFormFile("config-block", fmt.Sprintf("%s.block", channel))
    43  	Expect(err).NotTo(HaveOccurred())
    44  	part.Write(blockBytes)
    45  	err = writer.Close()
    46  	Expect(err).NotTo(HaveOccurred())
    47  
    48  	req, err := http.NewRequest(http.MethodPost, url, joinBody)
    49  	Expect(err).NotTo(HaveOccurred())
    50  	req.Header.Set("Content-Type", writer.FormDataContentType())
    51  
    52  	return req
    53  }
    54  
    55  func doBody(client *http.Client, req *http.Request) []byte {
    56  	resp, err := client.Do(req)
    57  	Expect(err).NotTo(HaveOccurred())
    58  	Expect(resp.StatusCode).To(Equal(http.StatusCreated))
    59  	bodyBytes, err := ioutil.ReadAll(resp.Body)
    60  	Expect(err).NotTo(HaveOccurred())
    61  	resp.Body.Close()
    62  
    63  	return bodyBytes
    64  }
    65  
    66  type ChannelList struct {
    67  	SystemChannel *ChannelInfoShort  `json:"systemChannel"`
    68  	Channels      []ChannelInfoShort `json:"channels"`
    69  }
    70  
    71  type ChannelInfoShort struct {
    72  	Name string `json:"name"`
    73  	URL  string `json:"url"`
    74  }
    75  
    76  func List(n *nwo.Network, o *nwo.Orderer) ChannelList {
    77  	authClient, _ := nwo.OrdererOperationalClients(n, o)
    78  	listChannelsURL := fmt.Sprintf("https://127.0.0.1:%d/participation/v1/channels", n.OrdererPort(o, nwo.AdminPort))
    79  
    80  	body := getBody(authClient, listChannelsURL)()
    81  	list := &ChannelList{}
    82  	err := json.Unmarshal([]byte(body), list)
    83  	Expect(err).NotTo(HaveOccurred())
    84  
    85  	return *list
    86  }
    87  
    88  func getBody(client *http.Client, url string) func() string {
    89  	return func() string {
    90  		resp, err := client.Get(url)
    91  		Expect(err).NotTo(HaveOccurred())
    92  		bodyBytes, err := ioutil.ReadAll(resp.Body)
    93  		Expect(err).NotTo(HaveOccurred())
    94  		resp.Body.Close()
    95  		return string(bodyBytes)
    96  	}
    97  }
    98  
    99  type ChannelInfo struct {
   100  	Name              string `json:"name"`
   101  	URL               string `json:"url"`
   102  	Status            string `json:"status"`
   103  	ConsensusRelation string `json:"consensusRelation"`
   104  	Height            uint64 `json:"height"`
   105  }
   106  
   107  func ListOne(n *nwo.Network, o *nwo.Orderer, channel string) ChannelInfo {
   108  	authClient, _ := nwo.OrdererOperationalClients(n, o)
   109  	listChannelURL := fmt.Sprintf("https://127.0.0.1:%d/participation/v1/channels/%s", n.OrdererPort(o, nwo.AdminPort), channel)
   110  
   111  	body := getBody(authClient, listChannelURL)()
   112  	c := &ChannelInfo{}
   113  	err := json.Unmarshal([]byte(body), c)
   114  	Expect(err).NotTo(HaveOccurred())
   115  	return *c
   116  }
   117  
   118  func Remove(n *nwo.Network, o *nwo.Orderer, channel string) {
   119  	authClient, _ := nwo.OrdererOperationalClients(n, o)
   120  	url := fmt.Sprintf("https://127.0.0.1:%d/participation/v1/channels/%s", n.OrdererPort(o, nwo.AdminPort), channel)
   121  
   122  	req, err := http.NewRequest(http.MethodDelete, url, nil)
   123  	Expect(err).NotTo(HaveOccurred())
   124  
   125  	resp, err := authClient.Do(req)
   126  	Expect(err).NotTo(HaveOccurred())
   127  	Expect(resp.StatusCode).To(Equal(http.StatusNoContent))
   128  }
   129  
   130  func ChannelListMatcher(list ChannelList, expectedChannels []string, systemChannel ...string) {
   131  	Expect(list).To(gstruct.MatchFields(gstruct.IgnoreExtras, gstruct.Fields{
   132  		"Channels":      channelsMatcher(expectedChannels),
   133  		"SystemChannel": systemChannelMatcher(systemChannel...),
   134  	}))
   135  }
   136  
   137  func channelsMatcher(channels []string) types.GomegaMatcher {
   138  	if len(channels) == 0 {
   139  		return BeEmpty()
   140  	}
   141  	matchers := make([]types.GomegaMatcher, len(channels))
   142  	for i, channel := range channels {
   143  		matchers[i] = channelInfoShortMatcher(channel)
   144  	}
   145  	return ConsistOf(matchers)
   146  }
   147  
   148  func systemChannelMatcher(systemChannel ...string) types.GomegaMatcher {
   149  	if len(systemChannel) == 0 {
   150  		return BeNil()
   151  	}
   152  	return gstruct.PointTo(channelInfoShortMatcher(systemChannel[0]))
   153  }
   154  
   155  func channelInfoShortMatcher(channel string) types.GomegaMatcher {
   156  	return gstruct.MatchFields(gstruct.IgnoreExtras, gstruct.Fields{
   157  		"Name": Equal(channel),
   158  		"URL":  Equal(fmt.Sprintf("/participation/v1/channels/%s", channel)),
   159  	})
   160  }