Skip to content

Commit

Permalink
Implement test scenarios using persistent test scenarioold test provi…
Browse files Browse the repository at this point in the history
…der and change user impl will be removed in following commit
  • Loading branch information
jakubschwan committed May 13, 2020
1 parent b994c5c commit f07ea85
Show file tree
Hide file tree
Showing 11 changed files with 279 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,7 @@ public interface WorkbenchKieServerScenario extends KieDeploymentScenario<Workbe
*/
default Optional<PrometheusDeployment> getPrometheusDeployment() {
return Optional.empty();
};
}

void changeUsernameAndPassword(String username, String password);
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,13 @@ public static KieServicesClient getKieServerClient(KieServerDeployment kieServer
kieServerDeployment.getUrl().toString() + "/services/rest/server", kieServerDeployment.getUsername(),
kieServerDeployment.getPassword(), clientTimeout);
configuration.addExtraClasses(extraClasses);
KieServicesClient kieServerClient = KieServicesFactory.newKieServicesClient(configuration);
return kieServerClient;
return KieServicesFactory.newKieServicesClient(configuration);
}

public static KieServicesClient getKieServerClient(String url, String username, String password) {
KieServicesConfiguration configuration = KieServicesFactory.newRestConfiguration(
url + "/services/rest/server", username, password, KIE_SERVER_TIMEOUT);
return KieServicesFactory.newKieServicesClient(configuration);
}

public static KieServicesClient getKieServerJmsClient(URL amqHost) {
Expand Down Expand Up @@ -139,8 +144,7 @@ public static KieServicesClient getSmartRouterClient(SmartRouterDeployment smart
KieServerConstants.CAPABILITY_CASE,
KieServerConstants.CAPABILITY_DMN);
configuration.setCapabilities(capabilities);
KieServicesClient kieServerClient = KieServicesFactory.newKieServicesClient(configuration);
return kieServerClient;
return KieServicesFactory.newKieServicesClient(configuration);
}

