IT STUDY LOG
Sprint - 빌드 및 테스트 자동화 본문
#해결 과제
💡 node.js 프로그램의 테스트를 위해서는 npm test 명령어를 CI, 즉 GitHub Action 상에서 자동으로 실행해줘야 합니다.
💡 먼저 공식 문서를 통해 GitHub Action의 사용방법을 알아봅시다.
- Creating a starter workflow
- Using starter workflow
- 공식 문서가 조금 어렵다면, 이 영상을 참고해 보세요.
💡 Actions 탭을 클릭하여 workflow가 제대로 작동했는지를 확인해 봅시다.
💡 레퍼런스를 참고하여 GitHub Action Node.js workflow를 만들고, 테스트가 통과하는지 확인하세요.
#실습 자료
#과제 항목별 진행 상황
✏️ 유닛 테스트 통과 : TDD Practice
1. 애플리케이션은 node.js로 작성되어 있습니다. node.js LTS 버전을 준비합니다.
$ nvm list available
$ nvm install --lts
$ nvm use default
2. 애플리케이션의 의존성(dependency)을 설치해야 합니다. npm install 명령을 이용해 의존성을 설치합니다.
$ npm install
3. 테스트가 통과하는지 확인하려면 npm test 명령을 이용합니다. 다음과 같이 테스트가 통과하지 않는 것을 먼저 확인하세요.
$ npm run test
> mini-node-server@1.0.0 test
> mocha ./test
Server listening on http://localhost:4000
유닛 테스트 101
1) 결과에 대한 기대값(expectation value)를 비교하여 유닛 테스트를 할 수 있습니다
2) 서버에 GET / 요청을 보내면 Hello World!라는 텍스트가 응답으로 옵니다
3) 서버에 POST /upper 요청에 body를 실어 보내면 응답은 대문자로 돌려줍니다
4) 서버에 POST /lower 요청에 body를 실어 보내면 응답은 소문자로 돌려줍니다
0 passing (140ms)
4 failing
1) 유닛 테스트 101
결과에 대한 기대값(expectation value)를 비교하여 유닛 테스트를 할 수 있습니다:
AssertionError: expected 2 to equal '기댓값이 채워지지 않았습니다'
at Context.<anonymous> (test/app.test.js:13:25)
at process.processImmediate (node:internal/timers:476:21)
2) 유닛 테스트 101
서버에 GET / 요청을 보내면 Hello World!라는 텍스트가 응답으로 옵니다:
AssertionError: expected 'Hello World!' to equal '기댓값이 채워지지 않았습니다'
+ expected - actual
-Hello World!
+기댓값이 채워지지 않았습니다
at /home/roheerumi/devops/class_study/cloud_study/cicd_study/ci_study/sprint-mini-node-server-with-github-action/test/app.test.js:21:32
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
3) 유닛 테스트 101
서버에 POST /upper 요청에 body를 실어 보내면 응답은 대문자로 돌려줍니다:
AssertionError: expected 'CODESTATES' to equal '기댓값이 채워지지 않았습니다'
+ expected - actual
-CODESTATES
+기댓값이 채워지지 않았습니다
at /home/roheerumi/devops/class_study/cloud_study/cicd_study/ci_study/sprint-mini-node-server-with-github-action/test/app.test.js:31:32
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
4) 유닛 테스트 101
서버에 POST /lower 요청에 body를 실어 보내면 응답은 소문자로 돌려줍니다:
AssertionError: expected 'codestates' to equal '기댓값이 채워지지 않았습니다'
+ expected - actual
-codestates
+기댓값이 채워지지 않았습니다
at /home/roheerumi/devops/class_study/cloud_study/cicd_study/ci_study/sprint-mini-node-server-with-github-action/test/app.test.js:41:32
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
4. test/app.test.js 파일을 열어 통과하지 않는 테스트를 수정하세요. FILL_ME_IN이라고 적힌 곳에 기댓값을 적어주면 됩니다.
$ npm run test
> mini-node-server@1.0.0 test
> mocha ./test
Server listening on http://localhost:4000
유닛 테스트 101
✔ 결과에 대한 기대값(expectation value)를 비교하여 유닛 테스트를 할 수 있습니다
✔ 서버에 GET / 요청을 보내면 Hello World!라는 텍스트가 응답으로 옵니다 (61ms)
✔ 서버에 POST /upper 요청에 body를 실어 보내면 응답은 대문자로 돌려줍니다 (41ms)
✔ 서버에 POST /lower 요청에 body를 실어 보내면 응답은 소문자로 돌려줍니다
4 passing (126ms)
✏️ GitHub Action을 이용해서 Node.js CI를 적용 : Workflow YAML 만들기
# ...<프로젝트 디렉토리>/.github/workflos/node.js.yml
name: Push & Pull Request Workflow
on:
# master로 push 했을 경우
push:
branches: master
# pull request를 보낸 경우
pull_request:
# types:
# - unassigned
# - labeled
# - unlabeled
# - opened
# - edited
# - closed
# - reopened
# - synchronize
# - converted_to_draft
# - ready_for_review
# - locked
# - unlocked
# - review_requested
# - review_request_removed
# - auto_merge_enabled
# - auto_merge_disabled
branches: master
# 수행하려는 워크플로우
jobs:
# 빌드 실행
build:
# O/S
runs-on: ubuntu-latest
# 여러 O/S, 버전으로 자동화가 가능
strategy:
matrix:
node-version: [ 16 ] # node 버전은 16 버전으로 반드시 지정
# 작업을 수행하는 일련의 단계들
steps:
# GitHub Action에서 제공하는 actions/checkout 라이브러리 사용
- uses: actions/checkout@v3
- name: Use Node.js version ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
- run: npm ci
- run: npm run build --if-present
- run: npm run test
#TROUBLE SHOOTING LOG
📝 문제 1 : PR시 trigger 미작동
- 원인 : 내 repository에 PR이 되지 않아서
- 해결 방안 : PR을 할 때 내 repository로 요청
#References
https://docs.github.com/en/actions/using-workflows/creating-starter-workflows-for-your-organization
https://docs.github.com/en/actions/using-workflows/using-starter-workflows
https://www.youtube.com/watch?v=GhhoHOpp9HY
https://docs.github.com/en/actions/automating-builds-and-tests
'devops bootcamp 4 > pair/team log' 카테고리의 다른 글
Sprint - 클라이언트 배포 파이프라인 (0) | 2023.04.24 |
---|---|
Sprint - 환경 변수 분리 (0) | 2023.04.21 |
Sprint - 도메인 연결과 CDN 및 HTTPS 적용 (0) | 2023.04.19 |
Sprint - 3 Tier 아키텍처 배포 (0) | 2023.04.17 |
Sprint - 애플리케이션 컨테이너화 (0) | 2023.04.13 |