【问题标题】:How to run spring boot integration test in Kubernetes如何在 Kubernetes 中运行 Spring Boot 集成测试
【发布时间】:2020-12-05 12:23:04
【问题描述】:

我在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

【问题讨论】:

    标签: spring-boot docker kubernetes integration-testing


    【解决方案1】:

    从测试来看,它是健康检查,而不是集成测试。理想情况下,应该在执行实际部署之前而不是之后使用 mvn test 作为持续集成的一部分运行集成测试。

    您真的不需要为健康检查编写测试并在应用程序部署到 kubernetes 后执行它。您可以简单地在部署 yaml 中定义 readiness probe,Kubernetes 会在将 pod 标记为 READY 并开始向其发送流量之前执行健康检查。

    如果您使用的是早于 2.3 的 spring boot 版本,那么您可以使用执行器端点 /actuator/health,如果您使用的是 spring boot 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());
            }
        }
    
    }
    

    【讨论】:

    • 感谢@Arghya,但如果我有多个框架连接到我的应用程序,如 redis、dynamodb。假设我有一个集成测试,我正在连接到 redis 并检索一些值。如何在 kubernetes 环境中验证与 redis 的连接
    • 如果您想在 redis 和 dynamo 上包含测试作为健康检查,您可以编写自定义 HealthIndicator。对于使用 redis 的集成测试..正如我所说,您应该在 mvn test 目标中运行它部署之前的 CI/CD 的一部分
    • 嗯,我想这是一个限制,因为您无法通过 mvn test 连接到 k8 服务,然后将其 dockerized 并部署到 CI/CD 中。我希望有一种情况允许在部署之前使用服务名称、k8 机密和配置来测试连接。因此,带有运行状况检查的失败部署不会破坏应用程序。
    猜你喜欢
    • 2020-02-10
    • 2015-02-10
    • 2020-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-23
    • 2015-08-20
    相关资源
    最近更新 更多