/** Fake installation on Unix. */
  @Test
  public void fakeUnixInstall() throws Exception {
    Assume.assumeFalse("If we're on Windows, don't bother doing this", Functions.isWindows());

    File bundle = File.createTempFile("fake-jdk-by-hudson", "sh");
    try {
      new FilePath(bundle)
          .write(
              "#!/bin/bash -ex\n"
                  + "mkdir -p jdk1.6.0_dummy/bin\n"
                  + "touch jdk1.6.0_dummy/bin/java",
              "ASCII");
      TaskListener l = StreamTaskListener.fromStdout();

      new JDKInstaller("", true)
          .install(
              new LocalLauncher(l),
              Platform.LINUX,
              new JDKInstaller.FilePathFileSystem(j.jenkins),
              l,
              tmp.getRoot().getPath(),
              bundle.getPath());

      assertTrue(new File(tmp.getRoot(), "bin/java").exists());
    } finally {
      bundle.delete();
    }
  }
  @Issue("JENKINS-30941")
  @Test
  public void cleanUpSucceeds() throws Exception {
    /** Issue was just present on Linux not windows - but the test will run on both */
    final String credentialsId = "zipfile";

    /* do the dance to get a simple zip file into jenkins */
    InputStream zipStream = this.getClass().getResourceAsStream("a.zip");
    try {
      assertThat(zipStream, is(not(nullValue())));
      File zip = tmp.newFile("a.zip");
      FileUtils.copyInputStreamToFile(zipStream, zip);
      FileItem fi = new FileItemImpl(zip);
      FileCredentialsImpl fc =
          new FileCredentialsImpl(
              CredentialsScope.GLOBAL, credentialsId, "Just a zip file", fi, fi.getName(), null);
      CredentialsProvider.lookupStores(j.jenkins)
          .iterator()
          .next()
          .addCredentials(Domain.global(), fc);
    } finally {
      IOUtils.closeQuietly(zipStream);
      zipStream = null;
    }

    final String unixFile = "/dir/testfile.txt";
    final String winFile =
        unixFile.replace(
            "/", "\\\\"); /* two \\ as we escape the code and then escape for the script */
    // if this file does not have a line ending then the text is not echoed to the log.
    // fixed in workflow 1.11+ (which is not released at the time of writing)
    final String contents = "Test of ZipFileBinding\n";

    WorkflowJob p = j.jenkins.createProject(WorkflowJob.class, "p");
    p.setDefinition(
        new CpsFlowDefinition(
            ""
                + "node {\n"
                + "  withCredentials([[$class: 'ZipFileBinding', credentialsId: '"
                + credentialsId
                + "', variable: 'ziploc']]) {\n"
                + (Functions.isWindows()
                    ? "    bat 'type %ziploc%" + winFile + "'\n"
                    : "    sh 'cat ${ziploc}" + unixFile + "'\n")
                + "    def text = readFile encoding: 'UTF-8', file: \"${env.ziploc}"
                + unixFile
                + "\"\n"
                + "    if (!text.equals('''"
                + contents
                + "''')) {\n"
                + "      error ('incorrect details from zip file')\n"
                + "    }\n"
                + "  }\n"
                + "}\n",
            true));

    WorkflowRun run = p.scheduleBuild2(0).get();
    j.assertBuildStatusSuccess(run);
    j.assertLogContains(contents, run);
  }
Beispiel #3
0
  @Test
  public void testIsSymlink() throws IOException, InterruptedException {
    Assume.assumeTrue(!Functions.isWindows());

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    StreamTaskListener l = new StreamTaskListener(baos);
    File d = Util.createTempDir();
    try {
      new FilePath(new File(d, "original")).touch(0);
      assertFalse(Util.isSymlink(new File(d, "original")));
      Util.createSymlink(d, "original", "link", l);

      assertTrue(Util.isSymlink(new File(d, "link")));

      // test linking to another directory
      File dir = new File(d, "dir");
      assertTrue("Couldn't create " + dir, dir.mkdir());
      assertFalse(Util.isSymlink(new File(d, "dir")));

      File anotherDir = new File(d, "anotherDir");
      assertTrue("Couldn't create " + anotherDir, anotherDir.mkdir());

      Util.createSymlink(d, "dir", "anotherDir/symlinkDir", l);
      // JENKINS-12331: either a bug in createSymlink or this isn't supposed to work:
      // assertTrue(Util.isSymlink(new File(d,"anotherDir/symlinkDir")));
    } finally {
      Util.deleteRecursive(d);
    }
  }
