Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RHPAM-2762 & RHPAM-2777 add tests to verify issues fixImplement test … #633

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,14 @@ public interface WorkbenchKieServerScenario extends KieDeploymentScenario<Workbe
*/
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 @@ -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 @@ -181,6 +181,30 @@ protected void registerTrustedSecret(Server server) {
server.setKeystoreSecret(OpenShiftConstants.getKieApplicationSecretName());
}

protected boolean isCustomTrustedSecretRegistered(Console console) {
if (console.getKeystoreSecret() != null && !console.getKeystoreSecret().isEmpty() && console.getEnv().length > 0) {
return Stream.of(console.getEnv()).map(Env::getName).anyMatch("HTTPS_NAME"::equals) &&
Stream.of(console.getEnv()).map(Env::getName).anyMatch("HTTPS_PASSWORD"::equals);
}
return false;
}

protected boolean isCustomTrustedSecretRegistered(SmartRouter smartRouter) {
if (smartRouter.getKeystoreSecret() != null && !smartRouter.getKeystoreSecret().isEmpty() && smartRouter.getEnv().length > 0) {
return Stream.of(smartRouter.getEnv()).map(Env::getName).anyMatch("KIE_SERVER_ROUTER_TLS_KEYSTORE_KEYALIAS"::equals) &&
Stream.of(smartRouter.getEnv()).map(Env::getName).anyMatch("KIE_SERVER_ROUTER_TLS_KEYSTORE_PASSWORD"::equals);
}
return false;
}

protected boolean isCustomTrustedSecretRegistered(Server server) {
if (server.getKeystoreSecret() != null && !server.getKeystoreSecret().isEmpty() && server.getEnv().length > 0) {
return Stream.of(server.getEnv()).map(Env::getName).anyMatch("HTTPS_NAME"::equals) &&
Stream.of(server.getEnv()).map(Env::getName).anyMatch("HTTPS_PASSWORD"::equals);
}
return false;
}

/**
* @return OpenShift client which is aware of KieApp custom resource.
*/
Expand Down
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 @@ -89,24 +94,29 @@ protected void deployCustomResource() {
gitProvider = Git.createProvider(project, request.getGitSettings());
}

registerTrustedSecret(kieApp.getSpec().getObjects().getConsole());

