Visitar URL original
fuchsia_ctl by dnfield · Pull Request #32 · flutter/packages · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
start tests, make more testable, license headers
  • Loading branch information
dnfield committed Sep 11, 2019
commit 137da9a568d480fe16f13ac43b28233adcd7c24c
28 changes: 28 additions & 0 deletions packages/fuchsia_ctl/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# fuchsia_ctl

This package is used by Flutter CI systems to manag paving and testing Fuchsia

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

manage

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Comment thread
christopherfujino marked this conversation as resolved.
Resolved
devices.

It offers some functionality similar to the `fx` command in the Fuchsia SDK.

It is not intended for general use.

## Building

This tool is meant to be published as an AOT compiled binary distributed via
CIPD. To build the AOT binary, run `tool/build.sh`. This must be done on a
Linux machine, and will automatically download a suitable version of Dart to
build the AOT bundle and include the `dartaotruntime`.

To create the CIPD package, make sure that the `build/` folder does not contain
any files from testing (e.g. a generated `.ssh` folder from paving or a copy of
`dev_finder` or `pm`). Then run:

```bash
cipd create -in build \
-name flutter/fuchsia_ctl/linux-amd64 \
-ref stable \
-tag version:n.n.n
```

with an appropriate version string, after running `tool/build.sh`.
164 changes: 68 additions & 96 deletions packages/fuchsia_ctl/bin/main.dart
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
// Copyright 2019 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 'dart:io';

import 'package:args/args.dart';
import 'package:file/file.dart';
import 'package:file/local.dart';
import 'package:fuchsia_ctl/fuchsia_ctl.dart';
import 'package:fuchsia_ctl/src/operation_result.dart';
import 'package:meta/meta.dart';
import 'package:uuid/uuid.dart';

typedef AsyncInt = Future<int> Function(String, DevFinder, ArgResults);
typedef AsyncResult = Future<OperationResult> Function(String, DevFinder, ArgResults);

