Basics for automated test.

This commit is contained in:
Michael Lipp 2023-08-19 15:22:31 +02:00
parent c006bd0a39
commit 6c98438fc5
9 changed files with 160 additions and 16 deletions

View file

@ -21,6 +21,8 @@ dependencies {
runtimeOnly 'com.electronwill.night-config:yaml:3.6.6'
runtimeOnly 'org.slf4j:slf4j-jdk14:[2.0.7,3)'
testImplementation 'io.fabric8:kubernetes-client:6.8.1'
}
application {
@ -75,3 +77,18 @@ task pushImages {
dependsOn pushLatestImage
}
test {
enabled = project.hasProperty("k8s.testCluster")
dependsOn project.tasks["pushImages"]
useJUnitPlatform()
testLogging {
showStandardStreams = true
}
systemProperty "k8s.testCluster", project.hasProperty("k8s.testCluster")
? project.getProperty("k8s.testCluster") : null
}

View file

@ -0,0 +1,35 @@
apiVersion: "vmoperator.jdrupes.org/v1"
kind: VirtualMachine
metadata:
namespace: vmop-dev
name: unittest-vm
spec:
resources:
requests:
cpu: 1
memory: 2Gi
loadBalancerService:
labels:
test2: null
test3: added
vm:
# state: Running
maximumRam: 4Gi
currentRam: 2Gi
maximumCpus: 4
currentCpus: 2
powerdownTimeout: 1
networks:
- user: {}
disks:
- cdrom:
# image: ""
image: https://download.fedoraproject.org/pub/fedora/linux/releases/38/Workstation/x86_64/iso/Fedora-Workstation-Live-x86_64-38-1.6.iso
# image: "Fedora-Workstation-Live-x86_64-38-1.6.iso"
display:
spice:
port: 5812

View file

@ -0,0 +1,81 @@
package org.jdrupes.vmoperator.manager;
import static org.junit.jupiter.api.Assertions.*;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClientBuilder;
import io.fabric8.kubernetes.client.dsl.base.ResourceDefinitionContext;
import io.fabric8.kubernetes.client.Config;
class BasicTests {
private static KubernetesClient client;
private static ResourceDefinitionContext vmsContext;
@BeforeAll
static void setUpBeforeClass() throws Exception {
var testCluster = System.getProperty("k8s.testCluster");
assertNotNull(testCluster);
// Get client
client = new KubernetesClientBuilder()
.withConfig(Config.autoConfigure(testCluster)).build();
// Context for working with our CR
vmsContext = new ResourceDefinitionContext.Builder()
.withGroup("vmoperator.jdrupes.org").withKind("VirtualMachine")
.withPlural("vms").withNamespaced(true).withVersion("v1").build();
// Cleanup
var resourcesInNamespace = client.genericKubernetesResources(vmsContext)
.inNamespace("vmop-dev");
resourcesInNamespace.withName("unittest-vm").delete();
// Update pod by scaling deployment
client.apps().deployments().inNamespace("vmop-dev")
.withName("vm-operator").scale(0);
client.apps().deployments().inNamespace("vmop-dev")
.withName("vm-operator").scale(1);
// Wait until available
for (int i = 0; i < 10; i++) {
if (client.apps().deployments().inNamespace("vmop-dev")
.withName("vm-operator").get().getStatus().getConditions()
.stream().filter(c -> "Available".equals(c.getType())).findAny()
.isPresent()) {
return;
}
}
fail("vm-operator not deployed.");
}
@AfterAll
static void tearDownAfterClass() throws Exception {
// Bring down manager
client.apps().deployments().inNamespace("vmop-dev")
.withName("vm-operator").scale(0);
client.close();
}
@Test
void test() throws IOException {
// Load from Yaml
var vm = client.genericKubernetesResources(vmsContext)
.load(Files
.newInputStream(Path.of("test-resources/unittest-vm.yaml")));
// Create Custom Resource
vm.create();
// Cleanup
var resourcesInNamespace = client.genericKubernetesResources(vmsContext)
.inNamespace("vmop-dev");
resourcesInNamespace.withName("unittest-vm").delete();
}
}

View file

@ -0,0 +1 @@
package org.jdrupes.vmoperator.manager;