|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.javaagent.tooling.config; |
| 7 | + |
| 8 | +import static org.assertj.core.api.Assertions.assertThat; |
| 9 | + |
| 10 | +import org.junit.Rule; |
| 11 | +import org.junit.contrib.java.lang.system.EnvironmentVariables; |
| 12 | +import org.junit.jupiter.api.AfterEach; |
| 13 | +import org.junit.jupiter.api.BeforeEach; |
| 14 | +import org.junit.jupiter.api.Test; |
| 15 | +import org.junit.jupiter.api.io.TempDir; |
| 16 | + |
| 17 | +import java.io.File; |
| 18 | +import java.io.FileWriter; |
| 19 | +import java.io.IOException; |
| 20 | +import java.util.Properties; |
| 21 | + |
| 22 | +class ConfigurationFileTest { |
| 23 | + |
| 24 | + @TempDir |
| 25 | + File tmpDir; |
| 26 | + |
| 27 | + @Rule |
| 28 | + public final EnvironmentVariables environmentVariables = new EnvironmentVariables(); |
| 29 | + |
| 30 | + private String originalSystemProperty; |
| 31 | + private String originalEnvironmentVariable; |
| 32 | + |
| 33 | + @BeforeEach |
| 34 | + void setUp() { |
| 35 | + originalSystemProperty = System.getProperty("otel.javaagent.configuration-file"); |
| 36 | + originalEnvironmentVariable = System.getenv("OTEL_JAVAAGENT_CONFIGURATION_FILE"); |
| 37 | + System.clearProperty("otel.javaagent.configuration-file"); |
| 38 | + } |
| 39 | + |
| 40 | + @AfterEach |
| 41 | + void tearDown() { |
| 42 | + if (originalSystemProperty != null) { |
| 43 | + System.setProperty("otel.javaagent.configuration-file", originalSystemProperty); |
| 44 | + } else { |
| 45 | + System.clearProperty("otel.javaagent.configuration-file"); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + void shouldUseEnvVar() throws IOException { |
| 51 | + String path = createFile("config", "property1=val-env"); |
| 52 | + environmentVariables.set("OTEL_JAVAAGENT_CONFIGURATION_FILE", path) |
| 53 | + |
| 54 | + Properties properties = ConfigurationFile.loadConfigFile(); |
| 55 | + |
| 56 | + assertThat(properties.get("property1")).isEqualTo("val-env"); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + void shouldUseSystemProperty() throws IOException { |
| 61 | + String path = createFile("config", "property1=val-sys"); |
| 62 | + System.setProperty("otel.javaagent.configuration-file", path); |
| 63 | + |
| 64 | + Properties properties = ConfigurationFile.loadConfigFile(); |
| 65 | + |
| 66 | + assertThat(properties.get("property1")).isEqualTo("val-sys"); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + void shouldUseSystemPropertyOverEnvVar() throws IOException { |
| 71 | + def pathEnv = createFile("configEnv", "property1=val-env") |
| 72 | + String path = createFile("config", "property1=val-sys"); |
| 73 | + System.setProperty("otel.javaagent.configuration-file", path); |
| 74 | + environmentVariables.set("OTEL_JAVAAGENT_CONFIGURATION_FILE", pathEnv) |
| 75 | + |
| 76 | + Properties properties = ConfigurationFile.loadConfigFile(); |
| 77 | + |
| 78 | + assertThat(properties.get("property1")).isEqualTo("val-sys"); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + void shouldReturnEmptyPropertiesIfFileDoesNotExist() { |
| 83 | + System.setProperty("otel.javaagent.configuration-file", "somePath"); |
| 84 | + |
| 85 | + Properties properties = ConfigurationFile.loadConfigFile(); |
| 86 | + |
| 87 | + assertThat(properties).isEmpty(); |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + void shouldReturnEmptyPropertiesIfPropertyIsNotSet() { |
| 92 | + Properties properties = ConfigurationFile.loadConfigFile(); |
| 93 | + |
| 94 | + assertThat(properties).isEmpty(); |
| 95 | + } |
| 96 | + |
| 97 | + private String createFile(String name, String contents) throws IOException { |
| 98 | + File file = new File(tmpDir, name); |
| 99 | + try (FileWriter writer = new FileWriter(file)) { |
| 100 | + writer.write(contents); |
| 101 | + } |
| 102 | + return file.getAbsolutePath(); |
| 103 | + } |
| 104 | +} |
0 commit comments