-
Notifications
You must be signed in to change notification settings - Fork 146
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
End2end test for dockerized pub_worker build and execution. (#7301)
- Loading branch information
Showing
3 changed files
with
165 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
import 'dart:convert'; | ||
import 'dart:io'; | ||
|
||
import 'package:pub_worker/payload.dart'; | ||
|
||
final _ciEnv = Platform.environment['CI']?.toLowerCase(); | ||
final _isRunningOnCI = _ciEnv == 'true' || _ciEnv == '1'; | ||
|
||
Future<void> buildDockerImage() async { | ||
final gitRootDir = | ||
(await Process.run('git', ['rev-parse', '--show-toplevel'])) | ||
.stdout | ||
.toString() | ||
.trim(); | ||
final pr = await Process.start( | ||
'docker', | ||
[ | ||
'build', | ||
'.', | ||
'--tag=pub_worker', | ||
'--file=Dockerfile.worker', | ||
], | ||
workingDirectory: gitRootDir, | ||
mode: ProcessStartMode.inheritStdio, | ||
); | ||
final exitCode = await pr.exitCode; | ||
if (exitCode != 0) { | ||
throw Exception('process returned with exit code $exitCode'); | ||
} | ||
} | ||
|
||
Future<Process> startDockerAnalysis(Payload payload) async { | ||
return await Process.start( | ||
'docker', | ||
<String>[ | ||
'run', | ||
if (!_isRunningOnCI) '-it', | ||
'--network=host', | ||
'--entrypoint=dart', | ||
'--rm', | ||
'pub_worker', | ||
'bin/pub_worker.dart', | ||
json.encode(payload), | ||
], | ||
mode: ProcessStartMode.inheritStdio, | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
import 'dart:convert'; | ||
import 'dart:io'; | ||
|
||
import 'package:http/http.dart' as http; | ||
import 'package:pana/pana.dart'; | ||
import 'package:pub_worker/payload.dart'; | ||
import 'package:pub_worker/src/testing/docker_utils.dart'; | ||
import 'package:pub_worker/src/testing/server.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
void main() { | ||
group('Dockerized end2end tests', () { | ||
final server = PubWorkerTestServer( | ||
[], | ||
fallbackPubHostedUrl: 'https://pub.dev', | ||
); | ||
setUpAll( | ||
() async { | ||
await server.start(); | ||
}, | ||
); | ||
|
||
tearDownAll(() async { | ||
await server.stop(); | ||
}); | ||
|
||
Future<String> analyzePackage(String package, [String? version]) async { | ||
if (version == null) { | ||
final url = server.baseUrl.resolve('/api/packages/$package'); | ||
final rs = await http.get(url); | ||
if (rs.statusCode != 200) { | ||
throw Exception('Unexpected status code on $url: ${rs.statusCode}'); | ||
} | ||
final map = json.decode(rs.body) as Map; | ||
version = (map['latest'] as Map)['version'] as String; | ||
} | ||
|
||
final payload = Payload( | ||
package: package, | ||
pubHostedUrl: '${server.baseUrl}', | ||
versions: [ | ||
VersionTokenPair( | ||
version: version, | ||
token: 'secret-token', | ||
), | ||
], | ||
); | ||
|
||
final p = await startDockerAnalysis(payload); | ||
final exitCode = await p.exitCode; | ||
if (exitCode != 0) { | ||
throw Exception( | ||
'Failed to analyze $package $version with exitCode $exitCode'); | ||
} | ||
|
||
return version; | ||
} | ||
|
||
// TODO: investigate why this is not running on GitHub properly | ||
test( | ||
'build and use docker image to analyze packages', | ||
() async { | ||
await buildDockerImage(); | ||
|
||
final packages = ['retry', 'url_launcher']; | ||
final versions = | ||
await Future.wait(packages.map((p) => analyzePackage(p))); | ||
|
||
for (var i = 0; i < packages.length; i++) { | ||
final package = packages[i]; | ||
final version = versions[i]; | ||
final result = await server.waitForResult(package, version); | ||
|
||
final docIndex = result.index.lookup('doc/index.html'); | ||
expect(docIndex, isNotNull); | ||
|
||
final panaSummaryBytes = result.lookup('summary.json'); | ||
expect(panaSummaryBytes, isNotNull); | ||
final summary = Summary.fromJson( | ||
json.decode(utf8.decode(gzip.decode(panaSummaryBytes!))) | ||
as Map<String, dynamic>); | ||
final report = summary.report!; | ||
expect(report.maxPoints, greaterThan(100)); | ||
expect(report.grantedPoints, report.maxPoints); | ||
} | ||
|
||
// TODO: consider docker cleanup | ||
}, | ||
timeout: Timeout(Duration(minutes: 15)), | ||
); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters