Skip to content

통합 테스트 환경 구축 #31

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
9 tasks done
junha-ahn opened this issue Jul 15, 2023 · 2 comments · Fixed by #44
Closed
9 tasks done

통합 테스트 환경 구축 #31

junha-ahn opened this issue Jul 15, 2023 · 2 comments · Fixed by #44
Assignees
Labels
enhancement New feature or request major Major topic

Comments

@junha-ahn
Copy link
Member

junha-ahn commented Jul 15, 2023

Description

testcontainers를 이용한 통합 테스트를 위한 환경 구축

  • Docker Compose 구성
  • testcontainer에 docker-compose 환경 공유 고려

To do

ect. testcontainer setup study

  • 1. spring boot initializr add testcontainer
  • 2. print mysql version
  • 3. test simple api
  • 4. use singleton pattern container
  • 5. use Docker-compose

Test Checklist

@junha-ahn junha-ahn added the enhancement New feature or request label Jul 15, 2023
@junha-ahn junha-ahn self-assigned this Jul 15, 2023
@junha-ahn junha-ahn moved this to Todo in Kanban backend Jul 15, 2023
@junha-ahn junha-ahn mentioned this issue Jul 16, 2023
13 tasks
@junha-ahn
Copy link
Member Author

junha-ahn commented Jul 16, 2023

Testcontainer for gradle

reference

example

PR

  • you should install docker
  • print testcontainer mysql version
// build.gradle.kts

dependencies {
        ...
	runtimeOnly("com.mysql:mysql-connector-j")
	testImplementation("org.springframework.boot:spring-boot-testcontainers")
	testImplementation("org.testcontainers:junit-jupiter")
	testImplementation("org.testcontainers:mysql")
}
package com.group4.ticketingservice

import org.junit.jupiter.api.Test
import org.springframework.boot.test.context.TestConfiguration
import org.springframework.boot.testcontainers.service.connection.ServiceConnection
import org.springframework.context.annotation.Bean
import org.testcontainers.containers.MySQLContainer

// @SpringBootTest()
@TestConfiguration(proxyBeanMethods = false)
class MySQLContainerTest {

    @Bean
    @ServiceConnection
    fun mysqlContainer(): MySQLContainer<*> {
        return MySQLContainer("mysql:latest")
    }

    @Test
    fun printMysqlVersion() {
        val mysqlContainer = mysqlContainer()
        mysqlContainer.start()
        val mysqlVersion = mysqlContainer.execInContainer("mysql", "--version")
        println("MySQL version: $mysqlVersion")
    }
}

COMMIT

$ ./gradlew test
$ docker ps 
CONTAINER ID   IMAGE                       COMMAND                  CREATED          STATUS          PORTS                                                    NAMES
32d2b99ba756   mysql:latest                "docker-entrypoint.s…"   34 seconds ago   Up 33 seconds   33060/tcp, 0.0.0.0:32771->3306/tcp, :::32771->3306/tcp   objective_gates
a6ed58c36aa8   testcontainers/ryuk:0.4.0   "/bin/ryuk"              34 seconds ago   Up 33 seconds   0.0.0.0:32770->8080/tcp, :::32770->8080/tcp              testcontainers-ryuk-6ce2605b-946a-476a-8bdc-901c7294246a

# After test

$ docker ps 
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

image

  • actions test 성공
  • DinD or DoD로 잘 돌아간건가?...

Testcontainer for docker-compose

reference

에러 해결

코드 예시

Testcontainer for git actions

image

Reference

접기/펼치기

image
image
image
image
image

Tech Stack

  • Java 17
  • JUnit 5 + Spring Boot Test
  • Testcontainers

Potential Problem

  • Long execution time
    • How to make containers start quicker
      • started in parallel
      • Be aware of pull policy
      • ...

This was referenced Jul 16, 2023
@junha-ahn junha-ahn linked a pull request Jul 16, 2023 that will close this issue
@junha-ahn junha-ahn moved this from Todo to In Progress in Kanban backend Jul 17, 2023
@junha-ahn junha-ahn changed the title Docker Compose 세팅 통합 테스트 환경 구축 Jul 17, 2023
@junha-ahn junha-ahn mentioned this issue Jul 18, 2023
2 tasks
@junha-ahn junha-ahn linked a pull request Jul 18, 2023 that will close this issue
2 tasks
@junha-ahn
Copy link
Member Author

junha-ahn commented Jul 19, 2023

경험 공유

상황

  • Kotlin + Spring Boot에 대한 이해 부족 (메인스택은 Nodejs이고, 현재 인프라팀으로써, 인프라 공부만 하고 있음)
  • 하지만 해당 이슈는 Kotlin + Spring Boot 및 junit(및 기타 라이브러리) 에 대한 "최신" 지식을 요구함

해당 이슈를 해결 하는 과정에서 여러 글과 공식 문서를 읽었지만 "Kotlin"으로 설명해놓은 최신 (또는 다양한) 예시는 없었다.

따라서 계속적인 검색, GPT 등 여러 방법을 썼으나

  • 공식 문서 포함 대부분 글은 Java로 작성되어 있다. (=> kotlin covert 불가능, 인텔리제이또한 사용하지 않고, vscode codespace 사용 - 싸지방)
  • 어떤 글은 Kotlin + Spring Boot 이나 Junit4를 사용
  • 어떤 글은 Kotlin + Spring Boot + Junit5이나, 내가 원하는 예제 (Singleton 패턴)이 아님

위와 같은 문제점에 마주치고, 2일간 (하루에 4시간씩) 체계적 문제 해결에 실패. (계속 뭔지도 모르고 복붙만 진행)

해결방법

git repo에 내가 원하는 기술을 검색하고 최신순 정렬을해서, "가장 생생한 예시"를 확인하고 문제 해결

@github-project-automation github-project-automation bot moved this from In Progress to Done in Kanban backend Jul 20, 2023
@junha-ahn junha-ahn added the major Major topic label Aug 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request major Major topic
Projects
Status: Done
2 participants