411cb1fb67241125203629229600ecbd821eb9c7 |
|
19-Nov-2015 |
Paul Duffin <paulduffin@google.com> |
Fix escaping and grouping of arguments to the Target shells Vogar allows custom arguments to be passed to the classes being tested. If these contain special shell characters then the command line used to run the tests may not be correct. e.g. If the argument was "-Cvm.args=-Xmx256M -Xms256M" (which tells Caliper to run the benchmark in a process with an initial and maximum heap size of 256M) then when the command is actually run by the adb shell they are split into two arguments, "-Cvm.args=-Xmx256M" and "-Xms256M" which causes Caliper to fail as the latter is an unrecognized Caliper option. The solution is to simply escape any special shell characters with a backslash (\) to ensure that they are treated as literal characters. Another bug is that any use of an argument "-c" will result in all the following arguments being grouped into one, e.g. fred -c wilma barney betty will become fred -c "wilma barney betty" This behaviour is required when the -c option is part of "sh" "-c" but not in any other case. This is currently implemented in Command which is the wrong place as it's a generic class independent of the Target and the grouping is dependent on the target shell. After some investigation it turns out that while grouping isn't required for AdbTarget and SshTarget like it is for HostTarget neither does it break anything. Therefore, for simplicity's sake all targets will group their arguments into one when executing the command line. That will be done before it is appended to the targetProcessPrefix(). The escaping and grouping will be done by a new Target.ScriptBuilder class, instances of which will be created by a Target.newScriptBuilder() method that will make sure that the ScriptBuilder returned will be appropriate for the target shell. It will provide support for specifying inline environment variables, a working directory and adding tokens to the command line. It will escape where necessary, e.g. it will not escape the environment variable name or assignment but will escape the value. It will escape all the tokens. When asked to construct an appropriate command line to execute that script it will group the script contents into one argument and append it to a target specific prefix. VmCommandBuilder will use the new ScriptBuilder to build the command line that it then passes to Command. It will also be changed to track environment variables explicitly (will use the existing but unused env field and method) which it will then pass onto the ScriptBuilder. Finally, it will track the workingDirectory as well. DeviceRuntime and HostRuntime will both change how they use VmCommandBuilder to make use of the env(String, String) method and the new workingDirectory(File). Command will remove the processArgs() method and simply take a copy of its args. Added new tests for ScriptBuilder.escape(String) method and a JUnit 4 TestSuite AllTests that can be used to run all the tests that currently work within an IDE. Change-Id: I0351c9fd3fe9d42c1b454251cf78ff74fafb08b2
/external/vogar/test/vogar/android/DeviceRuntimeSshTargetTest.java
|
7029503206e4c89fe167c3389c1062d89cf35c52 |
|
19-Nov-2015 |
Paul Duffin <paulduffin@google.com> |
Refactoring to make more testable Prior to making a change in the behaviour to fix an issue with the quoting of command line arguments this refactors the existing code and adds some tests to illustrate the existing behaviour. This will minimize the chances that the fix will break existing code. The changes are explained below in more detail. This applied a couple of common refactorings to a number of classes: * Where classes were creating a lot of subsidiary objects in their constructor I switched to using the dependency injection pattern (i.e. passing in parameters to the constructor). That makes it easier for tests to override the behaviour. * Where classes were being given a huge object (e.g. Run) but only required a small number of it's fields I replaced the Run constructor parameter with the individual parts. LocalTarget - Replaced Run with Log, Mkdir, Rm. Run - Passed in more parameters into the constructor, moved the code that created those objects into Vogar.run(). Also, make the vogarJar() method work when running from a class directory to allow it to work from within IDEA as well as when running from jars. SshTarget - Removed a bit of duplication of the command line prefix used to run a script remotely. Target - Method defaultDeviceDir() was removed and the implementations were made static and accessed directly from Vogar. That is necessary to break a dependency cycle; AdbTarget required AndroidSdk, which required DeviceFileCache, which required runnerDir, which required Target.defaultDeviceDir(). Previously, that was broken by passing the DeviceFileCache to the AndroidSdk after construction using the setCaches() method but that has been removed as part of this refactoring. Vogar - Made the constructor and parseArgs(String[]) visible for testing. Moved some of the code from Run's constructor here to create the values to be passed into its expanded constructor. TargetType was added to encapsulate the defaultDeviceDir() for each Target implementation class. That allowed the cycle described in Target to be broken. AdbTarget - Replaced Run with individual fields. Moved functionality from AndroidSdk that's only used by this class into here (waitForDevice(), ensureDirectory(File), remount(), rm(File), forwardTcp(int), push() and pull()). Bringing in push() also required moving the pushCache from AndroidSdk as well. Added DeviceFilesystem as a parameter to avoid having to have a reference to AndroidSdk. AndroidSdk - Moved logic that runs shell commands to calculate the compilationClasspath and androidJarPath out of the constructor and into a static method to allow tests to pass dummy values straight into the constructor. Marked constructor as VisibleForTesting. Removed setCaches(..) method and pushCache, passed the HostFileCache straight into the constructor, marking the dexCache field as final. Added explicit exception when can't find platforms directory rather than NullPointerException. Moved methods that are only used by either AdbTarget or DeviceFileCache into those classes. DeviceFileCache - Replaced AndroidSdk with DeviceFileSystem and moved cp(File, File) and mv(File, File) from AndroidSdk into here. DeviceRuntime - Added a Supplier<String> parameter to provide the device user name in order to allow tests to provide their own. That is needed because otherwise the test will attempt to connect to a device to determine the user name. Command - Added method to access the args for testing, moved the special processing of "-c" option from start() into a method called from the constructor. That allows the test to verify the processing of that without actually executing the command. Cleaned up unused workingDirectory method and some minor warnings. Added some tests and fixed a compile issue with the JUnitRunnerTest. Change-Id: Ib1676ee19a4f0e7a8944b2708a6dbe3899d1d292
/external/vogar/test/vogar/android/DeviceRuntimeSshTargetTest.java
|