OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 library gcloud.test.common_e2e; |
| 6 |
| 7 import 'dart:async'; |
| 8 import 'dart:io'; |
| 9 |
| 10 import 'package:unittest/unittest.dart'; |
| 11 import 'package:googleapis_auth/auth_io.dart' as auth; |
| 12 import 'package:http/http.dart' as http; |
| 13 |
| 14 import 'common.dart'; |
| 15 |
| 16 const PROJECT = 'test-project'; |
| 17 |
| 18 // Enviroment variables for specifying the cloud project to use and the |
| 19 // location of the service account key for that project. |
| 20 const String PROJECT_ENV = 'GCLOUD_E2E_TEST_PROJECT'; |
| 21 const String SERVICE_KEY_LOCATION_ENV = 'GCLOUD_E2E_TEST_KEY'; |
| 22 |
| 23 // Default project and service key location used when running on the package |
| 24 // bot. |
| 25 const String DEFAULT_PROJECT = 'dart-gcloud-e2e'; |
| 26 const String DEFAULT_KEY_LOCATION = |
| 27 'gs://dart-archive-internal/keys/dart-gcloud-e2e.json'; |
| 28 |
| 29 // Used for storage e2e tests: |
| 30 // |
| 31 // List operations on buckets are eventually consistent. Bucket deletion is |
| 32 // also dependent on list operations to ensure the bucket is empty before |
| 33 // deletion. |
| 34 // |
| 35 // So this can make tests flaky. The following delay is introduced as an |
| 36 // attempt to account for that. |
| 37 const STORAGE_LIST_DELAY = const Duration(seconds: 5); |
| 38 |
| 39 |
| 40 bool onBot() { |
| 41 // When running on the package-bot the current user is chrome-bot. |
| 42 var envName; |
| 43 if (Platform.isWindows) { |
| 44 envName = 'USERNAME'; |
| 45 } else { |
| 46 envName = 'USER'; |
| 47 } |
| 48 return Platform.environment[envName] == 'chrome-bot'; |
| 49 } |
| 50 |
| 51 // Get the service key from the specified location. |
| 52 Future<String> serviceKeyJson(String serviceKeyLocation) { |
| 53 if (!serviceKeyLocation.startsWith('gs://')) { |
| 54 throw new Exception('Service key location must start with gs://'); |
| 55 } |
| 56 var future; |
| 57 if (onBot()) { |
| 58 future = Process.run( |
| 59 'python', ['third_party/gsutil/gsutil', 'cat', serviceKeyLocation], |
| 60 runInShell: true); |
| 61 } else { |
| 62 var gsutil = Platform.isWindows ? 'gsutil.cmd' : 'gsutil'; |
| 63 future = Process.run(gsutil, ['cat', serviceKeyLocation]); |
| 64 } |
| 65 return future.then((result) { |
| 66 if (result.exitCode != 0) { |
| 67 throw new Exception('Failed to run gsutil, ${result.stderr}'); |
| 68 } |
| 69 return result.stdout; |
| 70 }); |
| 71 } |
| 72 |
| 73 typedef Future AuthCallback(String project, http.Client client); |
| 74 |
| 75 Future withAuthClient(List<String> scopes, |
| 76 AuthCallback callback, |
| 77 {bool trace: false}) { |
| 78 String project = Platform.environment[PROJECT_ENV]; |
| 79 String serviceKeyLocation = Platform.environment[SERVICE_KEY_LOCATION_ENV]; |
| 80 |
| 81 if (!onBot() && (project == null || serviceKeyLocation == null)) { |
| 82 throw new StateError( |
| 83 'Envoronment variables $PROJECT_ENV and $SERVICE_KEY_LOCATION_ENV ' |
| 84 'required when not running on the package bot'); |
| 85 } |
| 86 |
| 87 project = project != null ? project : DEFAULT_PROJECT; |
| 88 serviceKeyLocation = |
| 89 serviceKeyLocation != null ? serviceKeyLocation : DEFAULT_KEY_LOCATION; |
| 90 |
| 91 return serviceKeyJson(serviceKeyLocation).then((keyJson) { |
| 92 var creds = new auth.ServiceAccountCredentials.fromJson(keyJson); |
| 93 return auth.clientViaServiceAccount(creds, scopes).then((client) { |
| 94 if (trace) client = new TraceClient(client); |
| 95 return callback(project, client).whenComplete(() => client.close()); |
| 96 }); |
| 97 }); |
| 98 } |
| 99 |
| 100 Future runE2EUnittest(Function callback) { |
| 101 var config = new E2EConfiguration(); |
| 102 |
| 103 unittestConfiguration = config; |
| 104 callback(); |
| 105 |
| 106 return config.done; |
| 107 } |
| 108 |
| 109 class E2EConfiguration extends SimpleConfiguration { |
| 110 final Completer _completer = new Completer(); |
| 111 |
| 112 Future get done => _completer.future; |
| 113 |
| 114 onDone(success) { |
| 115 new Future.sync(() { |
| 116 super.onDone(success); |
| 117 }).then((_) => _completer.complete(_)) |
| 118 .catchError((error, stack) => _completer.completeError(error, stack)); |
| 119 } |
| 120 } |
OLD | NEW |