Terratest Helm Charts 在 Go 单元测试中失败

Terratest Helm Charts 在 Go 单元测试中失败

php小编草莓发现,Terratest Helm Charts 在 Go 单元测试中经常遇到失败的问题。Terratest 是一个用于编写自动化、可重复的基础设施测试的 Go 语言库,而 Helm Charts 则是 Kubernetes 的包管理工具。在使用 Terratest 进行 Helm Charts 的单元测试时,有时会出现各种各样的问题,导致测试失败。本文将探讨这些问题的原因,并提供相应的解决方案,帮助开发者更好地应对这些挑战。无论是初学者还是有经验的开发者,都能从本文中获得有益的知识和技巧。

问题内容

我正在尝试使用 terratest 对我的 helm 图表进行单元测试,但遇到了一个奇怪的错误:

这是我的单元测试:

  1. package grafanaimport ( "fmt" "testing" corev1 "k8s.io/api/core/v1" "github.com/gruntwork-io/terratest/modules/helm")func testgrafanahelmcharttemplate(t *testing.t) { // path to the helm chart we will test helmchartgrafanapath := "../../../open-electrons-monitoring" // setup the args. for this test, we will set the following input values: // - image=grafana:latest options := &helm.options{ setvalues: map[string]string{"image": "grafana:latest"}, } // run rendertemplate to render the template and capture the output. output := helm.rendertemplate(t, options, helmchartgrafanapath, "pod", []string{"templates/grafana/grafana-deployment.yml"}) // now we use kubernetes/client-go library to render the template output into the pod struct. this will // ensure the pod resource is rendered correctly. var pod corev1.pod helm.unmarshalk8syaml(t, output, &pod) // finally, we verify the pod spec is set to the expected container image value expectedcontainerimage := "grafana:latest" podcontainers := pod.spec.containers fmt.print(pod.spec) fmt.print("*********************************************************") if podcontainers[0].image != expectedcontainerimage { t.fatalf("rendered container image (%s) is not expected (%s)", podcontainers[0].image, expectedcontainerimage) }}

登录后复制

以下是部署的输出:

  1. testgrafanahelmcharttemplate 2023-02-12t18:59:01+01:00 logger.go:66: apiversion: apps/v1testgrafanahelmcharttemplate 2023-02-12t18:59:01+01:00 logger.go:66: kind: deploymenttestgrafanahelmcharttemplate 2023-02-12t18:59:01+01:00 logger.go:66: metadata:testgrafanahelmcharttemplate 2023-02-12t18:59:01+01:00 logger.go:66: name: grafana-open-electrons-monitoringtestgrafanahelmcharttemplate 2023-02-12t18:59:01+01:00 logger.go:66: namespace: open-electrons-monitoring-nstestgrafanahelmcharttemplate 2023-02-12t18:59:01+01:00 logger.go:66: labels:testgrafanahelmcharttemplate 2023-02-12t18:59:01+01:00 logger.go:66: app.kubernetes.io/name: open-electrons-grafanatestgrafanahelmcharttemplate 2023-02-12t18:59:01+01:00 logger.go:66: app.kubernetes.io/component: monitoringtestgrafanahelmcharttemplate 2023-02-12t18:59:01+01:00 logger.go:66: app.kubernetes.io/part-of: open-electrons-grafanatestgrafanahelmcharttemplate 2023-02-12t18:59:01+01:00 logger.go:66: app.kubernetes.io/managed-by: helmtestgrafanahelmcharttemplate 2023-02-12t18:59:01+01:00 logger.go:66: app.kubernetes.io/instance: open-electrons-grafanatestgrafanahelmcharttemplate 2023-02-12t18:59:01+01:00 logger.go:66: app.kubernetes.io/version: refs/tags/v0.0.11 # todo: better use the grafana versiontestgrafanahelmcharttemplate 2023-02-12t18:59:01+01:00 logger.go:66: spec:testgrafanahelmcharttemplate 2023-02-12t18:59:01+01:00 logger.go:66: replicas: 1testgrafanahelmcharttemplate 2023-02-12t18:59:01+01:00 logger.go:66: selector:testgrafanahelmcharttemplate 2023-02-12t18:59:01+01:00 logger.go:66: matchlabels:testgrafanahelmcharttemplate 2023-02-12t18:59:01+01:00 logger.go:66: app: open-electrons-grafanatestgrafanahelmcharttemplate 2023-02-12t18:59:01+01:00 logger.go:66: strategy:testgrafanahelmcharttemplate 2023-02-12t18:59:01+01:00 logger.go:66: rollingupdate:testgrafanahelmcharttemplate 2023-02-12t18:59:01+01:00 logger.go:66: maxsurge: 1testgrafanahelmcharttemplate 2023-02-12t18:59:01+01:00 logger.go:66: maxunavailable: 1testgrafanahelmcharttemplate 2023-02-12t18:59:01+01:00 logger.go:66: type: rollingupdatetestgrafanahelmcharttemplate 2023-02-12t18:59:01+01:00 logger.go:66: template:testgrafanahelmcharttemplate 2023-02-12t18:59:01+01:00 logger.go:66: metadata:testgrafanahelmcharttemplate 2023-02-12t18:59:01+01:00 logger.go:66: creationtimestamp: nulltestgrafanahelmcharttemplate 2023-02-12t18:59:01+01:00 logger.go:66: labels:testgrafanahelmcharttemplate 2023-02-12t18:59:01+01:00 logger.go:66: name: open-electrons-grafanatestgrafanahelmcharttemplate 2023-02-12t18:59:01+01:00 logger.go:66: spec:testgrafanahelmcharttemplate 2023-02-12t18:59:01+01:00 logger.go:66: securitycontext:testgrafanahelmcharttemplate 2023-02-12t18:59:01+01:00 logger.go:66: runasuser: 1000testgrafanahelmcharttemplate 2023-02-12t18:59:01+01:00 logger.go:66: runasgroup: 3000testgrafanahelmcharttemplate 2023-02-12t18:59:01+01:00 logger.go:66: fsgroup: 2000testgrafanahelmcharttemplate 2023-02-12t18:59:01+01:00 logger.go:66: runasnonroot: truetestgrafanahelmcharttemplate 2023-02-12t18:59:01+01:00 logger.go:66: containers:testgrafanahelmcharttemplate 2023-02-12t18:59:01+01:00 logger.go:66: - image: grafana/grafana:latesttestgrafanahelmcharttemplate 2023-02-12t18:59:01+01:00 logger.go:66: imagepullpolicy: ifnotpresenttestgrafanahelmcharttemplate 2023-02-12t18:59:01+01:00 logger.go:66: name: open-electrons-grafanatestgrafanahelmcharttemplate 2023-02-12t18:59:01+01:00 logger.go:66: ports:testgrafanahelmcharttemplate 2023-02-12t18:59:01+01:00 logger.go:66: - containerport: 3000testgrafanahelmcharttemplate 2023-02-12t18:59:01+01:00 logger.go:66: protocol: tcptestgrafanahelmcharttemplate 2023-02-12t18:59:01+01:00 logger.go:66: resources:testgrafanahelmcharttemplate 2023-02-12t18:59:01+01:00 logger.go:66: limits:testgrafanahelmcharttemplate 2023-02-12t18:59:01+01:00 logger.go:66: memory: "1gi"testgrafanahelmcharttemplate 2023-02-12t18:59:01+01:00 logger.go:66: cpu: "1000m"testgrafanahelmcharttemplate 2023-02-12t18:59:01+01:00 logger.go:66: requests:testgrafanahelmcharttemplate 2023-02-12t18:59:01+01:00 logger.go:66: memory: 500mtestgrafanahelmcharttemplate 2023-02-12t18:59:01+01:00 logger.go:66: cpu: "500m"testgrafanahelmcharttemplate 2023-02-12t18:59:01+01:00 logger.go:66: volumemounts:testgrafanahelmcharttemplate 2023-02-12t18:59:01+01:00 logger.go:66: - mountpath: /var/lib/grafanatestgrafanahelmcharttemplate 2023-02-12t18:59:01+01:00 logger.go:66: name: grafana-storagetestgrafanahelmcharttemplate 2023-02-12t18:59:01+01:00 logger.go:66: - mountpath: /etc/grafana/provisioning/datasourcestestgrafanahelmcharttemplate 2023-02-12t18:59:01+01:00 logger.go:66: name: grafana-datasourcestestgrafanahelmcharttemplate 2023-02-12t18:59:01+01:00 logger.go:66: readonly: falsetestgrafanahelmcharttemplate 2023-02-12t18:59:01+01:00 logger.go:66: restartpolicy: alwaystestgrafanahelmcharttemplate 2023-02-12t18:59:01+01:00 logger.go:66: terminationgraceperiodseconds: 30testgrafanahelmcharttemplate 2023-02-12t18:59:01+01:00 logger.go:66: volumes:testgrafanahelmcharttemplate 2023-02-12t18:59:01+01:00 logger.go:66: - name: grafana-storagetestgrafanahelmcharttemplate 2023-02-12t18:59:01+01:00 logger.go:66: emptydir: {}testgrafanahelmcharttemplate 2023-02-12t18:59:01+01:00 logger.go:66: - name: grafana-datasourcestestgrafanahelmcharttemplate 2023-02-12t18:59:01+01:00 logger.go:66: configmap:testgrafanahelmcharttemplate 2023-02-12t18:59:01+01:00 logger.go:66: defaultmode: 420testgrafanahelmcharttemplate 2023-02-12t18:59:01+01:00 logger.go:66: name: grafana-datasources{[] [] [] [] map[] false false false nil [] nil [] [] nil [] map[] [] }*********************************************************--- fail: testgrafanahelmcharttemplate (0.06s)

登录后复制

这是输出:

  1. panic: runtime error: index out of range [0] with length 0 [recovered] panic: runtime error: index out of range [0] with length 0goroutine 5 [running]:testing.tRunner.func1.2({0x1440620, 0xc0002a85b8}) /usr/local/go/src/testing/testing.go:1526 +0x24etesting.tRunner.func1() /usr/local/go/src/testing/testing.go:1529 +0x39fpanic({0x1440620, 0xc0002a85b8}) /usr/local/go/src/runtime/panic.go:884 +0x213

登录后复制

为什么会失败?我在这里缺少什么?

解决方法

我设法把它修好了。导入应该是这样的:

  1. appsv1 "k8s.io/api/apps/v1"

登录后复制

然后我必须修改 deployment 对象的实例化:

  1. var deployment appsv1.Deployment

登录后复制

而不是 pod 对象。

以上就是Terratest Helm Charts 在 Go 单元测试中失败的详细内容,更多请关注【创想鸟】其它相关文章!

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

点点赞赏,手留余香

给TA打赏
共0人
还没有人赞赏,快来当第一个赞赏的人吧!
    编程技术

    Go错误:无法在没有实例化的情况下使用泛型类型

    2025-3-4 21:40:16

    编程技术

    如何在 Go 中运行前台或后台 shell 命令

    2025-3-4 21:40:35

    0 条回复 A文章作者 M管理员
    欢迎您,新朋友,感谢参与互动!
      暂无讨论,说说你的看法吧
    个人中心
    购物车
    优惠劵
    今日签到
    私信列表
    搜索