Add code coverage for OS resources. (#2167)

* Add code coverage for OS resources.

* Fix assertion
This commit is contained in:
Anuraag Agrawal 2020-12-02 04:04:01 +09:00 committed by GitHub
parent 2aeaf1a392
commit d31995f75d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 95 additions and 6 deletions

View File

@ -6,42 +6,114 @@
package io.opentelemetry.sdk.extension.resources;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assumptions.assumeThat;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.common.ReadableAttributes;
import io.opentelemetry.sdk.resources.Resource;
import io.opentelemetry.sdk.resources.ResourceAttributes;
import org.junit.jupiter.api.Test;
import org.junitpioneer.jupiter.SetSystemProperty;
class OsResourceTest {
private static final OsResource RESOURCE = new OsResource();
@Test
@SetSystemProperty(key = "os.name", value = "Linux 4.11")
void linux() {
assumeThat(System.getProperty("os.name").toLowerCase()).startsWith("linux");
Attributes attributes = RESOURCE.getAttributes();
assertThat(attributes.get(ResourceAttributes.OS_NAME)).isEqualTo("LINUX");
assertThat(attributes.get(ResourceAttributes.OS_DESCRIPTION)).isNotEmpty();
}
@Test
@SetSystemProperty(key = "os.name", value = "MacOS X 11")
void macos() {
assumeThat(System.getProperty("os.name").toLowerCase()).startsWith("mac");
Attributes attributes = RESOURCE.getAttributes();
assertThat(attributes.get(ResourceAttributes.OS_NAME)).isEqualTo("DARWIN");
assertThat(attributes.get(ResourceAttributes.OS_DESCRIPTION)).isNotEmpty();
}
@Test
@SetSystemProperty(key = "os.name", value = "Windows 10")
void windows() {
assumeThat(System.getProperty("os.name").toLowerCase()).startsWith("windows");
Attributes attributes = RESOURCE.getAttributes();
assertThat(attributes.get(ResourceAttributes.OS_NAME)).isEqualTo("WINDOWS");
assertThat(attributes.get(ResourceAttributes.OS_DESCRIPTION)).isNotEmpty();
}
@Test
@SetSystemProperty(key = "os.name", value = "FreeBSD 10")
void freebsd() {
Attributes attributes = RESOURCE.getAttributes();
assertThat(attributes.get(ResourceAttributes.OS_NAME)).isEqualTo("FREEBSD");
assertThat(attributes.get(ResourceAttributes.OS_DESCRIPTION)).isNotEmpty();
}
@Test
@SetSystemProperty(key = "os.name", value = "NetBSD 10")
void netbsd() {
Attributes attributes = RESOURCE.getAttributes();
assertThat(attributes.get(ResourceAttributes.OS_NAME)).isEqualTo("NETBSD");
assertThat(attributes.get(ResourceAttributes.OS_DESCRIPTION)).isNotEmpty();
}
@Test
@SetSystemProperty(key = "os.name", value = "OpenBSD 10")
void openbsd() {
Attributes attributes = RESOURCE.getAttributes();
assertThat(attributes.get(ResourceAttributes.OS_NAME)).isEqualTo("OPENBSD");
assertThat(attributes.get(ResourceAttributes.OS_DESCRIPTION)).isNotEmpty();
}
@Test
@SetSystemProperty(key = "os.name", value = "DragonFlyBSD 10")
void dragonflybsd() {
Attributes attributes = RESOURCE.getAttributes();
assertThat(attributes.get(ResourceAttributes.OS_NAME)).isEqualTo("DRAGONFLYBSD");
assertThat(attributes.get(ResourceAttributes.OS_DESCRIPTION)).isNotEmpty();
}
@Test
@SetSystemProperty(key = "os.name", value = "HP-UX 10")
void hpux() {
Attributes attributes = RESOURCE.getAttributes();
assertThat(attributes.get(ResourceAttributes.OS_NAME)).isEqualTo("HPUX");
assertThat(attributes.get(ResourceAttributes.OS_DESCRIPTION)).isNotEmpty();
}
@Test
@SetSystemProperty(key = "os.name", value = "AIX 10")
void aix() {
Attributes attributes = RESOURCE.getAttributes();
assertThat(attributes.get(ResourceAttributes.OS_NAME)).isEqualTo("AIX");
assertThat(attributes.get(ResourceAttributes.OS_DESCRIPTION)).isNotEmpty();
}
@Test
@SetSystemProperty(key = "os.name", value = "Solaris 10")
void solaris() {
Attributes attributes = RESOURCE.getAttributes();
assertThat(attributes.get(ResourceAttributes.OS_NAME)).isEqualTo("SOLARIS");
assertThat(attributes.get(ResourceAttributes.OS_DESCRIPTION)).isNotEmpty();
}
@Test
@SetSystemProperty(key = "os.name", value = "Z/OS 10")
void zos() {
Attributes attributes = RESOURCE.getAttributes();
assertThat(attributes.get(ResourceAttributes.OS_NAME)).isEqualTo("ZOS");
assertThat(attributes.get(ResourceAttributes.OS_DESCRIPTION)).isNotEmpty();
}
@Test
@SetSystemProperty(key = "os.name", value = "RagOS 10")
void unknown() {
Attributes attributes = RESOURCE.getAttributes();
assertThat(attributes.get(ResourceAttributes.OS_NAME)).isNull();
assertThat(attributes.get(ResourceAttributes.OS_DESCRIPTION)).isNotEmpty();
}
@Test
void inDefault() {
ReadableAttributes attributes = Resource.getDefault().getAttributes();

View File

@ -12,17 +12,34 @@ import io.opentelemetry.api.common.ReadableAttributes;
import io.opentelemetry.sdk.resources.Resource;
import io.opentelemetry.sdk.resources.ResourceAttributes;
import org.junit.jupiter.api.Test;
import org.junitpioneer.jupiter.SetSystemProperty;
class ProcessResourceTest {
private static final ProcessResource RESOURCE = new ProcessResource();
@Test
void normal() {
@SetSystemProperty(key = "os.name", value = "Linux 4.12")
void notWindows() {
Attributes attributes = RESOURCE.getAttributes();
assertThat(attributes.get(ResourceAttributes.PROCESS_PID)).isGreaterThan(1);
assertThat(attributes.get(ResourceAttributes.PROCESS_EXECUTABLE_PATH)).contains("java");
assertThat(attributes.get(ResourceAttributes.PROCESS_EXECUTABLE_PATH))
.contains("java")
.doesNotEndWith(".exe");
assertThat(attributes.get(ResourceAttributes.PROCESS_COMMAND_LINE))
.contains(attributes.get(ResourceAttributes.PROCESS_EXECUTABLE_PATH));
}
@Test
@SetSystemProperty(key = "os.name", value = "Windows 10")
void windows() {
Attributes attributes = RESOURCE.getAttributes();
assertThat(attributes.get(ResourceAttributes.PROCESS_PID)).isGreaterThan(1);
assertThat(attributes.get(ResourceAttributes.PROCESS_EXECUTABLE_PATH))
.contains("java")
.endsWith(".exe");
assertThat(attributes.get(ResourceAttributes.PROCESS_COMMAND_LINE))
.contains(attributes.get(ResourceAttributes.PROCESS_EXECUTABLE_PATH));
}