From da7470823cff78f1af18805fef6acd851290ef72 Mon Sep 17 00:00:00 2001 From: Matheus Pimenta Date: Fri, 13 Dec 2024 14:36:39 +0000 Subject: [PATCH] Move common CRDs test chart to testutil package Signed-off-by: Matheus Pimenta --- internal/reconcile/install_test.go | 32 +--------------------------- internal/reconcile/upgrade_test.go | 33 +---------------------------- internal/testutil/mock_chart.go | 34 ++++++++++++++++++++++++++++++ 3 files changed, 36 insertions(+), 63 deletions(-) diff --git a/internal/reconcile/install_test.go b/internal/reconcile/install_test.go index b9994c4be..a2bca19ad 100644 --- a/internal/reconcile/install_test.go +++ b/internal/reconcile/install_test.go @@ -303,36 +303,6 @@ func TestInstall_Reconcile(t *testing.T) { } func TestInstall_Reconcile_withSubchartWithCRDs(t *testing.T) { - buildChart := func() *chart.Chart { - subChart := testutil.BuildChart( - testutil.ChartWithName("subchart"), - testutil.ChartWithManifestWithCustomName("sub-chart"), - testutil.ChartWithCRD(), - testutil.ChartWithValues(helmchartutil.Values{ - "foo": "bar", - "exports": map[string]any{"data": map[string]any{"myint": 123}}, - "default": map[string]any{"data": map[string]any{"myint": 456}}, - })) - mainChart := testutil.BuildChart( - testutil.ChartWithManifestWithCustomName("main-chart"), - testutil.ChartWithValues(helmchartutil.Values{ - "foo": "baz", - "myimports": map[string]any{"myint": 0}, - }), - testutil.ChartWithDependency(&chart.Dependency{ - Name: "subchart", - Condition: "subchart.enabled", - ImportValues: []any{ - "data", - map[string]any{ - "child": "default.data", - "parent": "myimports", - }, - }, - }, subChart)) - return mainChart - } - getValues := func(subchartValues map[string]any) helmchartutil.Values { return helmchartutil.Values{"subchart": subchartValues} } @@ -411,7 +381,7 @@ func TestInstall_Reconcile_withSubchartWithCRDs(t *testing.T) { store := helmstorage.Init(cfg.Driver) - chart := buildChart() + chart := testutil.BuildChartWithSubchartWithCRD() recorder := new(record.FakeRecorder) got := (NewInstall(cfg, recorder)).Reconcile(context.TODO(), &Request{ Object: obj, diff --git a/internal/reconcile/upgrade_test.go b/internal/reconcile/upgrade_test.go index 6eec3c72b..a453537c3 100644 --- a/internal/reconcile/upgrade_test.go +++ b/internal/reconcile/upgrade_test.go @@ -24,7 +24,6 @@ import ( "time" . "github.com/onsi/gomega" - "helm.sh/helm/v3/pkg/chart" helmchart "helm.sh/helm/v3/pkg/chart" helmchartutil "helm.sh/helm/v3/pkg/chartutil" helmrelease "helm.sh/helm/v3/pkg/release" @@ -435,36 +434,6 @@ func TestUpgrade_Reconcile(t *testing.T) { } func TestUpgrade_Reconcile_withSubchartWithCRDs(t *testing.T) { - buildChart := func() *chart.Chart { - subChart := testutil.BuildChart( - testutil.ChartWithName("subchart"), - testutil.ChartWithManifestWithCustomName("sub-chart"), - testutil.ChartWithCRD(), - testutil.ChartWithValues(helmchartutil.Values{ - "foo": "bar", - "exports": map[string]any{"data": map[string]any{"myint": 123}}, - "default": map[string]any{"data": map[string]any{"myint": 456}}, - })) - mainChart := testutil.BuildChart( - testutil.ChartWithManifestWithCustomName("main-chart"), - testutil.ChartWithValues(helmchartutil.Values{ - "foo": "baz", - "myimports": map[string]any{"myint": 0}, - }), - testutil.ChartWithDependency(&chart.Dependency{ - Name: "subchart", - Condition: "subchart.enabled", - ImportValues: []any{ - "data", - map[string]any{ - "child": "default.data", - "parent": "myimports", - }, - }, - }, subChart)) - return mainChart - } - getValues := func(subchartValues map[string]any) helmchartutil.Values { return helmchartutil.Values{"subchart": subchartValues} } @@ -569,7 +538,7 @@ func TestUpgrade_Reconcile_withSubchartWithCRDs(t *testing.T) { g.Expect(store.Create(r)).To(Succeed()) } - chart := buildChart() + chart := testutil.BuildChartWithSubchartWithCRD() recorder := new(record.FakeRecorder) got := NewUpgrade(cfg, recorder).Reconcile(context.TODO(), &Request{ Object: obj, diff --git a/internal/testutil/mock_chart.go b/internal/testutil/mock_chart.go index 94a0e239c..29380474c 100644 --- a/internal/testutil/mock_chart.go +++ b/internal/testutil/mock_chart.go @@ -20,6 +20,7 @@ import ( "fmt" helmchart "helm.sh/helm/v3/pkg/chart" + helmchartutil "helm.sh/helm/v3/pkg/chartutil" ) var manifestTmpl = `apiVersion: v1 @@ -246,3 +247,36 @@ func ChartWithValues(values map[string]any) ChartOption { opts.Values = values } } + +// BuildChartWithSubchartWithCRD returns a Helm chart object with a subchart +// that contains a CRD. Useful for testing helm-controller's staged CRDs-first +// deployment logic. +func BuildChartWithSubchartWithCRD() *helmchart.Chart { + subChart := BuildChart( + ChartWithName("subchart"), + ChartWithManifestWithCustomName("sub-chart"), + ChartWithCRD(), + ChartWithValues(helmchartutil.Values{ + "foo": "bar", + "exports": map[string]any{"data": map[string]any{"myint": 123}}, + "default": map[string]any{"data": map[string]any{"myint": 456}}, + })) + mainChart := BuildChart( + ChartWithManifestWithCustomName("main-chart"), + ChartWithValues(helmchartutil.Values{ + "foo": "baz", + "myimports": map[string]any{"myint": 0}, + }), + ChartWithDependency(&helmchart.Dependency{ + Name: "subchart", + Condition: "subchart.enabled", + ImportValues: []any{ + "data", + map[string]any{ + "child": "default.data", + "parent": "myimports", + }, + }, + }, subChart)) + return mainChart +}