From 6f781314745a1695acf344ebbc38f20c87086807 Mon Sep 17 00:00:00 2001 From: Zachary Anderson Date: Wed, 19 Aug 2020 14:41:05 -0700 Subject: [PATCH] WIP: [flutter_tool] Fix amberctl host address (#64020) --- .../lib/src/fuchsia/amber_ctl.dart | 38 +++++++++++------ .../lib/src/fuchsia/fuchsia_device.dart | 26 ++++++++++++ .../lib/src/fuchsia/fuchsia_pm.dart | 13 +----- .../fuchsia/fuchsia_device_test.dart | 41 +++++++++++++++++++ .../fuchsia/fuchsia_pm_test.dart | 15 ------- 5 files changed, 95 insertions(+), 38 deletions(-) diff --git a/packages/flutter_tools/lib/src/fuchsia/amber_ctl.dart b/packages/flutter_tools/lib/src/fuchsia/amber_ctl.dart index 3c806fb48c..1c27476dd3 100644 --- a/packages/flutter_tools/lib/src/fuchsia/amber_ctl.dart +++ b/packages/flutter_tools/lib/src/fuchsia/amber_ctl.dart @@ -58,25 +58,30 @@ class FuchsiaAmberCtl { /// Teaches the amber instance running on [device] about the Fuchsia package /// server accessible via [configUrl]. Future addSrc(FuchsiaDevice device, FuchsiaPackageServer server) async { - final String configUrl = '${server.interfaceStrippedUrl}/config.json'; - final RunResult result = - await device.shell('amber_ctl add_src -x -f $configUrl'); + final String localIp = await device.hostAddress; + final String configUrl = 'http://[$localIp]:${server.port}/config.json'; + final RunResult result = await device.shell( + 'amber_ctl add_src -x -f $configUrl', + ); return result.exitCode == 0; } /// Instructs the amber instance running on [device] to forget about the /// Fuchsia package server that it was accessing via [serverUrl]. Future rmSrc(FuchsiaDevice device, FuchsiaPackageServer server) async { - final RunResult result = - await device.shell('amber_ctl rm_src -n ${server.interfaceStrippedUrl}'); + final String localIp = await device.hostAddress; + final RunResult result = await device.shell( + 'amber_ctl rm_src -n http://[$localIp]:${server.port}/', + ); return result.exitCode == 0; } /// Instructs the amber instance running on [device] to prefetch the package /// [packageName]. Future getUp(FuchsiaDevice device, String packageName) async { - final RunResult result = - await device.shell('amber_ctl get_up -n $packageName'); + final RunResult result = await device.shell( + 'amber_ctl get_up -n $packageName', + ); return result.exitCode == 0; } @@ -84,15 +89,21 @@ class FuchsiaAmberCtl { /// pkg_resolver repo config, and teaches the pkg_resolver instance running /// on [device] about the [FuchsiaPackageServer]. Future addRepoCfg(FuchsiaDevice device, FuchsiaPackageServer server) async { - final String configUrl = '${server.interfaceStrippedUrl}/config.json'; - final RunResult result = - await device.shell('amber_ctl add_repo_cfg -n ${server.name} -f $configUrl'); + final String localIp = await device.hostAddress; + final String configUrl = 'http://[$localIp]:${server.port}/config.json'; + final RunResult result = await device.shell( + 'amber_ctl add_repo_cfg -n ${server.name} -f $configUrl', + ); return result.exitCode == 0; } /// Instructs the pkg_resolver instance running on [device] to prefetch the /// package [packageName]. - Future pkgCtlResolve(FuchsiaDevice device, FuchsiaPackageServer server, String packageName) async { + Future pkgCtlResolve( + FuchsiaDevice device, + FuchsiaPackageServer server, + String packageName, + ) async { final String packageUrl = 'fuchsia-pkg://${server.name}/$packageName'; final RunResult result = await device.shell('pkgctl resolve $packageUrl'); return result.exitCode == 0; @@ -100,7 +111,10 @@ class FuchsiaAmberCtl { /// Instructs the pkg_resolver instance running on [device] to forget about /// the Fuchsia package server that it was accessing via [serverUrl]. - Future pkgCtlRepoRemove(FuchsiaDevice device, FuchsiaPackageServer server) async { + Future pkgCtlRepoRemove( + FuchsiaDevice device, + FuchsiaPackageServer server, + ) async { final String repoUrl = 'fuchsia-pkg://${server.name}'; final RunResult result = await device.shell('pkgctl repo rm $repoUrl'); return result.exitCode == 0; diff --git a/packages/flutter_tools/lib/src/fuchsia/fuchsia_device.dart b/packages/flutter_tools/lib/src/fuchsia/fuchsia_device.dart index 59789e6093..37fef4400c 100644 --- a/packages/flutter_tools/lib/src/fuchsia/fuchsia_device.dart +++ b/packages/flutter_tools/lib/src/fuchsia/fuchsia_device.dart @@ -580,6 +580,32 @@ class FuchsiaDevice extends Device { /// [true] if the current host address is IPv6. bool get ipv6 => _ipv6 ??= isIPv6Address(id); + /// Return the address that the device should use to communicate with the + /// host. + Future get hostAddress async { + if (_cachedHostAddress != null) { + return _cachedHostAddress; + } + final RunResult result = await shell('echo \$SSH_CONNECTION'); + void fail() { + throwToolExit('Failed to get local address, aborting.\n$result'); + } + if (result.exitCode != 0) { + fail(); + } + final List splitResult = result.stdout.split(' '); + if (splitResult.isEmpty) { + fail(); + } + final String addr = splitResult[0].replaceAll('%', '%25'); + if (addr.isEmpty) { + fail(); + } + return _cachedHostAddress = addr; + } + + String _cachedHostAddress; + /// List the ports currently running a dart observatory. Future> servicePorts() async { const String findCommand = 'find /hub -name vmservice-port'; diff --git a/packages/flutter_tools/lib/src/fuchsia/fuchsia_pm.dart b/packages/flutter_tools/lib/src/fuchsia/fuchsia_pm.dart index e713c6b805..5d3c411cb5 100644 --- a/packages/flutter_tools/lib/src/fuchsia/fuchsia_pm.dart +++ b/packages/flutter_tools/lib/src/fuchsia/fuchsia_pm.dart @@ -195,20 +195,11 @@ class FuchsiaPackageServer { Process _process; - /// The URL that can be used by the device to access this package server. - String get url => Uri(scheme: 'http', host: _host, port: _port).toString(); - - /// The URL that is stripped of interface name if it is an ipv6 address, - /// which should be supplied to amber_ctl to configure access to host. - String get interfaceStrippedUrl => Uri( - scheme: 'http', - host: (isIPv6Address(_host.split('%').first)) ? '[${_host.split('%').first}]' : _host, - port: _port, - ).toString(); - // The name used to reference the server by fuchsia-pkg:// urls. final String name; + int get port => _port; + /// Uses [FuchiaPM.newrepo] and [FuchsiaPM.serve] to spin up a new Fuchsia /// package server. /// diff --git a/packages/flutter_tools/test/general.shard/fuchsia/fuchsia_device_test.dart b/packages/flutter_tools/test/general.shard/fuchsia/fuchsia_device_test.dart index e2f7826cd5..4c92500387 100644 --- a/packages/flutter_tools/test/general.shard/fuchsia/fuchsia_device_test.dart +++ b/packages/flutter_tools/test/general.shard/fuchsia/fuchsia_device_test.dart @@ -246,6 +246,47 @@ void main() { FuchsiaSdk: () => MockFuchsiaSdk(), ProcessManager: () => MockProcessManager(), }); + + testUsingContext('hostAddress parsing works', () async { + when(globals.processManager.run(any)).thenAnswer((Invocation _) async { + return ProcessResult( + 1, + 0, + 'fe80::8c6c:2fff:fe3d:c5e1%ethp0003 50666 fe80::5054:ff:fe63:5e7a%ethp0003 22', + '', + ); + }); + final FuchsiaDevice device = FuchsiaDevice('id'); + expect(await device.hostAddress, 'fe80::8c6c:2fff:fe3d:c5e1%25ethp0003'); + }, overrides: { + FuchsiaArtifacts: () => FuchsiaArtifacts(sshConfig: sshConfig), + FuchsiaSdk: () => MockFuchsiaSdk(), + ProcessManager: () => MockProcessManager(), + }); + + testUsingContext('hostAddress parsing throws tool error on failure', () async { + when(globals.processManager.run(any)).thenAnswer((Invocation _) async { + return ProcessResult(1, 1, '', ''); + }); + final FuchsiaDevice device = FuchsiaDevice('id'); + expect(() async => await device.hostAddress, throwsToolExit()); + }, overrides: { + FuchsiaArtifacts: () => FuchsiaArtifacts(sshConfig: sshConfig), + FuchsiaSdk: () => MockFuchsiaSdk(), + ProcessManager: () => MockProcessManager(), + }); + + testUsingContext('hostAddress parsing throws tool error on empty response', () async { + when(globals.processManager.run(any)).thenAnswer((Invocation _) async { + return ProcessResult(1, 0, '', ''); + }); + final FuchsiaDevice device = FuchsiaDevice('id'); + expect(() async => await device.hostAddress, throwsToolExit()); + }, overrides: { + FuchsiaArtifacts: () => FuchsiaArtifacts(sshConfig: sshConfig), + FuchsiaSdk: () => MockFuchsiaSdk(), + ProcessManager: () => MockProcessManager(), + }); }); group('displays friendly error when', () { diff --git a/packages/flutter_tools/test/general.shard/fuchsia/fuchsia_pm_test.dart b/packages/flutter_tools/test/general.shard/fuchsia/fuchsia_pm_test.dart index fb0315860d..bf3df7a9ee 100644 --- a/packages/flutter_tools/test/general.shard/fuchsia/fuchsia_pm_test.dart +++ b/packages/flutter_tools/test/general.shard/fuchsia/fuchsia_pm_test.dart @@ -71,21 +71,6 @@ void main() { FuchsiaArtifacts: () => mockFuchsiaArtifacts, ProcessManager: () => mockProcessManager, }); - - testWithoutContext('ipv6 formatting logic of FuchsiaPackageServer', () { - const String host = 'fe80::ec4:7aff:fecc:ea8f%eno2'; - const int port = 23; - - expect( - FuchsiaPackageServer('a', 'b', host, port).url, - 'http://[fe80::ec4:7aff:fecc:ea8f%25eno2]:23', - ); - - expect( - FuchsiaPackageServer('a', 'b', host, port).interfaceStrippedUrl, - 'http://[fe80::ec4:7aff:fecc:ea8f]:23', - ); - }); }); }