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

huangapple go评论96阅读模式
英文:

Terratest Helm Charts Fails with Go Unit tests

问题

我正在尝试使用Terratest对我的Helm charts进行单元测试,但遇到了一个奇怪的错误:

这是我的单元测试代码:

package grafana

import (
	"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)
	}
}

这是部署的输出:

TestGrafanaHelmChartTemplate 2023-02-12T18:59:01+01:00 logger.go:66: apiVersion: apps/v1
TestGrafanaHelmChartTemplate 2023-02-12T18:59:01+01:00 logger.go:66: kind: Deployment
TestGrafanaHelmChartTemplate 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-monitoring
TestGrafanaHelmChartTemplate 2023-02-12T18:59:01+01:00 logger.go:66:   namespace: open-electrons-monitoring-ns
TestGrafanaHelmChartTemplate 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-grafana
TestGrafanaHelmChartTemplate 2023-02-12T18:59:01+01:00 logger.go:66:     app.kubernetes.io/component: monitoring
TestGrafanaHelmChartTemplate 2023-02-12T18:59:01+01:00 logger.go:66:     app.kubernetes.io/part-of: open-electrons-grafana
TestGrafanaHelmChartTemplate 2023-02-12T18:59:01+01:00 logger.go:66:     app.kubernetes.io/managed-by: helm
TestGrafanaHelmChartTemplate 2023-02-12T18:59:01+01:00 logger.go:66:     app.kubernetes.io/instance: open-electrons-grafana
TestGrafanaHelmChartTemplate 2023-02-12T18:59:01+01:00 logger.go:66:     app.kubernetes.io/version: refs/tags/v0.0.11 # TODO: Better use the Grafana version
TestGrafanaHelmChartTemplate 2023-02-12T18:59:01+01:00 logger.go:66: spec:
TestGrafanaHelmChartTemplate 2023-02-12T18:59:01+01:00 logger.go:66:   replicas: 1
TestGrafanaHelmChartTemplate 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-grafana
TestGrafanaHelmChartTemplate 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: 1
TestGrafanaHelmChartTemplate 2023-02-12T18:59:01+01:00 logger.go:66:       maxUnavailable: 1
TestGrafanaHelmChartTemplate 2023-02-12T18:59:01+01:00 logger.go:66:     type: RollingUpdate
TestGrafanaHelmChartTemplate 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: null
TestGrafanaHelmChartTemplate 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-grafana
TestGrafanaHelmChartTemplate 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: 1000
TestGrafanaHelmChartTemplate 2023-02-12T18:59:01+01:00 logger.go:66:         runAsGroup: 3000
TestGrafanaHelmChartTemplate 2023-02-12T18:59:01+01:00 logger.go:66:         fsGroup: 2000
TestGrafanaHelmChartTemplate 2023-02-12T18:59:01+01:00 logger.go:66:         runAsNonRoot: true
TestGrafanaHelmChartTemplate 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:latest
TestGrafanaHelmChartTemplate 2023-02-12T18:59:01+01:00 logger.go:66:           imagePullPolicy: IfNotPresent
TestGrafanaHelmChartTemplate 2023-02-12T18:59:01+01:00 logger.go:66:           name: open-electrons-grafana
TestGrafanaHelmChartTemplate 2023-02-12T18:59:01+01:00 logger.go:66:           ports:
TestGrafanaHelmChartTemplate 2023-02-12T18:59:01+01:00 logger.go:66:             - containerPort: 3000
TestGrafanaHelmChartTemplate 2023-02-12T18:59:01+01:00 logger.go:66:               protocol: TCP
TestGrafanaHelmChartTemplate 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: 500M
TestGrafanaHelmChartTemplate 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/grafana
TestGrafanaHelmChartTemplate 2023-02-12T18:59:01+01:00 logger.go:66:               name: grafana-storage
TestGrafanaHelmChartTemplate 2023-02-12T18:59:01+01:00 logger.go:66:             - mountPath: /etc/grafana/provisioning/datasources
TestGrafanaHelmChartTemplate 2023-02-12T18:59:01+01:00 logger.go:66:               name: grafana-datasources
TestGrafanaHelmChartTemplate 2023-02-12T18:59:01+01:00 logger.go:66:               readOnly: false
TestGrafanaHelmChartTemplate 2023-02-12T18:59:01+01:00 logger.go:66:       restartPolicy: Always
TestGrafanaHelmChartTemplate 2023-02-12T18:59:01+01:00 logger.go:66:       terminationGracePeriodSeconds: 30
TestGrafanaHelmChartTemplate 2023-02-12T18:59:01+01:00 logger.go:66:       volumes:
TestGrafanaHelmChartTemplate 2023-02-12T18:59:01+01:00 logger.go:66:         - name: grafana-storage
TestGrafanaHelmChartTemplate 2023-02-12T18:59:01+01:00 logger.go:66:           emptyDir: {}
TestGrafanaHelmChartTemplate 2023-02-12T18:59:01+01:00 logger.go:66:         - name: grafana-datasources
TestGrafanaHelmChartTemplate 2023-02-12T18:59:01+01:00 logger.go:66:           configMap:
TestGrafanaHelmChartTemplate 2023-02-12T18:59:01+01:00 logger.go:66:             defaultMode: 420
TestGrafanaHelmChartTemplate 2023-02-12T18:59:01+01:00 logger.go:66:             name: grafana-datasources

