Study/오늘의 Youtube

(2020-05-06) Automated Testing for Terraform, Docker, Packer, Kubernetes, and More

chronosa 2020. 5. 6. 22:06

목차

     

     

    Terraform, Docker, Packer와 같은 infrastructure code를 Terratest를 이용하여 테스트하는 방법을 소개해주는 강의이다. 

    Terratest를 만든 gruntwork.io의 창업자 중 한명이 직접 강의하는 강의를 해주어서 그런지 설명을 쉽게 잘 해준다.

    Terraform 기준으로 내용을 정리하겠다.

     

    Static analysis (정적 분석)

    배포 없이 코드를 테스트하는 것으로, 문법적, 일반적인 에러를 처리할 수 있다.

    아래와 같은 카테고리로 분류할 수 있다.

    1. Compiler / Parser / interpreter
    2. Linter
    3. 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

    1. Deploy real infrastructure
    2. Validate it works
    3. 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가 필요하다면...

    디스크에 data를 저장한 후 Load하는 전략을 활용

    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)

    resource가 많아질 수록 failure가 발생할 확률이 높아짐.
    위로 올라갈수록 비용, 실행시간이 증가함

     

    모듈이 업데이트될때마다 그때그때 배포하고 검증하는 방법 (incremental e2e test)

     

    Conclusion

    결론 및 전체 내용 요약
    진짜 결론... 맞는 말이다.