skia_client.dart 15.7 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// 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' as io;

import 'package:file/file.dart';
import 'package:file/local.dart';
import 'package:path/path.dart' as path;
import 'package:platform/platform.dart';
import 'package:process/process.dart';

// If you are here trying to figure out how to use golden files in the Flutter
// repo itself, consider reading this wiki page:
// https://github.com/flutter/flutter/wiki/Writing-a-golden-file-test-for-package%3Aflutter

18
const String _kFlutterRootKey = 'FLUTTER_ROOT';
19
const String _kGoldctlKey = 'GOLDCTL';
20
const String _kTestBrowserKey = 'FLUTTER_TEST_BROWSER';
21

22 23 24
/// A client for uploading image tests and making baseline requests to the
/// Flutter Gold Dashboard.
class SkiaGoldClient {
25 26 27 28
  /// Creates a [SkiaGoldClient] with the given [workDirectory].
  ///
  /// All other parameters are optional. They may be provided in tests to
  /// override the defaults for [fs], [process], [platform], and [httpClient].
29 30 31 32 33
  SkiaGoldClient(
    this.workDirectory, {
    this.fs = const LocalFileSystem(),
    this.process = const LocalProcessManager(),
    this.platform = const LocalPlatform(),
34 35
    io.HttpClient? httpClient,
  }) : httpClient = httpClient ?? io.HttpClient();
36 37 38

  /// The file system to use for storing the local clone of the repository.
  ///
39 40
  /// This is useful in tests, where a local file system (the default) can be
  /// replaced by a memory file system.
41 42 43 44
  final FileSystem fs;

  /// A wrapper for the [dart:io.Platform] API.
  ///
45 46
  /// This is useful in tests, where the system platform (the default) can be
  /// replaced by a mock platform instance.
47 48 49 50
  final Platform platform;

  /// A controller for launching sub-processes.
  ///
51 52
  /// This is useful in tests, where the real process manager (the default) can
  /// be replaced by a mock process manager that doesn't really create
53 54 55 56 57
  /// sub-processes.
  final ProcessManager process;

  /// A client for making Http requests to the Flutter Gold dashboard.
  final io.HttpClient httpClient;
58 59

  /// The local [Directory] within the [comparisonRoot] for the current test
60
  /// context. In this directory, the client will create image and JSON files
61 62 63 64
  /// for the goldctl tool to use.
  ///
  /// This is informed by the [FlutterGoldenFileComparator] [basedir]. It cannot
  /// be null.
65 66 67 68 69 70
  final Directory workDirectory;

  /// The local [Directory] where the Flutter repository is hosted.
  ///
  /// Uses the [fs] file system.
  Directory get _flutterRoot => fs.directory(platform.environment[_kFlutterRootKey]);
71 72 73 74

  /// The path to the local [Directory] where the goldctl tool is hosted.
  ///
  /// Uses the [platform] environment in this implementation.
75
  String get _goldctl => platform.environment[_kGoldctlKey]!;
76 77 78 79

  /// Prepares the local work space for golden file testing and calls the
  /// goldctl `auth` command.
  ///
80 81
  /// This ensures that the goldctl tool is authorized and ready for testing.
  /// Used by the [FlutterPostSubmitFileComparator] and the
82
  /// [FlutterPreSubmitFileComparator].
83
  Future<void> auth() async {
84
    if (await clientIsAuthorized())
85
      return;
86 87
    final List<String> authCommand = <String>[
      _goldctl,
88 89 90 91
      'auth',
      '--work-dir', workDirectory
        .childDirectory('temp')
        .path,
92
      '--luci',
93 94
    ];

95
    final io.ProcessResult result = await process.run(authCommand);
96 97 98

    if (result.exitCode != 0) {
      final StringBuffer buf = StringBuffer()
99 100 101 102
        ..writeln('Skia Gold authorization failed.')
        ..writeln('Luci environments authenticate using the file provided '
          'by LUCI_CONTEXT. There may be an error with this file or Gold '
          'authentication.')
103
        ..writeln('Debug information for Gold:')
104 105
        ..writeln('stdout: ${result.stdout}')
        ..writeln('stderr: ${result.stderr}');
106
      throw Exception(buf.toString());
107
    }
108 109 110 111 112
  }

