-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for resources-support library
- Loading branch information
Showing
4 changed files
with
517 additions
and
0 deletions.
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
.../resources-support/src/test/java/com/google/cloud/opentelemetry/detectors/EnvVarMock.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* Copyright 2024 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed 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.google.cloud.opentelemetry.detectors; | ||
|
||
import java.util.Map; | ||
|
||
class EnvVarMock implements EnvironmentVariables { | ||
private final Map<String, String> mock; | ||
|
||
public EnvVarMock(Map<String, String> mock) { | ||
this.mock = mock; | ||
} | ||
|
||
@Override | ||
public String get(String key) { | ||
return mock.get(key); | ||
} | ||
} |
152 changes: 152 additions & 0 deletions
152
...support/src/test/java/com/google/cloud/opentelemetry/detectors/GCPMetadataConfigTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
/* | ||
* Copyright 2024 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed 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.google.cloud.opentelemetry.detectors; | ||
|
||
import static com.google.cloud.opentelemetry.detectors.TestUtils.stubEndpoint; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import com.github.tomakehurst.wiremock.junit5.WireMockTest; | ||
import java.util.stream.Stream; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.Arguments; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
|
||
@WireMockTest(httpPort = 8090) | ||
class GCPMetadataConfigTest { | ||
private static final String mockProjectId = "pid"; | ||
private static final String mockZone = "country-region-zone"; | ||
private static final String mockRegion = "country-region1"; | ||
private static final String mockInstanceId = "instance-id"; | ||
private static final String mockInstanceName = "instance-name"; | ||
private static final String mockInstanceType = "instance-type"; | ||
private static final String mockClusterName = "cluster-name"; | ||
private static final String mockClusterLocation = "cluster-location"; | ||
private static final String mockHostname = "hostname"; | ||
|
||
private final GCPMetadataConfig mockMetadataConfig = | ||
new GCPMetadataConfig("http://localhost:8090/"); | ||
|
||
@BeforeEach | ||
public void setupMockMetadataConfig() { | ||
stubEndpoint("/project/project-id", mockProjectId); | ||
stubEndpoint("/instance/zone", mockZone); | ||
stubEndpoint("/instance/region", mockRegion); | ||
stubEndpoint("/instance/id", mockInstanceId); | ||
stubEndpoint("/instance/name", mockInstanceName); | ||
stubEndpoint("/instance/machine-type", mockInstanceType); | ||
stubEndpoint("/instance/attributes/cluster-name", mockClusterName); | ||
stubEndpoint("/instance/attributes/cluster-location", mockClusterLocation); | ||
stubEndpoint("/instance/hostname", mockHostname); | ||
} | ||
|
||
@Test | ||
void testGetProjectId() { | ||
assertEquals(mockProjectId, mockMetadataConfig.getProjectId()); | ||
} | ||
|
||
/** Test Zone Retrieval */ | ||
@ParameterizedTest | ||
@MethodSource("provideZoneRetrievalArguments") | ||
void testGetZone(String stubbedMockZone, String expectedMockZone) { | ||
stubEndpoint("/instance/zone", stubbedMockZone); | ||
assertEquals(expectedMockZone, mockMetadataConfig.getZone()); | ||
} | ||
|
||
private static Stream<Arguments> provideZoneRetrievalArguments() { | ||
return Stream.of( | ||
Arguments.of(mockZone, mockZone), | ||
Arguments.of( | ||
"projects/640212054955/zones/australia-southeast1-a", "australia-southeast1-a"), | ||
Arguments.of("", null), | ||
Arguments.of(null, null)); | ||
} | ||
|
||
/** Test Region Retrieval */ | ||
@ParameterizedTest | ||
@MethodSource("provideRegionRetrievalArguments") | ||
void testGetRegion(String stubbedMockRegion, String expectedMockRegion) { | ||
stubEndpoint("/instance/region", stubbedMockRegion); | ||
assertEquals(expectedMockRegion, mockMetadataConfig.getRegion()); | ||
} | ||
|
||
private static Stream<Arguments> provideRegionRetrievalArguments() { | ||
return Stream.of( | ||
Arguments.of(mockRegion, mockRegion), | ||
Arguments.of("projects/640212054955/regions/us-central1", "us-central1"), | ||
Arguments.of("", null), | ||
Arguments.of(null, null)); | ||
} | ||
|
||
/** Test Region Retrieval from Zone */ | ||
@ParameterizedTest | ||
@MethodSource("provideZoneArguments") | ||
void testGetRegionFromZone(String stubbedMockZone, String expectedRegion) { | ||
stubEndpoint("/instance/zone", stubbedMockZone); | ||
assertEquals(expectedRegion, mockMetadataConfig.getRegionFromZone()); | ||
} | ||
|
||
private static Stream<Arguments> provideZoneArguments() { | ||
return Stream.of( | ||
Arguments.of(mockZone, "country-region"), | ||
Arguments.of("projects/640212054955/zones/australia-southeast1-a", "australia-southeast1"), | ||
Arguments.of("country-region", null), | ||
Arguments.of("", null), | ||
Arguments.of(null, null)); | ||
} | ||
|
||
/** Test Machine Type Retrieval */ | ||
@ParameterizedTest | ||
@MethodSource("provideMachineTypeRetrievalArguments") | ||
void testGetMachineType(String stubbedMockMachineType, String expectedMockMachineType) { | ||
stubEndpoint("/instance/machine-type", stubbedMockMachineType); | ||
assertEquals(expectedMockMachineType, mockMetadataConfig.getMachineType()); | ||
} | ||
|
||
private static Stream<Arguments> provideMachineTypeRetrievalArguments() { | ||
return Stream.of( | ||
Arguments.of(mockInstanceType, mockInstanceType), | ||
Arguments.of("projects/640212054955/machineTypes/e2-medium", "e2-medium"), | ||
Arguments.of("", null), | ||
Arguments.of(null, null)); | ||
} | ||
|
||
@Test | ||
void testGetInstanceId() { | ||
assertEquals(mockInstanceId, mockMetadataConfig.getInstanceId()); | ||
} | ||
|
||
@Test | ||
void testGetClusterName() { | ||
assertEquals(mockClusterName, mockMetadataConfig.getClusterName()); | ||
} | ||
|
||
@Test | ||
void testGetClusterLocation() { | ||
assertEquals(mockClusterLocation, mockMetadataConfig.getClusterLocation()); | ||
} | ||
|
||
@Test | ||
void testGetInstanceHostName() { | ||
assertEquals(mockHostname, mockMetadataConfig.getInstanceHostName()); | ||
} | ||
|
||
@Test | ||
void testGetInstanceName() { | ||
assertEquals(mockInstanceName, mockMetadataConfig.getInstanceName()); | ||
} | ||
} |
Oops, something went wrong.