简体   繁体   中英

Testing Spring Cloud Config Server

Are there any best practices around testing Spring Cloud Config Server?

I'm finding it difficult to get the configuration right when deploying to Kubernetes, and it takes time to fix and redeploy each time. Also there's the issue of regression failures, when changes are made.

First of all is there any way to write a smoke test that the application context will load? Similar to this:

@SpringBootTest
@TestPropertySource(properties = { "spring.config.location=classpath:application.yml" })
@Tag("Smoke")
public class TellusIngestionApplicationTests {

    @Test
    void contextLoads() {
    }

}

But rather than loading a test application.yml file, loading the config from the Spring Cloud Config Server, and testing different profiles? eg. development staging, production, etc.

Thanks to Spencer for the sample code!

Adjusting for JUnit5, I had to write a custom extension to start the config server before the application context started up (@BeforeAll didn't seem to work). I ended up with the following code:

import com.example.configserver.ConfigServerApplication;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.AfterAllCallback;
import org.junit.jupiter.api.extension.BeforeAllCallback;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.test.context.ActiveProfiles;

import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;

@ExtendWith(ClockApplicationTests.ConfigServerExtension.class)
@SpringBootTest(classes = ClockApplication.class,
        // Normally spring.cloud.config.enabled:true is the default but since we have the config server on the classpath
        // we need to set it explicitly.
        properties = {
            "spring.cloud.config.enabled:true",
            "management.security.enabled=false",
            "management.endpoints.web.exposure.include=*"
        },
        webEnvironment = RANDOM_PORT)
@Tag("Smoke")
class ClockApplicationTests {

    static class ConfigServerExtension implements BeforeAllCallback, AfterAllCallback {

        @Override
        public void beforeAll(ExtensionContext extensionContext) {
            if (server == null) {
                server = new SpringApplicationBuilder(ConfigServerApplication.class)
                        .run("--server.port=" + CONFIG_PORT,
                                "--spring.cloud.config.server.git.uri=???",
                                "--spring.cloud.config.server.git.username=???",
                                "--spring.cloud.config.server.git.password=???",
                                "--spring.cloud.config.server.git.default-label=master",
                                "--spring.cloud.config.server.git.search-paths=???");
            }
        }

        @Override
        public void afterAll(ExtensionContext extensionContext) {
            if (server != null) {
                server.close();
            }
        }
    }

    private static final int CONFIG_PORT = 8888;

    private static ConfigurableApplicationContext server;

    @Nested
    @ActiveProfiles("docker")
    @Tag("docker")
    class Docker {

        @Test
        void contextLoads() {
            // The application context will fail to load if the required properties are not found
        }

    }

    @Nested
    @ActiveProfiles("kubernetes")
    @Tag("kubernetes")
    class Kubernetes {

        @Test
        void contextLoads() {
            // The application context will fail to load if the required properties are not found
        }

    }

}

Note: this assumes you don't set default values for properties. Alternatively you could assert the values of properties, similar to Spencer's sample code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM