Skip to content

Commit 84ae9fe

Browse files
committed
Add test for Namespace custom resource
Signed-off-by: Stefan Prodan <[email protected]>
1 parent 250f620 commit 84ae9fe

File tree

2 files changed

+144
-0
lines changed

2 files changed

+144
-0
lines changed

internal/controller/kustomization_controller_test.go

+70
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,88 @@ limitations under the License.
1717
package controller
1818

1919
import (
20+
"context"
21+
"fmt"
2022
"testing"
23+
"time"
2124

25+
"github.com/fluxcd/pkg/apis/meta"
26+
sourcev1 "github.com/fluxcd/source-controller/api/v1"
2227
. "github.com/onsi/gomega"
2328
corev1 "k8s.io/api/core/v1"
29+
"k8s.io/apimachinery/pkg/api/errors"
2430
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
31+
"k8s.io/apimachinery/pkg/types"
2532
"k8s.io/client-go/tools/record"
2633
ctrl "sigs.k8s.io/controller-runtime"
2734
"sigs.k8s.io/controller-runtime/pkg/client"
2835

2936
kustomizev1 "github.com/fluxcd/kustomize-controller/api/v1"
3037
)
3138

39+
func TestKustomizationReconciler_StagedApply(t *testing.T) {
40+
g := NewWithT(t)
41+
42+
namespaceName := "kust-" + randStringRunes(5)
43+
namespace := &corev1.Namespace{
44+
ObjectMeta: metav1.ObjectMeta{Name: namespaceName},
45+
}
46+
g.Expect(k8sClient.Create(ctx, namespace)).ToNot(HaveOccurred())
47+
t.Cleanup(func() {
48+
g.Expect(k8sClient.Delete(ctx, namespace)).NotTo(HaveOccurred())
49+
})
50+
51+
err := createKubeConfigSecret(namespaceName)
52+
g.Expect(err).NotTo(HaveOccurred(), "failed to create kubeconfig secret")
53+
54+
artifactName := "val-" + randStringRunes(5)
55+
artifactChecksum, err := testServer.ArtifactFromDir("testdata/crds", artifactName)
56+
g.Expect(err).ToNot(HaveOccurred())
57+
58+
repositoryName := types.NamespacedName{
59+
Name: fmt.Sprintf("val-%s", randStringRunes(5)),
60+
Namespace: namespaceName,
61+
}
62+
63+
err = applyGitRepository(repositoryName, artifactName, "main/"+artifactChecksum)
64+
g.Expect(err).NotTo(HaveOccurred())
65+
66+
kustomization := &kustomizev1.Kustomization{}
67+
kustomization.Name = "test-kust"
68+
kustomization.Namespace = namespaceName
69+
kustomization.Spec = kustomizev1.KustomizationSpec{
70+
Interval: metav1.Duration{Duration: 10 * time.Minute},
71+
Prune: true,
72+
Path: "./",
73+
SourceRef: kustomizev1.CrossNamespaceSourceReference{
74+
Name: repositoryName.Name,
75+
Namespace: repositoryName.Namespace,
76+
Kind: sourcev1.GitRepositoryKind,
77+
},
78+
KubeConfig: &meta.KubeConfigReference{
79+
SecretRef: meta.SecretKeyReference{
80+
Name: "kubeconfig",
81+
},
82+
},
83+
}
84+
85+
g.Expect(k8sClient.Create(context.Background(), kustomization)).To(Succeed())
86+
87+
g.Eventually(func() bool {
88+
var obj kustomizev1.Kustomization
89+
_ = k8sClient.Get(context.Background(), client.ObjectKeyFromObject(kustomization), &obj)
90+
return isReconcileSuccess(&obj) && obj.Status.LastAttemptedRevision == "main/"+artifactChecksum
91+
}, timeout, time.Second).Should(BeTrue())
92+
93+
g.Expect(k8sClient.Delete(context.Background(), kustomization)).To(Succeed())
94+
95+
g.Eventually(func() bool {
96+
var obj kustomizev1.Kustomization
97+
err = k8sClient.Get(context.Background(), client.ObjectKeyFromObject(kustomization), &obj)
98+
return errors.IsNotFound(err)
99+
}, timeout, time.Second).Should(BeTrue())
100+
}
101+
32102
func TestKustomizationReconciler_deleteBeforeFinalizer(t *testing.T) {
33103
g := NewWithT(t)
34104

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
apiVersion: apiextensions.k8s.io/v1
2+
kind: CustomResourceDefinition
3+
metadata:
4+
name: namespaces.servicebus.azure.com
5+
spec:
6+
group: servicebus.azure.com
7+
names:
8+
kind: Namespace
9+
listKind: NamespaceList
10+
plural: namespaces
11+
singular: namespace
12+
scope: Namespaced
13+
versions:
14+
- additionalPrinterColumns:
15+
- jsonPath: .spec.type
16+
name: TYPE
17+
type: string
18+
name: v1beta20210101preview
19+
schema:
20+
openAPIV3Schema:
21+
description: Test is the Schema for the testing API
22+
properties:
23+
apiVersion:
24+
type: string
25+
kind:
26+
type: string
27+
metadata:
28+
type: object
29+
spec:
30+
description: TestSpec defines the desired state of a test run
31+
properties:
32+
type:
33+
description: Type of test
34+
type: string
35+
enum:
36+
- unit
37+
- integration
38+
valuesFrom:
39+
description: config reference
40+
type: string
41+
type: object
42+
status:
43+
default:
44+
observedGeneration: -1
45+
properties:
46+
observedGeneration:
47+
description: ObservedGeneration is the last observed generation.
48+
format: int64
49+
type: integer
50+
type: object
51+
type: object
52+
served: true
53+
storage: true
54+
subresources:
55+
status: {}
56+
---
57+
apiVersion: servicebus.azure.com/v1beta20210101preview
58+
kind: Namespace
59+
metadata:
60+
annotations:
61+
serviceoperator.azure.com/reconcile-policy: detach-on-delete
62+
name: sptribs-servicebus-preview
63+
namespace: sptribs
64+
spec:
65+
type: integration
66+
valuesFrom: test-config
67+
---
68+
apiVersion: v1
69+
kind: Namespace
70+
metadata:
71+
labels:
72+
slackChannel: special-tribunals-builds
73+
name: sptribs
74+

0 commit comments

Comments
 (0)