  /// Executes the `imgtest init` command in the goldctl tool.
  ///
  /// The `imgtest` command collects and uploads test results to the Skia Gold
113 114
  /// backend, the `init` argument initializes the current test. Used by the
  /// [FlutterPostSubmitFileComparator].
115
  Future<void> imgtestInit() async {
116 117
    final File keys = workDirectory.childFile('keys.json');
    final File failures = workDirectory.childFile('failures.json');
118 119 120 121 122

    await keys.writeAsString(_getKeysJSON());
    await failures.create();
    final String commitHash = await _getCurrentCommit();

123 124
    final List<String> imgtestInitCommand = <String>[
      _goldctl,
125 126
      'imgtest', 'init',
      '--instance', 'flutter',
127 128 129
      '--work-dir', workDirectory
        .childDirectory('temp')
        .path,
130 131 132 133 134 135
      '--commit', commitHash,
      '--keys-file', keys.path,
      '--failure-file', failures.path,
      '--passfail',
    ];

136
    if (imgtestInitCommand.contains(null)) {
137
      final StringBuffer buf = StringBuffer()
138 139 140
        ..writeln('A null argument was provided for Skia Gold imgtest init.')
        ..writeln('Please confirm the settings of your golden file test.')
        ..writeln('Arguments provided:');
141
      imgtestInitCommand.forEach(buf.writeln);
142
      throw Exception(buf.toString());
143 144
    }

145
    final io.ProcessResult result = await process.run(imgtestInitCommand);
146 147

    if (result.exitCode != 0) {
148 149
      final StringBuffer buf = StringBuffer()
        ..writeln('Skia Gold imgtest init failed.')
150
        ..writeln('An error occurred when initializing golden file test with ')
151 152 153
        ..writeln('goldctl.')
        ..writeln()
        ..writeln('Debug information for Gold:')
154 155
        ..writeln('stdout: ${result.stdout}')
        ..writeln('stderr: ${result.stderr}');
156
      throw Exception(buf.toString());
157
    }
158 159 160 161 162 163 164 165 166
  }

  /// Executes the `imgtest add` command in the goldctl tool.
  ///
  /// The `imgtest` command collects and uploads test results to the Skia Gold
  /// backend, the `add` argument uploads the current image test. A response is
  /// returned from the invocation of this command that indicates a pass or fail
  /// result.
  ///
167
  /// The [testName] and [goldenFile] parameters reference the current
168
  /// comparison being evaluated by the [FlutterPostSubmitFileComparator].
169
  Future<bool> imgtestAdd(String testName, File goldenFile) async {
170 171
    final List<String> imgtestCommand = <String>[
      _goldctl,
172
      'imgtest', 'add',
173 174 175 176
      '--work-dir', workDirectory
        .childDirectory('temp')
        .path,
      '--test-name', cleanTestName(testName),
177 178 179
      '--png-file', goldenFile.path,
    ];

180
    final io.ProcessResult result = await process.run(imgtestCommand);
181 182

    if (result.exitCode != 0) {
183 184 185
      // We do not want to throw for non-zero exit codes here, as an intentional
      // change or new golden file test expect non-zero exit codes. Logging here
      // is meant to inform when an unexpected result occurs.
186 187 188 189
      print('goldctl imgtest add stdout: ${result.stdout}');
      print('goldctl imgtest add stderr: ${result.stderr}');
    }

190
    return true;
191 192
  }

193 194 195
  /// Executes the `imgtest init` command in the goldctl tool for tryjobs.
  ///
  /// The `imgtest` command collects and uploads test results to the Skia Gold
196
  /// backend, the `init` argument initializes the current tryjob. Used by the
197
  /// [FlutterPreSubmitFileComparator].
198 199 200 201 202 203 204 205
  Future<void> tryjobInit() async {
    final File keys = workDirectory.childFile('keys.json');
    final File failures = workDirectory.childFile('failures.json');

    await keys.writeAsString(_getKeysJSON());
    await failures.create();
    final String commitHash = await _getCurrentCommit();

206 207
    final List<String> imgtestInitCommand = <String>[
      _goldctl,
208 209 210 211 212 213 214 215 216 217 218
      'imgtest', 'init',
      '--instance', 'flutter',
      '--work-dir', workDirectory
        .childDirectory('temp')
        .path,
      '--commit', commitHash,
      '--keys-file', keys.path,
      '--failure-file', failures.path,
      '--passfail',
      '--crs', 'github',
      '--patchset_id', commitHash,
219
      ...getCIArguments(),
220 221
    ];

222
    if (imgtestInitCommand.contains(null)) {
223
      final StringBuffer buf = StringBuffer()
224 225 226
        ..writeln('A null argument was provided for Skia Gold tryjob init.')
        ..writeln('Please confirm the settings of your golden file test.')
        ..writeln('Arguments provided:');
227
      imgtestInitCommand.forEach(buf.writeln);
228
      throw Exception(buf.toString());
229 230
    }

231
    final io.ProcessResult result = await process.run(imgtestInitCommand);
232 233 234 235

    if (result.exitCode != 0) {
      final StringBuffer buf = StringBuffer()
        ..writeln('Skia Gold tryjobInit failure.')
236
        ..writeln('An error occurred when initializing golden file tryjob with ')
237 238 239
        ..writeln('goldctl.')
        ..writeln()
        ..writeln('Debug information for Gold:')
240 241
        ..writeln('stdout: ${result.stdout}')
        ..writeln('stderr: ${result.stderr}');
242
      throw Exception(buf.toString());
243 244 245 246 247 248 249 250 251 252
    }
  }

  /// Executes the `imgtest add` command in the goldctl tool for tryjobs.
  ///
  /// The `imgtest` command collects and uploads test results to the Skia Gold
  /// backend, the `add` argument uploads the current image test. A response is
  /// returned from the invocation of this command that indicates a pass or fail
  /// result for the tryjob.
  ///
253
  /// The [testName] and [goldenFile] parameters reference the current
254
  /// comparison being evaluated by the [FlutterPreSubmitFileComparator].
255
  Future<void> tryjobAdd(String testName, File goldenFile) async {
256 257
    final List<String> imgtestCommand = <String>[
      _goldctl,
258 259 260 261 262 263 264 265
      'imgtest', 'add',
      '--work-dir', workDirectory
        .childDirectory('temp')
        .path,
      '--test-name', cleanTestName(testName),
      '--png-file', goldenFile.path,
    ];

266
    final io.ProcessResult result = await process.run(imgtestCommand);
267

268
    final String/*!*/ resultStdout = result.stdout.toString();
269 270 271 272 273 274 275 276 277 278 279 280
    if (result.exitCode != 0 &&
      !(resultStdout.contains('Untriaged') || resultStdout.contains('negative image'))) {
      final StringBuffer buf = StringBuffer()
        ..writeln('Unexpected Gold tryjobAdd failure.')
        ..writeln('Tryjob execution for golden file test $testName failed for')
        ..writeln('a reason unrelated to pixel comparison.')
        ..writeln()
        ..writeln('Debug information for Gold:')
        ..writeln('stdout: ${result.stdout}')
        ..writeln('stderr: ${result.stderr}')
        ..writeln();
      throw Exception(buf.toString());
281
    }
282 283
  }

284 285 286 287 288
  /// Returns the latest positive digest for the given test known to Flutter
  /// Gold at head.
  Future<String?> getExpectationForTest(String testName) async {
    late String? expectation;
    final String traceID = getTraceID(testName);
289 290
    await io.HttpOverrides.runWithHttpOverrides<Future<void>>(() async {
      final Uri requestForExpectations = Uri.parse(
291
        'https://flutter-gold.skia.org/json/v1/latestpositivedigest/$traceID'
292
      );
293
      late String rawResponse;
294 295 296 297
      try {
        final io.HttpClientRequest request = await httpClient.getUrl(requestForExpectations);
        final io.HttpClientResponse response = await request.close();
        rawResponse = await utf8.decodeStream(response);
298 299 300
        final dynamic jsonResponse = json.decode(rawResponse);
        if (jsonResponse is! Map<String, dynamic>)
          throw const FormatException('Skia gold expectations do not match expected format.');
301
        expectation = jsonResponse['digest'] as String?;
302 303 304 305 306 307 308
      } on FormatException catch (error) {
        print(
          'Formatting error detected requesting expectations from Flutter Gold.\n'
          'error: $error\n'
          'url: $requestForExpectations\n'
          'response: $rawResponse'
        );
309 310 311 312 313
        rethrow;
      }
    },
      SkiaGoldHttpOverrides(),
    );
314
    return expectation;
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
  }

