-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: helpers methods to test chart templates rendering (#18)
- Loading branch information
Showing
7 changed files
with
181 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
//go:build integration | ||
// +build integration | ||
|
||
package steps | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"testing" | ||
|
||
"github.com/krateoplatformops/installer/apis/workflows/v1alpha1" | ||
"github.com/krateoplatformops/installer/internal/cache" | ||
"github.com/krateoplatformops/installer/internal/helmclient" | ||
"github.com/krateoplatformops/installer/internal/ptr" | ||
"github.com/krateoplatformops/provider-runtime/pkg/logging" | ||
"helm.sh/helm/v3/pkg/action" | ||
pkgcli "helm.sh/helm/v3/pkg/cli" | ||
"helm.sh/helm/v3/pkg/getter" | ||
) | ||
|
||
func TestChartTemplate(t *testing.T) { | ||
const ( | ||
ns = "demo-system" | ||
chartPath = "../../../testdata/chart-unit-test" | ||
) | ||
|
||
data := []*v1alpha1.Data{ | ||
{ | ||
Name: "sftp.allowedMACs", | ||
Value: "null", | ||
AsString: ptr.To(true), | ||
}, | ||
{ | ||
Name: "env.KRATEO_GATEWAY_DNS_NAMES", | ||
Value: "{krateo-gateway.sticz.svc,$KRATEO_GATEWAY_INGRESS_HOST}", | ||
}, | ||
} | ||
|
||
env := cache.New[string, string]() | ||
env.Set("KRATEO_GATEWAY_INGRESS_HOST", "http://sti.cz") | ||
|
||
hdl, err := chartStepHandlerForNamespace(ns, env) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
opts := hdl.valuesOptions("test", data) | ||
|
||
yml, err := renderChart(&helmclient.ChartSpec{ | ||
Namespace: ns, | ||
ValuesOptions: opts, | ||
}, ns, chartPath) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
fmt.Println(yml) | ||
} | ||
|
||
func chartStepHandlerForNamespace(ns string, env *cache.Cache[string, string]) (*chartStepHandler, error) { | ||
rc, err := newRestConfig() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
cli, err := helmClientForNamespace(rc, ns) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
logr := logging.NewLogrLogger(newStdoutLogger()) | ||
|
||
return &chartStepHandler{ | ||
cli: cli, env: env, | ||
subst: func(k string) string { | ||
if v, ok := env.Get(k); ok { | ||
return v | ||
} | ||
|
||
return "$" + k | ||
}, | ||
logr: logr, | ||
}, nil | ||
} | ||
|
||
func renderChart(spec *helmclient.ChartSpec, ns, chartPath string) (string, error) { | ||
p := getter.All(pkgcli.New()) | ||
values, err := spec.GetValuesMap(p) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
chart, err := loadChart(os.DirFS(chartPath)) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
client := action.NewInstall(&action.Configuration{}) | ||
client.ClientOnly = true | ||
client.DryRun = true | ||
client.ReleaseName = "test" | ||
client.IncludeCRDs = true | ||
client.Namespace = ns | ||
|
||
rel, err := client.Run(chart, values) | ||
if err != nil { | ||
return "", fmt.Errorf("could not render helm chart correctly: %w", err) | ||
} | ||
return rel.Manifest, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
apiVersion: v2 | ||
name: some-chart | ||
version: 1.0.0 | ||
appVersion: 1.0.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
apiVersion: v1 | ||
kind: ConfigMap | ||
metadata: | ||
name: {{ printf "%s-%s" .Chart.Name .Release.Name | trunc 63 | trimSuffix "-" }} | ||
namespace: {{ .Release.Namespace }} | ||
labels: | ||
{{- with .Values.labels -}} | ||
{{ toYaml . | nindent 4 }} | ||
{{- end }} | ||
data: | ||
allowedMACs: {{ .Values.sftp.allowedMACs }} | ||
KRATEO_GATEWAY_DNS_NAMES: {{ .Values.env.KRATEO_GATEWAY_DNS_NAMES }} | ||
something: something |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
apiVersion: v1 | ||
kind: Secret | ||
metadata: | ||
name: {{ printf "%s-%s" .Chart.Name .Release.Name | trunc 63 | trimSuffix "-" }} | ||
namespace: {{ .Release.Namespace }} | ||
labels: | ||
{{- with .Values.labels -}} | ||
{{ toYaml . | nindent 4 }} | ||
{{- end }} | ||
stringData: | ||
something-secret: shhhhh |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
labels: | ||
some: default | ||
|
||
sftp: | ||
allowedMAC: xxx | ||
|
||
env: | ||
KRATEO_GATEWAY_DNS_NAMES: yyy |