|
14 | 14 | import oracle.kubernetes.utils.TestUtils;
|
15 | 15 | import org.hamcrest.Description;
|
16 | 16 | import org.hamcrest.Matcher;
|
| 17 | +import org.hamcrest.TypeSafeMatcher; |
17 | 18 | import org.junit.jupiter.api.AfterEach;
|
18 | 19 | import org.junit.jupiter.api.BeforeEach;
|
19 | 20 | import org.junit.jupiter.params.ParameterizedTest;
|
20 | 21 | import org.junit.jupiter.params.provider.Arguments;
|
21 | 22 | import org.junit.jupiter.params.provider.MethodSource;
|
22 | 23 |
|
| 24 | +import static oracle.kubernetes.operator.helpers.VersionCheckTest.FailsWithMatcher.failsWith; |
23 | 25 | import static oracle.kubernetes.operator.helpers.VersionCheckTest.TestType.LOG_MSG_TEST;
|
24 | 26 | import static oracle.kubernetes.operator.helpers.VersionCheckTest.TestType.VERSION_TEST;
|
| 27 | +import static oracle.kubernetes.operator.helpers.VersionCheckTest.TestType.VERSION_TOO_HIGH_TEST; |
25 | 28 | import static oracle.kubernetes.operator.helpers.VersionCheckTest.VersionMatcher.returnsVersion;
|
26 | 29 | import static oracle.kubernetes.operator.logging.MessageKeys.K8S_VERSION_CHECK;
|
27 | 30 | import static oracle.kubernetes.operator.logging.MessageKeys.K8S_VERSION_CHECK_FAILURE;
|
28 | 31 | import static oracle.kubernetes.operator.logging.MessageKeys.K8S_VERSION_TOO_LOW;
|
29 |
| -import static oracle.kubernetes.utils.LogMatcher.containsInfo; |
30 | 32 | import static oracle.kubernetes.utils.LogMatcher.containsWarning;
|
| 33 | +import static org.hamcrest.Matchers.allOf; |
| 34 | +import static org.hamcrest.Matchers.instanceOf; |
31 | 35 | import static org.hamcrest.junit.MatcherAssert.assertThat;
|
32 | 36 |
|
33 | 37 | class VersionCheckTest {
|
@@ -62,8 +66,8 @@ private static Stream<Arguments> getTestParams() {
|
62 | 66 | Arguments.of(VERSION_TEST, "1", "21", "10", returnsVersion(1, 21), ignoring(K8S_VERSION_CHECK)),
|
63 | 67 | Arguments.of(VERSION_TEST, "1", "22", "5", returnsVersion(1, 22), ignoring(K8S_VERSION_CHECK)),
|
64 | 68 | Arguments.of(VERSION_TEST, "1", "23", "7", returnsVersion(1, 23), ignoring(K8S_VERSION_CHECK)),
|
65 |
| - Arguments.of(VERSION_TEST, "2", "7", "", returnsVersion(2, 7), ignoring(K8S_VERSION_CHECK)), |
66 |
| - Arguments.of(LOG_MSG_TEST, "2", "", "", containsInfo(K8S_VERSION_CHECK), noIgnores()) |
| 69 | + Arguments.of(VERSION_TOO_HIGH_TEST, "1", "25", "", null, ignoring(K8S_VERSION_CHECK)), |
| 70 | + Arguments.of(VERSION_TOO_HIGH_TEST, "2", "7", "", null, ignoring(K8S_VERSION_CHECK)) |
67 | 71 | );
|
68 | 72 | }
|
69 | 73 |
|
@@ -134,6 +138,12 @@ void runTest(List<LogRecord> logRecords, Matcher matcher) {
|
134 | 138 | void runTest(List<LogRecord> logRecords, Matcher matcher) {
|
135 | 139 | assertThat(HealthCheckHelper.performK8sVersionCheck(), matcher);
|
136 | 140 | }
|
| 141 | + }, |
| 142 | + VERSION_TOO_HIGH_TEST { |
| 143 | + @Override |
| 144 | + void runTest(List<LogRecord> logRecords, Matcher matcher) { |
| 145 | + assertThat(HealthCheckHelper::performK8sVersionCheck, failsWith(IllegalStateException.class)); |
| 146 | + } |
137 | 147 | };
|
138 | 148 |
|
139 | 149 | abstract void runTest(List<LogRecord> logRecords, Matcher matcher);
|
@@ -172,4 +182,42 @@ public void describeTo(Description description) {
|
172 | 182 | describe(description, expectedMajor, expectedMinor);
|
173 | 183 | }
|
174 | 184 | }
|
| 185 | + |
| 186 | + @FunctionalInterface |
| 187 | + public interface IThrowingRunnable<E extends Throwable> { |
| 188 | + void run() throws E; |
| 189 | + } |
| 190 | + |
| 191 | + static class FailsWithMatcher<E extends Throwable> extends TypeSafeMatcher<IThrowingRunnable<E>> { |
| 192 | + private final Matcher<? super E> matcher; |
| 193 | + |
| 194 | + private FailsWithMatcher(final Matcher<? super E> matcher) { |
| 195 | + this.matcher = matcher; |
| 196 | + } |
| 197 | + |
| 198 | + public static <E extends Throwable> Matcher<IThrowingRunnable<E>> failsWith(final Class<E> throwableType) { |
| 199 | + return new FailsWithMatcher<>(instanceOf(throwableType)); |
| 200 | + } |
| 201 | + |
| 202 | + public static <E extends Throwable> Matcher<IThrowingRunnable<E>> failsWith( |
| 203 | + final Class<E> throwableType, final Matcher<? super E> throwableMatcher) { |
| 204 | + return new FailsWithMatcher<>(allOf(instanceOf(throwableType), throwableMatcher)); |
| 205 | + } |
| 206 | + |
| 207 | + @Override |
| 208 | + protected boolean matchesSafely(final IThrowingRunnable<E> runnable) { |
| 209 | + try { |
| 210 | + runnable.run(); |
| 211 | + return false; |
| 212 | + } catch (final Throwable ex) { |
| 213 | + return matcher.matches(ex); |
| 214 | + } |
| 215 | + } |
| 216 | + |
| 217 | + @Override |
| 218 | + public void describeTo(final Description description) { |
| 219 | + description.appendText("fails with ").appendDescriptionOf(matcher); |
| 220 | + } |
| 221 | + |
| 222 | + } |
175 | 223 | }
|
0 commit comments