@Test
 public void coreSimulatorServicesKillSucceedsEvenIfNoSuchProcess()
     throws IOException, InterruptedException {
   ImmutableList.Builder<Map.Entry<ProcessExecutorParams, FakeProcess>> fakeProcessesBuilder =
       ImmutableList.builder();
   fakeProcessesBuilder.add(
       new SimpleImmutableEntry<>(
           LAUNCHCTL_LIST_PARAMS,
           new FakeProcess(
               0,
               "87823\t0\tcom.apple.CoreSimulator.CoreSimulatorService.117.15.1.lkhDXxRPp5yy\n"
                   + "74617\t0\tcom.apple.CoreSimulator.SimDevice.CC1B0BAD-BAE6-4A53-92CF-F79850654057"
                   + ".launchd_sim\n"
                   + "74614\t0\tcom.apple.iphonesimulator.6564\n",
               "")));
   fakeProcessesBuilder.add(
       new SimpleImmutableEntry<>(
           ProcessExecutorParams.builder()
               .setCommand(
                   ImmutableList.of(
                       "launchctl",
                       "remove",
                       "com.apple.CoreSimulator.CoreSimulatorService.117.15.1.lkhDXxRPp5yy"))
               .build(),
           new FakeProcess(0)));
   fakeProcessesBuilder.add(
       new SimpleImmutableEntry<>(
           ProcessExecutorParams.builder()
               .setCommand(
                   ImmutableList.of(
                       "launchctl",
                       "remove",
                       "com.apple.CoreSimulator.SimDevice.CC1B0BAD-BAE6-4A53-92CF-F79850654057."
                           + "launchd_sim"))
               .build(),
           new FakeProcess(3)));
   fakeProcessesBuilder.add(
       new SimpleImmutableEntry<>(
           ProcessExecutorParams.builder()
               .setCommand(
                   ImmutableList.of("launchctl", "remove", "com.apple.iphonesimulator.6564"))
               .build(),
           new FakeProcess(0)));
   FakeProcessExecutor fakeProcessExecutor = new FakeProcessExecutor(fakeProcessesBuilder.build());
   AppleCoreSimulatorServiceController appleCoreSimulatorServiceController =
       new AppleCoreSimulatorServiceController(fakeProcessExecutor);
   assertThat(appleCoreSimulatorServiceController.killSimulatorProcesses(), is(true));
 }
Пример #2
0
 private String getGoEnvFromTool(ProcessExecutor processExecutor, String env) {
   Path goTool = getGoToolPath();
   Optional<Map<String, String>> goRootEnv =
       delegate
           .getPath("go", "root")
           .transform(
               new Function<Path, Map<String, String>>() {
                 @Override
                 public Map<String, String> apply(Path input) {
                   return ImmutableMap.of("GOROOT", input.toString());
                 }
               });
   try {
     ProcessExecutor.Result goToolResult =
         processExecutor.launchAndExecute(
             ProcessExecutorParams.builder()
                 .addCommand(goTool.toString(), "env", env)
                 .setEnvironment(goRootEnv)
                 .build(),
             EnumSet.of(ProcessExecutor.Option.EXPECTING_STD_ERR),
             /* stdin */ Optional.<String>absent(),
             /* timeOutMs */ Optional.<Long>absent(),
             /* timeoutHandler */ Optional.<Function<Process, Void>>absent());
     if (goToolResult.getExitCode() == 0) {
       return CharMatcher.WHITESPACE.trimFrom(goToolResult.getStdout().get());
     } else {
       throw new HumanReadableException(goToolResult.getStderr().get());
     }
   } catch (InterruptedException e) {
     throw Throwables.propagate(e);
   } catch (IOException e) {
     throw new HumanReadableException(e, "Could not run \"%s env %s\": %s", env, goTool);
   }
 }
 @Test
 public void coreSimulatorServicesKillFailsIfUnrecognizedError()
     throws IOException, InterruptedException {
   ImmutableList.Builder<Map.Entry<ProcessExecutorParams, FakeProcess>> fakeProcessesBuilder =
       ImmutableList.builder();
   fakeProcessesBuilder.add(
       new SimpleImmutableEntry<>(
           LAUNCHCTL_LIST_PARAMS,
           new FakeProcess(
               0,
               "87823\t0\tcom.apple.CoreSimulator.CoreSimulatorService.117.15.1.lkhDXxRPp5yy\n",
               "")));
   fakeProcessesBuilder.add(
       new SimpleImmutableEntry<>(
           ProcessExecutorParams.builder()
               .setCommand(
                   ImmutableList.of(
                       "launchctl",
                       "remove",
                       "com.apple.CoreSimulator.CoreSimulatorService.117.15.1.lkhDXxRPp5yy"))
               .build(),
           new FakeProcess(42)));
   FakeProcessExecutor fakeProcessExecutor = new FakeProcessExecutor(fakeProcessesBuilder.build());
   AppleCoreSimulatorServiceController appleCoreSimulatorServiceController =
       new AppleCoreSimulatorServiceController(fakeProcessExecutor);
   assertThat(appleCoreSimulatorServiceController.killSimulatorProcesses(), is(false));
 }
