From f4f33fd1085790079eb863f9b6f016a44f4eaf11 Mon Sep 17 00:00:00 2001 From: Jenn Magder Date: Mon, 1 Feb 2021 15:00:38 -0800 Subject: [PATCH] Dump logs on failing devicelab test to recipe artifact location (#74378) --- dev/devicelab/bin/tasks/module_test_ios.dart | 5 ++ dev/devicelab/lib/framework/host_agent.dart | 44 +++++++++++++++++ dev/devicelab/test/host_agent_test.dart | 51 ++++++++++++++++++++ 3 files changed, 100 insertions(+) create mode 100644 dev/devicelab/lib/framework/host_agent.dart create mode 100644 dev/devicelab/test/host_agent_test.dart diff --git a/dev/devicelab/bin/tasks/module_test_ios.dart b/dev/devicelab/bin/tasks/module_test_ios.dart index 1c3ae03894..5e848c0f2a 100644 --- a/dev/devicelab/bin/tasks/module_test_ios.dart +++ b/dev/devicelab/bin/tasks/module_test_ios.dart @@ -5,6 +5,7 @@ import 'dart:io'; import 'package:flutter_devicelab/framework/framework.dart'; +import 'package:flutter_devicelab/framework/host_agent.dart'; import 'package:flutter_devicelab/framework/ios.dart'; import 'package:flutter_devicelab/framework/task_result.dart'; import 'package:flutter_devicelab/framework/utils.dart'; @@ -292,6 +293,8 @@ Future main() async { section('Run platform unit tests'); await testWithNewIOSSimulator('TestAdd2AppSim', (String deviceId) { simulatorDeviceId = deviceId; + + final String resultBundlePath = path.join(hostAgent.dumpDirectory.path, 'module_test_ios-objc-${DateTime.now().toLocal().toIso8601String()}'); return inDirectory(objectiveCHostApp, () => exec( 'xcodebuild', @@ -304,6 +307,8 @@ Future main() async { 'Debug', '-destination', 'id=$deviceId', + '-resultBundlePath', + resultBundlePath, 'test', 'CODE_SIGNING_ALLOWED=NO', 'CODE_SIGNING_REQUIRED=NO', diff --git a/dev/devicelab/lib/framework/host_agent.dart b/dev/devicelab/lib/framework/host_agent.dart new file mode 100644 index 0000000000..abe164601e --- /dev/null +++ b/dev/devicelab/lib/framework/host_agent.dart @@ -0,0 +1,44 @@ +// 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/file.dart'; +import 'package:file/local.dart'; +import 'package:meta/meta.dart'; +import 'package:platform/platform.dart'; + +/// The current host machine running the tests. +HostAgent get hostAgent => HostAgent(platform: const LocalPlatform(), fileSystem: const LocalFileSystem()); + +/// Host machine running the tests. +class HostAgent { + HostAgent({@required Platform platform, @required FileSystem fileSystem}) + : _platform = platform, + _fileSystem = fileSystem; + + final Platform _platform; + final FileSystem _fileSystem; + + /// Creates a directory to dump file artifacts. + Directory get dumpDirectory { + if (_dumpDirectory == null) { + // Set in LUCI recipe. + final String directoryPath = _platform.environment['FLUTTER_LOGS_DIR']; + if (directoryPath == null) { + _dumpDirectory = _fileSystem.systemTempDirectory.createTempSync('flutter_test_logs.'); + print('Created tmp dump directory ${_dumpDirectory.path}'); + } else { + _dumpDirectory = _fileSystem.directory(directoryPath)..createSync(recursive: true); + print('Found FLUTTER_LOGS_DIR dump directory ${_dumpDirectory.path}'); + } + } + return _dumpDirectory; + } + + static Directory _dumpDirectory; + + @visibleForTesting + void resetDumpDirectory() { + _dumpDirectory = null; + } +} diff --git a/dev/devicelab/test/host_agent_test.dart b/dev/devicelab/test/host_agent_test.dart new file mode 100644 index 0000000000..555cd412d3 --- /dev/null +++ b/dev/devicelab/test/host_agent_test.dart @@ -0,0 +1,51 @@ +// 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/file.dart'; +import 'package:file/memory.dart'; +import 'package:flutter_devicelab/framework/host_agent.dart'; +import 'package:platform/platform.dart'; + +import 'common.dart'; + +void main() { + FileSystem fs; + setUp(() { + fs = MemoryFileSystem(); + hostAgent.resetDumpDirectory(); + }); + + tearDown(() { + hostAgent.resetDumpDirectory(); + }); + + group('dump directory', () { + test('set by environment', () async { + final Directory environmentDir = fs.directory(fs.path.join('home', 'logs')); + final FakePlatform fakePlatform = FakePlatform( + environment: {'FLUTTER_LOGS_DIR': environmentDir.path}, + operatingSystem: 'windows', + ); + final HostAgent agent = HostAgent(platform: fakePlatform, fileSystem: fs); + + expect(agent.dumpDirectory.existsSync(), isTrue); + expect(agent.dumpDirectory.path, environmentDir.path); + }); + + test('not set by environment', () async { + final FakePlatform fakePlatform = FakePlatform(environment: {}, operatingSystem: 'windows'); + final HostAgent agent = HostAgent(platform: fakePlatform, fileSystem: fs); + + expect(agent.dumpDirectory.existsSync(), isTrue); + }); + + test('is the same between host agent instances', () async { + final FakePlatform fakePlatform = FakePlatform(environment: {}, operatingSystem: 'windows'); + final HostAgent agent1 = HostAgent(platform: fakePlatform, fileSystem: fs); + final HostAgent agent2 = HostAgent(platform: fakePlatform, fileSystem: fs); + + expect(agent1.dumpDirectory.path, agent2.dumpDirectory.path); + }); + }); +}