Skip to content

Commit

Permalink
Cover custom init containers
Browse files Browse the repository at this point in the history
  • Loading branch information
fedinskiy committed Mar 20, 2024
1 parent 7a22940 commit 03a2e0c
Show file tree
Hide file tree
Showing 6 changed files with 204 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,8 @@ PanacheRepository is a facade class. Facade class can override certain methods t
PanacheRepositoryResource methods.

- AgroalPoolTest, will cover how the db pool is managed in terms of IDLE-timeout, max connections and concurrency.
- InitContainer tests, which cover, whether we can use custom init containers
- Dockerfile for custom init container can be found inside resources folder

### `sql-db/reactive-rest-data-panache`

Expand Down
11 changes: 11 additions & 0 deletions sql-db/panache-flyway/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,15 @@
<scope>test</scope>
</dependency>
</dependencies>
<profiles>
<profile>
<id>deploy-to-openshift-using-extension</id>
<dependencies>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-openshift</artifactId>
</dependency>
</dependencies>
</profile>
</profiles>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.quarkus.ts.sqldb.panacheflyway.init;

import java.util.ArrayList;
import java.util.Map;

class CustomYaml {
private final Map content;

CustomYaml(Object content) {
this.content = (Map) content;
}

CustomYaml get(String name) {
return new CustomYaml(this.content.get(name));
}

String getValue(String name) {
return (String) this.content.get(name);
}

public CustomYaml getFromArray(String name, int i) {
return new CustomYaml(((ArrayList) this.content.get(name)).get(i));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package io.quarkus.ts.sqldb.panacheflyway.init;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import org.apache.http.HttpStatus;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.yaml.snakeyaml.Yaml;

import io.quarkus.test.bootstrap.MySqlService;
import io.quarkus.test.bootstrap.RestService;
import io.quarkus.test.scenarios.OpenShiftDeploymentStrategy;
import io.quarkus.test.scenarios.OpenShiftScenario;
import io.quarkus.test.services.Container;
import io.quarkus.test.services.QuarkusApplication;

@OpenShiftScenario(deployment = OpenShiftDeploymentStrategy.UsingOpenShiftExtension)
public class OpenShiftDefaultInitContainerIT {

private final Path openShiftYaml = Paths.get("target/", this.getClass().getSimpleName(),
"app/target/kubernetes/openshift.yml");
private static final String CUSTOM_IMAGE = "quay.io/quarkusqeteam/wait:0.0.2";

@Container(image = "${mysql.80.image}", port = 3306, expectedLog = "Only MySQL server logs after this point")
static MySqlService database = new MySqlService();

@QuarkusApplication
static RestService app = new RestService()
.withProperty("quarkus.datasource.username", database.getUser())
.withProperty("quarkus.datasource.password", database.getPassword())
.withProperty("quarkus.datasource.jdbc.url", database::getJdbcUrl)
.withProperty("quarkus.openshift.init-task-defaults.wait-for-container.image", CUSTOM_IMAGE)
.withProperty("quarkus.flyway.schemas", database.getDatabase());

@Test
@Disabled
void migrated() {
String userList = app.given()
.accept("application/hal+json")
.when().get("/users/all?sort=name")
.then()
.statusCode(HttpStatus.SC_OK)
.extract().response().jsonPath().getString("_embedded.user_list.name");
assertEquals("[Alaba, Balaba]", userList);
}

@Test
void yaml() {
try (InputStream content = Files.newInputStream(openShiftYaml)) {
Iterable<Object> objects = new Yaml().loadAll(content);
String image = null;
for (Object object : objects) {
CustomYaml source = new CustomYaml(object);
if ("Deployment".equals(source.getValue("kind"))) {
image = source.get("spec")
.get("template")
.get("spec")
.getFromArray("initContainers", 0)
.getValue("image");
}
}
Assertions.assertEquals(CUSTOM_IMAGE, image);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package io.quarkus.ts.sqldb.panacheflyway.init;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import org.apache.http.HttpStatus;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.yaml.snakeyaml.Yaml;

import io.quarkus.test.bootstrap.MySqlService;
import io.quarkus.test.bootstrap.RestService;
import io.quarkus.test.scenarios.OpenShiftDeploymentStrategy;
import io.quarkus.test.scenarios.OpenShiftScenario;
import io.quarkus.test.services.Container;
import io.quarkus.test.services.QuarkusApplication;

@OpenShiftScenario(deployment = OpenShiftDeploymentStrategy.UsingOpenShiftExtension)
@Disabled("https://github.com/quarkusio/quarkus/issues/39230")
public class OpenShiftFlywayInitContainerIT {

private final Path openShiftYaml = Paths.get("target/", this.getClass().getSimpleName(),
"app/target/kubernetes/openshift.yml");
private static final String CUSTOM_IMAGE = "quay.io/quarkusqeteam/wait";

@Container(image = "${mysql.80.image}", port = 3306, expectedLog = "Only MySQL server logs after this point")
static MySqlService database = new MySqlService();

@QuarkusApplication
static RestService app = new RestService()
.withProperty("quarkus.datasource.username", database.getUser())
.withProperty("quarkus.datasource.password", database.getPassword())
.withProperty("quarkus.datasource.jdbc.url", database::getJdbcUrl)
.withProperty("quarkus.openshift.init-containers.wait-for-flyway.image", CUSTOM_IMAGE)
.withProperty("quarkus.openshift.init-containers.wait-for-flyway.image-pull-policy", "Always")
.withProperty("quarkus.flyway.schemas", database.getDatabase());

@Test
@Disabled
void migrated() {
String userList = app.given()
.accept("application/hal+json")
.when().get("/users/all?sort=name")
.then()
.statusCode(HttpStatus.SC_OK)
.extract().response().jsonPath().getString("_embedded.user_list.name");
assertEquals("[Alaba, Balaba]", userList);
}

@Test
void yaml() {
try (InputStream content = Files.newInputStream(openShiftYaml)) {
Iterable<Object> objects = new Yaml().loadAll(content);
CustomYaml initContainer = null;
for (Object object : objects) {
CustomYaml source = new CustomYaml(object);
if ("Deployment".equals(source.getValue("kind"))) {
initContainer = source.get("spec")
.get("template")
.get("spec")
.getFromArray("initContainers", 0);
}
}
Assertions.assertNotNull(initContainer);
Assertions.assertEquals(CUSTOM_IMAGE, initContainer.getValue("image"));
Assertions.assertEquals("Always", initContainer.getValue("imagePullPolicy"));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
16 changes: 16 additions & 0 deletions sql-db/panache-flyway/src/test/resources/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/env -S podman build . --tag quay.io/quarkusqeteam/wait:0.0.2 --file
FROM ubi8:latest

COPY <<EOF /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://pkgs.k8s.io/core:/stable:/v1.29/rpm/
enabled=1
gpgcheck=1
gpgkey=https://pkgs.k8s.io/core:/stable:/v1.29/rpm/repodata/repomd.xml.key
EOF
RUN dnf -y install kubectl

ADD --chmod=777 https://raw.githubusercontent.com/groundnuty/k8s-wait-for/master/wait_for.sh /usr/local/bin/wait_for.sh

ENTRYPOINT ["wait_for.sh"]

0 comments on commit 03a2e0c

Please sign in to comment.