Unverified Commit 5cac7c3b authored by Casey Hillers's avatar Casey Hillers Committed by GitHub

Add expectation test for upload metrics (#76642)

parent cf6d966e
......@@ -36,6 +36,7 @@ class Cocoon {
@visibleForTesting Client httpClient,
@visibleForTesting this.fs = const LocalFileSystem(),
@visibleForTesting this.processRunSync = Process.runSync,
@visibleForTesting this.requestRetryLimit = 5,
}) : _httpClient = AuthenticatedCocoonClient(serviceAccountTokenPath, httpClient: httpClient, filesystem: fs);
/// Client to make http requests to Cocoon.
......@@ -51,6 +52,9 @@ class Cocoon {
static final Logger logger = Logger('CocoonClient');
@visibleForTesting
final int requestRetryLimit;
String get commitSha => _commitSha ?? _readCommitSha();
String _commitSha;
......@@ -122,6 +126,7 @@ class Cocoon {
if (resultFile.existsSync()) {
resultFile.deleteSync();
}
logger.fine('Writing results: ' + json.encode(updateRequest));
resultFile.createSync();
resultFile.writeAsStringSync(json.encode(updateRequest));
}
......@@ -137,6 +142,7 @@ class Cocoon {
'BuilderName': builderName,
'NewStatus': result.succeeded ? 'Succeeded' : 'Failed',
};
logger.fine('Update request: $updateRequest');
// Make a copy of result data because we may alter it for validation below.
updateRequest['ResultData'] = result.data;
......@@ -177,7 +183,7 @@ class Cocoon {
final Response response = await retry(
() => _httpClient.post(url, body: json.encode(jsonData)),
retryIf: (Exception e) => e is SocketException || e is TimeoutException || e is ClientException,
maxAttempts: 5,
maxAttempts: requestRetryLimit,
);
return json.decode(response.body) as Map<String, dynamic>;
}
......
......@@ -104,6 +104,36 @@ void main() {
expect(resultJson, expectedJson);
});
test('uploads metrics sends expected post body', () async {
_processResult = ProcessResult(1, 0, commitSha, '');
const String uploadMetricsRequestWithSpaces = '{"CommitBranch":"master","CommitSha":"a4952838bf288a81d8ea11edfd4b4cd649fa94cc","BuilderName":"builder a b c","NewStatus":"Succeeded","ResultData":{},"BenchmarkScoreKeys":[]}';
final MockClient client = MockClient((Request request) async {
if (request.body == uploadMetricsRequestWithSpaces) {
return Response('{}', 200);
}
return Response('Expected: $uploadMetricsRequestWithSpaces\nReceived: ${request.body}', 500);
});
cocoon = Cocoon(
fs: fs,
httpClient: client,
processRunSync: runSyncStub,
serviceAccountTokenPath: serviceAccountTokenPath,
requestRetryLimit: 0,
);
const String resultsPath = 'results.json';
const String updateTaskJson = '{'
'"CommitBranch":"master",'
'"CommitSha":"$commitSha",'
'"BuilderName":"builder a b c",'
'"NewStatus":"Succeeded",'
'"ResultData":{},'
'"BenchmarkScoreKeys":[]}';
fs.file(resultsPath).writeAsStringSync(updateTaskJson);
await cocoon.sendResultsPath(resultsPath);
});
test('uploads expected update task payload from results file', () async {
_processResult = ProcessResult(1, 0, commitSha, '');
cocoon = Cocoon(
......@@ -111,6 +141,7 @@ void main() {
httpClient: mockClient,
processRunSync: runSyncStub,
serviceAccountTokenPath: serviceAccountTokenPath,
requestRetryLimit: 0,
);
const String resultsPath = 'results.json';
......@@ -132,6 +163,7 @@ void main() {
serviceAccountTokenPath: serviceAccountTokenPath,
fs: fs,
httpClient: mockClient,
requestRetryLimit: 0,
);
final TaskResult result = TaskResult.success(<String, dynamic>{});
......@@ -146,6 +178,7 @@ void main() {
serviceAccountTokenPath: serviceAccountTokenPath,
fs: fs,
httpClient: mockClient,
requestRetryLimit: 0,
);
final TaskResult result = TaskResult.success(<String, dynamic>{});
......@@ -160,6 +193,7 @@ void main() {
serviceAccountTokenPath: serviceAccountTokenPath,
fs: fs,
httpClient: mockClient,
requestRetryLimit: 0,
);
final TaskResult result = TaskResult.success(<String, dynamic>{});
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment