Skip to content

Commit

Permalink
Clean framework and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubschwan committed May 18, 2020
1 parent 29c3955 commit f1725ea
Show file tree
Hide file tree
Showing 13 changed files with 40 additions and 266 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,19 +67,4 @@ public interface WorkbenchDeployment extends Deployment {
* @return Workbench user password
*/
String getPassword();

/**
* Change Kie Workbench user password.
*
* @param newPassword New workbench user password
*/
void changePassword(String newPassword);

/**
* Change Kie Workbench user name and password.
*
* @param newUsername New workbench user name
* @param newPassword New workbench user password.
*/
void changeUsernameAndPassword(String newUsername, String newPassword);
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,12 @@ default Optional<PrometheusDeployment> getPrometheusDeployment() {
return Optional.empty();
}

/**
* Change Kie Admin username and password for the scenario.
* After the change are deployments updated and method waits for new available pods.
*
* @param username New admin name. (To change only password put there same username as it was)
* @param password New admin password.
*/
void changeUsernameAndPassword(String username, String password);
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,23 +60,4 @@ public void waitForScale() {
RouterUtil.waitForRouter(getUrl());
}
}

@Override
public void changePassword(String newPassword) {
if (isReady()) {
KieApp kieApp = kieAppClient.withName(OpenShiftConstants.getKieApplicationName()).get();
kieApp.getSpec().getCommonConfig().setAdminPassword(newPassword);
kieAppClient.createOrReplace(kieApp);
}
}

@Override
public void changeUsernameAndPassword(String newUsername, String newPassword) {
if (isReady()) {
KieApp kieApp = kieAppClient.withName(OpenShiftConstants.getKieApplicationName()).get();
kieApp.getSpec().getCommonConfig().setAdminUser(newUsername);
kieApp.getSpec().getCommonConfig().setAdminPassword(newPassword);
kieAppClient.createOrReplace(kieApp);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ protected void deployCustomResource() {
// Wait until the operator reconciliate the KieApp and add there missing informations
try {
logger.info("Waiting for 10 seconds to let KieApp deployment start - on Jenkins when kieApp replaced following waiters are skipped as old deployment exists.");
Thread.sleep(TimeUnit.SECONDS.toMillis(10)); //Dummy waiting
Thread.sleep(TimeUnit.SECONDS.toMillis(10)); // TODO what could work better?
} catch (InterruptedException e) {
throw new RuntimeException("Waiting was interrupted", e);
}
Expand Down Expand Up @@ -187,7 +187,7 @@ public SsoDeployment getSsoDeployment() {
}

@Override
public void changeUsernameAndPassword(final String username, final String password) {
public void changeUsernameAndPassword(String username, String password) {
if(getDeployments().stream().allMatch(Deployment::isReady)) {
kieApp = getKieAppClient().withName(OpenShiftConstants.getKieApplicationName()).get();
kieApp.getSpec().getCommonConfig().setAdminUser(username);
Expand Down
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 final boolean deployPrometheus;
private boolean deployPrometheus;
private PrometheusDeployment prometheusDeployment;

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

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

@Override
protected void deployCustomResource() {
// deploy application
getKieAppClient().createOrReplace(kieApp);
getKieAppClient().create(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 (final WaiterException e) {
} catch (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() {
final List<Deployment> deployments = new ArrayList<Deployment>(Arrays.asList(workbenchDeployment, kieServerDeployment));
List<Deployment> deployments = new ArrayList<Deployment>(Arrays.asList(workbenchDeployment, kieServerDeployment));
deployments.removeAll(Collections.singleton(null));
return deployments;
}

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

Expand Down Expand Up @@ -155,14 +155,7 @@ public Optional<PrometheusDeployment> getPrometheusDeployment() {
}

@Override
public void changeUsernameAndPassword(final String username, final String password) {
if(getDeployments().stream().allMatch(Deployment::isReady)) {
kieApp = getKieAppClient().withName(OpenShiftConstants.getKieApplicationName()).get();
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.");
}
public void changeUsernameAndPassword(String username, String password) {
throw new UnsupportedOperationException("Not supported yet.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,9 @@ public WorkbenchKieServerPersistentScenarioBuilderImpl() {
kieApp.getSpec().setCommonConfig(commonConfig);

Server server = new Server();
//server.addEnvs(authenticationEnvVars); TODO remove this? not needed as we have set user in common config
kieApp.getSpec().getObjects().addServer(server);

Console console = new Console();
//console.addEnvs(authenticationEnvVars); TODO remove this? not needed as we have set user in common config
kieApp.getSpec().getObjects().setConsole(console);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,14 +154,13 @@ public SsoDeployment getSsoDeployment() {

@Override
public void changeUsernameAndPassword(String username, String password) {
if(getDeployments().stream().allMatch(Deployment::isReady)) {
deploySecretAppUser(username,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{
getDeployments().parallelStream().forEach(this::scaleToZeroAndBackToReplicas);
} 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,10 @@
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 @@ -35,7 +32,6 @@
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 @@ -140,39 +136,6 @@ public Optional<PrometheusDeployment> getPrometheusDeployment() {

@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();
throw new UnsupportedOperationException("Not supported yet.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,6 @@ public void setOpenShift(OpenShift openShift) {
this.openShift = openShift;
}

protected Project getProject() {
return project;
}

@Override
public String getNamespace() {
return project.getName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,13 @@

import java.net.URI;
import java.net.URL;
import java.util.HashMap;
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.WorkbenchDeployment;
import org.kie.cloud.api.deployment.constants.DeploymentConstants;
import org.kie.cloud.openshift.constants.OpenShiftConstants;
import org.kie.cloud.openshift.resource.Project;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class WorkbenchDeploymentImpl extends OpenShiftDeployment implements WorkbenchDeployment {

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

private Optional<URL> insecureUrl;
private Optional<URL> secureUrl;
private Optional<URI> webSocketUri;
Expand Down Expand Up @@ -105,42 +95,4 @@ public String getServiceName() {
getSecureUrl().ifPresent(RouterUtil::waitForRouter);
}
}

@Override
public void changePassword(String newPassword) {
deploySecretAppUser(DeploymentConstants.getAppUser(), newPassword);
}

@Override
public void changeUsernameAndPassword(String newUsername, String newPassword) {
deploySecretAppUser(newUsername, newPassword);
}

private void deploySecretAppUser(String user, String password) {
logger.info("Delete old secret '{}'", DeploymentConstants.getAppCredentialsSecretName());
getOpenShift().secrets().withName(DeploymentConstants.getAppCredentialsSecretName()).delete();
new SimpleWaiter(() -> 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);

getProject().createSecret(DeploymentConstants.getAppCredentialsSecretName(), data);
new SimpleWaiter(() -> getOpenShift().getSecret(DeploymentConstants.getAppCredentialsSecretName()) != null).timeout(TimeUnit.MINUTES, 2)
.reason("Waiting for new secret to be created.")
.waitFor();
logger.info("Restart the environment to update Workbench deployment.");
scaleToZeroAndBackToReplicas();
}

private void scaleToZeroAndBackToReplicas() {
int replicas = getInstances().size();
scale(0);
waitForScale();
scale(replicas);
waitForScale();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,4 @@ public void setServiceName(String applicationName) {
getSecureUrl().ifPresent(RouterUtil::waitForRouter);
}
}

@Override
public void changePassword(String newPassword) {
throw new UnsupportedOperationException("Not supported");
}

@Override
public void changeUsernameAndPassword(String newUsername, String newPassword) {
throw new UnsupportedOperationException("Not supported");
}
}
Loading

0 comments on commit f1725ea

Please sign in to comment.