Skip to content

Commit

Permalink
Allow parse_and_send to use access tokens (flutter#22019)
Browse files Browse the repository at this point in the history
  • Loading branch information
liyuqian committed Oct 29, 2020
1 parent e61e8c2 commit acece00
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 17 deletions.
11 changes: 2 additions & 9 deletions .cirrus.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,9 @@ task:
ninja -C out/host_release
benchmark_host_script: |
cd $ENGINE_PATH/src/out/host_release/
./txt_benchmarks --benchmark_format=json > txt_benchmarks.json
./fml_benchmarks --benchmark_format=json > fml_benchmarks.json
./shell_benchmarks --benchmark_format=json > shell_benchmarks.json
./ui_benchmarks --benchmark_format=json > ui_benchmarks.json
$ENGINE_PATH/src/flutter/testing/benchmark/generate_metrics.sh
cd $ENGINE_PATH/src/flutter/testing/benchmark
pub get
dart bin/parse_and_send.dart ../../../out/host_release/txt_benchmarks.json
dart bin/parse_and_send.dart ../../../out/host_release/fml_benchmarks.json
dart bin/parse_and_send.dart ../../../out/host_release/shell_benchmarks.json
dart bin/parse_and_send.dart ../../../out/host_release/ui_benchmarks.json
upload_metrics.sh
# The following test depends on Flutter framework repo. It may fail if the
# framework repo is currently broken.
Expand Down
29 changes: 22 additions & 7 deletions testing/benchmark/bin/parse_and_send.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,33 @@ Future<List<FlutterEngineMetricPoint>> _parse(String jsonFileName) async {
return points;
}

Future<FlutterDestination> connectFlutterDestination() async {
const String kTokenPath = 'TOKEN_PATH';
const String kGcpProject = 'GCP_PROJECT';
final Map<String, String> env = Platform.environment;
if (env.containsKey(kTokenPath) && env.containsKey(kGcpProject)) {
return FlutterDestination.makeFromAccessToken(
File(env[kTokenPath]).readAsStringSync(),
env[kGcpProject],
);
}
return await FlutterDestination.makeFromCredentialsJson(
jsonDecode(Platform.environment['BENCHMARK_GCP_CREDENTIALS'])
as Map<String, dynamic>,
);
}

Future<void> main(List<String> args) async {
if (args.length != 1) {
throw 'Must have one argument: <benchmark_json_file>';
}
final List<FlutterEngineMetricPoint> points = await _parse(args[0]);

// The data will be sent to the Datastore of the GCP project specified through
// environment variable BENCHMARK_GCP_CREDENTIALS. The engine Cirrus job has
// currently configured the GCP project to flutter-cirrus for test. We'll
// eventually migrate to flutter-infra project once the test is done.
final FlutterDestination destination =
await FlutterDestination.makeFromCredentialsJson(
jsonDecode(Platform.environment['BENCHMARK_GCP_CREDENTIALS']),
);
// environment variable BENCHMARK_GCP_CREDENTIALS, or TOKEN_PATH/GCP_PROJECT.
// The engine Cirrus job has currently configured the GCP project to
// flutter-cirrus for test. We'll eventually migrate to flutter-infra project
// once the test is done.
final FlutterDestination destination = await connectFlutterDestination();
await destination.update(points);
}
27 changes: 26 additions & 1 deletion testing/benchmark/test/parse_and_send_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@
// found in the LICENSE file.

// @dart = 2.6
import 'dart:convert';
import 'dart:io';

import 'package:gcloud/src/datastore_impl.dart';
import 'package:googleapis_auth/auth_io.dart';
import 'package:path/path.dart' as path;
import 'package:test/test.dart';

void main() {
// In order to run this test, one should download a service account
// In order to run these tests, one should download a service account
// credentials json from a test GCP project, and put that json as
// `secret/test_gcp_credentials.json`. There's a `flutter-test` project for
// Flutter team members.
Expand All @@ -22,4 +26,25 @@ void main() {
'BENCHMARK_GCP_CREDENTIALS': testCred,
});
});

test('parse_and_send succeeds with access token.', () async {
final dynamic testCred =
jsonDecode(File('secret/test_gcp_credentials.json').readAsStringSync())
as Map<String, dynamic>;
final AutoRefreshingAuthClient client = await clientViaServiceAccount(
ServiceAccountCredentials.fromJson(testCred),
DatastoreImpl.SCOPES,
);
final String tokenPath =
path.join(Directory.systemTemp.absolute.path, 'parse_and_send_token');
File(tokenPath).writeAsStringSync(client.credentials.accessToken.data);
final ProcessResult result = Process.runSync('dart', <String>[
'bin/parse_and_send.dart',
'example/txt_benchmarks.json',
], environment: <String, String>{
'TOKEN_PATH': tokenPath,
'GCP_PROJECT': testCred['project_id'] as String,
});
expect(result.exitCode, 0);
});
}

0 comments on commit acece00

Please sign in to comment.