Sau những lần sửa đổi helm chart và bạn muốn chắc chắn nó vẫn chạy đúng các logic cũ đã được design từ trước đó. Chắc chắn bạn sẽ cần đến unittest.
Link: https://github.com/quintush/helm-unittest
Đi luôn vào demo.
1) Add unittest into helm
1.1) Plugin unittest
Đầu tiền nơi mà bạn chạy phải cài helm nhé. Sau đó bạn cài plugin:
helm plugin install https://github.com/quintush/helm-unittest
1.2) Docker
Ngoại ra để thích hợp cho môi trường ci/cd thì bạn cũng có các container đã được dựng sẵn:
# run help of latest helm with latest helm unittest plugin
docker run -ti --rm -v $(pwd):/apps quintush/helm-unittest
# run help of specific helm version with specific helm unittest plugin version
docker run -ti --rm -v $(pwd):/apps quintush/helm-unittest:3.3.0-0.2.2
# run unittests of a helm 2 chart
# make sure to mount local folder to /apps in container
docker run -ti --rm -v $(pwd):/apps quintush/helm-unittest:2.16.10-0.2.2 .
# run unittests of a helm 3 chart
# make sure to mount local folder to /apps in container
docker run -ti --rm -v $(pwd):/apps quintush/helm-unittest:3.3.0-0.2.2 -3 .
# run unittests of a helm 3 chart with Junit output for CI validation
# make sure to mount local folder to /apps in container
# the test-output.xml will be available in the local folder.
docker run -ti --rm -v $(pwd):/apps quintush/helm-unittest:3.3.0-0.2.2 -3 -o test-output.xml .
2) Practice
2.1) Running unit_test.
giờ bạn thực hiện git clone về nhé:
cd helm-unittest/test/data/v2/basic/
helm unittest .
Các file test kết thúc bằng chữ _test.yaml nhé
Link này chỉ cho bạn các cách thức khai báo set check để thực thi unittest.
https://github.com/quintush/helm-unittest/blob/master/DOCUMENT.md
Khó nhất của viết unittest, bạn cần tưởng tượng ra case sảy ra.
- asserts: array of assertion, required. The assertions to validate the rendered chart, check Assertion.
sau asserts là các so sánh và kiểm tra giữa điều kiện của unittest và temple được gen ra theo file value.
2.2) run unittest with hasDocuments by 2
Ở đây mình có 1 trường hợp đặc biệt hơn, 1file template nhưng gen 2 documents
Gen ra nhiều Persistentvolumeclaim he
file unittest như sau:
- it: render when declare single Persistentvolumeclaim name
values:
- ./values/multiple_pvc.yaml
template: pvc.yaml
asserts:
- hasDocuments:
count: 2
- isKind:
of: PersistentVolumeClaim
- isAPIVersion:
of: v1
- equal:
path: metadata.name
value: nfs-aiolos-src-pvc
- equal:
path: metadata.name
value: nfs-airflow-logs-pvc
Vậy chúng ta cần thêm documentIndex
Là hết lỗi thôi.
2.3) send values from unit-test to helm-chart.(update Tue, Jan 18th, 2021)
Ngoại việc bạn set value trong
+ file value.yaml của helm-chart
+ file value-test-xxx.yaml của unit-test
thì bạn có thể sử dụng khai báo “set” để gửi value vào helm-chart
set:
ingressGRPC.canary.canaryBy: header