Extend test runner command to update test flaky status (#86513)

This commit is contained in:
keyonghan
2021-07-19 09:46:03 -07:00
committed by GitHub
parent 1995da2c0c
commit 3aeb794298
5 changed files with 90 additions and 100 deletions

View File

@@ -1,32 +0,0 @@
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:args/command_runner.dart';
import '../framework/cocoon.dart';
class UploadMetricsCommand extends Command<void> {
UploadMetricsCommand() {
argParser.addOption('results-file', help: 'Test results JSON to upload to Cocoon.');
argParser.addOption(
'service-account-token-file',
help: 'Authentication token for uploading results.',
);
}
@override
String get name => 'upload-metrics';
@override
String get description => '[Flutter infrastructure] Upload metrics data to Cocoon';
@override
Future<void> run() async {
final String resultsPath = argResults!['results-file'] as String;
final String? serviceAccountTokenFile = argResults!['service-account-token-file'] as String?;
final Cocoon cocoon = Cocoon(serviceAccountTokenPath: serviceAccountTokenFile);
return cocoon.sendResultsPath(resultsPath);
}
}

View File

@@ -0,0 +1,50 @@
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:args/command_runner.dart';
import '../framework/cocoon.dart';
class UploadResultsCommand extends Command<void> {
UploadResultsCommand() {
argParser.addOption('results-file', help: 'Test results JSON to upload to Cocoon.');
argParser.addOption(
'service-account-token-file',
help: 'Authentication token for uploading results.',
);
argParser.addOption('test-flaky', help: 'Flag to show whether the test is flaky');
argParser.addOption(
'git-branch',
help: '[Flutter infrastructure] Git branch of the current commit. LUCI\n'
'checkouts run in detached HEAD state, so the branch must be passed.',
);
argParser.addOption('luci-builder', help: '[Flutter infrastructure] Name of the LUCI builder being run on.');
argParser.addOption('test-status', help: 'Test status: Succeeded|Failed');
}
@override
String get name => 'upload-metrics';
@override
String get description => '[Flutter infrastructure] Upload results data to Cocoon';
@override
Future<void> run() async {
final String? resultsPath = argResults!['results-file'] as String?;
final String? serviceAccountTokenFile = argResults!['service-account-token-file'] as String?;
final bool? isTestFlaky = argResults!['test-flaky'] as bool?;
final String? gitBranch = argResults!['git-branch'] as String?;
final String? builderName = argResults!['luci-builder'] as String?;
final String? testStatus = argResults!['test-status'] as String?;
final Cocoon cocoon = Cocoon(serviceAccountTokenPath: serviceAccountTokenFile);
return cocoon.sendResultsPath(
resultsPath: resultsPath,
isTestFlaky: isTestFlaky,
gitBranch: gitBranch,
builderName: builderName,
testStatus: testStatus,
);
}
}

View File

@@ -77,35 +77,28 @@ class Cocoon {
/// 1. Run DeviceLab test, writing results to a known path
/// 2. Request service account token from luci auth (valid for at least 3 minutes)
/// 3. Upload results from (1) to Cocoon
Future<void> sendResultsPath(String resultsPath) async {
final File resultFile = fs.file(resultsPath);
final Map<String, dynamic> resultsJson = json.decode(await resultFile.readAsString()) as Map<String, dynamic>;
await _sendUpdateTaskRequest(resultsJson);
}
/// Send [TaskResult] to Cocoon.
// TODO(chillers): Remove when sendResultsPath is used in prod. https://github.com/flutter/flutter/issues/72457
Future<void> sendTaskResult({
required String builderName,
required TaskResult result,
required String gitBranch,
///
/// The `resultsPath` is not available for all tests. When it doesn't show up, we
/// need to append `CommitBranch`, `CommitSha`, and `BuilderName`.
Future<void> sendResultsPath({
String? resultsPath,
bool? isTestFlaky,
String? gitBranch,
String? builderName,
String? testStatus,
}) async {
assert(builderName != null);
assert(gitBranch != null);
assert(result != null);
// Skip logging on test runs
Logger.root.level = Level.ALL;
Logger.root.onRecord.listen((LogRecord rec) {
print('${rec.level.name}: ${rec.time}: ${rec.message}');
});
final Map<String, dynamic> updateRequest = _constructUpdateRequest(
gitBranch: gitBranch,
builderName: builderName,
result: result,
);
await _sendUpdateTaskRequest(updateRequest);
Map<String, dynamic> resultsJson = <String, dynamic>{};
if (resultsPath != null) {
final File resultFile = fs.file(resultsPath);
resultsJson = json.decode(await resultFile.readAsString()) as Map<String, dynamic>;
} else {
resultsJson['CommitBranch'] = gitBranch;
resultsJson['CommitSha'] = commitSha;
resultsJson['BuilderName'] = builderName;
resultsJson['NewStatus'] = testStatus;
}
resultsJson['TestFlaky'] = isTestFlaky ?? false;
await _sendUpdateTaskRequest(resultsJson);
}
/// Write the given parameters into an update task request and store the JSON in [resultsPath].