forked from Intersmash/intersmash
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e201f43
commit 476d776
Showing
12 changed files
with
6,031 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
216 changes: 216 additions & 0 deletions
216
...boss/intersmash/testsuite/provision/openshift/KeycloakQuarkusOperatorProvisionerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,216 @@ | ||
/** | ||
* Copyright (C) 2023 Red Hat, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* 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.jboss.intersmash.testsuite.provision.openshift; | ||
|
||
import java.io.IOException; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import org.jboss.intersmash.tools.application.openshift.KeycloakQuarkusOperatorApplication; | ||
import org.jboss.intersmash.tools.junit5.IntersmashExtension; | ||
import org.jboss.intersmash.tools.provision.openshift.KeycloakQuarkusOperatorProvisioner; | ||
import org.jboss.intersmash.tools.provision.openshift.operator.resources.OperatorGroup; | ||
import org.jboss.intersmash.tools.util.tls.CertificatesUtils; | ||
import org.junit.jupiter.api.AfterAll; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
import org.keycloak.k8s.v2alpha1.Keycloak; | ||
import org.keycloak.k8s.v2alpha1.KeycloakSpec; | ||
import org.keycloak.k8s.v2alpha1.keycloakspec.Hostname; | ||
import org.keycloak.k8s.v2alpha1.keycloakspec.Http; | ||
import org.keycloak.k8s.v2alpha1.keycloakspec.Ingress; | ||
import org.slf4j.event.Level; | ||
|
||
import cz.xtf.core.openshift.OpenShiftWaiters; | ||
import cz.xtf.core.openshift.OpenShifts; | ||
import cz.xtf.core.waiting.SimpleWaiter; | ||
import cz.xtf.junit5.annotations.CleanBeforeAll; | ||
import io.fabric8.kubernetes.api.model.DeletionPropagation; | ||
import io.fabric8.kubernetes.api.model.Secret; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
/** | ||
* Test the Keycloak Operator provisioning model and APIs | ||
* | ||
* See <br> | ||
* - https://github.com/keycloak/keycloak-operator/tree/master/deploy/examples | ||
* <p> | ||
* Not all examples are actually run, but these tests are more about the framework functionality verification than | ||
* about the Keycloak operator testing. | ||
*/ | ||
@Slf4j | ||
@CleanBeforeAll | ||
//@Disabled("WIP - Disabled until global-test.properties is configured with the required property") | ||
public class KeycloakQuarkusOperatorProvisionerTest { | ||
// Be aware that since we're using the static mock application, not all provisioner methods will work as expected! | ||
private static final KeycloakQuarkusOperatorProvisioner KEYCLOAK_OPERATOR_PROVISIONER = initializeOperatorProvisioner(); | ||
|
||
private static KeycloakQuarkusOperatorProvisioner initializeOperatorProvisioner() { | ||
KeycloakQuarkusOperatorProvisioner operatorProvisioner = new KeycloakQuarkusOperatorProvisioner( | ||
new KeycloakQuarkusOperatorApplication() { | ||
private static final String DEFAULT_KEYCLOAK_APP_NAME = "example-sso"; | ||
|
||
@Override | ||
public Keycloak getKeycloak() { | ||
Keycloak keycloak = new Keycloak(); | ||
keycloak.getMetadata().setName(DEFAULT_KEYCLOAK_APP_NAME); | ||
KeycloakSpec spec = new KeycloakSpec(); | ||
spec.setInstances(1L); | ||
Ingress ingress = new Ingress(); | ||
ingress.setEnabled(true); | ||
spec.setIngress(ingress); | ||
Hostname hostname = new Hostname(); | ||
hostname.setHostname(OpenShifts.master().generateHostname(DEFAULT_KEYCLOAK_APP_NAME)); | ||
spec.setHostname(hostname); | ||
CertificatesUtils.CertificateAndKey certificateAndKey = CertificatesUtils | ||
.generateSelfSignedCertificateAndKey(hostname.getHostname()); | ||
// create secret | ||
Secret tlsSecret = null; | ||
try { | ||
tlsSecret = CertificatesUtils.createTlsSecret( | ||
DEFAULT_KEYCLOAK_APP_NAME + "-tls-secret", | ||
certificateAndKey.key, | ||
certificateAndKey.certificate); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
// add config to keycloak | ||
Http http = new Http(); | ||
http.setTlsSecret(tlsSecret.getMetadata().getName()); | ||
spec.setHttp(http); | ||
keycloak.setSpec(spec); | ||
return keycloak; | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return DEFAULT_KEYCLOAK_APP_NAME; | ||
} | ||
}); | ||
return operatorProvisioner; | ||
} | ||
|
||
private String name; | ||
|
||
private static final Map<String, String> matchLabels = new HashMap<>(); | ||
|
||
@BeforeAll | ||
public static void createOperatorGroup() throws IOException { | ||
KEYCLOAK_OPERATOR_PROVISIONER.configure(); | ||
matchLabels.put("app", "sso"); | ||
IntersmashExtension.operatorCleanup(); | ||
// create operator group - this should be done by InteropExtension | ||
OpenShifts.adminBinary().execute("apply", "-f", OperatorGroup.SINGLE_NAMESPACE.save().getAbsolutePath()); | ||
// clean any leftovers | ||
KEYCLOAK_OPERATOR_PROVISIONER.unsubscribe(); | ||
} | ||
|
||
@AfterAll | ||
public static void removeOperatorGroup() { | ||
OpenShifts.adminBinary().execute("delete", "operatorgroup", "--all"); | ||
KEYCLOAK_OPERATOR_PROVISIONER.dismiss(); | ||
} | ||
|
||
@AfterEach | ||
public void customResourcesCleanup() { | ||
|
||
// delete keycloaks | ||
KEYCLOAK_OPERATOR_PROVISIONER.keycloakClient().list().getItems().stream() | ||
.map(resource -> resource.getMetadata().getName()).forEach(name -> KEYCLOAK_OPERATOR_PROVISIONER | ||
.keycloakClient().withName(name).withPropagationPolicy(DeletionPropagation.FOREGROUND).delete()); | ||
// delete realms | ||
KEYCLOAK_OPERATOR_PROVISIONER.keycloakRealmImportClient().list().getItems().stream() | ||
.map(resource -> resource.getMetadata().getName()).forEach(name -> KEYCLOAK_OPERATOR_PROVISIONER | ||
.keycloakRealmImportClient().withName(name).withPropagationPolicy(DeletionPropagation.FOREGROUND) | ||
.delete()); | ||
} | ||
|
||
/** | ||
* This test case creates and validates a basic {@link Keycloak} CR | ||
* | ||
* This is not an integration test, the goal here is to assess that the created CRs are configured as per the | ||
* model specification. | ||
* | ||
* See | ||
* <br> - https://github.com/keycloak/keycloak-operator/tree/master/deploy/examples/keycloak | ||
*/ | ||
@Test | ||
public void exampleSso() { | ||
KEYCLOAK_OPERATOR_PROVISIONER.subscribe(); | ||
try { | ||
name = "example-sso"; | ||
|
||
Keycloak keycloak = new Keycloak(); | ||
keycloak.getMetadata().setName(name); | ||
keycloak.getMetadata().setLabels(matchLabels); | ||
KeycloakSpec spec = new KeycloakSpec(); | ||
spec.setInstances(1L); | ||
Ingress ingress = new Ingress(); | ||
ingress.setEnabled(true); | ||
spec.setIngress(ingress); | ||
Hostname hostname = new Hostname(); | ||
hostname.setHostname(OpenShifts.master().generateHostname(name)); | ||
CertificatesUtils.CertificateAndKey certificateAndKey = CertificatesUtils | ||
.generateSelfSignedCertificateAndKey(hostname.getHostname()); | ||
// create secret | ||
Secret tlsSecret = null; | ||
try { | ||
tlsSecret = CertificatesUtils.createTlsSecret( | ||
name + "-tls-secret", | ||
certificateAndKey.key, | ||
certificateAndKey.certificate); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
// add config to keycloak | ||
Http http = new Http(); | ||
http.setTlsSecret(tlsSecret.getMetadata().getName()); | ||
spec.setHttp(http); | ||
spec.setHostname(hostname); | ||
keycloak.setSpec(spec); | ||
|
||
verifyKeycloak(keycloak, true); | ||
} finally { | ||
KEYCLOAK_OPERATOR_PROVISIONER.unsubscribe(); | ||
} | ||
} | ||
|
||
private void verifyKeycloak(Keycloak keycloak, boolean waitForPods) { | ||
// create and verify that object exists | ||
KEYCLOAK_OPERATOR_PROVISIONER.keycloakClient().createOrReplace(keycloak); | ||
KEYCLOAK_OPERATOR_PROVISIONER.waitFor(keycloak); | ||
// two pods expected keycloak-0 and keycloak-postgresql-*, keycloak-0 won't start unless keycloak-postgresql-* is ready | ||
if (waitForPods) { | ||
OpenShiftWaiters.get(OpenShifts.master(), () -> false) | ||
.areExactlyNPodsReady(2, "app", keycloak.getKind().toLowerCase()).level(Level.DEBUG).waitFor(); | ||
log.debug(KEYCLOAK_OPERATOR_PROVISIONER.keycloakClient().withName(name).get().getStatus().toString()); | ||
} | ||
Assertions.assertEquals(keycloak.getSpec(), | ||
KEYCLOAK_OPERATOR_PROVISIONER.keycloakClient().withName(name).get().getSpec()); | ||
|
||
// delete and verify that object was removed | ||
KEYCLOAK_OPERATOR_PROVISIONER.keycloakClient().withName(name).withPropagationPolicy(DeletionPropagation.FOREGROUND) | ||
.delete(); | ||
new SimpleWaiter(() -> KEYCLOAK_OPERATOR_PROVISIONER.keycloakClient().list().getItems().size() == 0).level(Level.DEBUG) | ||
.waitFor(); | ||
if (waitForPods) { | ||
OpenShiftWaiters.get(OpenShifts.master(), () -> false) | ||
.areExactlyNPodsReady(0, "app", keycloak.getKind().toLowerCase()).level(Level.DEBUG).waitFor(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.