Skip to content
Permalink
Browse files

[FIXED JENKINS-16246] Build can now be failed if package uninstallati…

…on fails.
  • Loading branch information...
orrc committed Apr 11, 2013
1 parent 59e2ec5 commit f3fff7c9b96356c62cdc9c3f5d3a5fd33036b48d
@@ -26,16 +26,24 @@
/** 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 {
@@ -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;
}

@@ -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;
@@ -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);
}

/**
@@ -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");
}

/**
@@ -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>
@@ -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.
You can’t perform that action at this time.