Beispiel #4
0
  public HsErrPidList() {
    if (Functions.getIsUnitTest()) {
      return;
    }
    try {
      FileChannel ch = new FileInputStream(getSecretKeyFile()).getChannel();
      map = ch.map(MapMode.READ_ONLY, 0, 1);

      scan("./hs_err_pid%p.log");
      if (Functions.isWindows()) {
        File dir = Kernel32Utils.getTempDir();
        if (dir != null) {
          scan(dir.getPath() + "\\hs_err_pid%p.log");
        }
      } else {
        scan("/tmp/hs_err_pid%p.log");
      }
      // on different platforms, rules about the default locations are a lot more subtle.

      // check our arguments in the very end since this might fail on some platforms
      JavaVMArguments args = JavaVMArguments.current();
      for (String a : args) {
        // see http://www.oracle.com/technetwork/java/javase/felog-138657.html
        if (a.startsWith(ERROR_FILE_OPTION)) {
          scan(a.substring(ERROR_FILE_OPTION.length()));
        }
      }
    } catch (UnsupportedOperationException e) {
      // ignore
    } catch (Throwable e) {
      LOGGER.log(Level.WARNING, "Failed to list up hs_err_pid files", e);
    }
  }
Beispiel #5
0
 @Test
 public void deleteFile() throws Exception {
   Assume.assumeTrue(Functions.isWindows());
   Class<?> c;
   try {
     c = Class.forName("java.nio.file.FileSystemException");
   } catch (ClassNotFoundException x) {
     throw new AssumptionViolatedException("prior to JDK 7", x);
   }
   File d = Util.createTempDir();
   try {
     File f = new File(d, "f");
     OutputStream os = new FileOutputStream(f);
     try {
       Util.deleteFile(f);
       fail("should not have been deletable");
     } catch (IOException x) {
       assertEquals(c, x.getClass());
     } finally {
       os.close();
     }
   } finally {
     Util.deleteRecursive(d);
   }
 }
Beispiel #6
0
  public void testSymlink() throws Exception {
    if (Functions.isWindows()) return;

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    StreamTaskListener l = new StreamTaskListener(baos);
    File d = Util.createTempDir();
    try {
      new FilePath(new File(d, "a")).touch(0);
      Util.createSymlink(d, "a", "x", l);
      assertEquals("a", Util.resolveSymlink(new File(d, "x"), l));

      // test a long name
      StringBuilder buf = new StringBuilder(768);
      for (int i = 0; i < 768; i++) buf.append('0' + (i % 10));
      Util.createSymlink(d, buf.toString(), "x", l);

      String log = baos.toString();
      if (log.length() > 0) {
        System.err.println("log output: " + log);
        if (log.contains("ln failed: 78" /*ENAMETOOLONG*/)) {
          buf.setLength(0);
          // Try again with shorter name for this system
          for (int i = 0; i < 254; i++) buf.append('0' + (i % 10));
          Util.createSymlink(d, buf.toString(), "x", l);
        }
      }

      assertEquals(buf.toString(), Util.resolveSymlink(new File(d, "x"), l));
    } finally {
      Util.deleteRecursive(d);
    }
  }
 @Extension
 public static DescriptorImpl install() {
   if (!Functions.isWindows()) {
     return new DescriptorImpl();
   }
   return null;
 }
 /** @return configured dot executable or a default */
 public String getDotExeOrDefault() {
   if (Util.fixEmptyAndTrim(dotExe) == null) {
     return Functions.isWindows() ? "dot.exe" : "dot";
   } else {
     return dotExe;
   }
 }
    /**
     * If true, we can do ADSI/COM based look up that's far more reliable. False if we need to do
     * the authentication in pure Java via {@link ActiveDirectoryUnixAuthenticationProvider}
     */
    public boolean canDoNativeAuth() {
      if (!Functions.isWindows()) return false;

      try {
        ClassFactory.createConnection().dispose();
        return true;
      } catch (Throwable t) {
        if (!WARNED) {
          LOGGER.log(
              Level.INFO, "COM4J isn't working. Falling back to non-native authentication", t);
          WARNED = true;
        }
        return false;
      }
    }
  @Bug(22641)
  public void testProcessProperlyKilledUnix() throws Exception {
    ProcessTree.enabled = true;
    if (Functions.isWindows()) return; // This test does not involve windows.

    FreeStyleProject sleepProject = createFreeStyleProject();
    FreeStyleProject processJob = createFreeStyleProject();

    sleepProject.getBuildersList().add(new Shell("nohup sleep 100000 &"));

    assertBuildStatusSuccess(sleepProject.scheduleBuild2(0).get());

    processJob.getBuildersList().add(new Shell("ps -ef | grep sleep"));

    assertLogNotContains("sleep 100000", processJob.scheduleBuild2(0).get());
  }