if (!isCustomTrustedSecretRegistered(kieApp.getSpec().getObjects().getConsole())) {
registerTrustedSecret(kieApp.getSpec().getObjects().getConsole());
}
for (Server server : kieApp.getSpec().getObjects().getServers()) {
registerTrustedSecret(server);
if (!isCustomTrustedSecretRegistered(server)) {
registerTrustedSecret(server);
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need to check whether the ssl is configured? The values will remain the same, so it should not matter if we set the environment variables again and they won't change in the second execution, right?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the value remain same but once when ssl is configured, I think there is no need to creating it again. If there is existing configuration, we should use it. It's also closer to manual update, when is KieApp updated manually we do not created the custom secret again.

Copy link
Collaborator

@Sgitario Sgitario Jun 4, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I see the problem. The "env" field is a list (not a set), if we don't check this, we'll be adding the same value several times.
Maybe in order to avoid having the same hard coded values in difference places (example: "HTTPS_NAME" or "HTTPS_PASSWORD"), what about adding a transient method in the Server.java to add the env if it does not exist?

    @Transient
    public void addEnvIfNotSet(String name, String value) {
        if (Stream.of(getEnv()).map(Env::getName).noneMatch(envName -> StringUtils.equals(envName, name))) {
            addEnv(new Env(name, value));
        }
    }

Or adding this new method in OpenShiftOperatorScenario sending the Server instance:

    public void addEnvIfNotSet(Server server, String name, String value) {
        if (Stream.of(server.getEnv()).map(Env::getName).noneMatch(envName -> StringUtils.equals(envName, name))) {
            server.addEnv(new Env(name, value));
        }
    }

Then, it would be only about to replace the existing "addEnv" method to "addEnvIfNotSet" in OpenShiftOperatorScenario. We would not need to worry about having new methods to check existing envs or deal with duplicated hardcoded values.
What do you think?

}

// deploy application
getKieAppClient().create(kieApp);
// Wait until the operator reconciliate the KieApp and add there missing informations
getKieAppClient().createOrReplace(kieApp);
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();
new SimpleWaiter(this::isKieAppDeployed).reason("Waiting for KieApp to be deployed.")
.timeout(TimeUnit.MINUTES, 1)
.waitFor();

workbenchDeployment = new WorkbenchOperatorDeployment(project, getKieAppClient());
workbenchDeployment.setUsername(DeploymentConstants.getAppUser());
workbenchDeployment.setPassword(DeploymentConstants.getAppPassword());
workbenchDeployment.setUsername(kieApp.getSpec().getCommonConfig().getAdminUser());
workbenchDeployment.setPassword(kieApp.getSpec().getCommonConfig().getAdminPassword());

kieServerDeployment = new KieServerOperatorDeployment(project, getKieAppClient());
kieServerDeployment.setUsername(DeploymentConstants.getAppUser());
kieServerDeployment.setPassword(DeploymentConstants.getAppPassword());
kieServerDeployment.setUsername(kieApp.getSpec().getCommonConfig().getAdminUser());
kieServerDeployment.setPassword(kieApp.getSpec().getCommonConfig().getAdminPassword());

logger.info("Waiting until all services are created.");
try {
Expand All @@ -128,6 +138,10 @@ protected void deployCustomResource() {
storeProjectInfoToPersistentVolume(workbenchDeployment, "/opt/eap/standalone/data/kie");
}

private boolean isKieAppDeployed() {
return getKieAppClient().withName(OpenShiftConstants.getKieApplicationName()).get().getStatus().getPhase().equals("Deployed");
}

@Override
public WorkbenchDeployment getWorkbenchDeployment() {
return workbenchDeployment;
Expand Down Expand Up @@ -179,4 +193,36 @@ public SsoDeployment getSsoDeployment() {
public GitProvider getGitProvider() {
return gitProvider;
}

@Override
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);
jakubschwan marked this conversation as resolved.
Show resolved Hide resolved
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public class WorkbenchKieServerScenarioImpl extends OpenShiftOperatorScenario<Wo
private WorkbenchDeploymentImpl workbenchDeployment;
private KieServerDeploymentImpl kieServerDeployment;
private final ScenarioRequest request;

private PrometheusDeployment prometheusDeployment;
private GitProvider gitProvider;

Expand Down Expand Up @@ -162,4 +163,9 @@ public Optional<PrometheusDeployment> getPrometheusDeployment() {
public GitProvider getGitProvider() {
return gitProvider;
}

@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 @@ -66,14 +66,6 @@ public WorkbenchKieServerPersistentScenarioBuilderImpl() {
commonConfig.setAdminUser(DeploymentConstants.getAppUser());
commonConfig.setAdminPassword(DeploymentConstants.getAppPassword());
kieApp.getSpec().setCommonConfig(commonConfig);

Server server = new Server();
server.addEnvs(authenticationEnvVars);
kieApp.getSpec().getObjects().addServer(server);

Console console = new Console();
console.addEnvs(authenticationEnvVars);
jakubschwan marked this conversation as resolved.
Show resolved Hide resolved
kieApp.getSpec().getObjects().setConsole(console);
}

@Override
Expand Down
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,8 @@
import org.kie.cloud.api.git.GitProvider;
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 @@ -155,4 +160,9 @@ public SsoDeployment getSsoDeployment() {
public GitProvider getGitProvider() {
return gitProvider;
}

@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 @@ -32,6 +32,8 @@
import org.kie.cloud.api.deployment.constants.DeploymentConstants;
import org.kie.cloud.api.git.GitProvider;
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 @@ -141,4 +143,9 @@ public Optional<PrometheusDeployment> getPrometheusDeployment() {
public GitProvider getGitProvider() {
return gitProvider;
}

@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 @@ -31,8 +31,8 @@
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import cz.xtf.core.openshift.OpenShift;
import cz.xtf.core.waiting.SimpleWaiter;
import cz.xtf.core.openshift.OpenShift;
import io.fabric8.kubernetes.api.model.Pod;
import io.fabric8.kubernetes.api.model.Quantity;
import io.fabric8.kubernetes.api.model.Service;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright 2020 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.kie.cloud.integrationtests.persistence;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.kie.cloud.api.scenario.WorkbenchKieServerScenario;
import org.kie.cloud.integrationtests.category.TemplateNotSupported;
import org.kie.cloud.integrationtests.testproviders.PersistenceTestProvider;
import org.kie.cloud.tests.common.AbstractCloudIntegrationTest;
import org.kie.cloud.tests.common.ScenarioDeployer;

@Category({TemplateNotSupported.class})
public class WorkbenchAdminUserPersistenceIntegrationTest extends AbstractCloudIntegrationTest {

private static WorkbenchKieServerScenario deploymentScenario;

private static PersistenceTestProvider persistenceTestProvider;

@BeforeClass
public static void initializeDeployment() {
deploymentScenario = deploymentScenarioFactory.getWorkbenchKieServerPersistentScenarioBuilder()
.withInternalMavenRepo()
.build();
deploymentScenario.setLogFolderName(WorkbenchAdminUserPersistenceIntegrationTest.class.getSimpleName());
ScenarioDeployer.deployScenario(deploymentScenario);

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

@AfterClass
public static void cleanEnvironment() {
ScenarioDeployer.undeployScenario(deploymentScenario);
}

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

@Test
public void testAdminUserNameAndPasswordChange() {
persistenceTestProvider.testAdminUserNameAndPasswordChange(deploymentScenario);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import java.io.IOException;
import java.net.URL;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
Expand Down
Loading