Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce flakyness of Resource tests #12252

Merged
merged 6 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions instrumentation/resources/library/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,21 @@ tasks {
)
}
}

testing {
suites {
// Security Manager tests involve setup that can poison the environment for other tests
val testSecurityManager by registering(JvmTestSuite::class) {
dependencies {
implementation(project(":instrumentation:resources:library"))
implementation("io.opentelemetry:opentelemetry-sdk-common")
}
}
}
}

tasks {
check {
dependsOn(testing.suites)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@
public final class ProcessResource {

// copied from ProcessIncubatingAttributes
private static final AttributeKey<List<String>> PROCESS_COMMAND_ARGS =
static final AttributeKey<List<String>> PROCESS_COMMAND_ARGS =
AttributeKey.stringArrayKey("process.command_args");
private static final AttributeKey<String> PROCESS_COMMAND_LINE =
static final AttributeKey<String> PROCESS_COMMAND_LINE =
AttributeKey.stringKey("process.command_line");
private static final AttributeKey<String> PROCESS_EXECUTABLE_PATH =
static final AttributeKey<String> PROCESS_EXECUTABLE_PATH =
AttributeKey.stringKey("process.executable.path");
private static final AttributeKey<Long> PROCESS_PID = AttributeKey.longKey("process.pid");
static final AttributeKey<Long> PROCESS_PID = AttributeKey.longKey("process.pid");

// Note: This pattern doesn't support file paths with spaces in them.
// Important: This is statically used in buildResource, so must be declared/initialized first.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,7 @@
import io.opentelemetry.sdk.resources.Resource;
import io.opentelemetry.semconv.SchemaUrls;
import io.opentelemetry.semconv.incubating.HostIncubatingAttributes;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.condition.EnabledOnJre;
import org.junit.jupiter.api.condition.JRE;
import org.junit.jupiter.api.extension.ExtendWith;

class HostResourceTest {
@Test
Expand All @@ -30,18 +25,4 @@ void shouldCreateRuntimeAttributes() {
assertThat(attributes.get(HostIncubatingAttributes.HOST_NAME)).isNotBlank();
assertThat(attributes.get(HostIncubatingAttributes.HOST_ARCH)).isNotBlank();
}

@Nested
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@ExtendWith(SecurityManagerExtension.class)
@EnabledOnJre(
value = {JRE.JAVA_8, JRE.JAVA_11, JRE.JAVA_16},
disabledReason = "Java 17 deprecates security manager for removal")
static class SecurityManagerEnabled {
@Test
void empty() {
Attributes attributes = HostResource.buildResource().getAttributes();
assertThat(attributes.asMap()).containsOnlyKeys(HostIncubatingAttributes.HOST_NAME);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,7 @@
import io.opentelemetry.sdk.resources.Resource;
import io.opentelemetry.semconv.SchemaUrls;
import io.opentelemetry.semconv.incubating.OsIncubatingAttributes;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.condition.EnabledOnJre;
import org.junit.jupiter.api.condition.JRE;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junitpioneer.jupiter.SetSystemProperty;

class OsResourceTest {
Expand Down Expand Up @@ -138,17 +133,4 @@ void unknown() {
assertThat(resource.getAttribute(OsIncubatingAttributes.OS_TYPE)).isNull();
assertThat(resource.getAttribute(OsIncubatingAttributes.OS_DESCRIPTION)).isNotEmpty();
}

@Nested
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@ExtendWith(SecurityManagerExtension.class)
@EnabledOnJre(
value = {JRE.JAVA_8, JRE.JAVA_11, JRE.JAVA_16},
disabledReason = "Java 17 deprecates security manager for removal")
static class SecurityManagerEnabled {
@Test
void empty() {
assertThat(OsResource.buildResource()).isEqualTo(Resource.empty());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,7 @@
import io.opentelemetry.sdk.resources.Resource;
import io.opentelemetry.semconv.SchemaUrls;
import io.opentelemetry.semconv.incubating.ProcessIncubatingAttributes;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.condition.EnabledOnJre;
import org.junit.jupiter.api.condition.JRE;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junitpioneer.jupiter.SetSystemProperty;

class ProcessResourceTest {
Expand Down Expand Up @@ -52,18 +47,4 @@ void windows() {
// With Java 9+ and a compiled jar, ResourceAttributes.PROCESS_COMMAND_ARGS
// will be set instead of ResourceAttributes.PROCESS_COMMAND_LINE
}

@Nested
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@ExtendWith(SecurityManagerExtension.class)
@EnabledOnJre(
value = {JRE.JAVA_8, JRE.JAVA_11, JRE.JAVA_16},
disabledReason = "Java 17 deprecates security manager for removal")
static class SecurityManagerEnabled {
@Test
void empty() {
Attributes attributes = ProcessResource.buildResource().getAttributes();
assertThat(attributes.asMap()).containsOnlyKeys(ProcessIncubatingAttributes.PROCESS_PID);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,7 @@
import io.opentelemetry.sdk.resources.Resource;
import io.opentelemetry.semconv.SchemaUrls;
import io.opentelemetry.semconv.incubating.ProcessIncubatingAttributes;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.condition.EnabledOnJre;
import org.junit.jupiter.api.condition.JRE;
import org.junit.jupiter.api.extension.ExtendWith;

class ProcessRuntimeResourceTest {
@Test
Expand All @@ -32,17 +27,4 @@ void shouldCreateRuntimeAttributes() {
assertThat(attributes.get(ProcessIncubatingAttributes.PROCESS_RUNTIME_DESCRIPTION))
.isNotBlank();
}

@Nested
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@ExtendWith(SecurityManagerExtension.class)
@EnabledOnJre(
value = {JRE.JAVA_8, JRE.JAVA_11, JRE.JAVA_16},
disabledReason = "Java 17 deprecates security manager for removal")
static class SecurityManagerEnabled {
@Test
void empty() {
assertThat(ProcessRuntimeResource.buildResource()).isEqualTo(Resource.empty());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.resources;

import static org.assertj.core.api.Assertions.assertThat;

import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.sdk.resources.Resource;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.condition.EnabledOnJre;
import org.junit.jupiter.api.condition.JRE;
import org.junit.jupiter.api.extension.ExtendWith;

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@ExtendWith(SecurityManagerExtension.class)
@EnabledOnJre(
value = {JRE.JAVA_8, JRE.JAVA_11, JRE.JAVA_16},
disabledReason = "Java 17 deprecates security manager for removal")
class SecurityManagerResourceTest {

@Test
void hostResourceTestEmpty() {
Attributes attributes = HostResource.buildResource().getAttributes();
assertThat(attributes.asMap()).containsOnlyKeys(HostResource.HOST_NAME);
}

@Test
void osResourceEmpty() {
assertThat(OsResource.buildResource()).isEqualTo(Resource.empty());
}

@Test
void processResourceEmpty() {
Attributes attributes = ProcessResource.buildResource().getAttributes();
assertThat(attributes.asMap()).containsOnlyKeys(ProcessResource.PROCESS_PID);
}

@Test
void processRuntimeResourceEmpty() {
assertThat(ProcessRuntimeResource.buildResource()).isEqualTo(Resource.empty());
}
}
Loading