Skip to content

Commit 1a4cdea

Browse files
committed
test: add missing tests for StatefulSet with VolumeClaimTemplates for SSABasedGenericKubernetesResourceMatcher
Signed-off-by: David Sondermann <[email protected]>
1 parent 6f81e16 commit 1a4cdea

10 files changed

+366
-14
lines changed

Diff for: operator-framework-core/pom.xml

+5
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,11 @@
101101
<artifactId>junit-jupiter-engine</artifactId>
102102
<scope>test</scope>
103103
</dependency>
104+
<dependency>
105+
<groupId>org.junit.jupiter</groupId>
106+
<artifactId>junit-jupiter-params</artifactId>
107+
<scope>test</scope>
108+
</dependency>
104109
<dependency>
105110
<groupId>org.mockito</groupId>
106111
<artifactId>mockito-core</artifactId>

Diff for: operator-framework-core/src/test/java/io/javaoperatorsdk/operator/processing/dependent/kubernetes/SSABasedGenericKubernetesResourceMatcherTest.java

+39-14
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,42 @@
11
package io.javaoperatorsdk.operator.processing.dependent.kubernetes;
22

3-
import java.util.Map;
4-
5-
import org.junit.jupiter.api.BeforeEach;
6-
import org.junit.jupiter.api.Test;
7-
83
import io.fabric8.kubernetes.api.model.ConfigMap;
94
import io.fabric8.kubernetes.api.model.HasMetadata;
105
import io.fabric8.kubernetes.api.model.apps.Deployment;
6+
import io.fabric8.kubernetes.api.model.apps.StatefulSet;
117
import io.javaoperatorsdk.operator.MockKubernetesClient;
128
import io.javaoperatorsdk.operator.ReconcilerUtils;
139
import io.javaoperatorsdk.operator.api.config.ConfigurationService;
1410
import io.javaoperatorsdk.operator.api.config.ControllerConfiguration;
1511
import io.javaoperatorsdk.operator.api.reconciler.Context;
12+
import org.junit.jupiter.api.BeforeEach;
13+
import org.junit.jupiter.api.Test;
14+
import org.junit.jupiter.params.ParameterizedTest;
15+
import org.junit.jupiter.params.provider.ValueSource;
16+
17+
import java.util.Map;
1618

1719
import static org.assertj.core.api.Assertions.assertThat;
1820
import static org.mockito.Mockito.mock;
1921
import static org.mockito.Mockito.when;
2022

