Skip to content

Commit

Permalink
Vallum backward compatibility test
Browse files Browse the repository at this point in the history
  • Loading branch information
Erikvv committed Jan 17, 2025
1 parent a22d862 commit 74b9397
Show file tree
Hide file tree
Showing 10 changed files with 171 additions and 59 deletions.
49 changes: 49 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,55 @@ jobs:
run --rm --user root
zorm-test-github-actions
vallum:
runs-on: ubuntu-latest
services:
postgres:
image: postgres
ports:
- 5432:5432
env:
POSTGRES_PASSWORD: postgres
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Setup Gradle
uses: gradle/actions/setup-gradle@v3
- name: backward-compatibility-test
run: ./vallum/backward-compatibility-test.sh
env:
POSTGRES_URL: jdbc:postgresql://localhost:5432/postgres
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
BASE_URL: http://localhost:8082
CORS_ALLOW_ORIGIN_PATTERN: 'http://localhost:\d{2,5}'
# Keycloak test realm JWK
ACCESS_TOKEN_JWK: |
{
"kid": "n-X5BeXFe6pIXhDBsDu2mQ2VYc23RSZCFRU5Y6IuzC8",
"kty": "OKP",
"alg": "EdDSA",
"use": "sig",
"crv": "Ed25519",
"x": "ILokSbk8EKH-Q1aWo_TguuDRccoSVfEbmGSbm5gcn5I"
}
USER_ID: f9eee559-7bd8-4a53-99cf-aaef36dad8a1
CLIENT_ID: test-client-ztor-api
CLIENT_SECRET: ${{ secrets.KEYCLOAK_TEST_CLIENT_SECRET }}
- name: upload-test-results
if: always()
uses: actions/upload-artifact@v4
with:
name: test-results
path: ./vallum/build/reports/tests/test/

# Tests without a database
gradle:
runs-on: ubuntu-latest
Expand Down
2 changes: 1 addition & 1 deletion compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ services:
vallum-test:
extends:
service: gradle-base
command: vallum:test
entrypoint: ./vallum/backward-compatibility-test.sh
volumes:
- gradle-vallum-test-cache:/home/gradle/.gradle
depends_on:
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {LabelRow} from "../generic/label-row"
import {displayTimeZone, kotlinInstantToJsJodaInstant, prettyPrint} from "./time-series-util"
import {Dropdown} from "primereact/dropdown"
import {InputText} from "primereact/inputtext"
import {IntervalDropdown} from "./interval-dropdown"