这是输出:

panic: runtime error: index out of range [0] with length 0 [recovered]
	panic: runtime error: index out of range [0] with length 0

goroutine 5 [running]:
testing.tRunner.func1.2({0x1440620, 0xc0002a85b8})
	/usr/local/go/src/testing/testing.go:1526 +0x24e
testing.tRunner.func1()
	/usr/local/go/src/testing/testing.go:1529 +0x39f
panic({0x1440620, 0xc0002a85b8})
	/usr/local/go/src/runtime/panic.go:884 +0x213

为什么会失败?我在这里漏掉了什么?

英文:

I'm trying to unit test my Helm charts using Terratest, but running into a strange error:

Here is my unit test:

package grafana

import (
	"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)
	}
}

Here is what the output of the deployment looks like:

TestGrafanaHelmChartTemplate 2023-02-12T18:59:01+01:00 logger.go:66: apiVersion: apps/v1
TestGrafanaHelmChartTemplate 2023-02-12T18:59:01+01:00 logger.go:66: kind: Deployment
TestGrafanaHelmChartTemplate 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-monitoring
TestGrafanaHelmChartTemplate 2023-02-12T18:59:01+01:00 logger.go:66:   namespace: open-electrons-monitoring-ns
TestGrafanaHelmChartTemplate 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-grafana
TestGrafanaHelmChartTemplate 2023-02-12T18:59:01+01:00 logger.go:66:     app.kubernetes.io/component: monitoring
TestGrafanaHelmChartTemplate 2023-02-12T18:59:01+01:00 logger.go:66:     app.kubernetes.io/part-of: open-electrons-grafana
TestGrafanaHelmChartTemplate 2023-02-12T18:59:01+01:00 logger.go:66:     app.kubernetes.io/managed-by: helm
TestGrafanaHelmChartTemplate 2023-02-12T18:59:01+01:00 logger.go:66:     app.kubernetes.io/instance: open-electrons-grafana
TestGrafanaHelmChartTemplate 2023-02-12T18:59:01+01:00 logger.go:66:     app.kubernetes.io/version: refs/tags/v0.0.11 # TODO: Better use the Grafana version
TestGrafanaHelmChartTemplate 2023-02-12T18:59:01+01:00 logger.go:66: spec:
TestGrafanaHelmChartTemplate 2023-02-12T18:59:01+01:00 logger.go:66:   replicas: 1
TestGrafanaHelmChartTemplate 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-grafana
TestGrafanaHelmChartTemplate 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: 1
TestGrafanaHelmChartTemplate 2023-02-12T18:59:01+01:00 logger.go:66:       maxUnavailable: 1
TestGrafanaHelmChartTemplate 2023-02-12T18:59:01+01:00 logger.go:66:     type: RollingUpdate
TestGrafanaHelmChartTemplate 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: null
TestGrafanaHelmChartTemplate 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-grafana
TestGrafanaHelmChartTemplate 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: 1000
TestGrafanaHelmChartTemplate 2023-02-12T18:59:01+01:00 logger.go:66:         runAsGroup: 3000
TestGrafanaHelmChartTemplate 2023-02-12T18:59:01+01:00 logger.go:66:         fsGroup: 2000
TestGrafanaHelmChartTemplate 2023-02-12T18:59:01+01:00 logger.go:66:         runAsNonRoot: true
TestGrafanaHelmChartTemplate 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:latest
TestGrafanaHelmChartTemplate 2023-02-12T18:59:01+01:00 logger.go:66:           imagePullPolicy: IfNotPresent
TestGrafanaHelmChartTemplate 2023-02-12T18:59:01+01:00 logger.go:66:           name: open-electrons-grafana
TestGrafanaHelmChartTemplate 2023-02-12T18:59:01+01:00 logger.go:66:           ports:
TestGrafanaHelmChartTemplate 2023-02-12T18:59:01+01:00 logger.go:66:             - containerPort: 3000
TestGrafanaHelmChartTemplate 2023-02-12T18:59:01+01:00 logger.go:66:               protocol: TCP
TestGrafanaHelmChartTemplate 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: 500M
TestGrafanaHelmChartTemplate 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/grafana
TestGrafanaHelmChartTemplate 2023-02-12T18:59:01+01:00 logger.go:66:               name: grafana-storage
TestGrafanaHelmChartTemplate 2023-02-12T18:59:01+01:00 logger.go:66:             - mountPath: /etc/grafana/provisioning/datasources
TestGrafanaHelmChartTemplate 2023-02-12T18:59:01+01:00 logger.go:66:               name: grafana-datasources
TestGrafanaHelmChartTemplate 2023-02-12T18:59:01+01:00 logger.go:66:               readOnly: false
TestGrafanaHelmChartTemplate 2023-02-12T18:59:01+01:00 logger.go:66:       restartPolicy: Always
TestGrafanaHelmChartTemplate 2023-02-12T18:59:01+01:00 logger.go:66:       terminationGracePeriodSeconds: 30
TestGrafanaHelmChartTemplate 2023-02-12T18:59:01+01:00 logger.go:66:       volumes:
TestGrafanaHelmChartTemplate 2023-02-12T18:59:01+01:00 logger.go:66:         - name: grafana-storage
TestGrafanaHelmChartTemplate 2023-02-12T18:59:01+01:00 logger.go:66:           emptyDir: {}
TestGrafanaHelmChartTemplate 2023-02-12T18:59:01+01:00 logger.go:66:         - name: grafana-datasources
TestGrafanaHelmChartTemplate 2023-02-12T18:59:01+01:00 logger.go:66:           configMap:
TestGrafanaHelmChartTemplate 2023-02-12T18:59:01+01:00 logger.go:66:             defaultMode: 420
TestGrafanaHelmChartTemplate 2023-02-12T18:59:01+01:00 logger.go:66:             name: grafana-datasources
{[] [] [] []  <nil> <nil>  map[]   <nil>  false false false <nil> nil []   nil  [] []  <nil> nil [] <nil> <nil> <nil> map[] [] <nil>}*********************************************************--- FAIL: TestGrafanaHelmChartTemplate (0.06s)

Here is the output:

panic: runtime error: index out of range [0] with length 0 [recovered]
	panic: runtime error: index out of range [0] with length 0

goroutine 5 [running]:
testing.tRunner.func1.2({0x1440620, 0xc0002a85b8})
	/usr/local/go/src/testing/testing.go:1526 +0x24e
testing.tRunner.func1()
	/usr/local/go/src/testing/testing.go:1529 +0x39f
panic({0x1440620, 0xc0002a85b8})
	/usr/local/go/src/runtime/panic.go:884 +0x213

Why should this fail? What am I missing here?

答案1

得分: 0

我成功修复了。导入应该像这样:

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

然后我需要修改Deployment对象的实例化方式:

var deployment appsv1.Deployment

而不是Pod对象。

英文:

I managed to get it fixed. The import should be like this:

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

I then have to modify the instantiation of the Deployment object:

var deployment appsv1.Deployment

instead of the Pod object.

huangapple
  • 本文由 发表于 2023年2月13日 02:03:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/75429085.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定