Skip to content

Commit 781bd35

Browse files
authored
mysql e2e test (#786)
1 parent 12f6125 commit 781bd35

File tree

10 files changed

+117
-43
lines changed

10 files changed

+117
-43
lines changed

.github/workflows/e2e-test-mysql.yml

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# End to end integration test which deploys the Tomcat operator to a Kubernetes
2+
# (Kind) cluster and creates custom resources to verify the operator's functionality
3+
name: MySQL Schema Operator End to End test
4+
on:
5+
pull_request:
6+
branches: [ main, v1 ]
7+
jobs:
8+
tomcat_e2e_test:
9+
runs-on: ubuntu-latest
10+
env:
11+
KIND_CL_NAME: e2e-test
12+
steps:
13+
- name: Checkout
14+
uses: actions/checkout@v2
15+
16+
- name: clean resident local docker
17+
if: ${{ env.ACT }}
18+
continue-on-error: true
19+
run: |
20+
for DIMG in "$KIND_CL_NAME-control-plane "; do
21+
docker stop $DIMG ; docker rm $DIMG ;
22+
done ;
23+
sleep 1
24+
25+
- name: Create Kubernetes KinD Cluster
26+
uses: container-tools/[email protected]
27+
with:
28+
cluster_name: e2e-test
29+
registry: false
30+
31+
- name: Deploy MySQL DB
32+
working-directory: sample-operators/mysql-schema
33+
run: |
34+
kubectl create namespace mysql
35+
kubectl apply -f k8s/mysql-deployment.yaml
36+
kubectl apply -f k8s/mysql-service.yaml
37+
38+
- name: Set up Java and Maven
39+
uses: actions/setup-java@v2
40+
with:
41+
java-version: 11
42+
distribution: adopt-hotspot
43+
cache: 'maven'
44+
45+
- name: Build SDK
46+
run: mvn install -DskipTests
47+
48+
- name: build jib
49+
working-directory: sample-operators/mysql-schema
50+
run: |
51+
mvn --version
52+
mvn -B package jib:dockerBuild jib:buildTar -Djib-maven-image=mysql-schema-operator -DskipTests
53+
kind load image-archive target/jib-image.tar --name=${{ env.KIND_CL_NAME }}
54+
55+
- name: Apply CRDs
56+
working-directory: sample-operators/mysql-schema
57+
run: |
58+
kubectl apply -f target/classes/META-INF/fabric8/mysqlschemas.mysql.sample.javaoperatorsdk-v1.yml
59+
60+
- name: Deploy MySQL Operator
61+
working-directory: sample-operators/mysql-schema
62+
run: |
63+
kubectl apply -f k8s/operator.yaml
64+
65+
- name: Run E2E Tests
66+
working-directory: sample-operators/mysql-schema
67+
run: mvn -B test -P end-to-end-tests
68+
69+
- name: Dump state
70+
if: ${{ failure() }}
71+
run: |
72+
set +e
73+
echo "All namespaces"
74+
kubectl get ns
75+
echo "All objects in mysql"
76+
kubectl get all -n mysql-schema-test" -o yaml
77+
echo "Output of mysql pod"
78+
kubectl logs -l app=mysql-schema-operator -n mysql-schema
79+
echo "All objects in mysql-schema-test"
80+
kubectl get deployment,pod,tomcat,webapp -n mysql-schema-test" -o yaml

.github/workflows/end-to-end-tests.yml renamed to .github/workflows/e2e-test-tomcat.yml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
# End to end integration test which deploys the Tomcat operator to a Kubernetes
22
# (Kind) cluster and creates custom resources to verify the operator's functionality
3-
name: TomcatOperator End to End test
3+
name: Tomcat Operator End to End test
44
on:
5-
push:
6-
branches:
7-
- "*"
5+
pull_request:
6+
branches: [ main, v1 ]
87
jobs:
98
tomcat_e2e_test:
109
runs-on: ubuntu-latest

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/event/source/polling/PerResourcePollingEventSource.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ public void onResourceDeleted(R resource) {
9191
var resourceID = ResourceID.fromResource(resource);
9292
TimerTask task = timerTasks.remove(resourceID);
9393
if (task != null) {
94+
log.debug("Canceling task for resource: {}", resource);
9495
task.cancel();
9596
}
9697
cache.remove(resourceID);
@@ -100,7 +101,7 @@ private void checkAndRegisterTask(R resource) {
100101
var resourceID = ResourceID.fromResource(resource);
101102
if (timerTasks.get(resourceID) == null && (registerPredicate == null
102103
|| registerPredicate.test(resource))) {
103-
timer.schedule(new TimerTask() {
104+
var task = new TimerTask() {
104105
@Override
105106
public void run() {
106107
if (!isRunning()) {
@@ -112,7 +113,9 @@ public void run() {
112113
res.ifPresentOrElse(r -> pollForResource(r),
113114
() -> log.warn("No resource in cache for resource ID: {}", resourceID));
114115
}
115-
}, 0, period);
116+
};
117+
timerTasks.put(resourceID, task);
118+
timer.schedule(task, 0, period);
116119
}
117120
}
118121

sample-operators/mysql-schema/k8s/mysql-deployment.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ apiVersion: apps/v1
22
kind: Deployment
33
metadata:
44
name: mysql
5+
namespace: mysql
56
spec:
67
selector:
78
matchLabels:

sample-operators/mysql-schema/k8s/mysql-service.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ apiVersion: v1
22
kind: Service
33
metadata:
44
name: mysql
5+
namespace: mysql
56
spec:
67
ports:
78
- port: 3306

sample-operators/mysql-schema/k8s/operator.yaml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ spec:
2323
serviceAccountName: mysql-schema-operator # specify the ServiceAccount under which's RBAC persmissions the operator will be executed under
2424
containers:
2525
- name: operator
26-
image: ${DOCKER_REGISTRY}/mysql-schema-operator:${OPERATOR_VERSION}
27-
imagePullPolicy: Always
26+
image: mysql-schema-operator
27+
imagePullPolicy: IfNotPresent
2828
ports:
2929
- containerPort: 80
3030
env:
@@ -55,21 +55,21 @@ metadata:
5555
namespace: mysql-schema-operator
5656

5757
---
58-
apiVersion: rbac.authorization.k8s.io/v1beta1
58+
apiVersion: rbac.authorization.k8s.io/v1
5959
kind: ClusterRole
6060
metadata:
6161
name: mysql-schema-operator
6262
rules:
6363
- apiGroups:
6464
- mysql.sample.javaoperatorsdk
6565
resources:
66-
- schemas
66+
- mysqlschemas
6767
verbs:
6868
- "*"
6969
- apiGroups:
7070
- mysql.sample.javaoperatorsdk
7171
resources:
72-
- schemas/status
72+
- mysqlschemas/status
7373
verbs:
7474
- "*"
7575
- apiGroups:

sample-operators/mysql-schema/pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,13 @@
7171

7272
<build>
7373
<plugins>
74+
<plugin>
75+
<groupId>org.apache.maven.plugins</groupId>
76+
<artifactId>maven-surefire-plugin</artifactId>
77+
<configuration>
78+
<rerunFailingTestsCount>0</rerunFailingTestsCount>
79+
</configuration>
80+
</plugin>
7481
<plugin>
7582
<groupId>com.google.cloud.tools</groupId>
7683
<artifactId>jib-maven-plugin</artifactId>

sample-operators/mysql-schema/src/main/java/io/javaoperatorsdk/operator/sample/MySQLSchemaReconciler.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,12 @@ public List<EventSource> prepareEventSources(
5050

5151
@Override
5252
public UpdateControl<MySQLSchema> reconcile(MySQLSchema schema, Context context) {
53+
log.info("Reconciling MySQLSchema with name: {}", schema.getMetadata().getName());
5354
var dbSchema = context.getSecondaryResource(Schema.class);
55+
log.debug("Schema: {} found for: {} ", dbSchema, schema.getMetadata().getName());
5456
try (Connection connection = getConnection()) {
55-
if (dbSchema == null) {
57+
if (dbSchema.isEmpty()) {
58+
log.debug("Creating Schema and related resources for: {}", schema.getMetadata().getName());
5659
var schemaName = schema.getMetadata().getName();
5760
String password = RandomStringUtils.randomAlphanumeric(16);
5861
String secretName = String.format(SECRET_FORMAT, schemaName);
@@ -64,8 +67,10 @@ public UpdateControl<MySQLSchema> reconcile(MySQLSchema schema, Context context)
6467
updateStatusPojo(schema, secretName, userName);
6568
log.info("Schema {} created - updating CR status", schema.getMetadata().getName());
6669
return UpdateControl.updateStatus(schema);
70+
} else {
71+
log.debug("No update on MySQLSchema with name: {}", schema.getMetadata().getName());
72+
return UpdateControl.noUpdate();
6773
}
68-
return UpdateControl.noUpdate();
6974
} catch (SQLException e) {
7075
log.error("Error while creating Schema", e);
7176
throw new IllegalStateException(e);

sample-operators/mysql-schema/src/test/java/io/javaoperatorsdk/operator/sample/MySQLSchemaOperatorE2E.java

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package io.javaoperatorsdk.operator.sample;
22

3-
import java.io.File;
43
import java.io.IOException;
54

6-
import org.junit.jupiter.api.Disabled;
75
import org.junit.jupiter.api.Test;
86
import org.slf4j.Logger;
97
import org.slf4j.LoggerFactory;
@@ -16,20 +14,17 @@
1614
import io.javaoperatorsdk.operator.Operator;
1715
import io.javaoperatorsdk.operator.config.runtime.DefaultConfigurationService;
1816

19-
import com.fasterxml.jackson.databind.ObjectMapper;
20-
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
21-
2217
import static java.util.concurrent.TimeUnit.MINUTES;
2318
import static org.awaitility.Awaitility.await;
2419
import static org.hamcrest.CoreMatchers.equalTo;
2520
import static org.hamcrest.CoreMatchers.is;
2621
import static org.hamcrest.MatcherAssert.assertThat;
2722
import static org.hamcrest.Matchers.notNullValue;
2823

29-
@Disabled
3024
public class MySQLSchemaOperatorE2E {
3125

3226
final static String TEST_NS = "mysql-schema-test";
27+
final static String MY_SQL_NS = "mysql";
3328

3429
final static Logger log = LoggerFactory.getLogger(MySQLSchemaOperatorE2E.class);
3530

@@ -71,12 +66,7 @@ public void test() throws IOException {
7166
log.info("Creating test namespace {}", TEST_NS);
7267
client.namespaces().create(testNs);
7368

74-
log.info("Deploying MySQL server");
75-
deployMySQLServer(client);
76-
7769
log.info("Creating test MySQLSchema object: {}", testSchema);
78-
// var mysqlSchemaClient = client.customResources(MySQLSchema.class);
79-
// mysqlSchemaClient.inNamespace(TEST_NS).createOrReplace(testSchema);
8070
client.resource(testSchema).createOrReplace();
8171

8272
log.info("Waiting 5 minutes for expected resources to be created and updated");
@@ -90,23 +80,4 @@ public void test() throws IOException {
9080
});
9181
}
9282

93-
private void deployMySQLServer(KubernetesClient client) throws IOException {
94-
ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
95-
Deployment deployment =
96-
mapper.readValue(new File("k8s/mysql-deployment.yaml"), Deployment.class);
97-
deployment.getMetadata().setNamespace(TEST_NS);
98-
Service service = mapper.readValue(new File("k8s/mysql-service.yaml"), Service.class);
99-
service.getMetadata().setNamespace(TEST_NS);
100-
client.resource(deployment).createOrReplace();
101-
client.resource(service).createOrReplace();
102-
103-
log.info("Waiting for MySQL server to start");
104-
await().atMost(5, MINUTES).until(() -> {
105-
Deployment mysqlDeployment = client.apps().deployments().inNamespace(TEST_NS)
106-
.withName(deployment.getMetadata().getName()).get();
107-
return mysqlDeployment.getStatus().getReadyReplicas() != null
108-
&& mysqlDeployment.getStatus().getReadyReplicas() == 1;
109-
});
110-
}
111-
11283
}

sample-operators/tomcat-operator/pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,13 @@
6161

6262
<build>
6363
<plugins>
64+
<plugin>
65+
<groupId>org.apache.maven.plugins</groupId>
66+
<artifactId>maven-surefire-plugin</artifactId>
67+
<configuration>
68+
<rerunFailingTestsCount>0</rerunFailingTestsCount>
69+
</configuration>
70+
</plugin>
6471
<plugin>
6572
<groupId>com.google.cloud.tools</groupId>
6673
<artifactId>jib-maven-plugin</artifactId>

0 commit comments

Comments
 (0)