const placeholder = `
bijvoorbeeld:
Expand Down Expand Up @@ -51,9 +50,6 @@ export const TimeSeriesTextarea: FunctionComponent<{timeSeries: TimeSeries, setT
<LabelRow label="Tijdzone">
<Dropdown options={[{ label: 'Nederlandse tijd', value: displayTimeZone }]} value={displayTimeZone}/>
</LabelRow>
<LabelRow label="Meetinterval">
<IntervalDropdown timeStep={internalTimeSeries.timeStep} setTimeStep={timeStep => setTimeSeriesImpl(internalTimeSeries.withTimeStep(timeStep))} />
</LabelRow>
<LabelRow label="Plak hier de waarden in kWh">
<InputTextarea
id="values"
Expand Down
26 changes: 26 additions & 0 deletions vallum/backward-compatibility-test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/bin/bash
set -ex

# This verifies that the latest version of Ztor works with older versions of Vallum.
# Probably there is a more flexible way of testing this using different ClassLoaders instead of

CURRENT=$(git branch --show-current)

if [[ -z $CURRENT ]]; then
CURRENT=$(git rev-parse HEAD)
fi

mkdir -p shadowJar
gradle vallum:shadowJar
mv vallum/build/libs/vallum-dev-all.jar shadowJar/vallum-current-all.jar

git switch --detach 14c1cb1
gradle vallum:shadowJar
mv vallum/build/libs/vallum-dev-all.jar shadowJar/vallum-14c1cb1-all.jar

git switch production
gradle vallum:shadowJar
mv vallum/build/libs/vallum-dev-all.jar shadowJar/vallum-production-all.jar

git switch --detach $CURRENT
gradle vallum:test
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.zenmo.vallum;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.List;

import org.junit.jupiter.api.Test;

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

public class BackwardCompatibilityTest {
@Test
public void test() throws IOException, ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
String[] shadowJarPaths = {
"../shadowJar/vallum-14c1cb1-all.jar",
"../shadowJar/vallum-production-all.jar",
"../shadowJar/vallum-current-all.jar",
};

var stopZtor = InitKt.initZtor(8082);

for (var shadowJarPath: shadowJarPaths) {
testVallumVersion(shadowJarPath);
}

stopZtor.stop();
}

public void testVallumVersion(String shadowJarPath) throws IOException, ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
File jarFile = new File(shadowJarPath).getCanonicalFile();
System.out.println(jarFile.getPath());
if (!jarFile.exists()) {
throw new RuntimeException("Jarfile doesnt exist");
}
URL jarUrl = jarFile.toURI().toURL();
URL[] urls = {jarUrl};
var loader = new URLClassLoader(urls, null);

var vallumClass = loader.loadClass("com.zenmo.vallum.Vallum");
var constructor = vallumClass.getDeclaredConstructor(String.class, String.class, String.class, String.class);
var oldVallum = constructor.newInstance(System.getenv("CLIENT_ID"), System.getenv("CLIENT_SECRET"), "http://localhost:8082", "https://keycloak.zenmo.com/realms/testrealm/protocol/openid-connect/token");

/*
* TODO: write the code like normal java rather then reflection.
* I don't know how to do this with this class loader stuff
*/
var getSurveysByProject = vallumClass.getMethod("getSurveysByProject", String.class);
var surveys = (List) getSurveysByProject.invoke(oldVallum, "Waardkwartier");
assertEquals(1, surveys.size());
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
package com.zenmo.vallum;

import com.zenmo.vallum.Vallum;
import com.zenmo.zummon.companysurvey.Address;
import com.zenmo.zummon.companysurvey.GridConnection;
Expand All @@ -14,12 +16,13 @@
public class VallumTest {
@Test
public void test() {
var stopZtor = InitKt.initZtor();
var port = 8083;
var stopZtor = InitKt.initZtor(port);

Vallum vallum = new Vallum(
System.getenv("CLIENT_ID"),
System.getenv("CLIENT_SECRET"),
"http://localhost:8082",
"http://localhost:" + port,
"https://keycloak.zenmo.com/realms/testrealm/protocol/openid-connect/token"
);

Expand Down Expand Up @@ -61,4 +64,5 @@ public void test() {

stopZtor.stop();
}

}
9 changes: 7 additions & 2 deletions vallum/src/test/kotlin/Init.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
package com.zenmo.vallum

import com.zenmo.orm.companysurvey.ProjectRepository
import com.zenmo.orm.companysurvey.SurveyRepository
import com.zenmo.orm.companysurvey.createMockSurvey
Expand All @@ -12,7 +14,8 @@ import org.jetbrains.exposed.sql.SchemaUtils
import org.jetbrains.exposed.sql.transactions.transaction
import java.util.*

fun initZtor(): StopZtor {
@JvmOverloads
fun initZtor(port: Int = 8082): StopZtor {
val db = connectToPostgres()
val schema = Schema(db.connector().schema)
transaction(db) {
Expand All @@ -36,5 +39,7 @@ fun initZtor(): StopZtor {
surveyRepository.save(createMockSurvey(projectName1))
surveyRepository.save(createMockSurvey(projectName2))

return startTestServer()
return startTestServer(port)
}


21 changes: 0 additions & 21 deletions ztor/src/main/kotlin/com/zenmo/ztor/Application.kt
Original file line number Diff line number Diff line change
Expand Up @@ -50,27 +50,6 @@ fun main(args: Array<String>) {
.start(wait = true)
}

fun interface StopZtor {
fun stop(): Unit
}

fun startTestServer(): StopZtor {
val server = embeddedServer(Netty, port = 8082, host = "0.0.0.0", module = Application::vallumMinimal)
.start(wait = false)

return StopZtor { server.stop(1000, 1000) }
}

fun Application.vallumMinimal() {
configureHTTP()
configureMonitoring()
configureSerialization()
configureAuthentication()
val db = configureDatabases()
configureRouting()
configureStatusPages()
}

fun Application.module() {
configureHTTP()
configureMonitoring()
Expand Down
28 changes: 28 additions & 0 deletions ztor/src/main/kotlin/com/zenmo/ztor/TestServer.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.zenmo.ztor

import com.zenmo.ztor.plugins.*
import io.ktor.server.application.*
import io.ktor.server.engine.*
import io.ktor.server.netty.*

fun interface StopZtor {
fun stop(): Unit
}

@JvmOverloads
fun startTestServer(port: Int = 8082): StopZtor {
val server = embeddedServer(Netty, port = port, host = "0.0.0.0", module = Application::vallumMinimal)
.start(wait = false)

return StopZtor { server.stop(1000, 1000) }
}

fun Application.vallumMinimal() {
configureHTTP()
configureMonitoring()
configureSerialization()
configureAuthentication()
val db = configureDatabases()
configureRouting()
configureStatusPages()
}

0 comments on commit 74b9397

Please sign in to comment.