Test:support environment variable metadata test (#710)

pull/719/head
lingxiao,wu 2 years ago committed by GitHub
parent 4ea885d18c
commit 631e2f1d45
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -6,3 +6,4 @@
- [Optimize:optimize spring annotation.](https://github.com/Tencent/spring-cloud-tencent/pull/688) - [Optimize:optimize spring annotation.](https://github.com/Tencent/spring-cloud-tencent/pull/688)
- [docs:update PR template.](https://github.com/Tencent/spring-cloud-tencent/pull/691) - [docs:update PR template.](https://github.com/Tencent/spring-cloud-tencent/pull/691)
- [Code optimize & add junit tests.](https://github.com/Tencent/spring-cloud-tencent/pull/703) - [Code optimize & add junit tests.](https://github.com/Tencent/spring-cloud-tencent/pull/703)
- [Test:support environment variable metadata test](https://github.com/Tencent/spring-cloud-tencent/pull/710)

@ -99,6 +99,12 @@
<artifactId>spring-boot-actuator-autoconfigure</artifactId> <artifactId>spring-boot-actuator-autoconfigure</artifactId>
<optional>true</optional> <optional>true</optional>
</dependency> </dependency>
<dependency>
<groupId>uk.org.webcompere</groupId>
<artifactId>system-stubs-junit4</artifactId>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>
</project> </project>

@ -107,7 +107,8 @@ public class StaticMetadataManager {
String value = entry.getValue(); String value = entry.getValue();
if (StringUtils.isNotBlank(key) if (StringUtils.isNotBlank(key)
&& (key.startsWith(ENV_METADATA_PREFIX) || key.equals(ENV_TRAFFIC_CONTENT_RAW_TRANSHEADERS)) && (key.startsWith(ENV_METADATA_PREFIX) || key.equals(ENV_TRAFFIC_CONTENT_RAW_TRANSHEADERS))
&& !key.equals(ENV_METADATA_CONTENT_TRANSITIVE)) { && !key.equals(ENV_METADATA_CONTENT_TRANSITIVE)
&& !key.equals(ENV_METADATA_CONTENT_DISPOSABLE)) {
String sourceKey = ""; String sourceKey = "";
if (key.equals(ENV_TRAFFIC_CONTENT_RAW_TRANSHEADERS)) { if (key.equals(ENV_TRAFFIC_CONTENT_RAW_TRANSHEADERS)) {
sourceKey = key; sourceKey = key;
@ -229,6 +230,7 @@ public class StaticMetadataManager {
mergedMetadataResult.putAll(configMetadata); mergedMetadataResult.putAll(configMetadata);
mergedMetadataResult.putAll(envMetadata); mergedMetadataResult.putAll(envMetadata);
mergedMetadataResult.putAll(customSPIMetadata); mergedMetadataResult.putAll(customSPIMetadata);
this.mergedStaticMetadata = Collections.unmodifiableMap(mergedMetadataResult); this.mergedStaticMetadata = Collections.unmodifiableMap(mergedMetadataResult);
Map<String, String> mergedTransitiveMetadataResult = new HashMap<>(); Map<String, String> mergedTransitiveMetadataResult = new HashMap<>();

@ -27,10 +27,12 @@ import java.util.Set;
import com.tencent.cloud.common.metadata.config.MetadataLocalProperties; import com.tencent.cloud.common.metadata.config.MetadataLocalProperties;
import com.tencent.cloud.common.spi.InstanceMetadataProvider; import com.tencent.cloud.common.spi.InstanceMetadataProvider;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner; import org.mockito.junit.MockitoJUnitRunner;
import uk.org.webcompere.systemstubs.rules.EnvironmentVariablesRule;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
@ -48,6 +50,12 @@ public class StaticMetadataManagerTest {
@Mock @Mock
private MetadataLocalProperties metadataLocalProperties; private MetadataLocalProperties metadataLocalProperties;
/**
* EnvironmentVariablesRule.
*/
@Rule
public EnvironmentVariablesRule rule = new EnvironmentVariablesRule();
@Test @Test
public void testParseConfigMetadata() { public void testParseConfigMetadata() {
Map<String, String> content = new HashMap<>(); Map<String, String> content = new HashMap<>();
@ -147,6 +155,32 @@ public class StaticMetadataManagerTest {
} }
@Test
public void testEnvMetadata() {
// set env
rule.set("SCT_METADATA_CONTENT_TRANSITIVE", "transitiveKey");
rule.set("SCT_METADATA_CONTENT_DISPOSABLE", "disposableKey");
rule.set("SCT_METADATA_CONTENT_transitiveKey", "transitiveValue");
rule.set("SCT_METADATA_CONTENT_disposableKey", "disposableValue");
rule.set("SCT_TRAFFIC_CONTENT_RAW_TRANSHEADERS", "header1,header2,header3");
StaticMetadataManager metadataManager = new StaticMetadataManager(metadataLocalProperties, null);
Map<String, String> allEnvMetadata = metadataManager.getAllEnvMetadata();
Assert.assertTrue(allEnvMetadata.containsKey("transitiveKey"));
Assert.assertTrue(allEnvMetadata.containsKey("disposableKey"));
Map<String, String> envDisposableMetadata = metadataManager.getEnvDisposableMetadata();
Assert.assertTrue(envDisposableMetadata.containsKey("disposableKey"));
Assert.assertEquals(envDisposableMetadata.get("disposableKey"), "disposableValue");
Map<String, String> envTransitiveMetadata = metadataManager.getEnvTransitiveMetadata();
Assert.assertTrue(envTransitiveMetadata.containsKey("transitiveKey"));
Assert.assertEquals(envTransitiveMetadata.get("transitiveKey"), "transitiveValue");
String transHeaderFromEnv = metadataManager.getTransHeaderFromEnv();
Assert.assertEquals(transHeaderFromEnv, "header1,header2,header3");
}
static class MockedMetadataProvider implements InstanceMetadataProvider { static class MockedMetadataProvider implements InstanceMetadataProvider {
@Override @Override

@ -81,6 +81,7 @@
<protobuf-java.version>3.16.1</protobuf-java.version> <protobuf-java.version>3.16.1</protobuf-java.version>
<bcprov-jdk15on.version>1.69</bcprov-jdk15on.version> <bcprov-jdk15on.version>1.69</bcprov-jdk15on.version>
<guava.version>31.0.1-jre</guava.version> <guava.version>31.0.1-jre</guava.version>
<system-stubs-junit4.version>2.0.1</system-stubs-junit4.version>
<!-- Maven Plugin Versions --> <!-- Maven Plugin Versions -->
<maven-source-plugin.version>3.2.0</maven-source-plugin.version> <maven-source-plugin.version>3.2.0</maven-source-plugin.version>
@ -267,6 +268,13 @@
<version>${mocktio.version}</version> <version>${mocktio.version}</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>uk.org.webcompere</groupId>
<artifactId>system-stubs-junit4</artifactId>
<version>${system-stubs-junit4.version}</version>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>
</dependencyManagement> </dependencyManagement>

Loading…
Cancel
Save