Terratest Helm图表未通过Go单元测试

wtlkbnrh  于 2023-02-14  发布在  Go
关注(0)|答案(1)|浏览(88)

我尝试使用Terratest对我的Helm图表进行单元测试,但遇到了一个奇怪的错误:
下面是我的单元测试:

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
{[] [] [] []  <nil> <nil>  map[]   <nil>  false false false <nil> nil []   nil  [] []  <nil> nil [] <nil> <nil> <nil> map[] [] <nil>}*********************************************************--- FAIL: TestGrafanaHelmChartTemplate (0.06s)

下面是输出:

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

为什么会失败?我错过了什么?

kh212irz

kh212irz1#

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

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

然后,我必须修改Deployment对象的示例化:

var deployment appsv1.Deployment

而不是Pod对象。

相关问题