Skip to content

Commit

Permalink
Merge pull request #832 from sameerawickramasekara/tinkerer-operations
Browse files Browse the repository at this point in the history
Improve integration test log downloads
  • Loading branch information
kasunbg authored Jun 28, 2018
2 parents b4a65a0 + 3eecea9 commit 232a6ef
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,21 @@ public static String deriveTestIntegrationLogFilePath(TestPlan testPlan, Boolean
TestGridConstants.TEST_INTEGRATION_LOG_FILE_NAME).toString();
}

/**
* Returns the path of the directory where log files will be downloaded.
*
* @param testPlan TestPlan object
* @return File download location path
* @throws TestGridException when there is an error deriving the path
*/
public static String deriveLogDownloadLocation(TestPlan testPlan)
throws TestGridException {
String productName = testPlan.getDeploymentPattern().getProduct().getName();
String testPlanDirName = TestGridUtil.deriveTestPlanDirName(testPlan);
return Paths.get(getTestGridHomePath(), TestGridConstants.TESTGRID_JOB_DIR, productName,
TestGridConstants.TESTGRID_BUILDS_DIR, testPlanDirName).toString();
}

/**
* Returns the path of the integration test log file.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ public void execute(TestPlan testPlan, InfrastructureConfig infrastructureConfig
logger.warn(String.format("Unable retrieve agents for test plan with id %s , %n " +
"Continuing the build with no log download support", testPlan.getId()));
}
//Log file download
OSCategory osCategory = getOSCatagory(testPlan.getInfraParameters());
try {
Optional<TinkererClient> executer = TinkererClientFactory.getExecuter(osCategory);
Expand All @@ -190,11 +191,11 @@ public void execute(TestPlan testPlan, InfrastructureConfig infrastructureConfig
} catch (ReportGeneratorInitializingException e) {
logger.error("Error while initializing the report generators " +
"for TestPlan of " + testPlan.getDeploymentPattern()
.getProduct().getName());
.getProduct().getName(), e);
} catch (ReportGeneratorException e) {
logger.error("Error while generating the report for " +
"TestPlan of " + testPlan.getDeploymentPattern()
.getProduct().getName());
.getProduct().getName(), e);
}
// Test plan completed. Persist the testplan status
persistTestPlanStatus(testPlan);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ private void processTestgridYaml(TestgridYaml testgridYaml, JobConfigFile jobCon
String fileName = String
.format("%s-%02d%s", TestGridConstants.TEST_PLAN_YAML_PREFIX, (i + 1), FileUtil.YAML_EXTENSION);
testPlan.setKeyFileLocation(jobConfigFile.getKeyFileLocation());
testPlan.setJobProperties(jobConfigFile.getProperties());
String output = yaml.dump(testPlan);

//Remove reference ids from yaml
Expand Down
49 changes: 36 additions & 13 deletions core/src/main/java/org/wso2/testgrid/tinkerer/UnixClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,19 @@
import org.slf4j.LoggerFactory;
import org.wso2.testgrid.common.Agent;
import org.wso2.testgrid.common.DeploymentCreationResult;
import org.wso2.testgrid.common.Host;
import org.wso2.testgrid.common.TestGridConstants;
import org.wso2.testgrid.common.TestPlan;
import org.wso2.testgrid.common.exception.TestGridException;
import org.wso2.testgrid.common.util.StringUtil;
import org.wso2.testgrid.common.util.TestGridUtil;
import org.wso2.testgrid.tinkerer.exception.TinkererOperationException;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Base64;
import java.util.HashMap;
Expand All @@ -62,7 +65,8 @@ public class UnixClient extends TinkererClient {

private static final Logger logger = LoggerFactory.getLogger(UnixClient.class);
private static final String SCENARIO_LOG_LOCATION = "/repository/logs/";
private static final String INTEGRATION_LOG_LOCATION = "/logs";
private static final String INTEGRATION_LOG_LOCATION = "/logs/";
private static final String WORKSPACE_DIR_POSIX = "WORKSPACE_DIR_POSIX";

@Override
public void downloadLogs(DeploymentCreationResult deploymentCreationResult, TestPlan testPlan)
Expand Down Expand Up @@ -126,11 +130,12 @@ public void downloadLogs(DeploymentCreationResult deploymentCreationResult, Test
}
logLocation = productBasePath + SCENARIO_LOG_LOCATION;
} else if (TestGridConstants.TEST_TYPE_INTEGRATION.equals(testType)) {
for (Host host : deploymentCreationResult.getHosts()) {
if (host.getLabel().equals("workspace")) {
productBasePath = host.getIp();
break;
}
if (testPlan.getJobProperties().containsKey(WORKSPACE_DIR_POSIX)) {
productBasePath = testPlan.getJobProperties().getProperty(WORKSPACE_DIR_POSIX);
} else {
throw new TinkererOperationException("Product workspace path is not present, Please check if" +
"entry is present in job-config.yml file : "
+ "\nfor test plan :" + testPlan.getId());
}
logLocation = productBasePath + INTEGRATION_LOG_LOCATION;
}
Expand Down Expand Up @@ -180,21 +185,17 @@ public void downloadLogs(DeploymentCreationResult deploymentCreationResult, Test
String key = Base64.getEncoder()
.encodeToString(FileUtils.readFileToByteArray(new File(testPlan.getKeyFileLocation())));
String source = logLocation + logFileName;
String destination = TestGridUtil.getTestGridHomePath() + File.separator +
testPlan.getDeploymentPattern().getProduct().getName() + logFileName;
String destination = Paths.get(TestGridUtil.deriveLogDownloadLocation(testPlan), logFileName)
.toString();
String bastianIP = deploymentCreationResult.getBastianIP();
//TODO set correct download path so the log files are in the correct place in the
// folder hierarchy
String downloadPath = this.getTinkererBase() + "test-plan/" + agent.getTestPlanId() +
"/agent/" + agent.getInstanceName() + "/stream-file";

Map<String, Object> payload = new HashMap<>();
payload.put("code", "STREAM_FILE");
Map<String, Object> subPayload = new HashMap<>();
subPayload.put("key", key);
subPayload.put("source", source);
subPayload.put("destination", destination);
subPayload.put("destination", destination);
if (bastianIP != null) {
subPayload.put("bastian-ip", bastianIP);
}
Expand All @@ -216,6 +217,11 @@ public void downloadLogs(DeploymentCreationResult deploymentCreationResult, Test
"\nError message :"
+ EntityUtils.toString(((CloseableHttpResponse) httpResponse).getEntity()));
}
//verify the downloaded files are present in the location
if (!verifyFileDownload(destination)) {
throw new TinkererOperationException("Failed to download the file :"
+ destination);
}
logger.debug("Download Location :" + destination);
} catch (IOException e) {
throw new TinkererOperationException("Error occurred while downloading " +
Expand All @@ -230,6 +236,12 @@ public void downloadLogs(DeploymentCreationResult deploymentCreationResult, Test
+ "\nfrom agent :" + agent.getAgentId()
+ "\nRunning on instance : " + agent.getInstanceName()
+ "\nfor test plan :" + testPlan.getId(), e);
} catch (TestGridException e) {
throw new TinkererOperationException("Error occurred deriving the destination path " +
"for logfile : " + logFileName
+ "\nfrom agent :" + agent.getAgentId()
+ "\nRunning on instance : " + agent.getInstanceName()
+ "\nfor test plan :" + testPlan.getId(), e);
}
}
logger.info("Successfully downloaded all log files ");
Expand All @@ -243,5 +255,16 @@ public void downloadLogs(DeploymentCreationResult deploymentCreationResult, Test
*/
private static class PropertyType extends TypeToken<HashMap<String, String>> {
}

