Skip to content

Commit

Permalink
[JENKINS-50843] - Stop persisting Build Loggers within the AWSDeviceF…
Browse files Browse the repository at this point in the history
…armRecorder publisher
  • Loading branch information
oleg-nenashev committed Apr 2, 2018
1 parent e7fb4bf commit 12ffb27
Showing 1 changed file with 53 additions and 53 deletions.
Expand Up @@ -147,9 +147,6 @@ public class AWSDeviceFarmRecorder extends Recorder {
public String xctestUiArtifact;
public String xctestUiFilter;

// Fields not populated by the JSON binder.
public PrintStream log;

// ignore device farm run errors
public Boolean ignoreRunError;

Expand Down Expand Up @@ -404,7 +401,7 @@ public boolean perform(AbstractBuild build, Launcher launcher, BuildListener lis
EnvVars env = build.getEnvironment(listener);
Map<String, String> parameters = build.getBuildVariables();

log = listener.getLogger();
final PrintStream log = listener.getLogger();

// Artifacts location for this build on master.
FilePath artifactsDir = new FilePath(build.getArtifactsDir());
Expand All @@ -413,9 +410,9 @@ public boolean perform(AbstractBuild build, Launcher launcher, BuildListener lis
FilePath workspace = build.getWorkspace();

// Validate user selection & input values.
boolean isValid = validateConfiguration() && validateTestConfiguration();
boolean isValid = validateConfiguration(log) && validateTestConfiguration(log);
if (!isValid) {
writeToLog("Invalid configuration.");
writeToLog(log, "Invalid configuration.");
return false;
}

Expand All @@ -430,7 +427,7 @@ public boolean perform(AbstractBuild build, Launcher launcher, BuildListener lis
// Accept 'ADF_PROJECT' build parameter as an overload from job configuration.
String projectNameParameter = parameters.get("AWSDEVICEFARM_PROJECT");
if (projectNameParameter != null && !projectNameParameter.isEmpty()) {
writeToLog(String.format("Using overloaded project '%s' from build parameters", projectNameParameter));
writeToLog(log, String.format("Using overloaded project '%s' from build parameters", projectNameParameter));
projectName = projectNameParameter;
}

Expand All @@ -445,28 +442,28 @@ public boolean perform(AbstractBuild build, Launcher launcher, BuildListener lis
}

// Get AWS Device Farm project from user provided name.
writeToLog(String.format("Using Project '%s'", projectName));
writeToLog(log, String.format("Using Project '%s'", projectName));
Project project = adf.getProject(projectName);

// Accept 'ADF_DEVICE_POOL' build parameter as an overload from job configuration.
String devicePoolParameter = parameters.get("AWSDEVICEFARM_DEVICE_POOL");
if (devicePoolParameter != null) {
writeToLog(String.format("Using overloaded device pool '%s' from build parameters", devicePoolParameter));
writeToLog(log, String.format("Using overloaded device pool '%s' from build parameters", devicePoolParameter));
devicePoolName = devicePoolParameter;
}

// Get AWS Device Farm device pool from user provided name.
writeToLog(String.format("Using DevicePool '%s'", devicePoolName));
writeToLog(log, String.format("Using DevicePool '%s'", devicePoolName));
DevicePool devicePool = adf.getDevicePool(project, devicePoolName);

// Upload app.
String appArn = null;
if (ifWebApp != null && ifWebApp){
writeToLog("Tesing a Web App.");
writeToLog(log, "Tesing a Web App.");

}
else {
writeToLog(String.format("Using App '%s'", env.expand(appArtifact)));
writeToLog(log, String.format("Using App '%s'", env.expand(appArtifact)));
Upload appUpload = adf.uploadApp(project, appArtifact);
appArn = appUpload.getArn();
}
Expand All @@ -479,7 +476,7 @@ public boolean perform(AbstractBuild build, Launcher launcher, BuildListener lis
}

// Upload test content.
writeToLog("Getting test to schedule.");
writeToLog(log, "Getting test to schedule.");
ScheduleRunTest testToSchedule = getScheduleRunTest(env, adf, project);

if (ifVideoRecording != null && !ifVideoRecording) {
Expand All @@ -491,25 +488,25 @@ public boolean perform(AbstractBuild build, Launcher launcher, BuildListener lis


// State the Appium Version.
if (testToRun.equalsIgnoreCase("APPIUM_JAVA_JUNIT")) writeToLog(String.format("Using appium version: %s", appiumVersionJunit));
else if (testToRun.equalsIgnoreCase("APPIUM_WEB_JAVA_JUNIT")) writeToLog(String.format("Using appium version: %s", appiumVersionJunit));
else if (testToRun.equalsIgnoreCase("APPIUM_JAVA_TESTNG")) writeToLog(String.format("Using appium version: %s", appiumVersionTestng));
else if (testToRun.equalsIgnoreCase("APPIUM_WEB_JAVA_TESTNG")) writeToLog(String.format("Using appium version: %s", appiumVersionTestng));
else if (testToRun.equalsIgnoreCase("APPIUM_PYTHON")) writeToLog(String.format("Using appium version: %s", appiumVersionPython));
else if (testToRun.equalsIgnoreCase("APPIUM_WEB_PYTHON")) writeToLog(String.format("Using appium version: %s", appiumVersionPython));
if (testToRun.equalsIgnoreCase("APPIUM_JAVA_JUNIT")) writeToLog(log, String.format("Using appium version: %s", appiumVersionJunit));
else if (testToRun.equalsIgnoreCase("APPIUM_WEB_JAVA_JUNIT")) writeToLog(log, String.format("Using appium version: %s", appiumVersionJunit));
else if (testToRun.equalsIgnoreCase("APPIUM_JAVA_TESTNG")) writeToLog(log, String.format("Using appium version: %s", appiumVersionTestng));
else if (testToRun.equalsIgnoreCase("APPIUM_WEB_JAVA_TESTNG")) writeToLog(log, String.format("Using appium version: %s", appiumVersionTestng));
else if (testToRun.equalsIgnoreCase("APPIUM_PYTHON")) writeToLog(log, String.format("Using appium version: %s", appiumVersionPython));
else if (testToRun.equalsIgnoreCase("APPIUM_WEB_PYTHON")) writeToLog(log, String.format("Using appium version: %s", appiumVersionPython));


// Upload the extra data.
String extraDataArn = null;
if (extraData != null && extraData) {
writeToLog(String.format("Using Extra Data '%s'", env.expand(extraDataArtifact)));
writeToLog(log, String.format("Using Extra Data '%s'", env.expand(extraDataArtifact)));
Upload extraDataUpload = adf.uploadExtraData(project, extraDataArtifact);
extraDataArn = extraDataUpload.getArn();
}

// Schedule test run.
TestType testType = TestType.fromValue(testToSchedule.getType());
writeToLog(String.format("Scheduling '%s' run '%s'", testType, deviceFarmRunName));
writeToLog(log, String.format("Scheduling '%s' run '%s'", testType, deviceFarmRunName));

ScheduleRunConfiguration configuration = getScheduleRunConfiguration(isRunUnmetered, deviceLocation, radioDetails);
configuration.setExtraDataPackageArn(extraDataArn);
Expand All @@ -518,33 +515,33 @@ public boolean perform(AbstractBuild build, Launcher launcher, BuildListener lis

String runArn = run.getRun().getArn();
try {
writeToLog(String.format("View the %s run in the AWS Device Farm Console: %s", testType, AWSDeviceFarmUtils.getRunUrlFromArn(runArn)));
writeToLog(log, String.format("View the %s run in the AWS Device Farm Console: %s", testType, AWSDeviceFarmUtils.getRunUrlFromArn(runArn)));
} catch (ArrayIndexOutOfBoundsException e) {
writeToLog(String.format("Could not parse project ID and run ID from run ARN: %s", runArn));
writeToLog(log, String.format("Could not parse project ID and run ID from run ARN: %s", runArn));
}

// Attach AWS Device Farm action to poll periodically and update results UI.
AWSDeviceFarmTestResultAction action = new AWSDeviceFarmTestResultAction(build, null, log);
build.addAction(action);

// Wait for test result to complete will updating status periodically.
writeToLog("Waiting for test run to complete.");
writeToLog(log, "Waiting for test run to complete.");
action.waitForRunCompletion(adf, run);
writeToLog("Test run is complete.");
writeToLog(log, "Test run is complete.");


// Download results archive and store it.
if (storeResults) {
// Create results storage directory which will contain the unzip logs/screenshots pulled from AWS Device Farm.
FilePath resultsDir = new FilePath(artifactsDir, "AWS Device Farm Results");
resultsDir.mkdirs();
writeToLog(String.format("Storing AWS Device Farm results in directory %s", resultsDir));
writeToLog(log, String.format("Storing AWS Device Farm results in directory %s", resultsDir));

Map<String, FilePath> jobs = getJobs(adf, run, resultsDir);
Map<String, FilePath> suites = getSuites(adf, run, jobs);
Map<String, FilePath> tests = getTests(adf, run, suites);

writeToLog("Downloading AWS Device Farm results archive...");
writeToLog(log, "Downloading AWS Device Farm results archive...");
// Iterating over all values in the Enum.
for (ArtifactCategory category : new ArrayList<ArtifactCategory>(Arrays.asList(ArtifactCategory.values()))) {
ListArtifactsResult result = adf.listArtifacts(run.getRun().getArn(), category);
Expand All @@ -558,13 +555,13 @@ public boolean perform(AbstractBuild build, Launcher launcher, BuildListener lis
artifactFilePath.write().write(IOUtils.toByteArray(url.openStream()));
}
}
writeToLog(String.format("Results archive saved in %s", artifactsDir.getName()));
writeToLog(log, String.format("Results archive saved in %s", artifactsDir.getName()));
}

// Set Jenkins build result based on AWS Device Farm test result.
build.setResult(action.getBuildResult(ignoreRunError));
} catch (AWSDeviceFarmException e) {
writeToLog(e.getMessage());
writeToLog(log, e.getMessage());
return false;
}

Expand Down Expand Up @@ -930,37 +927,38 @@ private ScheduleRunTest getScheduleRunTest(EnvVars env, AWSDeviceFarm adf, Proje
/**
* Validate top level configuration values.
*
* @param log Destination Task Log
* @return Whether or not the configuration is valid.
*/
private boolean validateConfiguration() {
private boolean validateConfiguration(@Nonnull PrintStream log) {
String roleArn = getRoleArn();
String akid = getAkid();
String skid = getSkid();

// [Required]: Auth Credentials
if ((roleArn == null || roleArn.isEmpty()) && (akid == null || akid.isEmpty() || skid == null || skid.isEmpty())) {
writeToLog("Either IAM Role ARN or AKID/SKID must be set.");
writeToLog(log, "Either IAM Role ARN or AKID/SKID must be set.");
return false;
}

// [Required]: Project
if (projectName == null || projectName.isEmpty()) {
writeToLog("Project must be set.");
writeToLog(log, "Project must be set.");
return false;
}
// [Required]: DevicePool
if (devicePoolName == null || devicePoolName.isEmpty()) {
writeToLog("DevicePool must be set.");
writeToLog(log, "DevicePool must be set.");
return false;
}
// [Required]: App Artifact
if (!ifWebApp && (appArtifact == null || appArtifact.isEmpty())) {
writeToLog("Application Artifact must be set.");
writeToLog(log, "Application Artifact must be set.");
return false;
}
// [Required]: At least one test.
if (testToRun == null || stringToTestType(testToRun) == null) {
writeToLog("A test type must be set.");
writeToLog(log, "A test type must be set.");
return false;
}
return true;
Expand All @@ -969,24 +967,25 @@ private boolean validateConfiguration() {
/**
* Validate user selected test type and additional configuration values.
*
* @param log Destination Task Log
* @return Whether or not the test configuration is valid.
*/
private boolean validateTestConfiguration() {
private boolean validateTestConfiguration(@Nonnull PrintStream log) {
TestType testType = stringToTestType(testToRun);

switch (testType) {
case BUILTIN_FUZZ: {
// [Optional]: EventCount (int)
if (eventCount != null && !eventCount.isEmpty()) {
if (!eventCount.matches("^\\d+$")) {
writeToLog("EventCount must be a number.");
writeToLog(log,"EventCount must be a number.");
return false;
}
}
// [Optional]: Seed (int)
if (seed != null && !seed.isEmpty()) {
if (!seed.matches("^\\d+$")) {
writeToLog("Seed must be a number.");
writeToLog(log, "Seed must be a number.");
return false;
}
}
Expand All @@ -1000,7 +999,7 @@ private boolean validateTestConfiguration() {

case APPIUM_JAVA_JUNIT: {
if (appiumJavaJUnitTest == null || appiumJavaJUnitTest.isEmpty()) {
writeToLog("Appium Java Junit test must be set.");
writeToLog(log, "Appium Java Junit test must be set.");
return false;
}

Expand All @@ -1009,7 +1008,7 @@ private boolean validateTestConfiguration() {

case APPIUM_JAVA_TESTNG: {
if (appiumJavaTestNGTest == null || appiumJavaTestNGTest.isEmpty()) {
writeToLog("Appium Java TestNG test must be set.");
writeToLog(log, "Appium Java TestNG test must be set.");
return false;
}

Expand All @@ -1018,7 +1017,7 @@ private boolean validateTestConfiguration() {

case APPIUM_PYTHON: {
if (appiumPythonTest == null || appiumPythonTest.isEmpty()) {
writeToLog("Appium Python test must be set.");
writeToLog(log, "Appium Python test must be set.");
return false;
}

Expand All @@ -1027,7 +1026,7 @@ private boolean validateTestConfiguration() {

case APPIUM_WEB_JAVA_JUNIT: {
if (appiumJavaJUnitTest == null || appiumJavaJUnitTest.isEmpty()) {
writeToLog("Appium Java Junit test for the web application must be set.");
writeToLog(log, "Appium Java Junit test for the web application must be set.");
return false;
}

Expand All @@ -1036,7 +1035,7 @@ private boolean validateTestConfiguration() {

case APPIUM_WEB_JAVA_TESTNG: {
if (appiumJavaTestNGTest == null || appiumJavaTestNGTest.isEmpty()) {
writeToLog("Appium Java TestNG test for the web application must be set.");
writeToLog(log, "Appium Java TestNG test for the web application must be set.");
return false;
}

Expand All @@ -1045,7 +1044,7 @@ private boolean validateTestConfiguration() {

case APPIUM_WEB_PYTHON: {
if (appiumPythonTest == null || appiumPythonTest.isEmpty()) {
writeToLog("Appium Python test for the web application must be set.");
writeToLog(log, "Appium Python test for the web application must be set.");
return false;
}

Expand All @@ -1055,12 +1054,12 @@ private boolean validateTestConfiguration() {
case CALABASH: {
// [Required]: Features Path
if (calabashFeatures == null || calabashFeatures.isEmpty()) {
writeToLog("Calabash Features must be set.");
writeToLog(log, "Calabash Features must be set.");
return false;
}
// [Required]: Features.zip
if (!calabashFeatures.endsWith(".zip")) {
writeToLog("Calabash content must be of type .zip");
writeToLog(log, "Calabash content must be of type .zip");
return false;
}

Expand All @@ -1070,7 +1069,7 @@ private boolean validateTestConfiguration() {
case INSTRUMENTATION: {
// [Required]: Tests Artifact
if (junitArtifact == null || junitArtifact.isEmpty()) {
writeToLog("JUnit tests Artifact must be set.");
writeToLog(log, "JUnit tests Artifact must be set.");
return false;
}

Expand All @@ -1079,7 +1078,7 @@ private boolean validateTestConfiguration() {

case UIAUTOMATOR: {
if (uiautomatorArtifact == null || uiautomatorArtifact.isEmpty()) {
writeToLog("UI Automator tests artifact must be set.");
writeToLog(log, "UI Automator tests artifact must be set.");
return false;
}

Expand All @@ -1088,7 +1087,7 @@ private boolean validateTestConfiguration() {

case UIAUTOMATION: {
if (uiautomationArtifact == null || uiautomationArtifact.isEmpty()) {
writeToLog("UI Automation tests artifact must be set.");
writeToLog(log, "UI Automation tests artifact must be set.");
return false;
}

Expand All @@ -1097,7 +1096,7 @@ private boolean validateTestConfiguration() {

case XCTEST: {
if (xctestArtifact == null || xctestArtifact.isEmpty()) {
writeToLog("XC tests artifact must be set.");
writeToLog(log, "XC tests artifact must be set.");
return false;
}

Expand All @@ -1106,15 +1105,15 @@ private boolean validateTestConfiguration() {

case XCTEST_UI: {
if (xctestUiArtifact == null || xctestUiArtifact.isEmpty()) {
writeToLog("XCTest UI tests artifact must be set.");
writeToLog(log, "XCTest UI tests artifact must be set.");
return false;
}

break;
}

default: {
writeToLog("Must select a test type to run.");
writeToLog(log, "Must select a test type to run.");
return false;
}
}
Expand All @@ -1125,9 +1124,10 @@ private boolean validateTestConfiguration() {
/**
* Helper method for writing entries to the Jenkins log.
*
* @param log Destination log
* @param msg The message to be written to the Jenkins log.
*/
private void writeToLog(String msg) {
private void writeToLog(PrintStream log, String msg) {
log.println(String.format("[AWSDeviceFarm] %s", msg));
}

Expand Down

0 comments on commit 12ffb27

Please sign in to comment.