Пример #4
0
  private int runTestsExternal(
      final CommandRunnerParams params,
      Build build,
      Iterable<String> command,
      Iterable<TestRule> testRules)
      throws InterruptedException, IOException {
    TestRunningOptions options = getTestRunningOptions(params);

    // Walk the test rules, collecting all the specs.
    List<ExternalTestRunnerTestSpec> specs = Lists.newArrayList();
    for (TestRule testRule : testRules) {
      if (!(testRule instanceof ExternalTestRunnerRule)) {
        params
            .getBuckEventBus()
            .post(
                ConsoleEvent.severe(
                    String.format(
                        "Test %s does not support external test running",
                        testRule.getBuildTarget())));
        return 1;
      }
      ExternalTestRunnerRule rule = (ExternalTestRunnerRule) testRule;
      specs.add(rule.getExternalTestRunnerSpec(build.getExecutionContext(), options));
    }

    // Serialize the specs to a file to pass into the test runner.
    Path infoFile =
        params
            .getCell()
            .getFilesystem()
            .resolve(BuckConstant.SCRATCH_PATH.resolve("external_runner_specs.json"));
    Files.createDirectories(infoFile.getParent());
    Files.deleteIfExists(infoFile);
    params.getObjectMapper().writerWithDefaultPrettyPrinter().writeValue(infoFile.toFile(), specs);

    // Launch and run the external test runner, forwarding it's stdout/stderr to the console.
    // We wait for it to complete then returns its error code.
    ListeningProcessExecutor processExecutor = new ListeningProcessExecutor();
    ProcessExecutorParams processExecutorParams =
        ProcessExecutorParams.builder()
            .addAllCommand(command)
            .addAllCommand(withDashArguments)
            .addCommand("--buck-test-info", infoFile.toString())
            .setDirectory(params.getCell().getFilesystem().getRootPath().toFile())
            .build();
    ForwardingProcessListener processListener =
        new ForwardingProcessListener(
            Channels.newChannel(params.getConsole().getStdOut()),
            Channels.newChannel(params.getConsole().getStdErr()));
    ListeningProcessExecutor.LaunchedProcess process =
        processExecutor.launchProcess(processExecutorParams, processListener);
    try {
      return processExecutor.waitForProcess(process, Long.MAX_VALUE, TimeUnit.DAYS);
    } finally {
      processExecutor.destroyProcess(process, /* force */ false);
      processExecutor.waitForProcess(process, Long.MAX_VALUE, TimeUnit.DAYS);
    }
  }
Пример #5
0
 private ProcessExecutorParams makeProcessExecutorParams() {
   ProcessExecutorParams.Builder builder = ProcessExecutorParams.builder();
   builder.setDirectory(workingDirectory.toAbsolutePath().toFile());
   builder.setCommand(command);
   if (codeSignIdentitySupplier.isPresent()) {
     builder.addCommand(
         "--sign", CodeSignStep.getIdentityArg(codeSignIdentitySupplier.get().get()));
   }
   return builder.build();
 }
Пример #6
0
 private ProcessExecutor.Result doRunCommand(List<String> command)
     throws IOException, InterruptedException {
   ProcessExecutorParams params = ProcessExecutorParams.builder().setCommand(command).build();
   ProcessExecutor executor = new DefaultProcessExecutor(new TestConsole());
   String currentDir = System.getProperty("user.dir");
   try {
     System.setProperty("user.dir", destPath.toAbsolutePath().toString());
     return executor.launchAndExecute(params);
   } finally {
     System.setProperty("user.dir", currentDir);
   }
 }
Пример #7
0
 private static PythonVersion getPythonVersion(ProcessExecutor processExecutor, Path pythonPath)
     throws InterruptedException {
   try {
     ProcessExecutor.Result versionResult =
         processExecutor.launchAndExecute(
             ProcessExecutorParams.builder().addCommand(pythonPath.toString(), "-V").build(),
             EnumSet.of(ProcessExecutor.Option.EXPECTING_STD_ERR),
             /* stdin */ Optional.<String>absent(),
             /* timeOutMs */ Optional.<Long>absent(),
             /* timeoutHandler */ Optional.<Function<Process, Void>>absent());
     return extractPythonVersion(pythonPath, versionResult);
   } catch (IOException e) {
     throw new HumanReadableException(
         e, "Could not run \"%s --version\": %s", pythonPath, e.getMessage());
   }
 }
Пример #8
0
 @Test
 public void xctoolCommandWithAppAndLogicTests() throws Exception {
   FakeProjectFilesystem projectFilesystem = new FakeProjectFilesystem();
   XctoolRunTestsStep step =
       new XctoolRunTestsStep(
           projectFilesystem,
           Paths.get("/path/to/xctool"),
           Optional.<Long>absent(),
           "iphonesimulator",
           Optional.of("name=iPhone 5s,OS=8.2"),
           ImmutableSet.of(Paths.get("/path/to/FooLogicTest.xctest")),
           ImmutableMap.of(Paths.get("/path/to/FooAppTest.xctest"), Paths.get("/path/to/Foo.app")),
           Paths.get("/path/to/output.json"),
           Optional.<XctoolRunTestsStep.StdoutReadingCallback>absent());
   ProcessExecutorParams xctoolParams =
       ProcessExecutorParams.builder()
           .setCommand(
               ImmutableList.of(
                   "/path/to/xctool",
                   "-reporter",
                   "json-stream",
                   "-sdk",
                   "iphonesimulator",
                   "-destination",
                   "name=iPhone 5s,OS=8.2",
                   "run-tests",
                   "-logicTest",
                   "/path/to/FooLogicTest.xctest",
                   "-appTest",
                   "/path/to/FooAppTest.xctest:/path/to/Foo.app"))
           .setDirectory(projectFilesystem.getRootPath().toAbsolutePath().toFile())
           .setRedirectOutput(ProcessBuilder.Redirect.PIPE)
           .build();
   FakeProcess fakeXctoolSuccess = new FakeProcess(0, "", "");
   FakeProcessExecutor processExecutor =
       new FakeProcessExecutor(ImmutableMap.of(xctoolParams, fakeXctoolSuccess));
   ExecutionContext executionContext =
       TestExecutionContext.newBuilder()
           .setProcessExecutor(processExecutor)
           .setEnvironment(ImmutableMap.<String, String>of())
           .build();
   assertThat(step.execute(executionContext), equalTo(0));
 }
 @Test
 public void coreSimulatorServicePathFetchedFromLaunchctlPrint()
     throws IOException, InterruptedException {
   ImmutableList.Builder<Map.Entry<ProcessExecutorParams, FakeProcess>> fakeProcessesBuilder =
       ImmutableList.builder();
   fakeProcessesBuilder.add(
       new SimpleImmutableEntry<>(
           LAUNCHCTL_LIST_PARAMS,
           new FakeProcess(
               0,
               "87823\t0\tcom.apple.CoreSimulator.CoreSimulatorService.117.15.1.lkhDXxRPp5yy\n",
               "")));
   fakeProcessesBuilder.add(
       new SimpleImmutableEntry<>(
           ProcessExecutorParams.builder()
               .setCommand(
                   ImmutableList.of(
                       "launchctl",
                       "print",
                       "user/42/com.apple.CoreSimulator.CoreSimulatorService.117.15.1.lkhDXxRPp5yy"))
               .build(),
           new FakeProcess(
               0,
               "com.apple.CoreSimulator.CoreSimulatorService.117.15.1.lkhDXxRPp5yy = {\n"
                   + "    path = xcode-dir/Developer/Library/PrivateFrameworks/CoreSimulator.framework"
                   + "/Versions/A/XPCServices/com.apple.CoreSimulator.CoreSimulatorService.xpc\n"
                   + "}\n",
               "")));
   FakeProcessExecutor fakeProcessExecutor = new FakeProcessExecutor(fakeProcessesBuilder.build());
   AppleCoreSimulatorServiceController appleCoreSimulatorServiceController =
       new AppleCoreSimulatorServiceController(fakeProcessExecutor);
   Optional<Path> coreSimulatorServicePath =
       appleCoreSimulatorServiceController.getCoreSimulatorServicePath(new FakeUserIdFetcher(42));
   Optional<Path> expected =
       Optional.of(
           Paths.get(
               "xcode-dir/Developer/Library/PrivateFrameworks/CoreSimulator.framework/"
                   + "Versions/A/XPCServices/com.apple.CoreSimulator.CoreSimulatorService.xpc"));
   assertThat(coreSimulatorServicePath, is(equalTo(expected)));
 }
