132 lines
3.1 KiB
Go
132 lines
3.1 KiB
Go
/*
|
|
Copyright 2020 The Flux authors
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
*/
|
|
|
|
package controllers
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
v2 "github.com/fluxcd/helm-controller/api/v2beta1"
|
|
sourcev1 "github.com/fluxcd/source-controller/api/v1beta2"
|
|
. "github.com/onsi/gomega"
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
"k8s.io/apimachinery/pkg/runtime"
|
|
"k8s.io/client-go/tools/record"
|
|
"sigs.k8s.io/controller-runtime/pkg/client/fake"
|
|
)
|
|
|
|
func TestHelmReleaseReconciler_getHelmChart(t *testing.T) {
|
|
g := NewWithT(t)
|
|
|
|
scheme := runtime.NewScheme()
|
|
g.Expect(v2.AddToScheme(scheme)).To(Succeed())
|
|
g.Expect(sourcev1.AddToScheme(scheme)).To(Succeed())
|
|
|
|
chart := &sourcev1.HelmChart{
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
Namespace: "some-namespace",
|
|
Name: "some-chart-name",
|
|
},
|
|
}
|
|
|
|
tests := []struct {
|
|
name string
|
|
rel *v2.HelmRelease
|
|
chart *sourcev1.HelmChart
|
|
expectChart bool
|
|
wantErr bool
|
|
disallowCrossNS bool
|
|
}{
|
|
{
|
|
name: "retrieves HelmChart object from Status",
|
|
rel: &v2.HelmRelease{
|
|
Status: v2.HelmReleaseStatus{
|
|
HelmChart: "some-namespace/some-chart-name",
|
|
},
|
|
},
|
|
chart: chart,
|
|
expectChart: true,
|
|
},
|
|
{
|
|
name: "no HelmChart found",
|
|
rel: &v2.HelmRelease{
|
|
Status: v2.HelmReleaseStatus{
|
|
HelmChart: "some-namespace/some-chart-name",
|
|
},
|
|
},
|
|
chart: nil,
|
|
expectChart: false,
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "no HelmChart in Status",
|
|
rel: &v2.HelmRelease{
|
|
Status: v2.HelmReleaseStatus{
|
|
HelmChart: "",
|
|
},
|
|
},
|
|
chart: chart,
|
|
expectChart: false,
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "ACL disallows cross namespace",
|
|
rel: &v2.HelmRelease{
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
Namespace: "default",
|
|
},
|
|
Status: v2.HelmReleaseStatus{
|
|
HelmChart: "some-namespace/some-chart-name",
|
|
},
|
|
},
|
|
chart: chart,
|
|
expectChart: false,
|
|
wantErr: true,
|
|
disallowCrossNS: true,
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
builder := fake.NewClientBuilder()
|
|
builder.WithScheme(scheme)
|
|
if tt.chart != nil {
|
|
builder.WithObjects(tt.chart)
|
|
}
|
|
|
|
r := &HelmReleaseReconciler{
|
|
Client: builder.Build(),
|
|
EventRecorder: record.NewFakeRecorder(32),
|
|
NoCrossNamespaceRef: tt.disallowCrossNS,
|
|
}
|
|
|
|
got, err := r.getHelmChart(context.TODO(), tt.rel)
|
|
if tt.wantErr {
|
|
g.Expect(err).To(HaveOccurred())
|
|
g.Expect(got).To(BeNil())
|
|
return
|
|
}
|
|
g.Expect(err).ToNot(HaveOccurred())
|
|
expect := g.Expect(got.ObjectMeta)
|
|
if tt.expectChart {
|
|
expect.To(BeEquivalentTo(tt.chart.ObjectMeta))
|
|
} else {
|
|
expect.To(BeNil())
|
|
}
|
|
})
|
|
}
|
|
}
|