Skip to content

Commit 0a4d841

Browse files
authored
[FLINK-39220] Add unit tests for ConditionsUtils (#1068)
Signed-off-by: Harshit Gupta <hg2t4e@gmail.com>
1 parent 93b6960 commit 0a4d841

1 file changed

Lines changed: 216 additions & 0 deletions

File tree

Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.flink.kubernetes.operator.api.utils;
19+
20+
import org.apache.flink.api.common.JobStatus;
21+
import org.apache.flink.kubernetes.operator.api.status.FlinkDeploymentStatus;
22+
import org.apache.flink.kubernetes.operator.api.status.JobManagerDeploymentStatus;
23+
24+
import io.fabric8.kubernetes.api.model.Condition;
25+
import org.junit.jupiter.api.Test;
26+
import org.junit.jupiter.params.ParameterizedTest;
27+
import org.junit.jupiter.params.provider.Arguments;
28+
import org.junit.jupiter.params.provider.MethodSource;
29+
30+
import java.time.Instant;
31+
import java.util.Collections;
32+
import java.util.List;
33+
import java.util.stream.Stream;
34+
35+
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
36+
import static org.junit.jupiter.api.Assertions.assertEquals;
37+
import static org.junit.jupiter.api.Assertions.assertNotEquals;
38+
import static org.junit.jupiter.api.Assertions.assertNotNull;
39+
import static org.junit.jupiter.api.Assertions.assertNull;
40+
41+
/** Test for {@link ConditionsUtils}. */
42+
public class ConditionsUtilsTest {
43+
44+
private static Stream<Arguments> updateLastTransitionTimeParams() {
45+
String existingConditionLastTransitionTime = Instant.now().toString();
46+
47+
return Stream.of(
48+
// Test Empty Conditions
49+
Arguments.of(
50+
Collections.emptyList(),
51+
new Condition(
52+
"lastTransitionTime", "message", 0L, "reason", "status", "type"),
53+
new Condition(
54+
"lastTransitionTime", "message", 0L, "reason", "status", "type"),
55+
false),
56+
57+
// Test Correct Update
58+
Arguments.of(
59+
List.of(
60+
new Condition(
61+
existingConditionLastTransitionTime,
62+
"message",
63+
0L,
64+
"reason",
65+
"status",
66+
"type")),
67+
new Condition(
68+
existingConditionLastTransitionTime,
69+
"message",
70+
0L,
71+
"reason",
72+
"status",
73+
"type"),
74+
new Condition(
75+
"lastTransitionTime", "message", 0L, "reason", "status", "type"),
76+
true),
77+
78+
// Test Status Unchanged
79+
Arguments.of(
80+
List.of(
81+
new Condition(
82+
existingConditionLastTransitionTime,
83+
"message",
84+
0L,
85+
"reason",
86+
"status",
87+
"type")),
88+
new Condition(
89+
existingConditionLastTransitionTime,
90+
"message",
91+
0L,
92+
"reason",
93+
"status",
94+
"type"),
95+
new Condition(
96+
existingConditionLastTransitionTime,
97+
"message",
98+
0L,
99+
"reason",
100+
"status",
101+
"type"),
102+
true),
103+
104+
// Test Status Changed
105+
Arguments.of(
106+
List.of(
107+
new Condition(
108+
existingConditionLastTransitionTime,
109+
"message",
110+
0L,
111+
"reason",
112+
"status1",
113+
"type")),
114+
new Condition(
115+
existingConditionLastTransitionTime,
116+
"message",
117+
0L,
118+
"reason",
119+
"status2",
120+
"type"),
121+
new Condition(
122+
existingConditionLastTransitionTime,
123+
"message",
124+
0L,
125+
"reason",
126+
"status2",
127+
"type"),
128+
false),
129+
130+
// Test Multiple Conditions with Multiple Types
131+
Arguments.of(
132+
List.of(
133+
new Condition("time1", "message", 0L, "reason", "status", "type1"),
134+
new Condition("time2", "message", 0L, "reason", "status", "type1"),
135+
new Condition("time3", "message", 0L, "reason", "status", "type2")),
136+
new Condition("time1", "message", 0L, "reason", "status", "type1"),
137+
new Condition(
138+
"lastTransitionTime", "message", 0L, "reason", "status", "type1"),
139+
true),
140+
141+
// Test No Matching Type
142+
Arguments.of(
143+
List.of(
144+
new Condition(
145+
existingConditionLastTransitionTime,
146+
"message",
147+
0L,
148+
"reason",
149+
"status",
150+
"type1")),
151+
new Condition(
152+
existingConditionLastTransitionTime,
153+
"message",
154+
0L,
155+
"reason",
156+
"status",
157+
"type2"),
158+
new Condition(
159+
"lastTransitionTime", "message", 0L, "reason", "status", "type2"),
160+
false));
161+
}
162+
163+
@ParameterizedTest
164+
@MethodSource("updateLastTransitionTimeParams")
165+
void testUpdateLastTransitionTime(
166+
List<Condition> conditions, Condition expected, Condition actual, boolean equals) {
167+
ConditionsUtils.updateLastTransitionTime(conditions, actual);
168+
169+
assertEquals(expected.getMessage(), actual.getMessage());
170+
assertEquals(expected.getObservedGeneration(), actual.getObservedGeneration());
171+
assertEquals(expected.getReason(), actual.getReason());
172+
assertEquals(expected.getStatus(), actual.getStatus());
173+
assertEquals(expected.getType(), actual.getType());
174+
175+
if (equals) {
176+
assertEquals(expected.getLastTransitionTime(), actual.getLastTransitionTime());
177+
} else {
178+
assertNotEquals(expected.getLastTransitionTime(), actual.getLastTransitionTime());
179+
}
180+
}
181+
182+
@Test
183+
void testNullUpdateLastTransitionTime() {
184+
List<Condition> conditions = null;
185+
Condition condition = null;
186+
187+
assertDoesNotThrow(() -> ConditionsUtils.updateLastTransitionTime(conditions, condition));
188+
189+
assertNull(condition);
190+
}
191+
192+
@Test
193+
void testCreateApplicationModeCondition() {
194+
Condition condition = ConditionsUtils.createApplicationModeCondition(JobStatus.RUNNING);
195+
196+
assertNotNull(condition);
197+
assertEquals(FlinkDeploymentStatus.CONDITION_TYPE_RUNNING, condition.getType());
198+
assertEquals("True", condition.getStatus());
199+
assertEquals("Running", condition.getReason());
200+
assertEquals("Job status RUNNING", condition.getMessage());
201+
}
202+
203+
@Test
204+
void testCreateSessionModeCondition() {
205+
Condition condition =
206+
ConditionsUtils.createSessionModeCondition(JobManagerDeploymentStatus.READY);
207+
208+
assertNotNull(condition);
209+
assertEquals(FlinkDeploymentStatus.CONDITION_TYPE_RUNNING, condition.getType());
210+
assertEquals("True", condition.getStatus());
211+
assertEquals("JobManagerReady", condition.getReason());
212+
assertEquals(
213+
"JobManager is running and ready to receive REST API calls",
214+
condition.getMessage());
215+
}
216+
}

0 commit comments

Comments
 (0)