Skip to content

Commit

Permalink
Move common CRDs test chart to testutil package
Browse files Browse the repository at this point in the history
Signed-off-by: Matheus Pimenta <matheuscscp@gmail.com>
  • Loading branch information
matheuscscp committed Dec 13, 2024
1 parent 0987c72 commit da74708
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 63 deletions.
32 changes: 1 addition & 31 deletions internal/reconcile/install_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
}
Expand Down Expand Up @@ -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,
Expand Down
33 changes: 1 addition & 32 deletions internal/reconcile/upgrade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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}
}
Expand Down Expand Up @@ -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,
Expand Down
34 changes: 34 additions & 0 deletions internal/testutil/mock_chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"fmt"

helmchart "helm.sh/helm/v3/pkg/chart"
helmchartutil "helm.sh/helm/v3/pkg/chartutil"
)

var manifestTmpl = `apiVersion: v1
Expand Down Expand Up @@ -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
}

0 comments on commit da74708

Please sign in to comment.