Skip to content

Commit

Permalink
Merge pull request #177 from radislavB/fix-test-runner-credentials2
Browse files Browse the repository at this point in the history
Try to use existing credentials then creating UFT test runner
  • Loading branch information
radislavB authored Jan 20, 2019
2 parents 0a4d885 + 9f5fe3e commit 9017883
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,7 @@

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;


/**
Expand All @@ -62,6 +58,7 @@ public class ExecutorConnectivityService {
private static final Logger logger = LogManager.getLogger(ExecutorConnectivityService.class);
private static final Map<Permission, String> requirePremissions = initRequirePremissions();
private static final Map<Permission, String> credentialsPremissions = initCredentialsPremissions();
private static final String PLUGIN_NAME = "Application Automation Tools";

/**
* Validate that scm repository is valid
Expand All @@ -81,7 +78,7 @@ public static OctaneResponse checkRepositoryConnectivity(TestConnectivityInfo te
}


Jenkins jenkins = Jenkins.getActiveInstance();
Jenkins jenkins = Jenkins.getInstance();

List<String> permissionResult = checkCIPermissions(jenkins, credentials != null);

Expand Down Expand Up @@ -120,41 +117,43 @@ public static OctaneResponse upsertRepositoryCredentials(final CredentialsInfo c

OctaneResponse result = DTOFactory.getInstance().newDTO(OctaneResponse.class);
result.setStatus(HttpStatus.SC_CREATED);
BaseStandardCredentials jenkinsCredentials = null;

if (StringUtils.isNotEmpty(credentialsInfo.getCredentialsId())) {
BaseStandardCredentials cred = getCredentialsById(credentialsInfo.getCredentialsId());
if (cred != null) {
jenkinsCredentials = getCredentialsById(credentialsInfo.getCredentialsId());
if (jenkinsCredentials != null) {
BaseStandardCredentials newCred = new UsernamePasswordCredentialsImpl(CredentialsScope.GLOBAL, credentialsInfo.getCredentialsId(),
null, credentialsInfo.getUsername(), credentialsInfo.getPassword());
CredentialsStore store = new SystemCredentialsProvider.StoreImpl();
try {
store.updateCredentials(Domain.global(), cred, newCred);
result.setStatus(HttpStatus.SC_CREATED);
result.setBody(newCred.getId());
store.updateCredentials(Domain.global(), jenkinsCredentials, newCred);
} catch (IOException e) {
logger.error("Failed to update credentials " + e.getMessage());
result.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
result.setBody("Failed to update credentials " + e.getMessage());
}
return result;
}
}
if (StringUtils.isNotEmpty(credentialsInfo.getUsername()) && credentialsInfo.getPassword() != null) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String desc = "Created by the Microfocus Application Automation Tools plugin on " + formatter.format(new Date());
BaseStandardCredentials c = new UsernamePasswordCredentialsImpl(CredentialsScope.GLOBAL, credentialsInfo.getCredentialsId(), desc, credentialsInfo.getUsername(), credentialsInfo.getPassword());
CredentialsStore store = new SystemCredentialsProvider.StoreImpl();
try {
store.addCredentials(Domain.global(), c);
result.setStatus(HttpStatus.SC_CREATED);
result.setBody(c.getId());
} catch (IOException e) {
logger.error("Failed to add credentials " + e.getMessage());
result.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
result.setBody("Failed to add credentials " + e.getMessage());
} else if (StringUtils.isNotEmpty(credentialsInfo.getUsername()) && credentialsInfo.getPassword() != null) {
jenkinsCredentials = tryGetCredentialsByUsernamePassword(credentialsInfo.getUsername(), credentialsInfo.getPassword());
if (jenkinsCredentials == null) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String desc = String.format("Created by the Microfocus %s plugin on %s", PLUGIN_NAME, formatter.format(new Date()));
BaseStandardCredentials c = new UsernamePasswordCredentialsImpl(CredentialsScope.GLOBAL, credentialsInfo.getCredentialsId(), desc, credentialsInfo.getUsername(), credentialsInfo.getPassword());
CredentialsStore store = new SystemCredentialsProvider.StoreImpl();
try {
store.addCredentials(Domain.global(), c);
} catch (IOException e) {
logger.error("Failed to add credentials " + e.getMessage());
result.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
result.setBody("Failed to add credentials " + e.getMessage());
}
}
}

if (jenkinsCredentials != null) {
result.setBody(jenkinsCredentials.getId());
}

return result;
}

Expand All @@ -167,6 +166,18 @@ private static BaseStandardCredentials getCredentialsById(String credentialsId)
return null;
}

private static UsernamePasswordCredentialsImpl tryGetCredentialsByUsernamePassword(String username, String password) {
List<UsernamePasswordCredentialsImpl> list = CredentialsProvider.lookupCredentials(UsernamePasswordCredentialsImpl.class, (Item) null, null, (DomainRequirement) null);
for (UsernamePasswordCredentialsImpl cred : list) {
if (StringUtils.equalsIgnoreCase(cred.getUsername(), username)
&& StringUtils.equals(cred.getPassword().getPlainText(), password)
&& cred.getDescription() != null && cred.getDescription().contains(PLUGIN_NAME)) {
return cred;
}
}
return null;
}

private static List<String> checkCIPermissions(final Jenkins jenkins, boolean hasCredentials) {
List<String> result = new ArrayList<>();
checkPermissions(jenkins, result, requirePremissions);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,8 +291,8 @@ private static FreeStyleProject getDiscoveryJobForUftExecutor(DiscoveryInfo disc

setScmRepository(discoveryInfo.getScmRepository(), discoveryInfo.getScmRepositoryCredentialsId(), proj, false);
setBuildDiscarder(proj, 20);
addConstantParameter(proj, UftConstants.EXECUTOR_ID_PARAMETER_NAME, discoveryInfo.getExecutorId(), "ALM Octane testing tool connection ID");
addConstantParameter(proj, UftConstants.EXECUTOR_LOGICAL_NAME_PARAMETER_NAME, discoveryInfo.getExecutorLogicalName(), "ALM Octane testing tool connection logical name");
addConstantParameter(proj, UftConstants.EXECUTOR_ID_PARAMETER_NAME, discoveryInfo.getExecutorId(), "ALM Octane test runner ID");
addConstantParameter(proj, UftConstants.EXECUTOR_LOGICAL_NAME_PARAMETER_NAME, discoveryInfo.getExecutorLogicalName(), "ALM Octane test runner logical name");
addBooleanParameter(proj, UftConstants.FULL_SCAN_PARAMETER_NAME, false, "Specify whether to synchronize the set of tests on ALM Octane with the whole SCM repository or to update the set of tests on ALM Octane based on the latest commits.");

//set polling once in two minutes
Expand Down Expand Up @@ -516,8 +516,10 @@ public static FreeStyleProject createExecutor(DiscoveryInfo discoveryInfo) {
setBuildDiscarder(proj, 40);
addStringParameter(proj, UftConstants.TESTS_TO_RUN_PARAMETER_NAME, "", "Tests to run");
addStringParameter(proj, UftConstants.CHEKOUT_DIR_PARAMETER_NAME, "${WORKSPACE}\\${CHECKOUT_SUBDIR}", "Shared UFT directory");
addConstantParameter(proj, UftConstants.EXECUTOR_ID_PARAMETER_NAME, discoveryInfo.getExecutorId(), "ALM Octane testing tool connection ID");
addConstantParameter(proj, UftConstants.EXECUTOR_LOGICAL_NAME_PARAMETER_NAME, discoveryInfo.getExecutorLogicalName(), "ALM Octane testing tool connection logical name");
addConstantParameter(proj, UftConstants.TEST_RUNNER_ID_PARAMETER_NAME, discoveryInfo.getExecutorId(), "ALM Octane test runner ID");
addConstantParameter(proj, UftConstants.TEST_RUNNER_LOGICAL_NAME_PARAMETER_NAME, discoveryInfo.getExecutorLogicalName(), "ALM Octane test runner logical name");
addStringParameter(proj, UftConstants.SUITE_ID_PARAMETER_NAME, "", "ALM Octane test suite ID");
addStringParameter(proj, UftConstants.SUITE_RUN_ID_PARAMETER_NAME, "", "The ID of the ALM Octane test suite run to associate with the test run results.");

addExecutionAssignedNode(proj);
addTimestamper(proj);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,21 +115,6 @@ private static FilePath handleWorkspaceActionWithoutWorkspace(FlowNode n, Worksp
logger.warn("Node getNode = " + action.getNode());
FilePath workspace = null;

//check if computer can be found - only for diagnostic purpose
if (action.getNode() != null) {
try {
Jenkins j = Jenkins.getInstance();
Computer c = j.getComputer(action.getNode());
if (c != null) {
logger.warn("Computer is found : " + c.getDisplayName());
} else {
logger.warn("Computer is not found");
}
} catch (Exception e) {
logger.warn("Failed to find computer : " + e.getMessage() + ", error type : " + e.getClass().getName());
}
}

if (StringUtils.isNotEmpty(action.getPath())) {
logger.warn("Node getPath is not empty, return getPath as workspace");
workspace = new FilePath(new File(action.getPath()));
Expand Down

0 comments on commit 9017883

Please sign in to comment.