Test:support environment variable metadata test (#698)

Co-authored-by: Haotian Zhang <928016560@qq.com>
pull/718/head
lingxiao,wu 2 years ago committed by GitHub
parent b8950e2071
commit d124cfd4e1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -6,3 +6,4 @@
- [Optimize: Register the service with the ProviderAPI#registerInstance method.](https://github.com/Tencent/spring-cloud-tencent/pull/686) - [Optimize: Register the service with the ProviderAPI#registerInstance method.](https://github.com/Tencent/spring-cloud-tencent/pull/686)
- [docs:update PR template.](https://github.com/Tencent/spring-cloud-tencent/pull/690) - [docs:update PR template.](https://github.com/Tencent/spring-cloud-tencent/pull/690)
- [Optimize: remote deprecated method](https://github.com/Tencent/spring-cloud-tencent/pull/697) - [Optimize: remote deprecated method](https://github.com/Tencent/spring-cloud-tencent/pull/697)
- [Test:support environment variable metadata test](https://github.com/Tencent/spring-cloud-tencent/pull/698)

@ -99,6 +99,11 @@
<artifactId>spring-boot-actuator-autoconfigure</artifactId> <artifactId>spring-boot-actuator-autoconfigure</artifactId>
<optional>true</optional> <optional>true</optional>
</dependency> </dependency>
</dependencies>
<dependency>
<groupId>uk.org.webcompere</groupId>
<artifactId>system-stubs-junit4</artifactId>
<scope>test</scope>
</dependency>
</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;

@ -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<>();
@ -134,12 +142,12 @@ public class StaticMetadataManagerTest {
Assert.assertEquals("v1", metadata.get("k1")); Assert.assertEquals("v1", metadata.get("k1"));
Assert.assertEquals("v22", metadata.get("k2")); Assert.assertEquals("v22", metadata.get("k2"));
Assert.assertEquals("zone2", metadataManager.getZone());
Assert.assertEquals("region1", metadataManager.getRegion());
Assert.assertTrue(CollectionUtils.isEmpty(metadataManager.getAllEnvMetadata())); Assert.assertTrue(CollectionUtils.isEmpty(metadataManager.getAllEnvMetadata()));
Assert.assertTrue(CollectionUtils.isEmpty(metadataManager.getEnvTransitiveMetadata())); Assert.assertTrue(CollectionUtils.isEmpty(metadataManager.getEnvTransitiveMetadata()));
Assert.assertEquals("zone2", metadataManager.getZone());
Assert.assertEquals("region1", metadataManager.getRegion());
Map<String, String> locationInfo = metadataManager.getLocationMetadata(); Map<String, String> locationInfo = metadataManager.getLocationMetadata();
Assert.assertEquals("zone2", locationInfo.get("zone")); Assert.assertEquals("zone2", locationInfo.get("zone"));
Assert.assertEquals("region1", locationInfo.get("region")); Assert.assertEquals("region1", locationInfo.get("region"));
@ -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

@ -80,6 +80,7 @@
<byte-buddy.version>1.12.10</byte-buddy.version> <byte-buddy.version>1.12.10</byte-buddy.version>
<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>
<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>
@ -248,6 +249,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