Skip to content

Commit 87315bb

Browse files
committed
Add e2e test to cover optional values
1 parent 56a69be commit 87315bb

File tree

5 files changed

+87
-12
lines changed

5 files changed

+87
-12
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
apiVersion: v1
2+
kind: Pod
3+
metadata:
4+
name: leader-election-operator-2
5+
spec:
6+
serviceAccountName: leader-election-operator
7+
containers:
8+
- name: operator
9+
image: leader-election-operator
10+
imagePullPolicy: Never
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
apiVersion: v1
2+
kind: ServiceAccount
3+
metadata:
4+
name: leader-election-operator
5+
6+
---
7+
apiVersion: v1
8+
kind: Pod
9+
metadata:
10+
name: leader-election-operator-1
11+
spec:
12+
serviceAccountName: leader-election-operator
13+
containers:
14+
- name: operator
15+
image: leader-election-operator
16+
imagePullPolicy: Never
17+
18+
---
19+
apiVersion: rbac.authorization.k8s.io/v1
20+
kind: ClusterRoleBinding
21+
metadata:
22+
name: operator-admin
23+
subjects:
24+
- kind: ServiceAccount
25+
name: leader-election-operator
26+
roleRef:
27+
kind: ClusterRole
28+
name: leader-election-operator
29+
apiGroup: ""
30+
31+
---
32+
apiVersion: rbac.authorization.k8s.io/v1
33+
kind: ClusterRole
34+
metadata:
35+
name: leader-election-operator
36+
rules:
37+
- apiGroups:
38+
- "apiextensions.k8s.io"
39+
resources:
40+
- customresourcedefinitions
41+
verbs:
42+
- '*'
43+
- apiGroups:
44+
- "sample.javaoperatorsdk"
45+
resources:
46+
- leaderelectiontestcustomresources
47+
- leaderelectiontestcustomresources/status
48+
verbs:
49+
- '*'
50+
- apiGroups:
51+
- "coordination.k8s.io"
52+
resources:
53+
- "leases"
54+
verbs:
55+
- '*'
56+

sample-operators/leader-election/pom.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@
5151
<version>${project.version}</version>
5252
<scope>test</scope>
5353
</dependency>
54+
<dependency>
55+
<groupId>org.junit.jupiter</groupId>
56+
<artifactId>junit-jupiter-params</artifactId>
57+
</dependency>
5458
</dependencies>
5559
<build>
5660
<plugins>
@@ -75,4 +79,4 @@
7579
</plugins>
7680
</build>
7781

78-
</project>
82+
</project>

sample-operators/leader-election/src/main/java/io/javaoperatorsdk/operator/sample/LeaderElectionTestOperator.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,17 @@ public static void main(String[] args) {
1717

1818
log.info("Starting operator with identity: {}", identity);
1919

20+
LeaderElectionConfiguration leaderElectionConfiguration =
21+
namespace == null
22+
? new LeaderElectionConfiguration("leader-election-test")
23+
: new LeaderElectionConfiguration("leader-election-test", namespace, identity);
24+
2025
var client = new KubernetesClientBuilder().build();
21-
Operator operator = new Operator(client,
22-
c -> c.withLeaderElectionConfiguration(
23-
new LeaderElectionConfiguration("leader-election-test", namespace, identity)));
26+
Operator operator =
27+
new Operator(client, c -> c.withLeaderElectionConfiguration(leaderElectionConfiguration));
2428

2529
operator.register(new LeaderElectionTestReconciler(identity));
2630
operator.installShutdownHook();
2731
operator.start();
2832
}
29-
3033
}

sample-operators/leader-election/src/test/java/io/javaoperatorsdk/operator/sample/LeaderElectionE2E.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@
1313

1414
import org.junit.jupiter.api.AfterEach;
1515
import org.junit.jupiter.api.BeforeEach;
16-
import org.junit.jupiter.api.Test;
1716
import org.junit.jupiter.api.condition.EnabledIfSystemProperty;
17+
import org.junit.jupiter.params.ParameterizedTest;
18+
import org.junit.jupiter.params.provider.ValueSource;
1819
import org.slf4j.Logger;
1920
import org.slf4j.LoggerFactory;
2021

@@ -46,14 +47,15 @@ class LeaderElectionE2E {
4647
private String namespace;
4748
private KubernetesClient client;
4849

49-
@Test
50+
@ParameterizedTest
51+
@ValueSource(strings = {"namespace-inferred-", ""})
5052
// not for local mode by design
5153
@EnabledIfSystemProperty(named = "test.deployment", matches = "remote")
52-
void otherInstancesTakesOverWhenSteppingDown() {
54+
void otherInstancesTakesOverWhenSteppingDown(String yamlFilePrefix) {
5355
log.info("Applying custom resource");
5456
applyCustomResource();
5557
log.info("Deploying operator instances");
56-
deployOperatorsInOrder();
58+
deployOperatorsInOrder(yamlFilePrefix);
5759

5860
log.info("Awaiting custom resource reconciliations");
5961
await().pollDelay(Duration.ofSeconds(MINIMAL_SECONDS_FOR_RENEWAL))
@@ -130,16 +132,16 @@ void tearDown() {
130132
.untilAsserted(() -> assertThat(client.namespaces().withName(namespace).get()).isNull());
131133
}
132134

133-
private void deployOperatorsInOrder() {
135+
private void deployOperatorsInOrder(String yamlFilePrefix) {
134136
log.info("Installing 1st instance");
135-
applyResources("k8s/operator.yaml");
137+
applyResources("k8s/" + yamlFilePrefix + "operator.yaml");
136138
await().atMost(Duration.ofSeconds(POD_STARTUP_TIMEOUT)).untilAsserted(() -> {
137139
var pod = client.pods().inNamespace(namespace).withName(OPERATOR_1_POD_NAME).get();
138140
assertThat(pod.getStatus().getContainerStatuses().get(0).getReady()).isTrue();
139141
});
140142

141143
log.info("Installing 2nd instance");
142-
applyResources("k8s/operator-instance-2.yaml");
144+
applyResources("k8s/" + yamlFilePrefix + "operator-instance-2.yaml");
143145
await().atMost(Duration.ofSeconds(POD_STARTUP_TIMEOUT)).untilAsserted(() -> {
144146
var pod = client.pods().inNamespace(namespace).withName(OPERATOR_2_POD_NAME).get();
145147
assertThat(pod.getStatus().getContainerStatuses().get(0).getReady()).isTrue();

0 commit comments

Comments
 (0)