Beispiel #11
0
  @Test
  public void testSymlink() throws Exception {
    Assume.assumeTrue(!Functions.isWindows());

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    StreamTaskListener l = new StreamTaskListener(baos);
    File d = Util.createTempDir();
    try {
      new FilePath(new File(d, "a")).touch(0);
      assertNull(Util.resolveSymlink(new File(d, "a")));
      Util.createSymlink(d, "a", "x", l);
      assertEquals("a", Util.resolveSymlink(new File(d, "x")));

      // test a long name
      StringBuilder buf = new StringBuilder(768);
      for (int i = 0; i < 768; i++) buf.append((char) ('0' + (i % 10)));
      Util.createSymlink(d, buf.toString(), "x", l);

      String log = baos.toString();
      if (log.length() > 0) System.err.println("log output: " + log);

      assertEquals(buf.toString(), Util.resolveSymlink(new File(d, "x")));

      // test linking from another directory
      File anotherDir = new File(d, "anotherDir");
      assertTrue("Couldn't create " + anotherDir, anotherDir.mkdir());

      Util.createSymlink(d, "a", "anotherDir/link", l);
      assertEquals("a", Util.resolveSymlink(new File(d, "anotherDir/link")));

      // JENKINS-12331: either a bug in createSymlink or this isn't supposed to work:
      // assertTrue(Util.isSymlink(new File(d,"anotherDir/link")));

      File external = File.createTempFile("something", "");
      try {
        Util.createSymlink(d, external.getAbsolutePath(), "outside", l);
        assertEquals(external.getAbsolutePath(), Util.resolveSymlink(new File(d, "outside")));
      } finally {
        assertTrue(external.delete());
      }
    } finally {
      Util.deleteRecursive(d);
    }
  }
Beispiel #12
0
  /**
   * Gets the {@link User} object by its id or full name.
   *
   * @param create If true, this method will never return null for valid input (by creating a new
   *     {@link User} object if none exists.) If false, this method will return null if {@link User}
   *     object with the given name doesn't exist.
   */
  public static User get(String idOrFullName, boolean create) {
    if (idOrFullName == null) return null;
    String id =
        idOrFullName
            .replace('\\', '_')
            .replace('/', '_')
            .replace('<', '_')
            .replace('>', '_'); // 4 replace() still faster than regex
    if (Functions.isWindows()) id = id.replace(':', '_');

    synchronized (byName) {
      User u = byName.get(id);
      if (u == null) {
        User tmp = new User(id, idOrFullName);
        if (create || tmp.getConfigFile().exists()) {
          byName.put(id, u = tmp);
        }
      }
      return u;
    }
  }
    private boolean createSdCard(File homeDir) {
      // Build command: mksdcard 32M /home/foo/.android/avd/whatever.avd/sdcard.img
      ArgumentListBuilder builder =
          Utils.getToolCommand(androidSdk, !Functions.isWindows(), Tool.MKSDCARD, null);
      builder.add(sdCardSize);
      builder.add(new File(getAvdDirectory(homeDir), "sdcard.img"));

      // Run!
      try {
        ProcessBuilder procBuilder = new ProcessBuilder(builder.toList());
        if (androidSdkHome != null) {
          procBuilder.environment().put("ANDROID_SDK_HOME", androidSdkHome);
        }
        procBuilder.start().waitFor();
      } catch (InterruptedException ex) {
        return false;
      } catch (IOException ex) {
        return false;
      }

      return true;
    }
 private void configureDumpEnvBuilder() throws IOException {
   if (Functions.isWindows()) project.getBuildersList().add(new BatchFile("set"));
   else project.getBuildersList().add(new Shell("export"));
 }
Beispiel #15
0
 public String call() throws IOException {
   return Functions.isWindows() ? "sh" : "/bin/sh";
 }
