Test:support environment variable metadata test. (#711)

pull/720/head
lingxiao,wu 2 years ago committed by GitHub
parent e37de52366
commit 095a1fdaaf
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/689)
- [docs:update PR template.](https://github.com/Tencent/spring-cloud-tencent/pull/692)
- [Code optimize & add junit tests.](https://github.com/Tencent/spring-cloud-tencent/pull/702)
- [Test:support environment variable metadata test.](https://github.com/Tencent/spring-cloud-tencent/pull/711)

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

@ -13,7 +13,6 @@
* 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 com.tencent.cloud.common.metadata;
@ -109,8 +108,9 @@ public class StaticMetadataManager {
String value = entry.getValue();
if (StringUtils.isNotBlank(key)
&& (key.startsWith(ENV_METADATA_PREFIX) || key.equals(ENV_TRAFFIC_CONTENT_RAW_TRANSHEADERS))
&& !key.equals(ENV_METADATA_CONTENT_TRANSITIVE)) {
String sourceKey;
&& !key.equals(ENV_METADATA_CONTENT_TRANSITIVE)
&& !key.equals(ENV_METADATA_CONTENT_DISPOSABLE)) {
String sourceKey = "";
if (key.equals(ENV_TRAFFIC_CONTENT_RAW_TRANSHEADERS)) {
sourceKey = key;
envNotReportMetadata.put(sourceKey, value);

@ -27,10 +27,12 @@ import java.util.Set;
import com.tencent.cloud.common.metadata.config.MetadataLocalProperties;
import com.tencent.cloud.common.spi.InstanceMetadataProvider;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import uk.org.webcompere.systemstubs.rules.EnvironmentVariablesRule;
import org.springframework.util.CollectionUtils;
@ -47,6 +49,12 @@ public class StaticMetadataManagerTest {
@Mock
private MetadataLocalProperties metadataLocalProperties;
/**
* EnvironmentVariablesRule.
*/
@Rule
public EnvironmentVariablesRule rule = new EnvironmentVariablesRule();
@Test
public void testParseConfigMetadata() {
Map<String, String> content = new HashMap<>();
@ -146,6 +154,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 {
@Override

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

Loading…
Cancel
Save