-
Notifications
You must be signed in to change notification settings - Fork 9.1k
Expand file tree
/
Copy pathdiscovery_service.go
More file actions
154 lines (129 loc) · 5.41 KB
/
discovery_service.go
File metadata and controls
154 lines (129 loc) · 5.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
Copyright IBM Corp All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package runner
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"github.com/hyperledger/fabric/integration/pvtdata/helpers"
"github.com/hyperledger/fabric/protos/discovery"
. "github.com/onsi/gomega"
"github.com/tedsuo/ifrit/ginkgomon"
)
// DiscoveryService wraps sd cli and enables discovering peers, config and endorsement descriptors
type DiscoveryService struct {
// The location of the discovery service executable
Path string
// Path to the config file that will be generated in GenerateConfig function and will be used later in all other functions
ConfigFilePath string
// Log level that will be used in discovery service executable
LogLevel string
}
func (sd *DiscoveryService) setupEnvironment(cmd *exec.Cmd) {
for _, env := range os.Environ() {
cmd.Env = append(cmd.Env, env)
}
if sd.LogLevel != "" {
cmd.Env = append(cmd.Env, fmt.Sprintf("CORE_LOGGING_LEVEL=%s", sd.LogLevel))
}
}
func (sd *DiscoveryService) GenerateConfig(userCert string, userKey string, msp string, extraArgs ...string) *ginkgomon.Runner {
cmd := exec.Command(sd.Path, "saveConfig", "--configFile", sd.ConfigFilePath, "--userCert", userCert, "--userKey", userKey, "--MSP", msp)
cmd.Args = append(cmd.Args, extraArgs...)
sd.setupEnvironment(cmd)
return ginkgomon.New(ginkgomon.Config{
Name: "discover saveConfig",
AnsiColorCode: "92m",
Command: cmd,
})
}
func (sd *DiscoveryService) DiscoverPeers(channel string, server string, extraArgs ...string) *ginkgomon.Runner {
cmd := exec.Command(sd.Path, "peers", "--configFile", sd.ConfigFilePath, "--channel", channel, "--server", server)
cmd.Args = append(cmd.Args, extraArgs...)
sd.setupEnvironment(cmd)
return ginkgomon.New(ginkgomon.Config{
Name: "discover peers",
AnsiColorCode: "95m",
Command: cmd,
})
}
func (sd *DiscoveryService) DiscoverConfig(channel string, server string, extraArgs ...string) *ginkgomon.Runner {
cmd := exec.Command(sd.Path, "config", "--configFile", sd.ConfigFilePath, "--channel", channel, "--server", server)
cmd.Args = append(cmd.Args, extraArgs...)
sd.setupEnvironment(cmd)
return ginkgomon.New(ginkgomon.Config{
Name: "discover config",
AnsiColorCode: "96m",
Command: cmd,
})
}
func (sd *DiscoveryService) DiscoverEndorsers(channel string, server string, chaincodeName string, collectionName string, extraArgs ...string) *ginkgomon.Runner {
var args []string
if collectionName == "" {
args = []string{"endorsers", "--configFile", sd.ConfigFilePath, "--channel", channel, "--server", server, "--chaincode", chaincodeName}
} else {
args = []string{"endorsers", "--configFile", sd.ConfigFilePath, "--channel", channel, "--server", server, "--chaincode", chaincodeName, fmt.Sprintf("--collection=%s:%s", chaincodeName, collectionName)}
}
cmd := exec.Command(sd.Path, args...)
cmd.Args = append(cmd.Args, extraArgs...)
sd.setupEnvironment(cmd)
return ginkgomon.New(ginkgomon.Config{
Name: "discover endorsers",
AnsiColorCode: "97m",
Command: cmd,
})
}
func SetupDiscoveryService(sd *DiscoveryService, org int, configFilePath string, userCert string, userKeyDir string) *DiscoveryService {
sd.ConfigFilePath = configFilePath
sd.LogLevel = "debug"
files, err := ioutil.ReadDir(userKeyDir)
Expect(err).NotTo(HaveOccurred())
userKey := ""
Expect(len(files)).To(Equal(1))
for _, f := range files {
userKey = filepath.Join(userKeyDir, f.Name())
}
msp := fmt.Sprintf("Org%dMSP", org)
sdRunner := sd.GenerateConfig(userCert, userKey, msp)
err = helpers.Execute(sdRunner)
Expect(err).NotTo(HaveOccurred())
return sd
}
func VerifyAllPeersDiscovered(sd *DiscoveryService, expectedPeers []helpers.DiscoveredPeer, channel string, server string) ([]helpers.DiscoveredPeer, bool) {
sdRunner := sd.DiscoverPeers(channel, server)
err := helpers.Execute(sdRunner)
Expect(err).NotTo(HaveOccurred())
var discoveredPeers []helpers.DiscoveredPeer
json.Unmarshal(sdRunner.Buffer().Contents(), &discoveredPeers)
if helpers.CheckPeersContainsExpectedPeers(expectedPeers, discoveredPeers) &&
helpers.CheckPeersContainsExpectedPeers(discoveredPeers, expectedPeers) {
return discoveredPeers, true
}
return discoveredPeers, false
}
func VerifyConfigDiscovered(sd *DiscoveryService, expectedConfig discovery.ConfigResult, channel string, server string) bool {
sdRunner := sd.DiscoverConfig(channel, server)
err := helpers.Execute(sdRunner)
Expect(err).NotTo(HaveOccurred())
var config discovery.ConfigResult
json.Unmarshal(sdRunner.Buffer().Contents(), &config)
return helpers.CheckConfigContainsExpectedConfig(expectedConfig, config) &&
helpers.CheckConfigContainsExpectedConfig(config, expectedConfig)
}
func VerifyEndorsersDiscovered(sd *DiscoveryService, expectedEndorsementDescriptor helpers.EndorsementDescriptor, channel string, server string, chaincodeName string, collectionName string) bool {
sdRunner := sd.DiscoverEndorsers(channel, server, chaincodeName, collectionName)
if err := helpers.Execute(sdRunner); err != nil {
return false
}
var endorsers []helpers.EndorsementDescriptor
if err := json.Unmarshal(sdRunner.Buffer().Contents(), &endorsers); err != nil {
return false
}
return helpers.CheckEndorsementContainsExpectedEndorsement(expectedEndorsementDescriptor, endorsers[0]) &&
helpers.CheckEndorsementContainsExpectedEndorsement(endorsers[0], expectedEndorsementDescriptor)
}