使用 helm unittest 时导致“没有与 gotpl 关联的模板”错误的原因是什么?

问题描述 投票:0回答:2

首先我应该说我对 helm 世界相当陌生 - 然而我正在学习其他人开发的一些 helm 图表,并希望添加一些单元测试。我已经安装了https://github.com/helm-unittest/helm-unittest,并创建了一个基本的测试文件:

suite: test xyz deployment
templates:
  - xyz-deployment.yaml
tests:
  - it: will not do much yet
    set:
      image.tag: latest
    asserts:
      - isKind:
        of: Deployment

我的图表文件结构如下:

> xyz
  > templates
     xyz.configmap.yaml
     xyz.deployment.yaml
     <others>
  > tests
     xyz_test.yaml

xyz.deployment.yaml
内我有(为了简洁而剪掉):

spec:
  template:
    metadata:
      annotations:
        checksum/config: {{ include (print $.Template.BasePath "/xyz-configmap.yaml") . | sha256sum }}

linting 运行干净(除了图标推荐)。然而,当我运行

helm unittest xyz
时,我得到:

$ helm unittest xyz

### Chart [ xyz ] xyz

 FAIL  test xyz deployment xyz/tests/xyz_test.yaml
        - deployment should render
                Error: template: xyz/templates/xyz-deployment.yaml:16:30: executing "xyz/templates/xyz-deployment.yaml" at <include (print $.Template.BasePath "/xyz-configmap.yaml") .>: error calling include: template: no template "xyz/templates/xyz-configmap.yaml" associated with template "gotpl"


Charts:      1 failed, 0 passed, 1 total
Test Suites: 1 failed, 0 passed, 1 total
Tests:       1 failed, 1 errored, 0 passed, 1 total
Snapshot:    0 passed, 0 total
Time:        14.423497ms

Error: plugin "unittest" exited with error

显然 configmap.yaml 在那里,所以我不知道为什么单元测试插件报告找不到它。有人可以帮忙吗?

unit-testing kubernetes-helm
2个回答
2
投票

终于找到答案了!更改我的测试文件,使其看起来更像:

suite: test xyz deployment
tests:
  - it: will not do much yet
    set:
      image.tag: latest
    template: xyz-deployment.yaml
    asserts:
      - isKind:
        of: Deployment

即,在单个规范块中指定模板,而不是在顶层,这是一种享受!


0
投票

找到相关的GH问题:https://github.com/helm-unittest/helm-unittest/issues/293

在每个测试下设置模板将允许其工作:

suite: test xyz deployment
tests:
  - it: will not do much yet
    set:
      image.tag: latest
    template: xyz-deployment.yaml
    asserts:
      - isKind:
        of: Deployment
© www.soinside.com 2019 - 2024. All rights reserved.