Skip to content

Commit

Permalink
Add test for CRDs not applied when condition is false
Browse files Browse the repository at this point in the history
Signed-off-by: Matheus Pimenta <[email protected]>
  • Loading branch information
matheuscscp committed Dec 10, 2024
1 parent 8b2ec09 commit d14404d
Show file tree
Hide file tree
Showing 2 changed files with 170 additions and 0 deletions.
105 changes: 105 additions & 0 deletions internal/reconcile/install_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ import (
helmstorage "helm.sh/helm/v3/pkg/storage"
helmdriver "helm.sh/helm/v3/pkg/storage/driver"
corev1 "k8s.io/api/core/v1"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/tools/record"
"sigs.k8s.io/controller-runtime/pkg/client"

eventv1 "github.com/fluxcd/pkg/apis/event/v1beta1"
"github.com/fluxcd/pkg/apis/meta"
Expand Down Expand Up @@ -300,6 +302,109 @@ func TestInstall_Reconcile(t *testing.T) {
}
}

func TestInstall_Reconcile_withoutSubchartCRDWhenConditionIsFalse(t *testing.T) {
subChart := testutil.BuildChart(
testutil.ChartWithName("subchart"),
testutil.ChartWithManifestWithCustomName("sub-chart"),
testutil.ChartWithCRD())
mainChart := testutil.BuildChart(testutil.ChartWithManifestWithCustomName("main-chart"))
mainChart.Metadata.Dependencies = []*chart.Dependency{{
Name: "subchart",
Condition: "subchart.enabled",
}}
mainChart.AddDependency(subChart)
values := helmchartutil.Values{
"subchart": map[string]any{
"enabled": false,
},
}

expectConditions := []metav1.Condition{
*conditions.TrueCondition(meta.ReadyCondition, v2.InstallSucceededReason,
"Helm install succeeded"),
*conditions.TrueCondition(v2.ReleasedCondition, v2.InstallSucceededReason,
"Helm install succeeded"),
}

expectHistory := func(releases []*helmrelease.Release) v2.Snapshots {
return v2.Snapshots{
release.ObservedToSnapshot(release.ObserveRelease(releases[0])),
}
}

g := NewWithT(t)

namedNS, err := testEnv.CreateNamespace(context.TODO(), mockReleaseNamespace)
g.Expect(err).NotTo(HaveOccurred())
t.Cleanup(func() {
_ = testEnv.Delete(context.TODO(), namedNS)
})
releaseNamespace := namedNS.Name

obj := &v2.HelmRelease{
Spec: v2.HelmReleaseSpec{
ReleaseName: mockReleaseName,
TargetNamespace: releaseNamespace,
StorageNamespace: releaseNamespace,
Timeout: &metav1.Duration{Duration: 100 * time.Millisecond},
},
}

getter, err := RESTClientGetterFromManager(testEnv.Manager, obj.GetReleaseNamespace())
g.Expect(err).ToNot(HaveOccurred())

cfg, err := action.NewConfigFactory(getter,
action.WithStorage(action.DefaultStorageDriver, obj.GetStorageNamespace()),
)
g.Expect(err).ToNot(HaveOccurred())

store := helmstorage.Init(cfg.Driver)

recorder := new(record.FakeRecorder)
got := (NewInstall(cfg, recorder)).Reconcile(context.TODO(), &Request{
Object: obj,
Chart: mainChart,
Values: values,
})
g.Expect(got).ToNot(HaveOccurred())

g.Expect(obj.Status.Conditions).To(conditions.MatchConditions(expectConditions))

releases, _ := store.History(mockReleaseName)
releaseutil.SortByRevision(releases)

g.Expect(obj.Status.History).To(testutil.Equal(expectHistory(releases)))

// Assert main chart configmap is present.
mainChartCM := &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: "cm-main-chart",
Namespace: releaseNamespace,
},
}
err = testEnv.Get(context.TODO(), client.ObjectKeyFromObject(mainChartCM), mainChartCM)
g.Expect(err).NotTo(HaveOccurred())

// Assert subchart configmap is absent.
subChartCM := &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: "cm-sub-chart",
Namespace: releaseNamespace,
},
}
err = testEnv.Get(context.TODO(), client.ObjectKeyFromObject(subChartCM), subChartCM)
g.Expect(err).To(HaveOccurred())

// Assert subchart CRD is absent.
subChartCRD := &apiextensionsv1.CustomResourceDefinition{
ObjectMeta: metav1.ObjectMeta{
Name: "crontabs.stable.example.com",
},
}
err = testEnv.Get(context.TODO(), client.ObjectKeyFromObject(subChartCRD), subChartCRD)
g.Expect(err).To(HaveOccurred())
}

func TestInstall_failure(t *testing.T) {
var (
obj = &v2.HelmRelease{
Expand Down
65 changes: 65 additions & 0 deletions internal/testutil/mock_chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ data:
foo: bar
`

var manifestWithCustomNameTmpl = `apiVersion: v1
kind: ConfigMap
metadata:
name: cm-%[1]s
namespace: %[2]s
data:
foo: bar
`

var manifestWithHookTmpl = `apiVersion: v1
kind: ConfigMap
metadata:
Expand Down Expand Up @@ -82,6 +91,38 @@ spec:
restartPolicy: Never
`

var crdManifest = `apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
name: crontabs.stable.example.com
spec:
group: stable.example.com
versions:
- name: v1
served: true
storage: true
schema:
openAPIV3Schema:
type: object
properties:
spec:
type: object
properties:
cronSpec:
type: string
image:
type: string
replicas:
type: integer
scope: Namespaced
names:
plural: crontabs
singular: crontab
kind: CronTab
shortNames:
- ct
`

// ChartOptions is a helper to build a Helm chart object.
type ChartOptions struct {
*helmchart.Chart
Expand Down Expand Up @@ -166,3 +207,27 @@ func ChartWithFailingTestHook() ChartOption {
})
}
}

// ChartWithManifestWithCustomName sets the name of the manifest.
func ChartWithManifestWithCustomName(name string) ChartOption {
return func(opts *ChartOptions) {
opts.Templates = []*helmchart.File{
{
Name: "templates/manifest",
Data: []byte(fmt.Sprintf(manifestWithCustomNameTmpl, name, "{{ default .Release.Namespace }}")),
},
}
}
}

// ChartWithCRD appends a CRD to the chart.
func ChartWithCRD() ChartOption {
return func(opts *ChartOptions) {
opts.Files = []*helmchart.File{
{
Name: "crds/crd.yaml",
Data: []byte(crdManifest),
},
}
}
}

0 comments on commit d14404d

Please sign in to comment.