const Map<String, AsyncInt> commands = <String, AsyncInt>{
const Map<String, AsyncResult> commands = <String, AsyncResult>{
'pave': pave,
'pm': pm,
'ssh': ssh,
Expand All @@ -28,8 +33,7 @@ Future<void> main(List<String> args) async {
help: 'The device node name to use. '
'If not specified, the first discoverable device will be used.')
..addOption('dev-finder-path',
defaultsTo: './dev_finder',
help: 'The path to the dev_finder executable.');
defaultsTo: './dev_finder', help: 'The path to the dev_finder executable.');
parser.addCommand('ssh')
..addFlag('interactive',
abbr: 'i',
Expand All @@ -39,15 +43,12 @@ Future<void> main(List<String> args) async {
abbr: 'c',
help: 'The command to run on the device. '
'If specified, --interactive is ignored.')
..addOption('identity-file',
defaultsTo: '.ssh/pkey', help: 'The key to use when SSHing.');
..addOption('identity-file', defaultsTo: '.ssh/pkey', help: 'The key to use when SSHing.');
parser.addCommand('pave')
..addOption('image',
abbr: 'i', help: 'The system image tgz to unpack and pave.');
..addOption('image', abbr: 'i', help: 'The system image tgz to unpack and pave.');

final ArgParser pmSubCommand = parser.addCommand('pm')
..addOption('pm-path',
defaultsTo: './pm', help: 'The path to the pm executable.')
..addOption('pm-path', defaultsTo: './pm', help: 'The path to the pm executable.')
..addOption('repo',
abbr: 'r',
help: 'The location of the repository folder to create, '
Expand All @@ -59,14 +60,10 @@ Future<void> main(List<String> args) async {
.addMultiOption('far', abbr: 'f', help: 'The .far files to publish.');

parser.addCommand('test')
..addOption('pm-path',
defaultsTo: './pm', help: 'The path to the pm executable.')
..addOption('identity-file',
defaultsTo: '.ssh/pkey', help: 'The key to use when SSHing.')
..addOption('target',
abbr: 't', help: 'The name of the target to pass to runtests.')
..addMultiOption('far',
abbr: 'f', help: 'The .far files to include for the test.');
..addOption('pm-path', defaultsTo: './pm', help: 'The path to the pm executable.')
..addOption('identity-file', defaultsTo: '.ssh/pkey', help: 'The key to use when SSHing.')
..addOption('target', abbr: 't', help: 'The name of the target to pass to runtests.')
..addMultiOption('far', abbr: 'f', help: 'The .far files to include for the test.');

final ArgResults results = parser.parse(args);

Expand All @@ -75,22 +72,24 @@ Future<void> main(List<String> args) async {
stderr.writeln(parser.usage);
return;
}
final AsyncInt command = commands[results.command.name];
final AsyncResult command = commands[results.command.name];
if (command == null) {
stderr.writeln('Unkown command ${results.command.name}.');
stderr.writeln(parser.usage);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we have non-zero exit code here as well?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done (here and line 82)

return;
}
final int returnCode = await command(
final OperationResult result = await command(
results['device-name'],
DevFinder(results['dev-finder-path']),
results.command,
);
exit(returnCode);
if (!result.success) {
exit(-1);
}
}

@visibleForTesting
Future<int> ssh(
Future<OperationResult> ssh(
String deviceName,
DevFinder devFinder,
ArgResults args,
Expand All @@ -104,36 +103,32 @@ Future<int> ssh(
identityFilePath: identityFile,
);
}
final ProcessResult result = await sshClient.runCommand(
final OperationResult result = await sshClient.runCommand(
targetIp,
identityFilePath: identityFile,
command: args['command'].split(' '),
);
stdout.writeln(
'==================================== STDOUT ====================================');
stdout.writeln(result.stdout);
stderr.writeln(
'==================================== STDERR ====================================');
stderr.writeln(result.stderr);
stdout.writeln(
'==================================== EXCODE ====================================');
stdout.writeln(result.exitCode);
return result.exitCode;
stdout
.writeln('==================================== STDOUT ====================================');
stdout.writeln(result.info);
stderr
.writeln('==================================== STDERR ====================================');
stderr.writeln(result.error);
return result;
}

@visibleForTesting
Future<int> pave(
Future<OperationResult> pave(
String deviceName,
DevFinder devFinder,
ArgResults args,
) async {
const ImagePaver paver = ImagePaver();
final ProcessResult result = await paver.pave(args['image'], deviceName);
return result.exitCode;
return await paver.pave(args['image'], deviceName);
}

@visibleForTesting
Future<int> pm(
Future<OperationResult> pm(
String deviceName,
DevFinder devFinder,
ArgResults args,
Expand All @@ -145,32 +140,20 @@ Future<int> pm(
await Future<void>.delayed(Duration(seconds: 15));
return await server.close();
case 'newRepo':
final ProcessResult result = await server.newRepo(args['repo']);
return result.exitCode;
return await server.newRepo(args['repo']);
case 'publishRepo':
final ProcessResult result =
await server.publishRepo(args['repo'], args['far']);
return result.exitCode;
return await server.publishRepo(args['repo'], args['far']);
default:
throw ArgumentError('Command ${args.command.name} unknown.');
}
}

@visibleForTesting
Future<int> test(
Future<OperationResult> test(
String deviceName,
DevFinder devFinder,
ArgResults args,
) async {
int checkProcessResult(ProcessResult result, String failureMessage) {
stdout.writeln(result.stdout);
stderr.writeln(result.stderr);
if (result.exitCode != 0) {
stderr.writeln(failureMessage);
}
return result.exitCode;
}

const FileSystem fs = LocalFileSystem();
final String uuid = Uuid().v4();
final String identityFile = args['identity-file'];
Expand All @@ -184,69 +167,58 @@ Future<int> test(
final String targetIp = await devFinder.getTargetAddress(deviceName);
stdout.writeln('Using ${repo.path} as repo to serve to $targetIp...');
repo.createSync(recursive: true);
int result = checkProcessResult(
await server.newRepo(repo.path),
'Failed to create repo at $repo.',
);
if (result != 0) {
OperationResult result = await server.newRepo(repo.path);

if (!result.success) {
stderr.writeln('Failed to create repo at $repo.');
return result;
}

await server.serveRepo(repo.path, port: 54321);

result = checkProcessResult(
await ssh.runCommand(
targetIp,
identityFilePath: identityFile,
command: <String>[
'amberctl',
'add_src',
'-f', 'http://$localIp:${server.serverPort}/config.json', //
'-n', uuid,
],
),
'amberctl add_src failed, aborting.',
result = await ssh.runCommand(
targetIp,
identityFilePath: identityFile,
command: <String>[
'amberctl',
'add_src',
'-f', 'http://$localIp:${server.serverPort}/config.json', //
'-n', uuid,
],
);
if (result != 0) {
if (!result.success) {
stderr.writeln('amberctl add_src failed, aborting.');
return result;
}

for (String farFile in farFiles) {
result = checkProcessResult(
await server.publishRepo(repo.path, farFile),
'Failed to publish repo at $repo with $farFiles.',
);
if (result != 0) {
result = await server.publishRepo(repo.path, farFile);
if (!result.success) {
stderr.writeln('Failed to publish repo at $repo with $farFiles.');
return result;
}
final String packageName =
fs.file(farFile).basename.replaceFirst('-0.far', '');
final String packageName = fs.file(farFile).basename.replaceFirst('-0.far', '');
stdout.writeln('Adding $packageName...');
result = checkProcessResult(
await ssh.runCommand(
targetIp,
identityFilePath: identityFile,
command: <String>[
'amberctl',
'get_up',
'-n', packageName, //
],
),
'amberctl get_up failed, aborting.',
result = await ssh.runCommand(
targetIp,
identityFilePath: identityFile,
command: <String>[
'amberctl',
'get_up',
'-n', packageName, //
],
);
if (result != 0) {
if (!result.success) {
stderr.writeln('amberctl get_up failed, aborting.');
return result;
}
}

stdout.writeln('Test results:');
return checkProcessResult(
await ssh.runCommand(
targetIp,
identityFilePath: identityFile,
command: <String>['pkgfs/packages/$target/0/bin/app'],
),
'Test failed, aborting.',
return await ssh.runCommand(
targetIp,
identityFilePath: identityFile,
command: <String>['pkgfs/packages/$target/0/bin/app'],
);
} finally {
repo.deleteSync(recursive: true);
Expand Down
5 changes: 5 additions & 0 deletions packages/fuchsia_ctl/lib/fuchsia_ctl.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
// Copyright 2019 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.

export 'src/dev_finder.dart';
export 'src/image_paver.dart';
export 'src/operation_result.dart';
export 'src/package_server.dart';
export 'src/ssh_client.dart';
4 changes: 4 additions & 0 deletions packages/fuchsia_ctl/lib/src/dev_finder.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// Copyright 2019 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 'dart:io';

import 'package:meta/meta.dart';
Expand Down
Loading