Skip to content

Commit

Permalink
Fix spring autoconfigure test (#40980)
Browse files Browse the repository at this point in the history
* Specify mcr registry to use.

* Fix spelling

* Manually take care of container lifecycle.

* Fix naming of variable.

* Adding console.writeline.

* move environment variable.

* Remove Disabled annotation

* Add prebuild step to set env variable.
  • Loading branch information
conniey authored Jul 8, 2024
1 parent 9c96012 commit 33c79f2
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 13 deletions.
6 changes: 6 additions & 0 deletions sdk/spring/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -600,3 +600,9 @@ extends:
skipPublishDocGithubIo: true
skipPublishDocMs: true
releaseInBatch: ${{ parameters.release_springcloudazurestarterappconfigurationconfig }}
PreBuildSteps:
# AzureRedisAutoConfigurationTestContainerTest pulls in `ryuk` image because of testcontainers dependency.
# `ryuk` does not exist in mcr.microsoft.com image repository. Do not pull image.
- bash: |
echo "##vso[task.setvariable variable=TESTCONTAINERS_RYUK_DISABLED;]true"
displayName: 'Disable testcontainers ryuk'
5 changes: 5 additions & 0 deletions sdk/spring/pipeline/compatibility-tests-job.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ jobs:
- script: |
python -m pip install termcolor
displayName: 'Python module install'
# AzureRedisAutoConfigurationTestContainerTest pulls in `ryuk` image because of testcontainers dependency.
# `ryuk` does not exist in mcr.microsoft.com image repository. Do not pull image.
- bash: |
echo "##vso[task.setvariable variable=TESTCONTAINERS_RYUK_DISABLED;]true"
displayName: 'Disable testcontainers ryuk'
- bash: |
echo "##vso[task.setVariable variable=SPRING_CLOUD_AZURE_TEST_SUPPORTED_SPRING_CLOUD_VERSION]$(python ./sdk/spring/scripts/compatibility_get_spring_cloud_version.py -b $(SPRING_CLOUD_AZURE_TEST_SUPPORTED_SPRING_BOOT_VERSION))"
displayName: 'Set env'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@

import com.azure.identity.extensions.implementation.template.AzureAuthenticationTemplate;
import com.azure.spring.cloud.autoconfigure.redis.AzureJedisPasswordlessAutoConfiguration;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import org.junit.jupiter.api.condition.DisabledOnOs;
import org.junit.jupiter.api.condition.OS;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
Expand All @@ -21,7 +24,6 @@
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.testcontainers.utility.DockerImageName;

Expand All @@ -33,7 +35,7 @@

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE)
@Testcontainers
@Disabled("Disable for Container startup failed")
@DisabledOnOs({OS.WINDOWS, OS.MAC})
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class AzureRedisAutoConfigurationTestContainerTest {

Expand All @@ -42,17 +44,34 @@ public class AzureRedisAutoConfigurationTestContainerTest {

private static final String REDIS_PASSWORD = "fake-testcontainer-password";

@Container
private static GenericContainer<?> redis =
new GenericContainer<>(DockerImageName.parse("redis:5.0.3-alpine"))
.withCommand("--requirepass", REDIS_PASSWORD)
.withExposedPorts(6379);
/**
* Pulling Docker registry name from testcontainers.properties file as prefix.
*/
private static final GenericContainer<?> REDIS;

static {
System.out.println("TESTCONTAINERS_RYUK_DISABLED value: [" + System.getenv("TESTCONTAINERS_RYUK_DISABLED") + "]");

REDIS =
new GenericContainer<>(DockerImageName.parse("redis:6"))
.withCommand("--requirepass", REDIS_PASSWORD)
.withExposedPorts(6379);
}

@BeforeAll
public static void beforeAll() {
REDIS.start();
}

@AfterAll
public static void afterAll() {
REDIS.stop();
}

@DynamicPropertySource
static void redisProperties(DynamicPropertyRegistry registry) {
registry.add("spring.redis.host", redis::getHost);
registry.add("spring.redis.port", redis::getFirstMappedPort);
registry.add("spring.redis.host", REDIS::getHost);
registry.add("spring.redis.port", REDIS::getFirstMappedPort);
registry.add("spring.redis.ssl", () -> false);
registry.add("spring.redis.azure.passwordless-enabled", () -> true);
}
Expand All @@ -72,12 +91,11 @@ void testSetAndGet() {

@Test
@Order(2)
void testGetVauleAfterSet() {
void testGetValueAfterSet() {
String value = (String) redisTemplate.opsForValue().get("valueMap3");
Assertions.assertEquals("map3", value);
}


@Configuration
@Import({AzureJedisPasswordlessAutoConfiguration.class, RedisAutoConfiguration.class})
static class AzureRedisPasswordlessTestConfig {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hub.image.name.prefix=mcr.microsoft.com/cbl-mariner/base/
7 changes: 6 additions & 1 deletion sdk/spring/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ extends:
stages:
- template: /eng/pipelines/templates/stages/archetype-sdk-tests-isolated.yml
parameters:
EnvVars:
TESTCONTAINERS_RYUK_DISABLED: true
UseFederatedAuth: true
SupportedClouds: "Public,UsGov,China"
Clouds: "Public"
Expand Down Expand Up @@ -43,6 +45,8 @@ extends:
TestOptions: "-DskipSpringITs=false"
- template: /eng/pipelines/templates/stages/archetype-sdk-tests-isolated.yml
parameters:
EnvVars:
TESTCONTAINERS_RYUK_DISABLED: true
UseFederatedAuth: true
SupportedClouds: 'Public,UsGov,China'
Clouds: 'Public'
Expand Down Expand Up @@ -70,6 +74,8 @@ extends:
TestGoals: 'verify'
- template: /eng/pipelines/templates/stages/archetype-sdk-tests-isolated.yml
parameters:
EnvVars:
TESTCONTAINERS_RYUK_DISABLED: true
UseFederatedAuth: true
SupportedClouds: 'Public,UsGov'
Clouds: 'Public'
Expand Down Expand Up @@ -100,4 +106,3 @@ extends:
TestName: 'Spring_Data_Cosmos_Integration'
TestGoals: 'clean verify'
TestOptions: '$(ProfileFlag)'

0 comments on commit 33c79f2

Please sign in to comment.