Maison > développement back-end > Golang > le corps du texte

Les graphiques Helm Terratest échouent dans les tests unitaires Go

WBOY
Libérer: 2024-02-08 23:06:28
avant
916 Les gens l'ont consulté

L'éditeur php Strawberry a découvert que les graphiques Helm Terratest rencontraient souvent des problèmes d'échec dans les tests unitaires Go. Terratest est une bibliothèque Go permettant d'écrire des tests d'infrastructure automatisés et reproductibles, et Helm Charts est un outil de gestion de packages pour Kubernetes. Lors de l'utilisation de Terratest pour les tests unitaires de Helm Charts, divers problèmes surviennent parfois, entraînant l'échec du test. Cet article explorera les causes de ces problèmes et fournira les solutions correspondantes pour aider les développeurs à mieux relever ces défis. Que vous soyez débutant ou développeur expérimenté, vous pouvez acquérir des connaissances et des compétences utiles grâce à cet article.

Terratest Helm Charts 在 Go 单元测试中失败Contenu de la question

J'essaie d'utiliser terratest pour tester unitairement ma carte de barre, mais j'obtiens une erreur étrange :

Voici mon test unitaire :

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)
    }
}
Copier après la connexion

Voici le résultat du déploiement :

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)
Copier après la connexion

Voici le résultat :

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
Copier après la connexion

Pourquoi a-t-il échoué ? Qu'est-ce que j'oublie ici?

Solution

J'ai réussi à le réparer. L'importation devrait ressembler à ceci :

appsv1 "k8s.io/api/apps/v1"
Copier après la connexion

Ensuite je dois modifier l'instanciation de l'objet de déploiement :

var deployment appsv1.Deployment
Copier après la connexion

Au lieu d'un objet pod.

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

source:stackoverflow.com
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!