Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🤖 Update Helm chart unit tests #568

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion charts/test/unit/deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func defaultDeployment() appsv1.Deployment {
},
{
Name: "kube-rbac-proxy",
Image: "quay.io/brancz/kube-rbac-proxy:v0.18.2",
Image: fmt.Sprintf("%s:%s", helmChartValues.KubeRbacProxy.Image.Repository, helmChartValues.KubeRbacProxy.Image.Tag),
Args: []string{
"--secure-listen-address=0.0.0.0:8443",
"--upstream=http://127.0.0.1:8080/",
Expand Down
52 changes: 41 additions & 11 deletions charts/test/unit/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

"github.com/gruntwork-io/terratest/modules/helm"
"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v2"
"gopkg.in/yaml.v3"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
Expand Down Expand Up @@ -41,6 +41,7 @@ const (
var (
// Generic variables.
helmChartVersion = "0.0.0"
helmChartValues = &chartValues{}

// Deployment variables.
defaultDeploymentName = fmt.Sprintf("%s-%s", helmReleaseName, helmChartName)
Expand All @@ -58,12 +59,7 @@ var (
"app.kubernetes.io/name": helmChartName,
"control-plane": fmt.Sprintf("%s-controller-manager", helmReleaseName),
}
defaultDeploymentTemplateSpecLabels = map[string]string{
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is a cleanup of unused variables. This is not related to this change.

"control-plane": fmt.Sprintf("%s-controller-manager", helmReleaseName),
}
defaultDeploymentTerminationGracePeriodSeconds = int64(10)
defaultDeploymentTemplateVolumeName = "manager-config"
defaultDeploymentTemplateVolumeConfigMapName = fmt.Sprintf("%s-manager-config", helmReleaseName)

// RBAC variables.
defaultRBACRoleName = fmt.Sprintf("%s-leader-election-role", helmReleaseName)
Expand Down Expand Up @@ -93,29 +89,63 @@ func init() {
os.Exit(1)
}

if helmChartValues, err = getChartValues(); err != nil {
log.Fatal(err)
os.Exit(1)
}

defaultDeploymentLabels["helm.sh/chart"] = fmt.Sprintf("%s-%s", helmChartName, helmChartVersion)
defaultDeploymentLabels["app.kubernetes.io/version"] = helmChartVersion

defaultServiceAccountLabels["helm.sh/chart"] = fmt.Sprintf("%s-%s", helmChartName, helmChartVersion)
defaultServiceAccountLabels["app.kubernetes.io/version"] = helmChartVersion
}

type Chart struct {
// Chart.yaml
type chart struct {
Version string `yaml:"version"`
}

// values.yaml
type chartValues struct {
KubeRbacProxy kubeRbacProxy `yaml:"kubeRbacProxy"`
}

type kubeRbacProxy struct {
Image image `yaml:"image"`
}

type image struct {
Tag string `yaml:"tag"`
Repository string `yaml:"repository"`
}

func getChartVersion() (string, error) {
file, err := os.ReadFile(fmt.Sprintf("%s/Chart.yaml", helmChartPath))
if err != nil {
log.Fatalf("Error reading Chart.yaml: %v", err)
}

var chart Chart
if err := yaml.Unmarshal(file, &chart); err != nil {
return "", fmt.Errorf("Error unmarshalling YAML: %v", err)
var c chart
if err := yaml.Unmarshal(file, &c); err != nil {
return "", fmt.Errorf("error unmarshalling YAML: %v", err)
}

return c.Version, nil
}

func getChartValues() (*chartValues, error) {
file, err := os.ReadFile(fmt.Sprintf("%s/values.yaml", helmChartPath))
if err != nil {
log.Fatalf("Error reading values.yaml: %v", err)
}

var cv chartValues
if err := yaml.Unmarshal(file, &cv); err != nil {
return nil, fmt.Errorf("error unmarshalling YAML: %v", err)
}

return chart.Version, nil
return &cv, nil
}

func renderDeploymentManifest(t *testing.T, options *helm.Options) appsv1.Deployment {
Expand Down