Skip to content

Commit 9e460e9

Browse files
Microzuul CIGerrit Code Review
authored andcommitted
Merge "Add software factory reconcile test"
2 parents 1290c9d + cc0d9eb commit 9e460e9

File tree

4 files changed

+97
-2
lines changed

4 files changed

+97
-2
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ config/manager/kustomization.yaml
3131

3232
my-sf.yaml
3333
config/crd/bases/_.yaml
34+
config/crd/vendor/
3435

3536
# python sf_operator
3637
.eggs

Makefile

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ doc-serve: mkdocs
8282
$(LOCALBIN)/mkdocs/bin/mkdocs serve
8383

8484
.PHONY: test
85-
test: manifests generate fmt vet envtest ## Run tests.
85+
test: manifests generate fmt vet envtest vendor-crds ## Run tests.
8686
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) -p path)" go test ./... -coverprofile cover.out
8787

8888
##@ Build
@@ -216,6 +216,11 @@ staticcheck:
216216
mkdir -p $(GOBIN)
217217
test -L $(GOBIN)/staticcheck || ln -s $(LOCALBIN)/staticcheck $(GOBIN)/staticcheck
218218

219+
.PHONY: vendor-crds
220+
vendor-crds:
221+
@mkdir -p config/crd/vendor/
222+
@(test -f config/crd/vendor/monitoring.yaml || curl -Lo config/crd/vendor/monitoring.yaml https://github.com/prometheus-operator/prometheus-operator/releases/download/v0.82.2/stripped-down-crds.yaml)
223+
219224
# Catalog
220225
CATALOG_DIR=sf-operator-catalog
221226
CATALOG_FILE=$(CATALOG_DIR)/catalog.yaml
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// Copyright (C) 2025 Red Hat
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package controllers
5+
6+
import (
7+
"context"
8+
9+
//nolint:golint
10+
. "github.com/onsi/ginkgo/v2"
11+
. "github.com/onsi/gomega"
12+
sfv1 "github.com/softwarefactory-project/sf-operator/api/v1"
13+
corev1 "k8s.io/api/core/v1"
14+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
15+
"k8s.io/apimachinery/pkg/types"
16+
// "sigs.k8s.io/controller-runtime/pkg/client"
17+
"k8s.io/apimachinery/pkg/api/errors"
18+
"sigs.k8s.io/controller-runtime/pkg/reconcile"
19+
)
20+
21+
var _ = Describe("SoftwareFactory controller", func() {
22+
Context("SoftwareFactory CR validation", func() {
23+
24+
const TestName = "test-cr"
25+
26+
ctx := context.Background()
27+
28+
namespace := &corev1.Namespace{
29+
ObjectMeta: metav1.ObjectMeta{
30+
Name: TestName,
31+
Namespace: TestName,
32+
},
33+
}
34+
typeNamespaceName := types.NamespacedName{
35+
Name: TestName,
36+
Namespace: TestName,
37+
}
38+
sf := &sfv1.SoftwareFactory{}
39+
BeforeEach(func() {
40+
By("Creating the Namespace to perform the tests")
41+
err := k8sClient.Create(ctx, namespace)
42+
Expect(err).To(Not(HaveOccurred()))
43+
44+
By("creating the custom resource for the Kind SoftwareFactory")
45+
err = k8sClient.Get(ctx, typeNamespaceName, sf)
46+
if err != nil && errors.IsNotFound(err) {
47+
// Let's mock our custom resource at the same way that we would
48+
// apply on the cluster the manifest under config/samples
49+
sf = &sfv1.SoftwareFactory{
50+
ObjectMeta: metav1.ObjectMeta{
51+
Name: TestName,
52+
Namespace: namespace.Name,
53+
},
54+
Spec: sfv1.SoftwareFactorySpec{
55+
ConfigRepositoryLocation: sfv1.ConfigRepositoryLocationSpec{
56+
Name: "config",
57+
ZuulConnectionName: "conn",
58+
},
59+
},
60+
}
61+
62+
err = k8sClient.Create(ctx, sf)
63+
Expect(err).To(Not(HaveOccurred()))
64+
}
65+
})
66+
AfterEach(func() {
67+
k8sClient.Delete(context.TODO(), sf)
68+
})
69+
70+
It("Should use reconcile", func() {
71+
sfReconciler := &SoftwareFactoryReconciler{
72+
Client: k8sClient,
73+
Scheme: k8sClient.Scheme(),
74+
CancelFunc: cancel,
75+
}
76+
77+
sfReconciler.Reconcile(ctx, reconcile.Request{
78+
NamespacedName: typeNamespaceName,
79+
})
80+
81+
})
82+
})
83+
})

controllers/suite_test.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ import (
3232
logf "sigs.k8s.io/controller-runtime/pkg/log"
3333
"sigs.k8s.io/controller-runtime/pkg/log/zap"
3434

35+
opv1 "github.com/operator-framework/api/pkg/operators/v1alpha1"
36+
monitoring "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
3537
sfv1 "github.com/softwarefactory-project/sf-operator/api/v1"
3638
//+kubebuilder:scaffold:imports
3739
)
@@ -78,7 +80,7 @@ var _ = BeforeSuite(func() {
7880

7981
By("bootstrapping test environment")
8082
testEnv = &envtest.Environment{
81-
CRDDirectoryPaths: []string{filepath.Join("..", "config", "crd", "bases")},
83+
CRDDirectoryPaths: []string{filepath.Join("..", "config", "crd", "bases"), filepath.Join("..", "config", "crd", "vendor")},
8284
ErrorIfCRDPathMissing: true,
8385

8486
// The BinaryAssetsDirectory is only required if you want to run the tests directly
@@ -98,6 +100,10 @@ var _ = BeforeSuite(func() {
98100

99101
err = sfv1.AddToScheme(scheme.Scheme)
100102
Expect(err).NotTo(HaveOccurred())
103+
err = monitoring.AddToScheme(scheme.Scheme)
104+
Expect(err).NotTo(HaveOccurred())
105+
err = opv1.AddToScheme(scheme.Scheme)
106+
Expect(err).NotTo(HaveOccurred())
101107

102108
// +kubebuilder:scaffold:scheme
103109

0 commit comments

Comments
 (0)