Disable AppId tests on Vault 1.12 and newer.

Closes gh-798
This commit is contained in:
Mark Paluch
2023-06-19 11:46:51 +02:00
parent 02e4e0ca5d
commit 0ff82934db
3 changed files with 90 additions and 10 deletions

View File

@@ -20,6 +20,7 @@ import java.util.Map;
import org.junit.jupiter.api.BeforeEach;
import org.springframework.vault.util.DisabledOnVaultVersion;
import org.springframework.vault.util.IntegrationTestSupport;
/**
@@ -27,6 +28,7 @@ import org.springframework.vault.util.IntegrationTestSupport;
*
* @author Mark Paluch
*/
@DisabledOnVaultVersion("1.12")
public abstract class AppIdAuthenticationIntegrationTestBase extends IntegrationTestSupport {
@BeforeEach

View File

@@ -0,0 +1,54 @@
/*
* Copyright 2019-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.vault.util;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.junit.jupiter.api.extension.ExtendWith;
/**
* Annotation to guard a {@code @Test} to skip on or after the specified Vault version.
* <p>
* When applied at the class level, all test methods within that class are automatically
* disabled as well.
*
* <p>
* When applied at the method level, the presence of this annotation does not prevent the
* test class from being instantiated. Rather, it prevents the execution of the test
* method and method-level lifecycle callbacks such as {@code @BeforeEach} methods,
* {@code @AfterEach} methods, and corresponding extension APIs.
*
* @author Mark Paluch
* @see VaultVersionExtension
*/
@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@ExtendWith(VaultVersionExtension.class)
public @interface DisabledOnVaultVersion {
/**
* Minimum version to skip a test.
*/
String value();
}

View File

@@ -24,7 +24,8 @@ import org.junit.platform.commons.util.AnnotationUtils;
/**
* This is an {@link org.junit.jupiter.api.extension.ExecutionCondition} that supports the
* {@link RequiresVaultVersion @RequiresVaultVersion} annotation.
* {@link RequiresVaultVersion @RequiresVaultVersion} and
* {@link DisabledOnVaultVersion @DisabledOnVaultVersion} annotations.
*
* @author Mark Paluch
* @see RequiresVaultVersion
@@ -35,15 +36,18 @@ class VaultVersionExtension implements ExecutionCondition {
private static final ExtensionContext.Namespace VAULT = ExtensionContext.Namespace.create("vault.version");
private static final ConditionEvaluationResult ENABLED_BY_DEFAULT = ConditionEvaluationResult
.enabled("@VaultVersion is not present");
.enabled("@VaultVersion is not present");
@Override
public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) {
Optional<RequiresVaultVersion> optional = AnnotationUtils.findAnnotation(context.getElement(),
Optional<RequiresVaultVersion> required = AnnotationUtils.findAnnotation(context.getElement(),
RequiresVaultVersion.class);
if (!optional.isPresent()) {
Optional<DisabledOnVaultVersion> disabled = AnnotationUtils.findAnnotation(context.getElement(),
DisabledOnVaultVersion.class);
if (required.isEmpty() && disabled.isEmpty()) {
return ENABLED_BY_DEFAULT;
}
@@ -56,17 +60,37 @@ class VaultVersionExtension implements ExecutionCondition {
return initializer.prepare().getVersion();
}, Version.class);
RequiresVaultVersion requiredVersion = optional.get();
if (required.isPresent()) {
Version required = Version.parse(requiredVersion.value());
Version requiredVersion = Version.parse(required.get().value());
if (runningVersion.isGreaterThanOrEqualTo(requiredVersion)) {
return ConditionEvaluationResult
.enabled(String.format("Test is enabled, @VaultVersion(%s) is met with Vault running version %s",
requiredVersion, runningVersion));
}
if (runningVersion.isGreaterThanOrEqualTo(required)) {
return ConditionEvaluationResult
.enabled(String.format("@VaultVersion check passed current Vault version is %s", runningVersion));
.disabled(String.format("Test is disabled, @VaultVersion(%s) is not met with Vault running version %s",
requiredVersion, runningVersion));
}
return ConditionEvaluationResult.disabled(String
.format("@VaultVersion requires since version %s, current Vault version is %s", required, runningVersion));
if (disabled.isPresent()) {
Version disabledVersion = Version.parse(disabled.get().value());
if (runningVersion.isGreaterThanOrEqualTo(disabledVersion)) {
return ConditionEvaluationResult.disabled(String.format(
"Test is disabled, @DisabledOnVaultVersion(%s) is met with Vault running version %s",
disabledVersion, runningVersion));
}
return ConditionEvaluationResult.enabled(String.format(String.format(
"Test is enabled, @DisabledOnVaultVersion(%s) is not met with Vault running version %s",
disabledVersion, runningVersion)));
}
return ENABLED_BY_DEFAULT;
}
}