Skip to content
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

[Refactor/#209] 테스트 컨테이너 및 액션 개선 #327

Merged
merged 14 commits into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions .github/workflows/code-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,27 +36,27 @@ jobs:

- name: Jooq Code Generation
run: |
./gradlew --info jooqCodegenAll
./gradlew jooqCodegenAll

- name: Test with Gradle
run: |
./gradlew --info test
./gradlew test -parallel
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

전체 테스트를 수행하는 경우에는 병렬로 실행할 수 있도록 -parallel 옵션 추가하였습니다.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오 이거 ㄹㅇ 굳굳


- name: Build with Gradle
run: |
./gradlew --info api:build -x test
./gradlew api:build -x test

- name: Generate OpenAPI3
run: |
./gradlew --info api:openapi3 -PserverUrl=https://api.fewletter.site
./gradlew api:openapi3 -PserverUrl=https://api.fewletter.site

- name: Generate Swagger
run: |
./gradlew --info api:generateStaticSwaggerUIApi
./gradlew api:generateStaticSwaggerUIApi

- name : Docker Login
run: docker login -u ${{ secrets.DOCKER_USERNAME }} -p ${{ secrets.DOCKER_PASSWORD }}

- name: Build Docker Image
run: |
./gradlew --info api:buildDockerImage -PimageName=fewletter/api -PreleaseVersion=${{ env.RELEASE_VERSION }}
./gradlew api:buildDockerImage -PimageName=fewletter/api -PreleaseVersion=${{ env.RELEASE_VERSION }}
6 changes: 3 additions & 3 deletions .github/workflows/ecs-cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,15 @@ jobs:

- name: Test with Gradle
run: |
./gradlew test
./gradlew test -parallel

- name: Generate OpenAPI3
run: |
./gradlew --info api:openapi3 -PserverUrl=https://api.fewletter.site
./gradlew api:openapi3 -PserverUrl=https://api.fewletter.site

- name: Generate Swagger
run: |
./gradlew --info api:generateStaticSwaggerUIApi
./gradlew api:generateStaticSwaggerUIApi

- name: Build with Gradle bootBuildImage, tag, and push image to Amazon ECR
env:
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/ecs-dev-cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,15 @@ jobs:

- name: Test with Gradle
run: |
./gradlew test
./gradlew test -parallel

- name: Generate OpenAPI3
run: |
./gradlew --info api:openapi3 -PserverUrl=https://api.fewletter.shop
./gradlew api:openapi3 -PserverUrl=https://api.fewletter.shop

- name: Generate Swagger
run: |
./gradlew --info api:generateStaticSwaggerUIApi
./gradlew api:generateStaticSwaggerUIApi

- name: Build with Gradle bootBuildImage, tag, and push image to Amazon ECR
env:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ jobs:
run: chmod +x gradlew

- name: Test with Ktlint
run: ./gradlew --info ktlintCheck
run: ./gradlew ktlintCheck
4 changes: 2 additions & 2 deletions .github/workflows/sql-explain-hook.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ jobs:

- name: Jooq Code Generation
run: |
./gradlew --info jooqCodegenAll
./gradlew jooqCodegenAll

- name: Generate Explain Docs
run: |
./gradlew --info api-repo:generateExplainDocs
./gradlew api-repo:generateExplainDocs

- name: Zip Explain Docs
run: |
Expand Down
139 changes: 135 additions & 4 deletions .github/workflows/validate-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ env:
EMAIL_PASSWORD: ${{ secrets.EMAIL_PASSWORD }}

jobs:
validate-test:
setup-test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
Expand All @@ -34,8 +34,139 @@ jobs:

- name: Jooq Code Generation
run: |
./gradlew --info jooqCodegenAll
./gradlew jooqCodegenAll

- name: Test with Gradle
- name: Cache Api Repo Jooq
uses: actions/cache@v3
with:
path: |
./api-repo/src/generated
./api-repo/src/main/resources/db/migration
key: ${{ runner.os }}-api-repo-jooq-${{ hashFiles('./api-repo/src/generated/**', './api-repo/src/main/resources/db/migration/**') }}
restore-keys: |
${{ runner.os }}-api-repo-jooq-

- name: Cache Batch Jooq
uses: actions/cache@v3
with:
path: |
./batch/src/generated
./batch/src/main/resources/db/migration
key: ${{ runner.os }}-batch-jooq-${{ hashFiles('./batch/src/generated/**', './batch/src/main/resources/db/migration/**') }}
restore-keys: |
${{ runner.os }}-batch-jooq-
Comment on lines +39 to +57
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

jooq 관련 파일을 캐싱합니다.


api-test:
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

아래 각각의 모듈 별로 필요한 캐싱된 값을 받아 테스트를 수행합니다.

needs: setup-test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: '17'
distribution: 'temurin'

- name: Cache Gradle
uses: actions/cache@v3
with:
path: |
~/.gradle/caches
~/.gradle/wrapper
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
restore-keys: |
${{ runner.os }}-gradle-

- name: Cache Api Repo Jooq
uses: actions/cache@v3
with:
path: |
./api-repo/src/generated
./api-repo/src/main/resources/db/migration
key: ${{ runner.os }}-api-repo-jooq-${{ hashFiles('./api-repo/src/generated/**', './api-repo/src/main/resources/db/migration/**') }}
restore-keys: |
${{ runner.os }}-api-repo-jooq-

- name: Cache Batch Jooq
uses: actions/cache@v3
with:
path: |
./batch/src/generated
./batch/src/main/resources/db/migration
key: ${{ runner.os }}-batch-jooq-${{ hashFiles('./batch/src/generated/**', './batch/src/main/resources/db/migration/**') }}
restore-keys: |
${{ runner.os }}-batch-jooq-

- name: API Test with Gradle
run: |
./gradlew --info test
./gradlew api:test

api-repo-test:
needs: setup-test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: '17'
distribution: 'temurin'

- name: Cache Gradle
uses: actions/cache@v3
with:
path: |
~/.gradle/caches
~/.gradle/wrapper
key: ${{ runner.os }}-jooq-${{ hashFiles('./api-repo/src/generated/**', './api-repo/src/main/resources/db/migration/**', './batch/src/generated/**', './batch/src/main/resources/db/migration/**') }}
restore-keys: |
${{ runner.os }}-gradle-

- name: Cache Api Repo Jooq
uses: actions/cache@v3
with:
path: |
./api-repo/src/generated
./api-repo/src/main/resources/db/migration
key: ${{ runner.os }}-api-repo-jooq-${{ hashFiles('./api-repo/src/generated/**', './api-repo/src/main/resources/db/migration/**') }}
restore-keys: |
${{ runner.os }}-api-repo-jooq-

- name: API Repo Test with Gradle
run: |
./gradlew api-repo:test

batch-test:
needs: setup-test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: '17'
distribution: 'temurin'

- name: Cache Gradle
uses: actions/cache@v3
with:
path: |
~/.gradle/caches
~/.gradle/wrapper
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
restore-keys: |
${{ runner.os }}-gradle-

- name: Cache Batch Jooq
uses: actions/cache@v3
with:
path: |
./batch/src/generated
./batch/src/main/resources/db/migration
key: ${{ runner.os }}-batch-jooq-${{ hashFiles('./batch/src/generated/**', './batch/src/main/resources/db/migration/**') }}
restore-keys: |
${{ runner.os }}-batch-jooq-

- name: Batch Test with Gradle
run: |
./gradlew batch:test
2 changes: 1 addition & 1 deletion api-repo/src/test/resources/application-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ logging:
spring:
datasource:
hikari:
jdbcUrl: jdbc:mysql://localhost:13306/api?allowPublicKeyRetrieval=true&rewriteBatchedStatements=true&allowMultiQueries=true
jdbcUrl: jdbc:mysql://localhost:33306/api?allowPublicKeyRetrieval=true&rewriteBatchedStatements=true&allowMultiQueries=true
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
Expand Down
2 changes: 1 addition & 1 deletion api-repo/src/test/resources/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ services:
- TZ=Asia/Seoul
command: [ "--character-set-server=utf8mb4", "--collation-server=utf8mb4_unicode_ci", "--lower_case_table_names=1", "--max_connections=2048", "--wait_timeout=3600" ]
ports:
- "13306:3306"
- "33306:3306"
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

로컬 환경이 켜져있어도 테스트 수행할 수 있고
병렬로 테스트 수행하기 위해 포트를 변경해주었습니다.
-> 모듈별로 독립된 포트로

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이제 로컬에서 디비 컨태이너를 내리지 않고도 테스트가 가능하겠네요

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

네!!😃

volumes:
- ./mysql-init.d:/docker-entrypoint-initdb.d
101 changes: 101 additions & 0 deletions api/src/test/kotlin/com/few/api/web/controller/ControllerTestSpec.kt
Original file line number Diff line number Diff line change
@@ -1,15 +1,36 @@
package com.few.api.web.controller

import com.fasterxml.jackson.databind.ObjectMapper
import com.few.api.ApiMain
import com.few.api.domain.admin.document.usecase.*
import com.few.api.domain.article.usecase.ReadArticleUseCase
import com.few.api.domain.article.usecase.ReadArticlesUseCase
import com.few.api.domain.log.AddApiLogUseCase
import com.few.api.domain.member.usecase.SaveMemberUseCase
import com.few.api.domain.member.usecase.TokenUseCase
import com.few.api.domain.problem.usecase.BrowseProblemsUseCase
import com.few.api.domain.problem.usecase.CheckProblemUseCase
import com.few.api.domain.problem.usecase.ReadProblemUseCase
import com.few.api.domain.subscription.usecase.BrowseSubscribeWorkbooksUseCase
import com.few.api.domain.subscription.usecase.SubscribeWorkbookUseCase
import com.few.api.domain.subscription.usecase.UnsubscribeAllUseCase
import com.few.api.domain.subscription.usecase.UnsubscribeWorkbookUseCase
import com.few.api.domain.workbook.article.usecase.ReadWorkBookArticleUseCase
import com.few.api.domain.workbook.usecase.BrowseWorkbooksUseCase
import com.few.api.domain.workbook.usecase.ReadWorkbookUseCase
import com.few.api.security.token.TokenResolver
import com.few.api.web.handler.ApiControllerExceptionHandler
import org.junit.jupiter.api.extension.ExtendWith
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.restdocs.AutoConfigureRestDocs
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.boot.test.mock.mockito.MockBean
import org.springframework.restdocs.RestDocumentationExtension
import org.springframework.test.context.ActiveProfiles
import org.springframework.test.context.ContextConfiguration
import org.springframework.test.web.reactive.server.WebTestClient
import org.springframework.test.web.servlet.MockMvc

@ActiveProfiles(value = ["test", "new"])
@AutoConfigureRestDocs
Expand All @@ -19,6 +40,86 @@ import org.springframework.test.context.ContextConfiguration
@ContextConfiguration(initializers = [ControllerTestContainerInitializer::class])
abstract class ControllerTestSpec {

/** Common */
@Autowired
lateinit var apiControllerExceptionHandler: ApiControllerExceptionHandler

@Autowired
lateinit var objectMapper: ObjectMapper

lateinit var webTestClient: WebTestClient

@Autowired
lateinit var mockMvc: MockMvc

@MockBean
lateinit var tokenResolver: TokenResolver

/** AdminControllerTest */
@MockBean
lateinit var addArticleUseCase: AddArticleUseCase

@MockBean
lateinit var addWorkbookUseCase: AddWorkbookUseCase

@MockBean
lateinit var mapArticleUseCase: MapArticleUseCase

@MockBean
lateinit var convertContentUseCase: ConvertContentUseCase

@MockBean
lateinit var putImageUseCase: PutImageUseCase

/** ApiLogControllerTest */
@MockBean
lateinit var addApiLogUseCase: AddApiLogUseCase

/** ArticleControllerTest */
@MockBean
lateinit var readArticleUseCase: ReadArticleUseCase

@MockBean
lateinit var readArticlesUseCase: ReadArticlesUseCase

/** MemberControllerTest */
@MockBean
lateinit var saveMemberUseCase: SaveMemberUseCase

@MockBean
lateinit var tokenUseCase: TokenUseCase

/** ProblemControllerTest */
@MockBean
lateinit var browseProblemsUseCase: BrowseProblemsUseCase

@MockBean
lateinit var readProblemUseCase: ReadProblemUseCase

@MockBean
lateinit var checkProblemUseCase: CheckProblemUseCase

/** SubscriptionControllerTest */
@MockBean
lateinit var subscribeWorkbookUseCase: SubscribeWorkbookUseCase

@MockBean
lateinit var unsubscribeWorkbookUseCase: UnsubscribeWorkbookUseCase

@MockBean
lateinit var unsubscribeAllUseCase: UnsubscribeAllUseCase

@MockBean
lateinit var browseSubscribeWorkbooksUseCase: BrowseSubscribeWorkbooksUseCase

/** WorkBookArticleControllerTest */
@MockBean
lateinit var readWorkBookArticleUseCase: ReadWorkBookArticleUseCase

/** WorkBookControllerTest */
@MockBean
lateinit var readWorkbookUseCase: ReadWorkbookUseCase

@MockBean
lateinit var browseWorkBooksUseCase: BrowseWorkbooksUseCase
}
Loading
Loading