Пример #10
0
 @Override
 public StepExecutionResult execute(ExecutionContext context)
     throws IOException, InterruptedException {
   ImmutableList<String> lldbCommandPrefix = lldb.getCommandPrefix(resolver);
   ProcessExecutorParams params =
       ProcessExecutorParams.builder()
           .addCommand(lldbCommandPrefix.toArray(new String[lldbCommandPrefix.size()]))
           .build();
   return StepExecutionResult.of(
       context
           .getProcessExecutor()
           .launchAndExecute(
               params,
               ImmutableSet.<ProcessExecutor.Option>of(),
               Optional.of(
                   String.format(
                       "target create %s\ntarget symbols add %s",
                       binaryBuildRule.getPathToOutput(), location)),
               Optional.<Long>absent(),
               Optional.<Function<Process, Void>>absent())
           .getExitCode());
 }
Пример #11
0
  /**
   * Checks whether a binary or bundle already has a valid code signature.
   *
   * @param path Resolved path to the binary or bundle.
   * @return Whether the binary or bundle has a valid code signature.
   */
  public static boolean hasValidSignature(ProcessExecutor processExecutor, Path path)
      throws InterruptedException, IOException {
    ProcessExecutorParams processExecutorParams =
        ProcessExecutorParams.builder()
            .setCommand(ImmutableList.of("codesign", "-vvvv", path.toString()))
            .build();

    // Specify that stdout is expected, or else output may be wrapped in Ansi escape chars.
    Set<ProcessExecutor.Option> options =
        EnumSet.of(ProcessExecutor.Option.EXPECTING_STD_OUT, ProcessExecutor.Option.IS_SILENT);

    ProcessExecutor.Result result =
        processExecutor.launchAndExecute(
            processExecutorParams,
            options,
            /* stdin */ Optional.empty(),
            /* timeOutMs */ Optional.empty(),
            /* timeOutHandler */ Optional.empty());

    return result.getExitCode() == 0
        && result.getStderr().isPresent()
        && result.getStderr().get().contains(": satisfies its Designated Requirement");
  }
/** Unit tests for {@link AppleCoreSimulatorServiceController}. */
public class AppleCoreSimulatorServiceControllerTest {

  private static final ProcessExecutorParams LAUNCHCTL_LIST_PARAMS =
      ProcessExecutorParams.builder().setCommand(ImmutableList.of("launchctl", "list")).build();