2123
class SSABasedGenericKubernetesResourceMatcherTest {
2224

23-
Context<?> mockedContext = mock(Context.class);
25+
private final Context<?> mockedContext = mock();
2426

25-
SSABasedGenericKubernetesResourceMatcher<HasMetadata> matcher =
26-
new SSABasedGenericKubernetesResourceMatcher<>();
27+
private final SSABasedGenericKubernetesResourceMatcher<HasMetadata> matcher =
28+
SSABasedGenericKubernetesResourceMatcher.getInstance();
2729

2830
@BeforeEach
2931
@SuppressWarnings("unchecked")
3032
void setup() {
31-
var controllerConfiguration = mock(ControllerConfiguration.class);
32-
when(controllerConfiguration.fieldManager()).thenReturn("controller");
33-
var configurationService = mock(ConfigurationService.class);
34-
3533
final var client = MockKubernetesClient.client(HasMetadata.class);
3634
when(mockedContext.getClient()).thenReturn(client);
3735

36+
final var configurationService = mock(ConfigurationService.class);
37+
final var controllerConfiguration = mock(ControllerConfiguration.class);
3838
when(controllerConfiguration.getConfigurationService()).thenReturn(configurationService);
39+
when(controllerConfiguration.fieldManager()).thenReturn("controller");
3940
when(mockedContext.getControllerConfiguration()).thenReturn(controllerConfiguration);
4041
}
4142

@@ -116,9 +117,33 @@ void addedLabelInDesiredMakesMatchFail() {
116117
assertThat(matcher.matches(actualConfigMap, desiredConfigMap, mockedContext)).isFalse();
117118
}
118119

119-
private <R> R loadResource(String fileName, Class<R> clazz) {
120+
@ParameterizedTest
121+
@ValueSource(strings = {"sample-sts-volumeclaimtemplates-desired.yaml",
122+
"sample-sts-volumeclaimtemplates-desired-with-status.yaml",
123+
"sample-sts-volumeclaimtemplates-desired-with-volumemode.yaml"})
124+
void testSanitizeState_statefulSetWithVolumeClaims(String desiredResourceFileName) {
125+
var desiredStatefulSet = loadResource(desiredResourceFileName, StatefulSet.class);
126+
var actualStatefulSet = loadResource("sample-sts-volumeclaimtemplates.yaml",
127+
StatefulSet.class);
128+
129+
assertThat(matcher.matches(actualStatefulSet, desiredStatefulSet, mockedContext)).isTrue();
130+
}
131+
132+
@ParameterizedTest
133+
@ValueSource(strings = {"sample-sts-volumeclaimtemplates-desired-add.yaml",
134+
"sample-sts-volumeclaimtemplates-desired-update.yaml",
135+
"sample-sts-volumeclaimtemplates-desired-with-status-mismatch.yaml",
136+
"sample-sts-volumeclaimtemplates-desired-with-volumemode-mismatch.yaml"})
137+
void testSanitizeState_statefulSetWithVolumeClaims_withMismatch(String desiredResourceFileName) {
138+
var desiredStatefulSet = loadResource(desiredResourceFileName, StatefulSet.class);
139+
var actualStatefulSet = loadResource("sample-sts-volumeclaimtemplates.yaml",
140+
StatefulSet.class);
141+
142+
assertThat(matcher.matches(actualStatefulSet, desiredStatefulSet, mockedContext)).isFalse();
143+
}
144+
145+
private static <R> R loadResource(String fileName, Class<R> clazz) {
120146
return ReconcilerUtils.loadYaml(clazz, SSABasedGenericKubernetesResourceMatcherTest.class,
121147
fileName);
122148
}
123-
124149
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# desired StatefulSet with a VolumeClaimTemplate with an additional VolumeClaimTemplate
2+
apiVersion: apps/v1
3+
kind: StatefulSet
4+
metadata:
5+
name: "test"
6+
spec:
7+
replicas: 1
8+
selector:
9+
matchLabels:
10+
app: test-app
11+
serviceName: "nginx-service"
12+
template:
13+
metadata:
14+
labels:
15+
app: test-app
16+
spec:
17+
containers:
18+
- name: nginx
19+
image: nginx:1.17.0
20+
ports:
21+
- containerPort: 80
22+
volumeMounts:
23+
- name: persistent-storage
24+
mountPath: /usr/share/nginx/html
25+
volumeClaimTemplates:
26+
- metadata:
27+
name: persistent-storage
28+
spec:
29+
accessModes:
30+
- ReadWriteOnce
31+
resources:
32+
requests:
33+
storage: 1Gi
34+
storageClassName: standard
35+
- metadata:
36+
name: persistent-storage-new
37+
spec:
38+
accessModes:
39+
- ReadWriteOnce
40+
resources:
41+
requests:
42+
storage: 10Gi
43+
storageClassName: standard
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# desired StatefulSet with a VolumeClaimTemplate with an updated VolumeClaimTemplate
2+
apiVersion: apps/v1
3+
kind: StatefulSet
4+
metadata:
5+
name: "test"
6+
spec:
7+
replicas: 1
8+
selector:
9+
matchLabels:
10+
app: test-app
11+
serviceName: "nginx-service"
12+
template:
13+
metadata:
14+
labels:
15+
app: test-app
16+
spec:
17+
containers:
18+
- name: nginx
19+
image: nginx:1.17.0
20+
ports:
21+
- containerPort: 80
22+
volumeMounts:
23+
- name: persistent-storage
24+
mountPath: /usr/share/nginx/html
25+
volumeClaimTemplates:
26+
- metadata:
27+
name: persistent-storage
28+
spec:
29+
accessModes:
30+
- ReadWriteOnce
31+
resources:
32+
requests:
33+
storage: 2Gi
34+
storageClassName: standard
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# desired StatefulSet with a VolumeClaimTemplate with a mismatching spec.volumeClaimTemplates.spec.status
2+
apiVersion: apps/v1
3+
kind: StatefulSet
4+
metadata:
5+
name: "test"
6+
spec:
7+
replicas: 1
8+
selector:
9+
matchLabels:
10+
app: test-app
11+
serviceName: "nginx-service"
12+
template:
13+
metadata:
14+
labels:
15+
app: test-app
16+
spec:
17+
containers:
18+
- name: nginx
19+
image: nginx:1.17.0
20+
ports:
21+
- containerPort: 80
22+
volumeMounts:
23+
- name: persistent-storage
24+
mountPath: /usr/share/nginx/html
25+
volumeClaimTemplates:
26+
- metadata:
27+
name: persistent-storage
28+
spec:
29+
accessModes:
30+
- ReadWriteOnce
31+
resources:
32+
requests:
33+
storage: 1Gi
34+
storageClassName: standard
35+
status:
36+
phase: Bound
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# desired StatefulSet with a VolumeClaimTemplate with a matching spec.volumeClaimTemplates.spec.status
2+
apiVersion: apps/v1
3+
kind: StatefulSet
4+
metadata:
5+
name: "test"
6+
spec:
7+
replicas: 1
8+
selector:
9+
matchLabels:
10+
app: test-app
11+
serviceName: "nginx-service"
12+
template:
13+
metadata:
14+
labels:
15+
app: test-app
16+
spec:
17+
containers:
18+
- name: nginx
19+
image: nginx:1.17.0
20+
ports:
21+
- containerPort: 80
22+
volumeMounts:
23+
- name: persistent-storage
24+
mountPath: /usr/share/nginx/html
25+
volumeClaimTemplates:
26+
- metadata:
27+
name: persistent-storage
28+
spec:
29+
accessModes:
30+
- ReadWriteOnce
31+
resources:
32+
requests:
33+
storage: 1Gi
34+
storageClassName: standard
35+
status:
36+
phase: Pending
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# desired StatefulSet with a VolumeClaimTemplate with a mismatching spec.volumeClaimTemplates.spec.volumeMode
2+
apiVersion: apps/v1
3+
kind: StatefulSet
4+
metadata:
5+
name: "test"
6+
spec:
7+
replicas: 1
8+
selector:
9+
matchLabels:
10+
app: test-app
11+
serviceName: "nginx-service"
12+
template:
13+
metadata:
14+
labels:
15+
app: test-app
16+
spec:
17+
containers:
18+
- name: nginx
19+
image: nginx:1.17.0
20+
ports:
21+
- containerPort: 80
22+
volumeMounts:
23+
- name: persistent-storage
24+
mountPath: /usr/share/nginx/html
25+
volumeClaimTemplates:
26+
- metadata:
27+
name: persistent-storage
28+
spec:
29+
accessModes:
30+
- ReadWriteOnce
31+
resources:
32+
requests:
33+
storage: 1Gi
34+
storageClassName: standard
35+
volumeMode: Block
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# desired StatefulSet with a VolumeClaimTemplate with a matching spec.volumeClaimTemplates.spec.volumeMode
2+
apiVersion: apps/v1
3+
kind: StatefulSet
4+
metadata:
5+
name: "test"
6+
spec:
7+
replicas: 1
8+
selector:
9+
matchLabels:
10+
app: test-app
11+
serviceName: "nginx-service"
12+
template:
13+
metadata:
14+
labels:
15+
app: test-app
16+
spec:
17+
containers:
18+
- name: nginx
19+
image: nginx:1.17.0
20+
ports:
21+
- containerPort: 80
22+
volumeMounts:
23+
- name: persistent-storage
24+
mountPath: /usr/share/nginx/html
25+
volumeClaimTemplates:
26+
- metadata:
27+
name: persistent-storage
28+
spec:
29+
accessModes:
30+
- ReadWriteOnce
31+
resources:
32+
requests:
33+
storage: 1Gi
34+
storageClassName: standard
35+
volumeMode: Filesystem
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# desired StatefulSet with a VolumeClaimTemplate
2+
apiVersion: apps/v1
3+
kind: StatefulSet
4+
metadata:
5+
name: "test"
6+
spec:
7+
replicas: 1
8+
selector:
9+
matchLabels:
10+
app: test-app
11+
serviceName: "nginx-service"
12+
template:
13+
metadata:
14+
labels:
15+
app: test-app
16+
spec:
17+
containers:
18+
- name: nginx
19+
image: nginx:1.17.0
20+
ports:
21+
- containerPort: 80
22+
volumeMounts:
23+
- name: persistent-storage
24+
mountPath: /usr/share/nginx/html
25+
volumeClaimTemplates:
26+
- metadata:
27+
name: persistent-storage
28+
spec:
29+
accessModes:
30+
- ReadWriteOnce
31+
resources:
32+
requests:
33+
storage: 1Gi
34+
storageClassName: standard

0 commit comments

Comments
 (0)