Skip to content

Commit 536f3ed

Browse files
committed
Add e2e test to cover optional values
1 parent 56a69be commit 536f3ed

File tree

5 files changed

+97
-12
lines changed

5 files changed

+97
-12
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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
11+
env:
12+
- name: POD_NAME
13+
valueFrom:
14+
fieldRef:
15+
fieldPath: metadata.name
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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+
env:
18+
- name: POD_NAME
19+
valueFrom:
20+
fieldRef:
21+
fieldPath: metadata.name
22+
23+
---
24+
apiVersion: rbac.authorization.k8s.io/v1
25+
kind: ClusterRoleBinding
26+
metadata:
27+
name: operator-admin
28+
subjects:
29+
- kind: ServiceAccount
30+
name: leader-election-operator
31+
roleRef:
32+
kind: ClusterRole
33+
name: leader-election-operator
34+
apiGroup: ""
35+
36+
---
37+
apiVersion: rbac.authorization.k8s.io/v1
38+
kind: ClusterRole
39+
metadata:
40+
name: leader-election-operator
41+
rules:
42+
- apiGroups:
43+
- "apiextensions.k8s.io"
44+
resources:
45+
- customresourcedefinitions
46+
verbs:
47+
- '*'
48+
- apiGroups:
49+
- "sample.javaoperatorsdk"
50+
resources:
51+
- leaderelectiontestcustomresources
52+
- leaderelectiontestcustomresources/status
53+
verbs:
54+
- '*'
55+
- apiGroups:
56+
- "coordination.k8s.io"
57+
resources:
58+
- "leases"
59+
verbs:
60+
- '*'
61+

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)