목차
Terraform, Docker, Packer와 같은 infrastructure code를 Terratest를 이용하여 테스트하는 방법을 소개해주는 강의이다.
Terratest를 만든 gruntwork.io의 창업자 중 한명이 직접 강의하는 강의를 해주어서 그런지 설명을 쉽게 잘 해준다.
Terraform 기준으로 내용을 정리하겠다.
Static analysis (정적 분석)
배포 없이 코드를 테스트하는 것으로, 문법적, 일반적인 에러를 처리할 수 있다.
아래와 같은 카테고리로 분류할 수 있다.
- Compiler / Parser / interpreter
- Linter
- Dry run
1. Compiler / Parser / Interpreter
terraform validate, packer validate <template>
2. Linter
conftest, terraform-validate, tflint
3. Dry run
terraform plan, terraform-compliance
Unit tests
인프라는 Unit Test를 할 때 일반적인 Application 코드와는 다른 제약이 있다.
1) Single Method, Class는 각각의 Module에 해당된다. Module로 분리해야만 Unit Test가 가능하다.
2) 99% infra code는 외부세계와 연결되어 있고, 독립적으로 unit test를 수행할 수 없다.
즉, 직접 실제 환경에 배포하는 것이 유일한 방법!
(there's no pure unit testing for infrastructure code.)
test strategy
- Deploy real infrastructure
- Validate it works
- Undeploy the infrastruture
tools that help with this strategy
Terratest, kitchen-terraform, Inspec, ...
Examples of other ways to validate
- web service - HTTP Requests (Terratest http_helper package)
- server - SSH commands (Terratest ssh package)
- Cloud service - Cloud APIs (Terratest aws or gcp package)
- Database - SQL queries (MySQL driver for Go)
clean-up resource
cloud-nuke, aws-nuke ...
Intergration tests
test multiple "units" work together.
parallel
test 속도 개선을 위해 병렬적으로 실행해라.
단, conflict를 피하기 위해 테스트 시마다 unique한 값을 지정해라.
break your tests into independent test stages
특정 stage만을 test하기 위해 stage(step)을 나눠라.
each time you run test stages via go test, it's a seperate OS process.
만약 data share가 필요하다면...
Real Infrastructure can fail for intermittent reasons
to aviod "flaky"t test, add retries for know errors.
End-to-end tests
test your entire infrastructure works together.
위에서 소개했던 실제 환경에 배포하는 방법을 사용해야 한다.
근데 매번하기는 힘들고, 전체 배포에서는 failure가 발생할 확률이 높으므로
환경을 만들어두고 업데이트가 될 때마다 그때그때 테스트 환경에 배포하는 방법을 권고하고 있다.
(incremental e2e test)
Conclusion
'Study > 오늘의 Youtube' 카테고리의 다른 글
Learn JMeter in 60 minutes (0) | 2020.06.06 |
---|---|
AWS Kinesis Stream - I (0) | 2020.05.23 |
(2020-04-08) Best Practices of Infrastructure as Code with HashiCorp Terraform (0) | 2020.04.09 |
(2020-04-06) Monitoring, the Prometheus Way (0) | 2020.04.06 |