artifact_updater_test.dart 19.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
// 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:file/memory.dart';
import 'package:file_testing/file_testing.dart';
import 'package:flutter_tools/src/base/file_system.dart';
import 'package:flutter_tools/src/base/io.dart';
import 'package:flutter_tools/src/base/logger.dart';
import 'package:flutter_tools/src/base/os.dart';
11
import 'package:flutter_tools/src/base/platform.dart';
12
import 'package:flutter_tools/src/base/terminal.dart';
13
import 'package:flutter_tools/src/cache.dart';
14
import 'package:test/fake.dart';
15 16

import '../src/common.dart';
17
import '../src/fake_http_client.dart';
18
import '../src/fakes.dart';
19

20
final Platform testPlatform = FakePlatform();
21

22 23
void main() {
  testWithoutContext('ArtifactUpdater can download a zip archive', () async {
24
    final FakeOperatingSystemUtils operatingSystemUtils = FakeOperatingSystemUtils();
25 26 27 28 29 30
    final MemoryFileSystem fileSystem = MemoryFileSystem.test();
    final BufferLogger logger = BufferLogger.test();
    final ArtifactUpdater artifactUpdater = ArtifactUpdater(
      fileSystem: fileSystem,
      logger: logger,
      operatingSystemUtils: operatingSystemUtils,
31
      platform: testPlatform,
32
      httpClient: FakeHttpClient.any(),
33 34 35 36 37 38
      tempStorage: fileSystem.currentDirectory.childDirectory('temp')
        ..createSync(),
    );

    await artifactUpdater.downloadZipArchive(
      'test message',
39
      Uri.parse('http://test.zip'),
40 41 42 43 44 45
      fileSystem.currentDirectory.childDirectory('out'),
    );
    expect(logger.statusText, contains('test message'));
    expect(fileSystem.file('out/test'), exists);
  });

46
  testWithoutContext('ArtifactUpdater can download a zip archive and delete stale files', () async {
47
    final FakeOperatingSystemUtils operatingSystemUtils = FakeOperatingSystemUtils();
48 49 50 51 52 53 54
    final MemoryFileSystem fileSystem = MemoryFileSystem.test();
    final BufferLogger logger = BufferLogger.test();
    final ArtifactUpdater artifactUpdater = ArtifactUpdater(
      fileSystem: fileSystem,
      logger: logger,
      operatingSystemUtils: operatingSystemUtils,
      platform: testPlatform,
55
      httpClient: FakeHttpClient.any(),
56 57 58 59 60 61 62 63 64 65
      tempStorage: fileSystem.currentDirectory.childDirectory('temp')
        ..createSync(),
    );
    // Unrelated file from another cache.
    fileSystem.file('out/bar').createSync(recursive: true);
    // Stale file from current cache.
    fileSystem.file('out/test/foo.txt').createSync(recursive: true);

    await artifactUpdater.downloadZipArchive(
      'test message',
66
      Uri.parse('http://test.zip'),
67 68 69 70 71 72 73 74
      fileSystem.currentDirectory.childDirectory('out'),
    );
    expect(logger.statusText, contains('test message'));
    expect(fileSystem.file('out/test'), exists);
    expect(fileSystem.file('out/bar'), exists);
    expect(fileSystem.file('out/test/foo.txt'), isNot(exists));
  });

75 76
  testWithoutContext('ArtifactUpdater will not validate the md5 hash if the '
    'x-goog-hash header is present but missing an md5 entry', () async {
77
    final FakeOperatingSystemUtils operatingSystemUtils = FakeOperatingSystemUtils();
78 79 80 81 82 83 84 85
    final MemoryFileSystem fileSystem = MemoryFileSystem.test();
    final BufferLogger logger = BufferLogger.test();

    final ArtifactUpdater artifactUpdater = ArtifactUpdater(
      fileSystem: fileSystem,
      logger: logger,
      operatingSystemUtils: operatingSystemUtils,
      platform: testPlatform,
86
      httpClient: FakeHttpClient.list(<FakeRequest>[
87
        FakeRequest(Uri.parse('http://test.zip'), response: const FakeResponse(
88 89 90 91 92
          headers: <String, List<String>>{
            'x-goog-hash': <String>[],
          }
        )),
      ]),
93 94 95 96 97 98
      tempStorage: fileSystem.currentDirectory.childDirectory('temp')
        ..createSync(),
    );

    await artifactUpdater.downloadZipArchive(
      'test message',
99
      Uri.parse('http://test.zip'),
100 101 102 103 104 105 106 107
      fileSystem.currentDirectory.childDirectory('out'),
    );
    expect(logger.statusText, contains('test message'));
    expect(fileSystem.file('out/test'), exists);
  });

  testWithoutContext('ArtifactUpdater will validate the md5 hash if the '
    'x-goog-hash header is present', () async {
108
    final FakeOperatingSystemUtils operatingSystemUtils = FakeOperatingSystemUtils();
109 110 111 112 113 114 115 116
    final MemoryFileSystem fileSystem = MemoryFileSystem.test();
    final BufferLogger logger = BufferLogger.test();

    final ArtifactUpdater artifactUpdater = ArtifactUpdater(
      fileSystem: fileSystem,
      logger: logger,
      operatingSystemUtils: operatingSystemUtils,
      platform: testPlatform,
117
      httpClient: FakeHttpClient.list(<FakeRequest>[
118
        FakeRequest(Uri.parse('http://test.zip'), response: const FakeResponse(
119 120 121 122 123 124 125 126 127
          body: <int>[0],
          headers: <String, List<String>>{
            'x-goog-hash': <String>[
              'foo-bar-baz',
              'md5=k7iFrf4NoInN9jSQT9WfcQ=='
            ],
          }
        )),
      ]),
128 129 130 131 132 133
      tempStorage: fileSystem.currentDirectory.childDirectory('temp')
        ..createSync(),
    );

    await artifactUpdater.downloadZipArchive(
      'test message',
134
      Uri.parse('http://test.zip'),
135 136 137 138 139 140 141 142
      fileSystem.currentDirectory.childDirectory('out'),
    );
    expect(logger.statusText, contains('test message'));
    expect(fileSystem.file('out/test'), exists);
  });

  testWithoutContext('ArtifactUpdater will validate the md5 hash if the '
    'x-goog-hash header is present and throw if it does not match', () async {
143
    final FakeOperatingSystemUtils operatingSystemUtils = FakeOperatingSystemUtils();
144 145 146 147 148 149 150 151
    final MemoryFileSystem fileSystem = MemoryFileSystem.test();
    final BufferLogger logger = BufferLogger.test();

    final ArtifactUpdater artifactUpdater = ArtifactUpdater(
      fileSystem: fileSystem,
      logger: logger,
      operatingSystemUtils: operatingSystemUtils,
      platform: testPlatform,
152
      httpClient: FakeHttpClient.list(<FakeRequest>[
153
         FakeRequest(Uri.parse('http://test.zip'), response: const FakeResponse(
154 155 156 157 158 159 160 161
           body: <int>[0],
           headers: <String, List<String>>{
             'x-goog-hash': <String>[
              'foo-bar-baz',
              'md5=k7iFrf4SQT9WfcQ=='
            ],
          }
        )),
162
       FakeRequest(Uri.parse('http://test.zip'), response: const FakeResponse(
163 164 165 166 167 168 169 170
           headers: <String, List<String>>{
             'x-goog-hash': <String>[
              'foo-bar-baz',
              'md5=k7iFrf4SQT9WfcQ=='
            ],
          }
        )),
      ]),
171 172 173 174
      tempStorage: fileSystem.currentDirectory.childDirectory('temp')
        ..createSync(),
    );

175
    await expectLater(() async => artifactUpdater.downloadZipArchive(
176
      'test message',
177
      Uri.parse('http://test.zip'),
178 179 180 181
      fileSystem.currentDirectory.childDirectory('out'),
    ), throwsToolExit(message: 'k7iFrf4SQT9WfcQ==')); // validate that the hash mismatch message is included.
  });

182
  testWithoutContext('ArtifactUpdater will restart the status ticker if it needs to retry the download', () async {
183
    final FakeOperatingSystemUtils operatingSystemUtils = FakeOperatingSystemUtils();
184 185 186
    final MemoryFileSystem fileSystem = MemoryFileSystem.test();
    final Logger logger = StdoutLogger(
      terminal: Terminal.test(supportsColor: true),
187
      stdio: FakeStdio(),
188 189 190 191 192 193 194
      outputPreferences: OutputPreferences.test(),
    );
    final ArtifactUpdater artifactUpdater = ArtifactUpdater(
      fileSystem: fileSystem,
      logger: logger,
      operatingSystemUtils: operatingSystemUtils,
      platform: testPlatform,
195
      httpClient: FakeHttpClient.list(<FakeRequest>[
196 197
        FakeRequest(Uri.parse('http://test.zip'), responseError: const HttpException('')),
        FakeRequest(Uri.parse('http://test.zip')),
198
      ]),
199 200 201 202 203 204
      tempStorage: fileSystem.currentDirectory.childDirectory('temp')
        ..createSync(),
    );

    await artifactUpdater.downloadZipArchive(
      'test message',
205
      Uri.parse('http://test.zip'),
206 207 208 209 210 211
      fileSystem.currentDirectory.childDirectory('out'),
    );

    expect(fileSystem.file('out/test'), exists);
  });

212
  testWithoutContext('ArtifactUpdater will re-attempt on a non-200 response', () async {
213
    final FakeOperatingSystemUtils operatingSystemUtils = FakeOperatingSystemUtils();
214 215
    final MemoryFileSystem fileSystem = MemoryFileSystem.test();
    final BufferLogger logger = BufferLogger.test();
216

217 218 219 220 221
    final ArtifactUpdater artifactUpdater = ArtifactUpdater(
      fileSystem: fileSystem,
      logger: logger,
      operatingSystemUtils: operatingSystemUtils,
      platform: testPlatform,
222
      httpClient: FakeHttpClient.list(<FakeRequest>[
223 224
        FakeRequest(Uri.parse('http://test.zip'), response: const FakeResponse(statusCode: HttpStatus.preconditionFailed)),
        FakeRequest(Uri.parse('http://test.zip'), response: const FakeResponse(statusCode: HttpStatus.preconditionFailed)),
225
      ]),
226 227 228 229
      tempStorage: fileSystem.currentDirectory.childDirectory('temp')
        ..createSync(),
    );

230
    await expectLater(() async => artifactUpdater.downloadZipArchive(
231
      'test message',
232
      Uri.parse('http://test.zip'),
233 234 235 236 237 238 239 240
      fileSystem.currentDirectory.childDirectory('out'),
    ), throwsToolExit());

    expect(logger.statusText, contains('test message'));
    expect(fileSystem.file('out/test'), isNot(exists));
  });

  testWithoutContext('ArtifactUpdater will tool exit on an ArgumentError from http client with base url override', () async {
241
    final FakeOperatingSystemUtils operatingSystemUtils = FakeOperatingSystemUtils();
242 243 244 245 246 247 248 249 250 251 252
    final MemoryFileSystem fileSystem = MemoryFileSystem.test();
    final BufferLogger logger = BufferLogger.test();
    final ArtifactUpdater artifactUpdater = ArtifactUpdater(
      fileSystem: fileSystem,
      logger: logger,
      operatingSystemUtils: operatingSystemUtils,
      platform: FakePlatform(
        environment: <String, String>{
          'FLUTTER_STORAGE_BASE_URL': 'foo-bar'
        },
      ),
253 254 255
      httpClient: FakeHttpClient.list(<FakeRequest>[
        FakeRequest(Uri.parse('http:///foo-bar/test.zip'), responseError: ArgumentError())
      ]),
256 257 258 259
      tempStorage: fileSystem.currentDirectory.childDirectory('temp')
        ..createSync(),
    );

260
    await expectLater(() async => artifactUpdater.downloadZipArchive(
261 262 263 264 265 266 267 268 269 270
      'test message',
      Uri.parse('http:///foo-bar/test.zip'),
      fileSystem.currentDirectory.childDirectory('out'),
    ), throwsToolExit());

    expect(logger.statusText, contains('test message'));
    expect(fileSystem.file('out/test'), isNot(exists));
  });

  testWithoutContext('ArtifactUpdater will rethrow on an ArgumentError from http client without base url override', () async {
271
    final FakeOperatingSystemUtils operatingSystemUtils = FakeOperatingSystemUtils();
272 273 274 275 276 277 278
    final MemoryFileSystem fileSystem = MemoryFileSystem.test();
    final BufferLogger logger = BufferLogger.test();
    final ArtifactUpdater artifactUpdater = ArtifactUpdater(
      fileSystem: fileSystem,
      logger: logger,
      operatingSystemUtils: operatingSystemUtils,
      platform: testPlatform,
279
      httpClient: FakeHttpClient.list(<FakeRequest>[
280
        FakeRequest(Uri.parse('http://test.zip'), responseError: ArgumentError()),
281
      ]),
282 283 284 285
      tempStorage: fileSystem.currentDirectory.childDirectory('temp')
        ..createSync(),
    );

286
    await expectLater(() async => artifactUpdater.downloadZipArchive(
287
      'test message',
288
      Uri.parse('http://test.zip'),
289
      fileSystem.currentDirectory.childDirectory('out'),
290
    ), throwsArgumentError);
291 292 293 294 295

    expect(logger.statusText, contains('test message'));
    expect(fileSystem.file('out/test'), isNot(exists));
  });

296
  testWithoutContext('ArtifactUpdater will re-download a file if unzipping fails', () async {
297
    final FakeOperatingSystemUtils operatingSystemUtils = FakeOperatingSystemUtils();
298 299 300 301 302 303
    final MemoryFileSystem fileSystem = MemoryFileSystem.test();
    final BufferLogger logger = BufferLogger.test();
    final ArtifactUpdater artifactUpdater = ArtifactUpdater(
      fileSystem: fileSystem,
      logger: logger,
      operatingSystemUtils: operatingSystemUtils,
304
      platform: testPlatform,
305
      httpClient: FakeHttpClient.any(),
306 307 308 309 310 311 312
      tempStorage: fileSystem.currentDirectory.childDirectory('temp')
        ..createSync(),
    );
    operatingSystemUtils.failures = 1;

    await artifactUpdater.downloadZipArchive(
      'test message',
313
      Uri.parse('http://test.zip'),
314 315 316 317 318 319
      fileSystem.currentDirectory.childDirectory('out'),
    );
    expect(logger.statusText, contains('test message'));
    expect(fileSystem.file('out/test'), exists);
  });

320
  testWithoutContext('ArtifactUpdater will de-download a file if unzipping fails on windows', () async {
321
    final FakeOperatingSystemUtils operatingSystemUtils = FakeOperatingSystemUtils(windows: true);
322 323 324 325 326 327 328
    final MemoryFileSystem fileSystem = MemoryFileSystem.test();
    final BufferLogger logger = BufferLogger.test();
    final ArtifactUpdater artifactUpdater = ArtifactUpdater(
      fileSystem: fileSystem,
      logger: logger,
      operatingSystemUtils: operatingSystemUtils,
      platform: testPlatform,
329
      httpClient: FakeHttpClient.any(),
330 331 332 333 334 335 336
      tempStorage: fileSystem.currentDirectory.childDirectory('temp')
        ..createSync(),
    );
    operatingSystemUtils.failures = 1;

    await artifactUpdater.downloadZipArchive(
      'test message',
337
      Uri.parse('http://test.zip'),
338 339 340 341 342 343
      fileSystem.currentDirectory.childDirectory('out'),
    );
    expect(logger.statusText, contains('test message'));
    expect(fileSystem.file('out/test'), exists);
  });

344
  testWithoutContext('ArtifactUpdater will bail with a tool exit if unzipping fails more than twice', () async {
345
    final FakeOperatingSystemUtils operatingSystemUtils = FakeOperatingSystemUtils();
346 347 348 349 350 351
    final MemoryFileSystem fileSystem = MemoryFileSystem.test();
    final BufferLogger logger = BufferLogger.test();
    final ArtifactUpdater artifactUpdater = ArtifactUpdater(
      fileSystem: fileSystem,
      logger: logger,
      operatingSystemUtils: operatingSystemUtils,
352
      platform: testPlatform,
353
      httpClient: FakeHttpClient.any(),
354 355 356 357 358 359 360
      tempStorage: fileSystem.currentDirectory.childDirectory('temp')
        ..createSync(),
    );
    operatingSystemUtils.failures = 2;

    expect(artifactUpdater.downloadZipArchive(
      'test message',
361
      Uri.parse('http://test.zip'),
362
      fileSystem.currentDirectory.childDirectory('out'),
363
    ), throwsToolExit());
364 365 366 367
    expect(fileSystem.file('te,[/test'), isNot(exists));
    expect(fileSystem.file('out/test'), isNot(exists));
  });

368
  testWithoutContext('ArtifactUpdater will bail if unzipping fails more than twice on Windows', () async {
369
    final FakeOperatingSystemUtils operatingSystemUtils = FakeOperatingSystemUtils(windows: true);
370 371 372 373 374 375 376
    final MemoryFileSystem fileSystem = MemoryFileSystem.test();
    final BufferLogger logger = BufferLogger.test();
    final ArtifactUpdater artifactUpdater = ArtifactUpdater(
      fileSystem: fileSystem,
      logger: logger,
      operatingSystemUtils: operatingSystemUtils,
      platform: testPlatform,
377
      httpClient: FakeHttpClient.any(),
378 379 380 381 382 383 384
      tempStorage: fileSystem.currentDirectory.childDirectory('temp')
        ..createSync(),
    );
    operatingSystemUtils.failures = 2;

    expect(artifactUpdater.downloadZipArchive(
      'test message',
385
      Uri.parse('http://test.zip'),
386
      fileSystem.currentDirectory.childDirectory('out'),
387
    ), throwsToolExit());
388 389 390 391
    expect(fileSystem.file('te,[/test'), isNot(exists));
    expect(fileSystem.file('out/test'), isNot(exists));
  });

392
  testWithoutContext('ArtifactUpdater can download a tar archive', () async {
393
    final FakeOperatingSystemUtils operatingSystemUtils = FakeOperatingSystemUtils();
394 395 396 397 398 399
    final MemoryFileSystem fileSystem = MemoryFileSystem.test();
    final BufferLogger logger = BufferLogger.test();
    final ArtifactUpdater artifactUpdater = ArtifactUpdater(
      fileSystem: fileSystem,
      logger: logger,
      operatingSystemUtils: operatingSystemUtils,
400
      platform: testPlatform,
401
      httpClient: FakeHttpClient.any(),
402 403 404 405 406 407
      tempStorage: fileSystem.currentDirectory.childDirectory('temp')
        ..createSync(),
    );

    await artifactUpdater.downloadZippedTarball(
      'test message',
408
      Uri.parse('http://test.zip'),
409 410 411 412 413 414
      fileSystem.currentDirectory.childDirectory('out'),
    );
    expect(fileSystem.file('out/test'), exists);
  });

  testWithoutContext('ArtifactUpdater will delete downloaded files if they exist.', () async {
415
    final FakeOperatingSystemUtils operatingSystemUtils = FakeOperatingSystemUtils();
416 417 418 419 420 421
    final MemoryFileSystem fileSystem = MemoryFileSystem.test();
    final BufferLogger logger = BufferLogger.test();
    final ArtifactUpdater artifactUpdater = ArtifactUpdater(
      fileSystem: fileSystem,
      logger: logger,
      operatingSystemUtils: operatingSystemUtils,
422
      platform: testPlatform,
423
      httpClient: FakeHttpClient.any(),
424 425 426 427 428 429 430 431 432 433 434 435 436 437
      tempStorage: fileSystem.currentDirectory.childDirectory('temp')
        ..createSync(),
    );

    artifactUpdater.downloadedFiles.addAll(<File>[
      fileSystem.file('a/b/c/d')..createSync(recursive: true),
      fileSystem.file('d/e/f'),
    ]);

    artifactUpdater.removeDownloadedFiles();

    expect(fileSystem.file('a/b/c/d'), isNot(exists));
    expect(logger.errorText, isEmpty);
  });
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460

  testWithoutContext('ArtifactUpdater will tool exit if deleting the existing artifacts fails with 32 on windows', () async {
    const int kSharingViolation = 32;
    final FileExceptionHandler handler = FileExceptionHandler();
    final FakeOperatingSystemUtils operatingSystemUtils = FakeOperatingSystemUtils();
    final MemoryFileSystem fileSystem = MemoryFileSystem.test(opHandle: handler.opHandle);
    final BufferLogger logger = BufferLogger.test();
    final ArtifactUpdater artifactUpdater = ArtifactUpdater(
      fileSystem: fileSystem,
      logger: logger,
      operatingSystemUtils: operatingSystemUtils,
      platform: FakePlatform(operatingSystem: 'windows'),
      httpClient: FakeHttpClient.any(),
      tempStorage: fileSystem.currentDirectory.childDirectory('temp')
        ..createSync(),
    );

    final Directory errorDirectory = fileSystem.currentDirectory
      .childDirectory('out')
      .childDirectory('test')
      ..createSync(recursive: true);
    handler.addError(errorDirectory, FileSystemOp.delete, const FileSystemException('', '', OSError('', kSharingViolation)));

461
    await expectLater(() async => artifactUpdater.downloadZippedTarball(
462 463 464 465 466 467 468 469
      'test message',
      Uri.parse('http://test.zip'),
      fileSystem.currentDirectory.childDirectory('out'),
    ), throwsToolExit(
      message: 'Failed to delete /out/test because the local file/directory is in use by another process'
    ));
    expect(fileSystem.file('out/test'), isNot(exists));
  });
470 471
}

472 473
class FakeOperatingSystemUtils extends Fake implements OperatingSystemUtils {
  FakeOperatingSystemUtils({this.windows = false});
474

475
  int failures = 0;
476
  final bool windows;
477 478 479 480 481

  @override
  void unzip(File file, Directory targetDirectory) {
    if (failures > 0) {
      failures -= 1;
482
      throw Exception();
483 484 485 486 487 488 489 490 491
    }
    targetDirectory.childFile(file.fileSystem.path.basenameWithoutExtension(file.path))
      .createSync();
  }

  @override
  void unpack(File gzippedTarFile, Directory targetDirectory) {
    if (failures > 0) {
      failures -= 1;
492
      throw Exception();
493 494 495 496 497
    }
    targetDirectory.childFile(gzippedTarFile.fileSystem.path.basenameWithoutExtension(gzippedTarFile.path))
      .createSync();
  }
}