  /// Returns a list of bytes representing the golden image retrieved from the
  /// Flutter Gold dashboard.
  ///
  /// The provided image hash represents an expectation from Flutter Gold.
  Future<List<int>>getImageBytes(String imageHash) async {
    final List<int> imageBytes = <int>[];
    await io.HttpOverrides.runWithHttpOverrides<Future<void>>(() async {
      final Uri requestForImage = Uri.parse(
        'https://flutter-gold.skia.org/img/images/$imageHash.png',
      );

      try {
        final io.HttpClientRequest request = await httpClient.getUrl(requestForImage);
        final io.HttpClientResponse response = await request.close();
        await response.forEach((List<int> bytes) => imageBytes.addAll(bytes));

      } catch(e) {
        rethrow;
      }
    },
      SkiaGoldHttpOverrides(),
    );
    return imageBytes;
  }

342 343
  /// Returns the current commit hash of the Flutter repository.
  Future<String> _getCurrentCommit() async {
344
    if (!_flutterRoot.existsSync()) {
345
      throw Exception('Flutter root could not be found: $_flutterRoot\n');
346 347 348
    } else {
      final io.ProcessResult revParse = await process.run(
        <String>['git', 'rev-parse', 'HEAD'],
349
        workingDirectory: _flutterRoot.path,
350
      );
351 352 353 354
      if (revParse.exitCode != 0) {
        throw Exception('Current commit of Flutter can not be found.');
      }
      return (revParse.stdout as String/*!*/).trim();
355 356 357 358 359 360
    }
  }

  /// Returns a JSON String with keys value pairs used to uniquely identify the
  /// configuration that generated the given golden file.
  ///
361 362 363
  /// Currently, the only key value pairs being tracked is the platform the
  /// image was rendered on, and for web tests, the browser the image was
  /// rendered on.
364
  String _getKeysJSON() {
365 366
    final Map<String, dynamic> keys = <String, dynamic>{
      'Platform' : platform.operatingSystem,
367
      'CI' : 'luci',
368
    };
369
    if (platform.environment[_kTestBrowserKey] != null) {
370
      keys['Browser'] = platform.environment[_kTestBrowserKey];
371
      keys['Platform'] = '${keys['Platform']}-browser';
372
    }
373
    return json.encode(keys);
374 375
  }

376 377 378
  /// Removes the file extension from the [fileName] to represent the test name
  /// properly.
  String cleanTestName(String fileName) {
379
    return fileName.split(path.extension(fileName))[0];
380 381
  }

382 383
  /// Returns a boolean value to prevent the client from re-authorizing itself
  /// for multiple tests.
384
  Future<bool> clientIsAuthorized() async {
385
    final File authFile = workDirectory.childFile(fs.path.join(
386 387
      'temp',
      'auth_opt.json',
388
    ))/*!*/;
389 390 391 392

    if(await authFile.exists()) {
      final String contents = await authFile.readAsString();
      final Map<String, dynamic> decoded = json.decode(contents) as Map<String, dynamic>;
393
      return !(decoded['GSUtil'] as bool/*!*/);
394 395
    }
    return false;
396
  }
397 398 399 400

  /// Returns a list of arguments for initializing a tryjob based on the testing
  /// environment.
  List<String> getCIArguments() {
401 402 403
    final String jobId = platform.environment['LOGDOG_STREAM_PREFIX']!.split('/').last;
    final List<String> refs = platform.environment['GOLD_TRYJOB']!.split('/');
    final String pullRequest = refs[refs.length - 2];
404 405 406

    return <String>[
      '--changelist', pullRequest,
407
      '--cis', 'buildbucket',
408 409 410
      '--jobid', jobId,
    ];
  }
411

412 413 414 415 416 417 418 419 420 421 422
  /// Returns a trace id based on the current testing environment to lookup
  /// the latest positive digest on Flutter Gold.
  ///
  /// Trace IDs are case sensitive and should be in alphabetical order for the
  /// keys, followed by the rest of the paramset, also in alphabetical order.
  /// There should also be leading and trailing commas.
  ///
  /// Example TraceID for Flutter Gold:
  ///   ',CI=cirrus,Platform=linux,name=cupertino.activityIndicator.inprogress.1.0,source_type=flutter,'
  String getTraceID(String testName) {
    return '${platform.environment[_kTestBrowserKey] == null ? ',' : ',Browser=${platform.environment[_kTestBrowserKey]},'}'
423
      'CI=luci,'
424 425 426
      'Platform=${platform.operatingSystem},'
      'name=$testName,'
      'source_type=flutter,';
427 428
  }
}
429 430

/// Used to make HttpRequests during testing.
431
class SkiaGoldHttpOverrides extends io.HttpOverrides { }