Beispiel #16
0
 public String getShellOrDefault() {
   if (shell == null) {
     return Functions.isWindows() ? "sh" : "/bin/sh";
   }
   return shell;
 }
    public Boolean call() throws AndroidEmulatorException {
      if (logger == null) {
        logger = listener.getLogger();
      }

      final File homeDir = Utils.getHomeDirectory(androidSdk.getSdkHome());
      final File avdDirectory = getAvdDirectory(homeDir);
      final boolean emulatorExists = getAvdConfigFile(homeDir).exists();

      // Can't do anything if a named emulator doesn't exist
      if (isNamedEmulator() && !emulatorExists) {
        throw new EmulatorDiscoveryException(Messages.AVD_DOES_NOT_EXIST(avdName, avdDirectory));
      }

      // Check whether AVD needs to be created
      boolean createSdCard = false;
      boolean createSnapshot = false;
      File snapshotsFile = new File(getAvdDirectory(homeDir), "snapshots.img");
      if (emulatorExists) {
        // AVD exists: check whether there's anything still to be set up
        File sdCardFile = new File(getAvdDirectory(homeDir), "sdcard.img");
        boolean sdCardRequired = getSdCardSize() != null;

        // Check if anything needs to be done for snapshot-enabled builds
        if (shouldUseSnapshots() && androidSdk.supportsSnapshots()) {
          if (!snapshotsFile.exists()) {
            createSnapshot = true;
          }

          // We should ensure that we start out with a clean SD card for the build
          if (sdCardRequired && sdCardFile.exists()) {
            sdCardFile.delete();
          }
        }

        // Flag that we need to generate an SD card, if there isn't one existing
        if (sdCardRequired && !sdCardFile.exists()) {
          createSdCard = true;
        }

        // If everything is ready, then return
        if (!createSdCard && !createSnapshot) {
          return true;
        }
      } else {
        AndroidEmulator.log(logger, Messages.CREATING_AVD(avdDirectory));
      }

      // We can't continue if we don't know where to find emulator images or tools
      if (!androidSdk.hasKnownRoot()) {
        throw new EmulatorCreationException(Messages.SDK_NOT_SPECIFIED());
      }
      final File sdkRoot = new File(androidSdk.getSdkRoot());
      if (!sdkRoot.exists()) {
        throw new EmulatorCreationException(Messages.SDK_NOT_FOUND(androidSdk.getSdkRoot()));
      }

      // If we need to initialise snapshot support for an existing emulator, do so
      if (createSnapshot) {
        // Copy the snapshots file into place
        File snapshotDir = new File(sdkRoot, "tools/lib/emulator");
        Util.copyFile(new File(snapshotDir, "snapshots.img"), snapshotsFile);

        // Update the AVD config file mark snapshots as enabled
        Map<String, String> configValues;
        try {
          configValues = parseAvdConfigFile(homeDir);
          configValues.put("snapshot.present", "true");
          writeAvdConfigFile(homeDir, configValues);
        } catch (IOException e) {
          throw new EmulatorCreationException(Messages.AVD_CONFIG_NOT_READABLE(), e);
        }
      }

      // If we need create an SD card for an existing emulator, do so
      if (createSdCard) {
        AndroidEmulator.log(logger, Messages.ADDING_SD_CARD(sdCardSize, getAvdName()));
        if (!createSdCard(homeDir)) {
          throw new EmulatorCreationException(Messages.SD_CARD_CREATION_FAILED());
        }

        // Update the AVD config file
        Map<String, String> configValues;
        try {
          configValues = parseAvdConfigFile(homeDir);
          configValues.put("sdcard.size", sdCardSize);
          writeAvdConfigFile(homeDir, configValues);
        } catch (IOException e) {
          throw new EmulatorCreationException(Messages.AVD_CONFIG_NOT_READABLE(), e);
        }
      }

      // Return if everything is now ready for use
      if (emulatorExists) {
        return true;
      }

      // Build up basic arguments to `android` command
      final StringBuilder args = new StringBuilder(100);
      args.append("create avd ");

      // Overwrite any existing files
      args.append("-f ");

      // Initialise snapshot support, regardless of whether we will actually use it
      if (androidSdk.supportsSnapshots()) {
        args.append("-a ");
      }

      if (sdCardSize != null) {
        args.append("-c ");
        args.append(sdCardSize);
        args.append(" ");
      }
      args.append("-s ");
      args.append(screenResolution.getSkinName());
      args.append(" -n ");
      args.append(getAvdName());
      boolean isUnix = !Functions.isWindows();
      ArgumentListBuilder builder =
          Utils.getToolCommand(androidSdk, isUnix, Tool.ANDROID, args.toString());

      // Tack on quoted platform name at the end, since it can be anything
      builder.add("-t");
      builder.add(osVersion.getTargetName());

      if (targetAbi != null) {
        builder.add("--abi");
        builder.add(targetAbi);
      }

      // Log command line used, for info
      AndroidEmulator.log(logger, builder.toStringWithQuote());

      // Run!
      boolean avdCreated = false;
      final Process process;
      try {
        ProcessBuilder procBuilder = new ProcessBuilder(builder.toList());
        if (androidSdk.hasKnownHome()) {
          procBuilder.environment().put("ANDROID_SDK_HOME", androidSdk.getSdkHome());
        }
        process = procBuilder.start();
      } catch (IOException ex) {
        throw new EmulatorCreationException(Messages.AVD_CREATION_FAILED());
      }

      // Redirect process's stderr to a stream, for logging purposes
      ByteArrayOutputStream stderr = new ByteArrayOutputStream();
      ByteArrayOutputStream stdout = new ByteArrayOutputStream();
      new StreamCopyThread("", process.getErrorStream(), stderr).start();

      // Command may prompt us whether we want to further customise the AVD.
      // Just "press" Enter to continue with the selected target's defaults.
      try {
        boolean processAlive = true;

        // Block until the command outputs something (or process ends)
        final PushbackInputStream in = new PushbackInputStream(process.getInputStream(), 10);
        int len = in.read();
        if (len == -1) {
          // Check whether the process has exited badly, as sometimes no output is valid.
          // e.g. When creating an AVD with Google APIs, no user input is requested.
          if (process.waitFor() != 0) {
            AndroidEmulator.log(logger, Messages.AVD_CREATION_FAILED());
            AndroidEmulator.log(logger, stderr.toString(), true);
            throw new EmulatorCreationException(Messages.AVD_CREATION_FAILED());
          }
          processAlive = false;
        }
        in.unread(len);

        // Write CRLF, if required
        if (processAlive) {
          final OutputStream stream = process.getOutputStream();
          stream.write('\r');
          stream.write('\n');
          stream.flush();
          stream.close();
        }

        // read the rest of stdout (for debugging purposes)
        Util.copyStream(in, stdout);
        in.close();

        // Wait for happy ending
        if (process.waitFor() == 0) {
          // Do a sanity check to ensure the AVD was really created
          avdCreated = getAvdConfigFile(homeDir).exists();
        }

      } catch (IOException e) {
        throw new EmulatorCreationException(Messages.AVD_CREATION_ABORTED(), e);
      } catch (InterruptedException e) {
        throw new EmulatorCreationException(Messages.AVD_CREATION_INTERRUPTED(), e);
      } finally {
        process.destroy();
      }

      // For reasons unknown, the return code may not be correctly reported on Windows.
      // So check whether stderr contains failure info (useful for other platforms too).
      String errOutput = stderr.toString();
      String output = stdout.toString();
      if (errOutput.contains("list targets")) {
        AndroidEmulator.log(logger, Messages.INVALID_AVD_TARGET(osVersion.getTargetName()));
        avdCreated = false;
        errOutput = null;
      } else if (errOutput.contains("more than one ABI")) {
        AndroidEmulator.log(
            logger, Messages.MORE_THAN_ONE_ABI(osVersion.getTargetName(), output), true);
        avdCreated = false;
        errOutput = null;
      }

      // Check everything went ok
      if (!avdCreated) {
        if (errOutput != null && errOutput.length() != 0) {
          AndroidEmulator.log(logger, stderr.toString(), true);
        }
        throw new EmulatorCreationException(Messages.AVD_CREATION_FAILED());
      }

      // Done!
      return false;
    }
Beispiel #18
0
    /**
     * Returns the name of the NAnt executable for the current platform
     *
     * @return the name of the NAnt executable for the current platform
     */
    public static String getExecutableName() {
      if (Functions.isWindows()) return NantConstants.NANT_EXECUTABLE_WINDOWS;

      return NantConstants.NANT_EXECUTABLE_UNIX;
    }