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 25, 2020
1 parent 29c3955 commit 1d0f3db
Show file tree
Hide file tree
Showing 13 changed files with 65 additions and 284 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 @@ -19,15 +19,20 @@
import java.time.Instant;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

import cz.xtf.core.waiting.SimpleWaiter;
import cz.xtf.core.waiting.SupplierWaiter;
import cz.xtf.core.waiting.WaiterException;
import io.fabric8.kubernetes.api.model.ObjectMeta;
import io.fabric8.kubernetes.api.model.Pod;
import org.kie.cloud.api.deployment.ControllerDeployment;
import org.kie.cloud.api.deployment.Deployment;
import org.kie.cloud.api.deployment.Instance;
import org.kie.cloud.api.deployment.KieServerDeployment;
import org.kie.cloud.api.deployment.SmartRouterDeployment;
import org.kie.cloud.api.deployment.SsoDeployment;
Expand Down Expand Up @@ -95,7 +100,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,14 +192,34 @@ 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)) {
List<String> oldInstances = workbenchDeployment.getInstances().stream().map(Instance::getName).collect(Collectors.toList());
oldInstances.addAll(kieServerDeployment.getInstances().stream().map(Instance::getName).collect(Collectors.toList()));

kieApp = getKieAppClient().withName(OpenShiftConstants.getKieApplicationName()).get();
kieApp.getSpec().getCommonConfig().setAdminUser(username);
kieApp.getSpec().getCommonConfig().setAdminPassword(password);
deployCustomResource();

try {
new SimpleWaiter(() -> areInstancesDeleted(oldInstances)).timeout(TimeUnit.MINUTES, 5).interval(TimeUnit.SECONDS, 5).waitFor();
} catch (WaiterException e) {
throw new RuntimeException("Timeout while deploying application.", e);
}
logger.info("Waiting for Workbench deployment to become ready.");
workbenchDeployment.waitForScale();

logger.info("Waiting for Kie server deployment to become ready.");
kieServerDeployment.waitForScale();
} else{
throw new RuntimeException("Application is not ready for Username and password change. Please check first that application is ready.");
}
}

private boolean areInstancesDeleted(Collection<String> oldInstancesNames) {
return project.getOpenShift().getPods().stream().map(Pod::getMetadata)
.map(ObjectMeta::getName)
.noneMatch(oldInstancesNames::contains);
}
}
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 @@ -30,8 +30,6 @@
import java.util.stream.Collectors;

import cz.xtf.core.openshift.OpenShift;
import cz.xtf.core.waiting.SimpleWaiter;
import io.fabric8.kubernetes.api.model.ObjectMeta;
import io.fabric8.kubernetes.api.model.Pod;
import io.fabric8.kubernetes.api.model.Quantity;
import io.fabric8.kubernetes.api.model.Service;
Expand Down Expand Up @@ -73,10 +71,6 @@ public void setOpenShift(OpenShift openShift) {
this.openShift = openShift;
}

protected Project getProject() {
return project;
}

@Override
public String getNamespace() {
return project.getName();
Expand Down Expand Up @@ -166,7 +160,6 @@ public void waitForScheduled() {
protected void waitUntilAllPodsAreReadyAndRunning(int expectedPods) {
waitUntilAllPodsAreReady(expectedPods);
waitUntilAllPodsAreRunning(expectedPods);
waitUntilOneSetOfDeploymentPodsAvailable();
}

protected void waitUntilAllPodsAreReady(int expectedPods) {
Expand All @@ -193,21 +186,6 @@ protected void waitUntilAllPodsAreRunning(int expectedPods) {
}
}

protected void waitUntilOneSetOfDeploymentPodsAvailable() {
try {
new SimpleWaiter(()-> openShift.getPods(getDeploymentConfigName()).stream().map(Pod::getMetadata)
.map(ObjectMeta::getName)
.map(name -> name.substring(0, name.lastIndexOf('-')))
.distinct()
.count() <= 1)
.reason("Waiting for only one set of deployment config pods available " + getDeploymentConfigName())
.timeout(OpenShiftResourceConstants.PODS_START_TO_READY_TIMEOUT)
.waitFor();
} catch (AssertionError e) {
throw new DeploymentTimeoutException("Timeout while waiting for pods to start.");
}
}

@Override
public void setRouterTimeout(Duration timeoutValue) {
RouteList list = getRoutes();
Expand Down
Loading

0 comments on commit 1d0f3db

Please sign in to comment.