public static ProcessServicesClient getProcessClient(KieServerDeployment kieServerDeployment) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,17 @@
public class KieServerControllerClientProvider {

public static KieServerControllerClient getKieServerControllerClient(WorkbenchDeployment workbenchDeployment) {
KieServerControllerClient kieServerControllerClient = KieServerControllerClientFactory.newRestClient(workbenchDeployment.getUrl().toString() + "/rest/controller",
return KieServerControllerClientFactory.newRestClient(workbenchDeployment.getUrl().toString() + "/rest/controller",
workbenchDeployment.getUsername(), workbenchDeployment.getPassword());
return kieServerControllerClient;
}

public static KieServerControllerClient getKieServerControllerClient(String url, String username, String password) {
return KieServerControllerClientFactory.newRestClient(url + "/rest/controller", username, password);
}

public static KieServerControllerClient getKieServerControllerClient(ControllerDeployment controllerDeployment) {
KieServerControllerClient kieServerControllerClient = KieServerControllerClientFactory.newRestClient(controllerDeployment.getUrl().toString() + "/rest/controller",
return KieServerControllerClientFactory.newRestClient(controllerDeployment.getUrl().toString() + "/rest/controller",
controllerDeployment.getUsername(), controllerDeployment.getPassword());
return kieServerControllerClient;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,4 +182,9 @@ public List<SmartRouterDeployment> getSmartRouterDeployments() {
public List<ControllerDeployment> getControllerDeployments() {
return Collections.emptyList();
}

@Override
public void changeUsernameAndPassword(String username, String password) {
throw new UnsupportedOperationException("Not supported.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -173,4 +173,9 @@ public List<ControllerDeployment> getControllerDeployments() {
public Optional<PrometheusDeployment> getPrometheusDeployment() {
return Optional.ofNullable(prometheusDeployment);
}

@Override
public void changeUsernameAndPassword(String username, String password) {
throw new UnsupportedOperationException("Not supported.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -169,4 +169,15 @@ public List<ControllerDeployment> getControllerDeployments() {
public SsoDeployment getSsoDeployment() {
return ssoDeployment;
}

@Override
public void changeUsernameAndPassword(final String username, final String password) {
if(getDeployments().stream().allMatch(Deployment::isReady)) {
kieApp.getSpec().getCommonConfig().setAdminUser(username);
kieApp.getSpec().getCommonConfig().setAdminPassword(password);
deployCustomResource();
} else{
throw new RuntimeException("Application is not ready for Username and password change. Please check first that application is ready.");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,20 +49,20 @@ public class WorkbenchKieServerScenarioImpl extends OpenShiftOperatorScenario<Wo

private WorkbenchDeploymentImpl workbenchDeployment;
private KieServerDeploymentImpl kieServerDeployment;
private boolean deployPrometheus;
private final boolean deployPrometheus;
private PrometheusDeployment prometheusDeployment;

private static final Logger logger = LoggerFactory.getLogger(WorkbenchKieServerScenarioImpl.class);

public WorkbenchKieServerScenarioImpl(KieApp kieApp, boolean deployPrometheus) {
public WorkbenchKieServerScenarioImpl(final KieApp kieApp, final boolean deployPrometheus) {
super(kieApp);
this.deployPrometheus = deployPrometheus;
}

@Override
protected void deployCustomResource() {
// deploy application
getKieAppClient().create(kieApp);
getKieAppClient().createOrReplace(kieApp);
// Wait until the operator reconciliate the KieApp and add there missing informations
new SupplierWaiter<KieApp>(() -> getKieAppClient().withName(OpenShiftConstants.getKieApplicationName()).get(), kieApp -> kieApp.getStatus() != null).reason("Waiting for reconciliation to initialize all fields.").timeout(TimeUnit.MINUTES,1).waitFor();

Expand All @@ -82,7 +82,7 @@ protected void deployCustomResource() {
try {
new SimpleWaiter(() -> workbenchDeployment.isReady()).reason("Waiting for Workbench service to be created.").timeout(TimeUnit.MINUTES, 1).waitFor();
new SimpleWaiter(() -> kieServerDeployment.isReady()).reason("Waiting for Kie server service to be created.").timeout(TimeUnit.MINUTES, 1).waitFor();
} catch (WaiterException e) {
} catch (final WaiterException e) {
throw new RuntimeException("Timeout while deploying application.", e);
}

Expand Down Expand Up @@ -119,13 +119,13 @@ public KieServerDeployment getKieServerDeployment() {

@Override
public List<Deployment> getDeployments() {
List<Deployment> deployments = new ArrayList<Deployment>(Arrays.asList(workbenchDeployment, kieServerDeployment));
final List<Deployment> deployments = new ArrayList<Deployment>(Arrays.asList(workbenchDeployment, kieServerDeployment));
deployments.removeAll(Collections.singleton(null));
return deployments;
}

private void storeProjectInfoToPersistentVolume(Deployment deployment, String persistentVolumeMountPath) {
String storeCommand = "echo \"Project " + projectName + ", time " + Instant.now() + "\" > " + persistentVolumeMountPath + "/info.txt";
private void storeProjectInfoToPersistentVolume(final Deployment deployment, final String persistentVolumeMountPath) {
final String storeCommand = "echo \"Project " + projectName + ", time " + Instant.now() + "\" > " + persistentVolumeMountPath + "/info.txt";
workbenchDeployment.getInstances().get(0).runCommand("/bin/bash", "-c", storeCommand);
}

Expand Down Expand Up @@ -153,4 +153,15 @@ public List<ControllerDeployment> getControllerDeployments() {
public Optional<PrometheusDeployment> getPrometheusDeployment() {
return Optional.ofNullable(prometheusDeployment);
}

@Override
public void changeUsernameAndPassword(final String username, final String password) {
if(getDeployments().stream().allMatch(Deployment::isReady)) {
kieApp.getSpec().getCommonConfig().setAdminUser(username);
kieApp.getSpec().getCommonConfig().setAdminPassword(password);
deployCustomResource();
} else{
throw new RuntimeException("Application is not ready for Username and password change. Please check first that application is ready.");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import cz.xtf.core.waiting.SimpleWaiter;
import org.kie.cloud.api.deployment.ControllerDeployment;
import org.kie.cloud.api.deployment.Deployment;
import org.kie.cloud.api.deployment.KieServerDeployment;
Expand All @@ -32,6 +35,7 @@
import org.kie.cloud.api.scenario.WorkbenchKieServerPersistentScenario;
import org.kie.cloud.api.scenario.WorkbenchKieServerScenario;
import org.kie.cloud.common.provider.KieServerControllerClientProvider;
import org.kie.cloud.openshift.constants.OpenShiftConstants;
import org.kie.cloud.openshift.constants.OpenShiftTemplateConstants;
import org.kie.cloud.openshift.constants.ProjectSpecificPropertyNames;
import org.kie.cloud.openshift.deployment.KieServerDeploymentImpl;
Expand Down Expand Up @@ -147,4 +151,42 @@ public List<ControllerDeployment> getControllerDeployments() {
public SsoDeployment getSsoDeployment() {
return ssoDeployment;
}

@Override
public void changeUsernameAndPassword(String username, String password) {
if(getDeployments().stream().allMatch(Deployment::isReady)) {
deploySecretAppUser(username,password);
logger.info("Restart the environment to update Workbench deployment.");
getDeployments().parallelStream().forEach(this::scaleToZeroAndBackToReplicas); // if parallel stream make mess because of common fork-join pool use normal stream and adjust scaling (scale all deployments to zero at the same time)
} else{
throw new RuntimeException("Application is not ready for Username and password change. Please check first that application is ready.");
}

}

private void deploySecretAppUser(String user, String password) {
logger.info("Delete old secret '{}'", DeploymentConstants.getAppCredentialsSecretName());
project.getOpenShift().secrets().withName(DeploymentConstants.getAppCredentialsSecretName()).delete();
new SimpleWaiter(() -> project.getOpenShift().getSecret(DeploymentConstants.getAppCredentialsSecretName()) == null).timeout(TimeUnit.MINUTES, 2)
.reason("Waiting for old secret to be deleted.")
.waitFor();

logger.info("Creating user secret '{}'", DeploymentConstants.getAppCredentialsSecretName());
Map<String, String> data = new HashMap<>();
data.put(OpenShiftConstants.KIE_ADMIN_USER, user);
data.put(OpenShiftConstants.KIE_ADMIN_PWD, password);

project.createSecret(DeploymentConstants.getAppCredentialsSecretName(), data);
new SimpleWaiter(() -> project.getOpenShift().getSecret(DeploymentConstants.getAppCredentialsSecretName()) != null).timeout(TimeUnit.MINUTES, 2)
.reason("Waiting for new secret to be created.")
.waitFor();
}

private void scaleToZeroAndBackToReplicas(Deployment deployment) {
int replicas = deployment.getInstances().size();
deployment.scale(0);
deployment.waitForScale();
deployment.scale(replicas);
deployment.waitForScale();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.TimeUnit;

import cz.xtf.core.waiting.SimpleWaiter;
import org.kie.cloud.api.deployment.ControllerDeployment;
import org.kie.cloud.api.deployment.Deployment;
import org.kie.cloud.api.deployment.KieServerDeployment;
Expand All @@ -32,6 +35,7 @@
import org.kie.cloud.api.deployment.constants.DeploymentConstants;
import org.kie.cloud.api.scenario.WorkbenchKieServerScenario;
import org.kie.cloud.common.provider.KieServerControllerClientProvider;
import org.kie.cloud.openshift.constants.OpenShiftConstants;
import org.kie.cloud.openshift.constants.OpenShiftTemplateConstants;
import org.kie.cloud.openshift.deployment.KieServerDeploymentImpl;
import org.kie.cloud.openshift.deployment.WorkbenchDeploymentImpl;
Expand Down Expand Up @@ -133,4 +137,42 @@ public List<ControllerDeployment> getControllerDeployments() {
public Optional<PrometheusDeployment> getPrometheusDeployment() {
return Optional.ofNullable(prometheusDeployment);
}

@Override
public void changeUsernameAndPassword(String username, String password) {
if(getDeployments().stream().allMatch(Deployment::isReady)) {
deploySecretAppUser(username,password);
logger.info("Restart the environment to update Workbench deployment.");
getDeployments().parallelStream().forEach(this::scaleToZeroAndBackToReplicas); // if parallel stream make mess because of common fork-join pool use normal stream and adjust scaling (scale all deployments to zero at the same time)
} else{
throw new RuntimeException("Application is not ready for Username and password change. Please check first that application is ready.");
}

}

private void deploySecretAppUser(String user, String password) {
logger.info("Delete old secret '{}'", DeploymentConstants.getAppCredentialsSecretName());
project.getOpenShift().secrets().withName(DeploymentConstants.getAppCredentialsSecretName()).delete();
new SimpleWaiter(() -> project.getOpenShift().getSecret(DeploymentConstants.getAppCredentialsSecretName()) == null).timeout(TimeUnit.MINUTES, 2)
.reason("Waiting for old secret to be deleted.")
.waitFor();

logger.info("Creating user secret '{}'", DeploymentConstants.getAppCredentialsSecretName());
Map<String, String> data = new HashMap<>();
data.put(OpenShiftConstants.KIE_ADMIN_USER, user);
data.put(OpenShiftConstants.KIE_ADMIN_PWD, password);

project.createSecret(DeploymentConstants.getAppCredentialsSecretName(), data);
new SimpleWaiter(() -> project.getOpenShift().getSecret(DeploymentConstants.getAppCredentialsSecretName()) != null).timeout(TimeUnit.MINUTES, 2)
.reason("Waiting for new secret to be created.")
.waitFor();
}

private void scaleToZeroAndBackToReplicas(Deployment deployment) {
int replicas = deployment.getInstances().size();
deployment.scale(0);
deployment.waitForScale();
deployment.scale(replicas);
deployment.waitForScale();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@
import org.junit.BeforeClass;
import org.junit.Test;
import org.kie.cloud.api.scenario.WorkbenchKieServerScenario;
import org.kie.cloud.integrationtests.testproviders.HttpsWorkbenchTestProvider;
import org.kie.cloud.integrationtests.testproviders.PersistenceTestProvider;
import org.kie.cloud.tests.common.AbstractCloudIntegrationTest;
import org.kie.cloud.tests.common.ScenarioDeployer;

public class WorkbenchAdminUserPersistenceIntegrationTest extends AbstractCloudIntegrationTest {

private static WorkbenchKieServerScenario deploymentScenario;

private static HttpsWorkbenchTestProvider httpsWorkbenchTestProvider;
private static PersistenceTestProvider persistenceTestProvider;

@BeforeClass
public static void initializeDeployment() {
Expand All @@ -37,7 +37,7 @@ public static void initializeDeployment() {
ScenarioDeployer.deployScenario(deploymentScenario);

// Setup test providers
httpsWorkbenchTestProvider = HttpsWorkbenchTestProvider.create();
persistenceTestProvider = PersistenceTestProvider.create();
}

@AfterClass
Expand All @@ -47,11 +47,11 @@ public static void cleanEnvironment() {

@Test
public void testAdminUserPasswordChange() {
httpsWorkbenchTestProvider.testAdminUserPasswordChange(deploymentScenario.getWorkbenchDeployment());
persistenceTestProvider.testAdminUserPasswordChange(deploymentScenario);
}

@Test
public void testAdminUserNameAndPasswordChange() {
httpsWorkbenchTestProvider.testAdminUserNameAndPasswordChange(deploymentScenario.getWorkbenchDeployment());
persistenceTestProvider.testAdminUserNameAndPasswordChange(deploymentScenario);
}
}
Loading

0 comments on commit f07ea85

Please sign in to comment.