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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
|
/*
* Helper functions which connect to a server, and check its logs for particular strings.
*/
var checkLog;
(function() {
"use strict";
if (checkLog) {
return; // Protect against this file being double-loaded.
}
checkLog = (function() {
let getGlobalLog = function(conn) {
let cmdRes;
try {
cmdRes = conn.adminCommand({getLog: 'global'});
} catch (e) {
// Retry with network errors.
print("checkLog ignoring failure: " + e);
return null;
}
return assert.commandWorked(cmdRes).log;
};
/*
* Calls the 'getLog' function on the provided connection 'conn' to see if the provided msg
* is found in the logs. Note: this function does not throw an exception, so the return
* value should not be ignored.
*/
const checkContainsOnce = function(conn, msg) {
const logMessages = getGlobalLog(conn);
if (logMessages === null) {
return false;
}
if (msg instanceof RegExp) {
for (let logMsg of logMessages) {
if (logMsg.search(msg) != -1) {
return true;
}
}
} else {
for (let logMsg of logMessages) {
if (logMsg.includes(msg)) {
return true;
}
}
}
return false;
};
const checkContainsOnceJson = function(conn, id, attrsDict) {
const logMessages = getGlobalLog(conn);
if (logMessages === null) {
return false;
}
for (let logMsg of logMessages) {
let obj;
try {
obj = JSON.parse(logMsg);
} catch (ex) {
print('checkLog.checkContainsOnce: JsonJSON.parse() failed: ' + tojson(ex) + ': ' +
logMsg);
throw ex;
}
if (obj.id === id) {
let allAttrMatch = true;
for (let attrKey in attrsDict) {
const attrValue = attrsDict[attrKey];
if (attrValue instanceof Function) {
if (!attrValue(obj.attr[attrKey])) {
allAttrMatch = false;
break;
}
} else {
if (obj.attr[attrKey] !== attrValue) {
allAttrMatch = false;
break;
}
}
}
if (allAttrMatch) {
return true;
}
}
}
return false;
};
/*
* Calls the 'getLog' function on the provided connection 'conn' to see if a log with the
* provided id is found in the logs. If the id is found it looks up the specified attrribute by
* attrName and checks if the msg is found in its value. Note: this function does not throw an
* exception, so the return value should not be ignored.
*/
const checkContainsOnceJsonStringMatch = function(conn, id, attrName, msg) {
const logMessages = getGlobalLog(conn);
if (logMessages === null) {
return false;
}
for (let logMsg of logMessages) {
if (logMsg.search(`\"id\":${id},`) != -1) {
if (logMsg.search(`\"${attrName}\":\"?[^\"|\\\"]*` + msg) != -1) {
return true;
}
}
}
return false;
};
/*
* Calls the 'getLog' function at regular intervals on the provided connection 'conn' until
* the provided 'msg' is found in the logs, or it times out. Throws an exception on timeout.
*/
let contains = function(conn, msg, timeout = 5 * 60 * 1000) {
// Don't run the hang analyzer because we don't expect contains() to always succeed.
assert.soon(function() {
return checkContainsOnce(conn, msg);
}, 'Could not find log entries containing the following message: ' + msg, timeout, 300, {
runHangAnalyzer: false
});
};
let containsJson = function(conn, id, attrsDict, timeout = 5 * 60 * 1000) {
// Don't run the hang analyzer because we don't expect contains() to always succeed.
assert.soon(
function() {
return checkContainsOnceJson(conn, id, attrsDict);
},
'Could not find log entries containing the following id: ' + id +
', and attrs: ' + attrsDict,
timeout,
300,
{runHangAnalyzer: false});
};
/*
* Calls the 'getLog' function at regular intervals on the provided connection 'conn' until
* the provided 'msg' is found in the logs 'expectedCount' times, or it times out.
* Throws an exception on timeout. If 'exact' is true, checks whether the count is exactly
* equal to 'expectedCount'. Otherwise, checks whether the count is at least equal to
* 'expectedCount'. Early returns when at least 'expectedCount' entries are found.
*/
let containsWithCount = function(
conn, msg, expectedCount, timeout = 5 * 60 * 1000, exact = true) {
let expectedStr = exact ? 'exactly ' : 'at least ';
assert.soon(
function() {
let count = 0;
let logMessages = getGlobalLog(conn);
if (logMessages === null) {
return false;
}
for (let i = 0; i < logMessages.length; i++) {
if (msg instanceof RegExp) {
if (logMessages[i].search(msg) != -1) {
count++;
}
} else {
if (logMessages[i].indexOf(msg) != -1) {
count++;
}
}
if (!exact && count >= expectedCount) {
print("checkLog found at least " + expectedCount +
" log entries containing the following message: " + msg);
return true;
}
}
return exact ? expectedCount === count : expectedCount <= count;
},
'Did not find ' + expectedStr + expectedCount + ' log entries containing the ' +
'following message: ' + msg,
timeout,
300);
};
/*
* Similar to containsWithCount, but checks whether there are at least 'expectedCount'
* instances of 'msg' in the logs.
*/
let containsWithAtLeastCount = function(conn, msg, expectedCount, timeout = 5 * 60 * 1000) {
containsWithCount(conn, msg, expectedCount, timeout, /*exact*/ false);
};
/*
* Converts a scalar or object to a string format suitable for matching against log output.
* Field names are not quoted, and by default strings which are not within an enclosing
* object are not escaped. Similarly, integer values without an enclosing object are
* serialized as integers, while those within an object are serialized as floats to one
* decimal point. NumberLongs are unwrapped prior to serialization.
*/
const formatAsLogLine = function(value, escapeStrings, toDecimal) {
if (typeof value === "string") {
return (escapeStrings ? `"${value}"` : value);
} else if (typeof value === "number") {
return (Number.isInteger(value) && toDecimal ? value.toFixed(1) : value);
} else if (value instanceof NumberLong) {
return `${value}`.match(/NumberLong..(.*)../m)[1];
} else if (typeof value !== "object") {
return value;
} else if (Object.keys(value).length === 0) {
return Array.isArray(value) ? "[]" : "{}";
}
let serialized = [];
escapeStrings = toDecimal = true;
for (let fieldName in value) {
const valueStr = formatAsLogLine(value[fieldName], escapeStrings, toDecimal);
serialized.push(Array.isArray(value) ? valueStr : `${fieldName}: ${valueStr}`);
}
return (Array.isArray(value) ? `[ ${serialized.join(', ')} ]`
: `{ ${serialized.join(', ')} }`);
};
/**
* Format at the json for the log file which has no extra spaces.
*/
const formatAsJsonLogLine = function(value, escapeStrings, toDecimal) {
if (typeof value === "string") {
return (escapeStrings ? `"${value}"` : value);
} else if (typeof value === "number") {
return (Number.isInteger(value) && toDecimal ? value.toFixed(1) : value);
} else if (value instanceof NumberLong) {
return `${value}`.match(/NumberLong..(.*)../m)[1];
} else if (typeof value !== "object") {
return value;
} else if (Object.keys(value).length === 0) {
return Array.isArray(value) ? "[]" : "{}";
}
let serialized = [];
escapeStrings = toDecimal = true;
for (let fieldName in value) {
const valueStr = formatAsJsonLogLine(value[fieldName], escapeStrings, toDecimal);
serialized.push(Array.isArray(value) ? valueStr : `"${fieldName}":${valueStr}`);
}
return (Array.isArray(value) ? `[${serialized.join(',')}]` : `{${serialized.join(',')}}`);
};
return {
getGlobalLog: getGlobalLog,
checkContainsOnce: checkContainsOnce,
checkContainsOnceJson: checkContainsOnceJson,
checkContainsOnceJsonStringMatch: checkContainsOnceJsonStringMatch,
contains: contains,
containsJson: containsJson,
containsWithCount: containsWithCount,
containsWithAtLeastCount: containsWithAtLeastCount,
formatAsLogLine: formatAsLogLine,
formatAsJsonLogLine: formatAsJsonLogLine
};
})();
})();
|