  @Test
  public void coreSimulatorServicePathFetchedFromLaunchctlPrint()
      throws IOException, InterruptedException {
    ImmutableList.Builder<Map.Entry<ProcessExecutorParams, FakeProcess>> fakeProcessesBuilder =
        ImmutableList.builder();
    fakeProcessesBuilder.add(
        new SimpleImmutableEntry<>(
            LAUNCHCTL_LIST_PARAMS,
            new FakeProcess(
                0,
                "87823\t0\tcom.apple.CoreSimulator.CoreSimulatorService.117.15.1.lkhDXxRPp5yy\n",
                "")));
    fakeProcessesBuilder.add(
        new SimpleImmutableEntry<>(
            ProcessExecutorParams.builder()
                .setCommand(
                    ImmutableList.of(
                        "launchctl",
                        "print",
                        "user/42/com.apple.CoreSimulator.CoreSimulatorService.117.15.1.lkhDXxRPp5yy"))
                .build(),
            new FakeProcess(
                0,
                "com.apple.CoreSimulator.CoreSimulatorService.117.15.1.lkhDXxRPp5yy = {\n"
                    + "    path = xcode-dir/Developer/Library/PrivateFrameworks/CoreSimulator.framework"
                    + "/Versions/A/XPCServices/com.apple.CoreSimulator.CoreSimulatorService.xpc\n"
                    + "}\n",
                "")));
    FakeProcessExecutor fakeProcessExecutor = new FakeProcessExecutor(fakeProcessesBuilder.build());
    AppleCoreSimulatorServiceController appleCoreSimulatorServiceController =
        new AppleCoreSimulatorServiceController(fakeProcessExecutor);
    Optional<Path> coreSimulatorServicePath =
        appleCoreSimulatorServiceController.getCoreSimulatorServicePath(new FakeUserIdFetcher(42));
    Optional<Path> expected =
        Optional.of(
            Paths.get(
                "xcode-dir/Developer/Library/PrivateFrameworks/CoreSimulator.framework/"
                    + "Versions/A/XPCServices/com.apple.CoreSimulator.CoreSimulatorService.xpc"));
    assertThat(coreSimulatorServicePath, is(equalTo(expected)));
  }

  @Test
  public void coreSimulatorServicesKilledSuccessfully() throws IOException, InterruptedException {
    ImmutableList.Builder<Map.Entry<ProcessExecutorParams, FakeProcess>> fakeProcessesBuilder =
        ImmutableList.builder();
    fakeProcessesBuilder.add(
        new SimpleImmutableEntry<>(
            LAUNCHCTL_LIST_PARAMS,
            new FakeProcess(
                0,
                "87823\t0\tcom.apple.CoreSimulator.CoreSimulatorService.117.15.1.lkhDXxRPp5yy\n"
                    + "74617\t0\tcom.apple.CoreSimulator.SimDevice.CC1B0BAD-BAE6-4A53-92CF-F79850654057"
                    + ".launchd_sim\n"
                    + "74614\t0\tcom.apple.iphonesimulator.6564\n",
                "")));
    fakeProcessesBuilder.add(
        new SimpleImmutableEntry<>(
            ProcessExecutorParams.builder()
                .setCommand(
                    ImmutableList.of(
                        "launchctl",
                        "remove",
                        "com.apple.CoreSimulator.CoreSimulatorService.117.15.1.lkhDXxRPp5yy"))
                .build(),
            new FakeProcess(0)));
    fakeProcessesBuilder.add(
        new SimpleImmutableEntry<>(
            ProcessExecutorParams.builder()
                .setCommand(
                    ImmutableList.of(
                        "launchctl",
                        "remove",
                        "com.apple.CoreSimulator.SimDevice.CC1B0BAD-BAE6-4A53-92CF-F79850654057."
                            + "launchd_sim"))
                .build(),
            new FakeProcess(0)));
    fakeProcessesBuilder.add(
        new SimpleImmutableEntry<>(
            ProcessExecutorParams.builder()
                .setCommand(
                    ImmutableList.of("launchctl", "remove", "com.apple.iphonesimulator.6564"))
                .build(),
            new FakeProcess(0)));
    FakeProcessExecutor fakeProcessExecutor = new FakeProcessExecutor(fakeProcessesBuilder.build());
    AppleCoreSimulatorServiceController appleCoreSimulatorServiceController =
        new AppleCoreSimulatorServiceController(fakeProcessExecutor);
    assertThat(appleCoreSimulatorServiceController.killSimulatorProcesses(), is(true));
  }

