Back to Repositories

Testing ElasticSearch 7.x Container Integration in Conductor OSS

This test suite establishes the foundation for ElasticSearch 7.x integration testing in Conductor OSS. It provides a reusable test environment with Docker-based Elasticsearch container setup and Spring configuration for database interaction testing.

Test Coverage Overview

The test suite provides comprehensive coverage for ElasticSearch 7.x integration, focusing on database persistence and indexing operations.

  • Base test configuration for Elasticsearch 7.17.16 container setup
  • Spring context configuration with property source management
  • Object mapper and ElasticSearch properties initialization
  • Container lifecycle management

Implementation Analysis

The implementation utilizes Spring’s test framework with JUnit for container-based testing. The approach leverages TestContainers for Elasticsearch instance management, ensuring isolated testing environments.

Key patterns include:
  • Spring Runner integration with custom configuration
  • Property-based test configuration
  • Automated container lifecycle management
  • Dependency injection for required components

Technical Details

Testing tools and configuration:
  • JUnit 4 test framework
  • Spring Test Context framework
  • TestContainers for Elasticsearch
  • Docker container with Elasticsearch 7.17.16
  • Custom TestConfiguration for properties
  • ObjectMapper for JSON handling

Best Practices Demonstrated

The test suite exemplifies several testing best practices for infrastructure components.

  • Proper test isolation using containers
  • Clear setup and teardown procedures
  • Configuration externalization
  • Abstract base class pattern for reusable test infrastructure
  • Proper resource cleanup with @AfterClass

conductor-oss/conductor

es7-persistence/src/test/java/com/netflix/conductor/es7/dao/index/ElasticSearchTest.java

            
/*
 * Copyright 2023 Conductor Authors.
 * <p>
 * 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
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * 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.netflix.conductor.es7.dao.index;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;
import org.testcontainers.elasticsearch.ElasticsearchContainer;
import org.testcontainers.utility.DockerImageName;

import com.netflix.conductor.common.config.TestObjectMapperConfiguration;
import com.netflix.conductor.es7.config.ElasticSearchProperties;

import com.fasterxml.jackson.databind.ObjectMapper;

@ContextConfiguration(
        classes = {TestObjectMapperConfiguration.class, ElasticSearchTest.TestConfiguration.class})
@RunWith(SpringRunner.class)
@TestPropertySource(
        properties = {"conductor.indexing.enabled=true", "conductor.elasticsearch.version=7"})
public abstract class ElasticSearchTest {

    @Configuration
    static class TestConfiguration {

        @Bean
        public ElasticSearchProperties elasticSearchProperties() {
            return new ElasticSearchProperties();
        }
    }

    protected static final ElasticsearchContainer container =
            new ElasticsearchContainer(
                    DockerImageName.parse("elasticsearch")
                            .withTag("7.17.16")); // this should match the client version

    @Autowired protected ObjectMapper objectMapper;

    @Autowired protected ElasticSearchProperties properties;

    @BeforeClass
    public static void startServer() {
        container.start();
    }

    @AfterClass
    public static void stopServer() {
        container.stop();
    }
}