Skip to content

Commit

Permalink
[FIXED JENKINS-16246] Build can now be failed if package uninstallati…
Browse files Browse the repository at this point in the history
…on fails.
  • Loading branch information
orrc committed Apr 11, 2013
1 parent 59e2ec5 commit f3fff7c
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 9 deletions.
Expand Up @@ -26,16 +26,24 @@ public class UninstallBuilder extends AbstractBuilder {
/** Package ID of the APK to be uninstalled. */
private final String packageId;

/** Whether to fail the build if uninstallation isn't successful. */
private final boolean failOnUninstallFailure;

@DataBoundConstructor
@SuppressWarnings("hiding")
public UninstallBuilder(String packageId) {
public UninstallBuilder(String packageId, boolean failOnUninstallFailure) {
this.packageId = Util.fixEmptyAndTrim(packageId);
this.failOnUninstallFailure = failOnUninstallFailure;
}

public String getPackageId() {
return packageId;
}

public boolean shouldFailBuildOnFailure() {
return failOnUninstallFailure;
}

@Override
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener)
throws InterruptedException, IOException {
Expand All @@ -59,9 +67,10 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListen

// Execute uninstallation
String deviceIdentifier = getDeviceIdentifier(build, listener);
uninstallApk(build, launcher, logger, androidSdk, deviceIdentifier, expandedPackageId);

// TODO: Evaluate success/failure and fail the build (if the user said we should do so)
boolean success = uninstallApk(build, launcher, logger, androidSdk, deviceIdentifier, expandedPackageId);
if (!success && failOnUninstallFailure) {
return false;
}
return true;
}

Expand Down
Expand Up @@ -20,6 +20,7 @@
import hudson.plugins.android_emulator.sdk.Tool;
import hudson.plugins.android_emulator.util.Utils;
import hudson.tasks.Builder;
import hudson.util.ForkOutputStream;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
Expand Down Expand Up @@ -186,15 +187,16 @@ private static String expandVariable(AbstractBuild<?, ?> build, BuildListener li
* @param androidSdk The Android SDK to use.
* @param deviceIdentifier The device from which the package should be removed.
* @param apkPath The path to the APK file.
* @return {@code true} iff uninstallation completed successfully.
* @throws IOException If execution failed.
* @throws InterruptedException If execution failed.
*/
protected static void uninstallApk(AbstractBuild<?, ?> build, Launcher launcher,
protected static boolean uninstallApk(AbstractBuild<?, ?> build, Launcher launcher,
PrintStream logger, AndroidSdk androidSdk, String deviceIdentifier, FilePath apkPath)
throws IOException, InterruptedException {
// Get package ID to uninstall
String packageId = getPackageIdForApk(build, launcher, logger, androidSdk, apkPath);
uninstallApk(build, launcher, logger, androidSdk, deviceIdentifier, packageId);
return uninstallApk(build, launcher, logger, androidSdk, deviceIdentifier, packageId);
}

/**
Expand All @@ -206,17 +208,23 @@ protected static void uninstallApk(AbstractBuild<?, ?> build, Launcher launcher,
* @param androidSdk The Android SDK to use.
* @param deviceIdentifier The device from which the package should be removed.
* @param packageId The ID of the Android package to remove from the given device.
* @return {@code true} iff uninstallation completed successfully.
* @throws IOException If execution failed.
* @throws InterruptedException If execution failed.
*/
protected static void uninstallApk(AbstractBuild<?, ?> build, Launcher launcher,
protected static boolean uninstallApk(AbstractBuild<?, ?> build, Launcher launcher,
PrintStream logger, AndroidSdk androidSdk, String deviceIdentifier, String packageId)
throws IOException, InterruptedException {
// Execute uninstallation
AndroidEmulator.log(logger, Messages.UNINSTALLING_APK(packageId));

ByteArrayOutputStream stdout = new ByteArrayOutputStream();
ForkOutputStream forkStream = new ForkOutputStream(logger, stdout);
String adbArgs = String.format("%s uninstall %s", deviceIdentifier, packageId);
Utils.runAndroidTool(launcher, build.getEnvironment(TaskListener.NULL),
logger, logger, androidSdk, Tool.ADB, adbArgs, null);
forkStream, logger, androidSdk, Tool.ADB, adbArgs, null);

// The package manager simply returns "Success" or "Failure" on stdout
return stdout.toString().contains("Success");
}

/**
Expand Down
Expand Up @@ -5,4 +5,10 @@
<f:description>${%ID of the Android package to be uninstalled}</f:description>
</f:entry>

<f:entry help="${resURL}/plugin/android-emulator/help-failOnUninstallFailure.html">
<f:checkbox id="android-emulator.failOnUninstallFailure" name="android-emulator.failOnUninstallFailure"
title="${%Fail the build if uninstallation fails}"
checked="${instance.shouldFailBuildOnFailure()}" />
</f:entry>

</j:jelly>
2 changes: 2 additions & 0 deletions src/main/webapp/help-failOnUninstallFailure.html
@@ -0,0 +1,2 @@
If the given package ID fails to be uninstalled (i.e. the Android package manager does not return
"Success") and this option is enabled, then the build will be marked as failed.

0 comments on commit f3fff7c

Please sign in to comment.