spring-boot - 如何在Kubernetes中运行Spring Boot集成测试

标签 spring-boot docker kubernetes integration-testing

我在src/intrgTest/groovy位置下对我的spring boot应用程序进行了一个简单的集成测试,下面是测试

@AutoConfigureMockMvc
@WebMvcTest
class WebControllerTest extends Specification {

  @Autowired
  private MockMvc mvc

  def "when get is performed then the response has status 200 and content is 'Hello world!'"() {
    expect: "Status is 200 and the response is 'Hello world!'"
    mvc.perform(get("/hello"))
      .andExpect(status().isOk())
      .andReturn()
      .response
      .contentAsString == "Hello world!"
  }
}
当我在Kubernetes中创建Pod时,我想运行此测试用例以检查应用程序是否正常运行。我该如何实现?
以下是kubernetes的deployment.yml文件
apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
kind: Deployment
metadata:
  name: spring-boot-deployment
spec:
  selector:
    matchLabels:
      app: spring-boot-app
  replicas: 2 
  template:
    metadata:
      labels:
        app: spring-boot-app
    spec:
      containers:
      - name: spring-boot-app
        image: spring-boot-test-images:63
        ports:
        - containerPort: 80

最佳答案

看测试,它是运行状况检查,而不是集成测试。理想情况下,应该在进行实际部署之前而不是之后,使用mvn test作为持续集成的一部分来运行集成测试。
在将应用程序部署到kubernetes上之后,您真的不需要编写运行状况检查测试并执行它。您可以简单地在部署yaml中定义readiness probe,Kubernetes将在将Pod标记为READY并开始向其发送流量之前执行运行状况检查。
如果使用的 Spring 引导版本早于2.3,则可以使用执行器端点/actuator/health;如果使用的是 Spring 引导2.3,则可以将/actuator/health/readiness端点用作就绪探针。

apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
kind: Deployment
metadata:
  name: spring-boot-deployment
spec:
  selector:
    matchLabels:
      app: spring-boot-app
  replicas: 2 
  template:
    metadata:
      labels:
        app: spring-boot-app
    spec:
      containers:
      - name: spring-boot-app
        image: spring-boot-test-images:63
        ports:
        - containerPort: 80
        readinessProbe:
          httpGet:
            port: 8080
            path: /actuator/health
        initialDelaySeconds: 10
如果要在某些外部系统(例如redis或dynamo)上包含测试作为运行状况检查,则可以为此编写custom health indicator。下面的示例来自spring提供的RedisHealthIndicator
public class RedisHealthIndicator extends AbstractHealthIndicator {

    private final RedisConnectionFactory redisConnectionFactory;

    public RedisHealthIndicator(RedisConnectionFactory connectionFactory) {
        super("Redis health check failed");
        Assert.notNull(connectionFactory, "ConnectionFactory must not be null");
        this.redisConnectionFactory = connectionFactory;
    }

    @Override
    protected void doHealthCheck(Health.Builder builder) throws Exception {
        RedisConnection connection = RedisConnectionUtils.getConnection(this.redisConnectionFactory);
        try {
            doHealthCheck(builder, connection);
        }
        finally {
            RedisConnectionUtils.releaseConnection(connection, this.redisConnectionFactory, false);
        }
    }

    private void doHealthCheck(Health.Builder builder, RedisConnection connection) {
        if (connection instanceof RedisClusterConnection) {
            RedisHealth.up(builder, ((RedisClusterConnection) connection).clusterGetClusterInfo());
        }
        else {
            RedisHealth.up(builder, connection.info());
        }
    }

}

关于spring-boot - 如何在Kubernetes中运行Spring Boot集成测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63432662/

相关文章:

windows - Windows上的Docker多阶段COPY c:\windows不允许

kubernetes - 在 Kubernetes yaml 配置文件中动态设置值

kubernetes - 使用aws-load-balancer重定向到301-还是我需要一个入口 Controller

java - Spring Boot 找不到 View

java - 在spring-boot中,是否可以在不成为spring bean的情况下获取属性?

java - 多个 QueueConnectionFactory 上的异常

java - 来自数据库的 JasperReport .jasper 文件

docker - docker容器与docker容器外部完全隔离吗?

php - Docker-通过docker-compose传递环境设置

amazon-web-services - LoadBalancer 外部 IP 卡在待处理状态