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

@ -13,3 +13,7 @@ The `kustomize.yaml` also changes the image repository for the
operator to a private repository for development. You have to
either remove this or adapt it to your own repository if you
also want to test your development version in a container.
If you want to run the unittests, this setup must be run with a private
repository and the private repository must match the one configured
for gradle pushImages.

View file

@ -1,12 +1,8 @@
# The values in comments are the defaults.
# Used for running manager outside Kubernetes.
# Keep in sync with kustomize.yaml
"/Manager":
"/Controller":
namespace: vmop-dev
"/Reconciler":
runnerData:
storageClassName: null
loadBalancerService: |
labels:
test1: info
test2: toBeDeleted

View file

@ -20,6 +20,21 @@ patches:
storage: 10Gi
storageClassName: local-path
- patch: |-
kind: ConfigMap
apiVersion: v1
metadata:
name: vm-operator
data:
# Keep in sync with config.yaml
config.yaml: |
"/Manager":
"/Controller":
namespace: vmop-dev
"/Reconciler":
runnerData:
storageClassName: null
- target:
group: apps
version: v1

View file

@ -14,11 +14,6 @@ spec:
cpu: 1
memory: 2Gi
loadBalancerService:
labels:
test2: null
test3: added
vm:
# state: Running
bootMenu: yes

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;

View file

@ -1,5 +1,5 @@
#
#Wed Aug 02 12:50:19 CEST 2023
#Sat Aug 19 11:33:46 CEST 2023
org.eclipse.jdt.core.formatter.insert_space_after_ellipsis=insert
org.eclipse.jdt.core.formatter.insert_space_after_comma_in_enum_declarations=insert
org.eclipse.jdt.core.formatter.insert_new_line_in_empty_annotation_declaration=insert
@ -22,12 +22,12 @@ org.eclipse.jdt.core.formatter.blank_lines_after_package=1
org.eclipse.jdt.core.formatter.insert_space_after_comma_in_multiple_local_declarations=insert
org.eclipse.jdt.core.formatter.alignment_for_arguments_in_enum_constant=16
org.eclipse.jdt.core.formatter.insert_space_after_opening_angle_bracket_in_parameterized_type_reference=do not insert
org.eclipse.jdt.core.formatter.comment.indent_root_tags=false
org.eclipse.jdt.core.formatter.wrap_before_or_operator_multicatch=true
org.eclipse.jdt.core.formatter.comment.indent_root_tags=false
org.eclipse.jdt.core.formatter.enabling_tag=@formatter\:on
org.eclipse.jdt.core.formatter.comment.count_line_length_from_starting_position=false
org.eclipse.jdt.core.formatter.insert_new_line_after_annotation_on_parameter=do not insert
org.eclipse.jdt.core.formatter.alignment_for_throws_clause_in_method_declaration=16
org.eclipse.jdt.core.formatter.insert_new_line_after_annotation_on_parameter=do not insert
org.eclipse.jdt.core.formatter.keep_then_statement_on_same_line=false
org.eclipse.jdt.core.formatter.insert_space_after_comma_in_explicitconstructorcall_arguments=insert
org.eclipse.jdt.core.formatter.insert_space_after_prefix_operator=do not insert
@ -43,8 +43,8 @@ org.eclipse.jdt.core.formatter.insert_space_between_empty_parens_in_method_invoc
org.eclipse.jdt.core.formatter.insert_space_after_assignment_operator=insert
org.eclipse.jdt.core.formatter.insert_space_before_opening_brace_in_type_declaration=insert
org.eclipse.jdt.core.formatter.insert_space_after_opening_paren_in_for=do not insert
org.eclipse.jdt.core.formatter.comment.preserve_white_space_between_code_and_line_comments=true
org.eclipse.jdt.core.formatter.insert_new_line_after_annotation_on_local_variable=insert
org.eclipse.jdt.core.formatter.comment.preserve_white_space_between_code_and_line_comments=true
org.eclipse.jdt.core.formatter.insert_space_before_closing_paren_in_method_invocation=do not insert
org.eclipse.jdt.core.formatter.alignment_for_union_type_in_multicatch=16
org.eclipse.jdt.core.formatter.number_of_blank_lines_at_beginning_of_method_body=0
@ -63,8 +63,8 @@ org.eclipse.jdt.core.formatter.alignment_for_type_parameters=16
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.formatter.comment.clear_blank_lines_in_block_comment=false
org.eclipse.jdt.core.formatter.insert_new_line_at_end_of_file_if_missing=do not insert
org.eclipse.jdt.core.formatter.insert_space_before_unary_operator=do not insert
org.eclipse.jdt.core.formatter.insert_space_after_comma_in_array_initializer=insert
org.eclipse.jdt.core.formatter.insert_space_before_unary_operator=do not insert
org.eclipse.jdt.core.formatter.format_line_comment_starting_on_first_column=false
org.eclipse.jdt.core.formatter.parentheses_positions_in_annotation=common_lines
org.eclipse.jdt.core.formatter.insert_space_before_ellipsis=do not insert