  @Test
  public void coreSimulatorServicesKillSucceedsEvenIfNoSuchProcess()
      throws IOException, InterruptedException {
    ImmutableList.Builder<Map.Entry<ProcessExecutorParams, FakeProcess>> fakeProcessesBuilder =
        ImmutableList.builder();
    fakeProcessesBuilder.add(
        new SimpleImmutableEntry<>(
            LAUNCHCTL_LIST_PARAMS,
            new FakeProcess(
                0,
                "87823\t0\tcom.apple.CoreSimulator.CoreSimulatorService.117.15.1.lkhDXxRPp5yy\n"
                    + "74617\t0\tcom.apple.CoreSimulator.SimDevice.CC1B0BAD-BAE6-4A53-92CF-F79850654057"
                    + ".launchd_sim\n"
                    + "74614\t0\tcom.apple.iphonesimulator.6564\n",
                "")));
    fakeProcessesBuilder.add(
        new SimpleImmutableEntry<>(
            ProcessExecutorParams.builder()
                .setCommand(
                    ImmutableList.of(
                        "launchctl",
                        "remove",
                        "com.apple.CoreSimulator.CoreSimulatorService.117.15.1.lkhDXxRPp5yy"))
                .build(),
            new FakeProcess(0)));
    fakeProcessesBuilder.add(
        new SimpleImmutableEntry<>(
            ProcessExecutorParams.builder()
                .setCommand(
                    ImmutableList.of(
                        "launchctl",
                        "remove",
                        "com.apple.CoreSimulator.SimDevice.CC1B0BAD-BAE6-4A53-92CF-F79850654057."
                            + "launchd_sim"))
                .build(),
            new FakeProcess(3)));
    fakeProcessesBuilder.add(
        new SimpleImmutableEntry<>(
            ProcessExecutorParams.builder()
                .setCommand(
                    ImmutableList.of("launchctl", "remove", "com.apple.iphonesimulator.6564"))
                .build(),
            new FakeProcess(0)));
    FakeProcessExecutor fakeProcessExecutor = new FakeProcessExecutor(fakeProcessesBuilder.build());
    AppleCoreSimulatorServiceController appleCoreSimulatorServiceController =
        new AppleCoreSimulatorServiceController(fakeProcessExecutor);
    assertThat(appleCoreSimulatorServiceController.killSimulatorProcesses(), is(true));
  }

  @Test
  public void coreSimulatorServicesKillFailsIfUnrecognizedError()
      throws IOException, InterruptedException {
    ImmutableList.Builder<Map.Entry<ProcessExecutorParams, FakeProcess>> fakeProcessesBuilder =
        ImmutableList.builder();
    fakeProcessesBuilder.add(
        new SimpleImmutableEntry<>(
            LAUNCHCTL_LIST_PARAMS,
            new FakeProcess(
                0,
                "87823\t0\tcom.apple.CoreSimulator.CoreSimulatorService.117.15.1.lkhDXxRPp5yy\n",
                "")));
    fakeProcessesBuilder.add(
        new SimpleImmutableEntry<>(
            ProcessExecutorParams.builder()
                .setCommand(
                    ImmutableList.of(
                        "launchctl",
                        "remove",
                        "com.apple.CoreSimulator.CoreSimulatorService.117.15.1.lkhDXxRPp5yy"))
                .build(),
            new FakeProcess(42)));
    FakeProcessExecutor fakeProcessExecutor = new FakeProcessExecutor(fakeProcessesBuilder.build());
    AppleCoreSimulatorServiceController appleCoreSimulatorServiceController =
        new AppleCoreSimulatorServiceController(fakeProcessExecutor);
    assertThat(appleCoreSimulatorServiceController.killSimulatorProcesses(), is(false));
  }
}