-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdiscover.test.ts
178 lines (166 loc) · 6.99 KB
/
discover.test.ts
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
import { authorizationHeader, generateEnrolledUser, post } from "./testUtils";
import config from "../src/config";
jest.setTimeout(10000);
describe("Discover scenario", () => {
const credentials = generateEnrolledUser();
it("should get discovery results for channel", async () => {
// Given
const { token } = await credentials;
const channelName = "my-channel1";
// When
const response = await post(`/discover/${channelName}`, {}, authorizationHeader(token));
// Then
expect(response).toEqual(
expect.objectContaining({
status: 200,
body: {
response: {
msps: {
OrdererMSP: {
admins: expect.stringContaining("-----BEGIN CERTIFICATE-----"),
id: "OrdererMSP",
intermediateCerts: "",
name: "OrdererMSP",
organizationalUnitIdentifiers: [],
rootCerts: expect.stringContaining("-----BEGIN CERTIFICATE-----"),
tlsIntermediateCerts: "",
tlsRootCerts: expect.stringContaining("-----BEGIN CERTIFICATE-----"),
},
Org1MSP: {
admins: expect.stringContaining("-----BEGIN CERTIFICATE-----"),
id: "Org1MSP",
intermediateCerts: "",
name: "Org1MSP",
organizationalUnitIdentifiers: [],
rootCerts: expect.stringContaining("-----BEGIN CERTIFICATE-----"),
tlsIntermediateCerts: "",
tlsRootCerts: expect.stringContaining("-----BEGIN CERTIFICATE-----"),
},
Org2MSP: {
admins: expect.stringContaining("-----BEGIN CERTIFICATE-----"),
id: "Org2MSP",
intermediateCerts: "",
name: "Org2MSP",
organizationalUnitIdentifiers: [],
rootCerts: expect.stringContaining("-----BEGIN CERTIFICATE-----"),
tlsIntermediateCerts: "",
tlsRootCerts: expect.stringContaining("-----BEGIN CERTIFICATE-----"),
},
},
orderers: {
OrdererMSP: {
endpoints: [
{
host: "orderer0.group1.root.com",
name: "orderer0.group1.root.com:7030",
port: 7030,
},
],
},
},
peers_by_org: {
Org1MSP: {
peers: expect.arrayContaining([
{
chaincodes: [
{ name: "chaincode1", version: "1" },
{ name: "_lifecycle", version: "1" },
],
endpoint: "peer0.org1.com:7041",
ledgerHeight: { high: 0, low: expect.anything(), unsigned: true },
mspid: "Org1MSP",
name: "peer0.org1.com:7041",
},
{
chaincodes: [
{ name: "chaincode1", version: "1" },
{ name: "_lifecycle", version: "1" },
],
endpoint: "peer1.org1.com:7042",
ledgerHeight: { high: 0, low: expect.anything(), unsigned: true },
mspid: "Org1MSP",
name: "peer1.org1.com:7042",
},
]),
},
Org2MSP: {
peers: expect.arrayContaining([
{
chaincodes: [
{ name: "chaincode1", version: "1" },
{ name: "_lifecycle", version: "1" },
],
endpoint: "peer0.org2.com:7061",
ledgerHeight: { high: 0, low: expect.anything(), unsigned: true },
mspid: "Org2MSP",
name: "peer0.org2.com:7061",
},
{
chaincodes: [
{ name: "chaincode1", version: "1" },
{ name: "_lifecycle", version: "1" },
],
endpoint: "peer1.org2.com:7062",
ledgerHeight: { high: 0, low: expect.anything(), unsigned: true },
mspid: "Org2MSP",
name: "peer1.org2.com:7062",
},
]),
},
},
timestamp: expect.anything(),
},
},
}),
);
});
it("should get discovery results for both channels", async () => {
/* Rationale: Fabric's DiscoveryService uses first endpoint and fails if, for instance,
* given peer did not join the channel. That's why we use a kind of round robin strategy
* for service discovery. If one of the peers fail, we did not return error, but query
* another one. And this is the thing the test aims to verify.
*/
// Given
const discoveryEndpoints = config.discovererConfigs.map((d) => d.url);
expect(discoveryEndpoints).toEqual([
"grpcs://peer0.org2.com:7061", // has access to my-channel1
"grpcs://wrong.org2.com:9999", // unavailable
"grpcs://peer1.org2.com:7062", // has access to my-channel1 and my-channel2
]);
const { token } = await credentials;
const easyChannel = "my-channel1"; // first discoverer will return the results
const hardChannel = "my-channel2"; // third disciverer will return results
const errorChannel = "my-channel-non-existing"; // no disciverer will return results
const getEndpoints = (response: Record<string, unknown>) => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const fromOrg1 = response.body.response.peers_by_org.Org1MSP.peers.map((p) => p.endpoint);
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const fromOrg2 = response.body.response.peers_by_org.Org2MSP.peers.map((p) => p.endpoint);
return fromOrg1.concat(fromOrg2).sort();
};
// When
const easyResponse = await post(`/discover/${easyChannel}`, {}, authorizationHeader(token));
const hardResponse = await post(`/discover/${hardChannel}`, {}, authorizationHeader(token));
const errorResponse = await post(`/discover/${errorChannel}`, {}, authorizationHeader(token));
// Then
expect(easyResponse).toEqual(expect.objectContaining({ status: 200, body: expect.anything() }));
expect(getEndpoints(easyResponse)).toEqual([
"peer0.org1.com:7041",
"peer0.org2.com:7061",
"peer1.org1.com:7042",
"peer1.org2.com:7062",
]);
expect(hardResponse).toEqual(expect.objectContaining({ status: 200, body: expect.anything() }));
expect(getEndpoints(hardResponse)).toEqual(["peer1.org1.com:7042", "peer1.org2.com:7062"]);
expect(errorResponse).toEqual({
status: 500,
body: {
message: `No available discoverers for channel ${errorChannel}`,
},
});
});
});