/**
* This method verifies if the specified file is present in the file system
*
* @param fileLocation path of the file
* @return true if exists, otherwise false
*/
private boolean verifyFileDownload(String fileLocation) {
Path path = Paths.get(fileLocation);
return Files.exists(path) && Files.isRegularFile(path);
}
}

2 changes: 2 additions & 0 deletions core/src/test/resources/job-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ deploymentRepository: workspace/deployment
scenarioTestsRepository: workspace/scenarioTests
testgridYamlLocation: workspace/testgrid.yaml
keyFileLocation: workspace/testkey.pem
properties:
PROPERTY1: value1
2 changes: 2 additions & 0 deletions core/src/test/resources/job-config2.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ deploymentRepository: workspace/deployment
scenarioTestsRepository: workspace/scenarioTests
testgridYamlLocation: workspace/testgrid.yaml
keyFileLocation: workspace/testkey.pem
properties:
PROPERTY1: value1
4 changes: 2 additions & 2 deletions core/src/test/resources/test-plan-01.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ infrastructureConfig:
phase: DESTROY
type: SHELL
infrastructureRepository: ./src/test/resources/workspace/infrastructure
jobProperties: {
}
jobProperties:
PROPERTY1: value1
keyFileLocation: ./src/test/resources/workspace/testkey.pem
scenarioConfig:
scenarios:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,18 +73,18 @@ public static void addConfigEntry(String keyContent, String bastianIP, Agent age
}
}
String bastianEntry = "bastian:" + agent.getTestPlanId();
String hostEntry = "host:" + agent.getInstanceName();
String hostEntry = "host:" + agent.getInstanceId();
if (bastianIP != null) {
//check if the bastian entry has been made
if (!containsEntry(file, bastianEntry)) {
//add entry
String entry = String.format("Host %s%n" +
" StrictHostKeyChecking=no%n" +
" UserKnownHostsFile=/dev/null%n" +
" User ubuntu%n" +
" User %s%n" +
" HostName %s%n" +
" IdentityFile %s%n",
bastianEntry, bastianIP, keyPath.toAbsolutePath().toString());
bastianEntry, agent.getInstanceUser(), bastianIP, keyPath.toAbsolutePath().toString());
Files.write(config, entry.getBytes(StandardCharsets.UTF_8), StandardOpenOption.APPEND);
}
if (!containsEntry(file, hostEntry)) {
Expand Down

0 comments on commit 232a6ef

Please sign in to comment.