-
Notifications
You must be signed in to change notification settings - Fork 166
Tests for admin action of API exporter sync. #8278
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
516d95a
Tests for admin action of API exporter sync.
isoos 398ad25
Remove assert.
isoos d280d7f
Update app/test/admin/exported_api_sync_test.dart
isoos 091a192
Update app/test/admin/exported_api_sync_test.dart
isoos c4d5612
fix renames
isoos File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,194 @@ | ||
| // Copyright (c) 2024, 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 'package:_pub_shared/data/admin_api.dart'; | ||
| import 'package:gcloud/storage.dart'; | ||
| import 'package:pub_dev/shared/configuration.dart'; | ||
| import 'package:pub_dev/shared/storage.dart'; | ||
| import 'package:pub_dev/shared/versions.dart'; | ||
| import 'package:test/test.dart'; | ||
|
|
||
| import '../shared/test_models.dart'; | ||
| import '../shared/test_services.dart'; | ||
|
|
||
| void main() { | ||
| group('Exported API sync admin action', () { | ||
| Future<void> _sync({ | ||
| List<String>? packages, | ||
| bool forceWrite = false, | ||
| }) async { | ||
| final api = createPubApiClient(authToken: siteAdminToken); | ||
| await api.adminInvokeAction( | ||
| 'exported-api-sync', | ||
| AdminInvokeActionArguments(arguments: { | ||
| 'packages': packages?.join(' ') ?? 'ALL', | ||
| if (forceWrite) 'force-write': 'true', | ||
| }), | ||
| ); | ||
| } | ||
|
|
||
| Future<Map<String, dynamic>> _list() async { | ||
isoos marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| final data = <String, dynamic>{}; | ||
| final bucket = | ||
| storageService.bucket(activeConfiguration.exportedApiBucketName!); | ||
| await for (final entry in bucket.list(delimiter: '')) { | ||
| if (!entry.isObject) continue; | ||
| final info = await bucket.info(entry.name); | ||
| final bytes = await bucket.readAsBytes(entry.name); | ||
| data[entry.name] = { | ||
| 'updated': info.updated.toIso8601String(), | ||
| 'metadata': { | ||
| 'contentType': info.metadata.contentType, | ||
| 'custom': info.metadata.custom, | ||
| }, | ||
| 'length': bytes.length, | ||
| 'bytes': bytes.toList(), | ||
| }; | ||
| } | ||
| return data; | ||
| } | ||
|
|
||
| testWithProfile('baseline checks', fn: () async { | ||
| await _sync(); | ||
| final data = await _list(); | ||
| expect(data.keys, hasLength(greaterThan(20))); | ||
| expect(data.keys, hasLength(lessThan(40))); | ||
|
|
||
| final oxygenFiles = data.keys.where((k) => k.contains('oxygen')).toSet(); | ||
| expect(oxygenFiles, hasLength(greaterThan(5))); | ||
| expect(oxygenFiles, { | ||
| '$runtimeVersion/api/archives/oxygen-1.0.0.tar.gz', | ||
| '$runtimeVersion/api/archives/oxygen-1.2.0.tar.gz', | ||
| '$runtimeVersion/api/archives/oxygen-2.0.0-dev.tar.gz', | ||
| '$runtimeVersion/api/packages/oxygen', | ||
| '$runtimeVersion/api/packages/oxygen/advisories', | ||
| 'latest/api/archives/oxygen-1.0.0.tar.gz', | ||
| 'latest/api/archives/oxygen-1.2.0.tar.gz', | ||
| 'latest/api/archives/oxygen-2.0.0-dev.tar.gz', | ||
| 'latest/api/packages/oxygen', | ||
| 'latest/api/packages/oxygen/advisories', | ||
| }); | ||
|
|
||
| final oxygenDataJson = data['latest/api/packages/oxygen']; | ||
| expect(oxygenDataJson, { | ||
| 'updated': isNotEmpty, | ||
| 'metadata': { | ||
| 'contentType': 'application/json; charset="utf-8"', | ||
| 'custom': { | ||
| 'validated': isNotEmpty, | ||
| }, | ||
| }, | ||
| 'length': greaterThan(100), | ||
| 'bytes': isNotEmpty, | ||
| }); | ||
| }); | ||
|
|
||
| testWithProfile('deleted files + full sync', fn: () async { | ||
| await _sync(); | ||
| final oldRoot = await _list(); | ||
|
|
||
| for (final e in oldRoot.entries) { | ||
| final path = e.key; | ||
| final oldData = e.value as Map; | ||
|
|
||
| final bucket = | ||
| storageService.bucket(activeConfiguration.exportedApiBucketName!); | ||
| await bucket.delete(path); | ||
|
|
||
| await _sync(); | ||
| final newRoot = await _list(); | ||
| expect(newRoot.containsKey(path), true); | ||
| final newData = newRoot[path] as Map; | ||
| expect(oldData['bytes'], isNotEmpty); | ||
| expect(oldData['bytes'], newData['bytes']); | ||
| } | ||
| }); | ||
|
|
||
| testWithProfile('delete files + selective sync', fn: () async { | ||
| await _sync(); | ||
| final oldRoot = await _list(); | ||
|
|
||
| final oxygenFiles = | ||
| oldRoot.keys.where((k) => k.contains('oxygen')).toList(); | ||
| expect(oxygenFiles, hasLength(greaterThan(5))); | ||
|
|
||
| for (final path in oxygenFiles) { | ||
| final bucket = | ||
| storageService.bucket(activeConfiguration.exportedApiBucketName!); | ||
| await bucket.delete(path); | ||
|
|
||
| await _sync(packages: ['neon']); | ||
| expect(await bucket.tryInfo(path), isNull); | ||
|
|
||
| await _sync(packages: ['oxygen']); | ||
| expect(await bucket.tryInfo(path), isNotNull); | ||
| } | ||
| }); | ||
|
|
||
| testWithProfile('modified files + selective sync', fn: () async { | ||
| await _sync(); | ||
| final oldRoot = await _list(); | ||
|
|
||
| final oxygenFiles = | ||
| oldRoot.keys.where((k) => k.contains('oxygen')).toList(); | ||
| expect(oxygenFiles, hasLength(greaterThan(5))); | ||
|
|
||
| for (final path in oxygenFiles) { | ||
| final oldData = oldRoot[path] as Map; | ||
| final bucket = | ||
| storageService.bucket(activeConfiguration.exportedApiBucketName!); | ||
| await bucket.writeBytes(path, [1]); | ||
|
|
||
| await _sync(packages: ['neon']); | ||
| expect((await bucket.info(path)).length, 1); | ||
|
|
||
| await _sync(packages: ['oxygen']); | ||
| expect((await bucket.info(path)).length, greaterThan(1)); | ||
|
|
||
| // also check file content | ||
| final newRoot = await _list(); | ||
| final newData = newRoot[path] as Map; | ||
| expect(newData['length'], greaterThan(1)); | ||
| expect(oldData['bytes'], newData['bytes']); | ||
| } | ||
| }); | ||
|
|
||
| testWithProfile('modified metadata + selective sync', fn: () async { | ||
| await _sync(); | ||
| final oldRoot = await _list(); | ||
|
|
||
| final oxygenFiles = | ||
| oldRoot.keys.where((k) => k.contains('oxygen')).toList(); | ||
| expect(oxygenFiles, hasLength(greaterThan(5))); | ||
|
|
||
| for (final path in oxygenFiles) { | ||
| final oldData = oldRoot[path] as Map; | ||
| final bucket = | ||
| storageService.bucket(activeConfiguration.exportedApiBucketName!); | ||
| await bucket.updateMetadata( | ||
| path, | ||
| ObjectMetadata( | ||
| contentType: 'text/plain', | ||
| custom: {'x': 'x'}, | ||
| ), | ||
| ); | ||
|
|
||
| await _sync(packages: ['neon']); | ||
| expect((await bucket.info(path)).metadata.custom, {'x': 'x'}); | ||
|
|
||
| await _sync(packages: ['oxygen']); | ||
| final newInfo = await bucket.info(path); | ||
| expect( | ||
| newInfo.metadata.contentType, oldData['metadata']['contentType']); | ||
| expect(newInfo.metadata.custom, {'validated': isNotEmpty}); | ||
|
|
||
| // also check file content | ||
| final newRoot = await _list(); | ||
| final newData = newRoot[path] as Map; | ||
| expect(newData['length'], greaterThan(1)); | ||
| expect(oldData['bytes'], newData['bytes']); | ||
| } | ||
| }); | ||
| }); | ||
| } | ||
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.