Skip to content

Commit 858ec3f

Browse files
committed
add tests to controllers package
1 parent 99cdc9f commit 858ec3f

File tree

7 files changed

+123
-14
lines changed

7 files changed

+123
-14
lines changed

.gitignore

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Dockerfile.cross
1414

1515
# Output of the go coverage tool, specifically when used with LiteIDE
1616
*.out
17+
coverage.*
1718

1819
# Kubernetes Generated files - skip generated files, except for vendored files
1920

@@ -25,7 +26,5 @@ Dockerfile.cross
2526
*.swo
2627
*~
2728

28-
*_test.go
29-
3029
# release
3130
out

Makefile

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,6 @@ fmt: ## Run go fmt against code.
5757
vet: ## Run go vet against code.
5858
go vet ./...
5959

60-
.PHONY: test
61-
test: manifests generate fmt vet envtest ## Run tests.
62-
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" go test ./... -coverprofile cover.out
63-
6460
CLUSTER_NAME := cappx-test
6561

6662
.PHONY: create-workload-cluster
@@ -71,6 +67,21 @@ create-workload-cluster: $(KUSTOMIZE) $(ENVSUBST) $(KUBECTL)
7167
delete-workload-cluster: $(KUBECTL)
7268
$(KUBECTL) delete cluster $(CLUSTER_NAME)
7369

70+
##@ Testing
71+
72+
SETUP_ENVTEST_VER := v0.0.0-20211110210527-619e6b92dab9
73+
SETUP_ENVTEST := $(LOCALBIN)/setup-envtest
74+
75+
.PHONY: test
76+
test: manifests generate fmt $(SETUP_ENVTEST)
77+
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" go test ./controllers $(TEST_ARGS)
78+
79+
.PHONY: test-cover
80+
test-cover: ## Run unit and integration tests and generate coverage report
81+
$(MAKE) test TEST_ARGS="$(TEST_ARGS) -coverprofile=coverage.out"
82+
go tool cover -func=coverage.out -o coverate.txt
83+
go tool cover -html=coverage.out -o coverage.html
84+
7485
##@ Build
7586

7687
.PHONY: build
@@ -206,4 +217,9 @@ $(ENVSUBST): $(LOCALBIN)
206217
kubectl: $(KUBECTL)
207218
$(KUBECTL): $(LOCALBIN)
208219
curl --retry 3 -fsL https://dl.k8s.io/release/$(KUBECTL_VER)/bin/$(GOOS)/$(GOARCH)/kubectl -o $(LOCALBIN)/kubectl
209-
chmod +x $(KUBECTL)
220+
chmod +x $(KUBECTL)
221+
222+
.PHONY: setup-envtest
223+
setup-envtest: $(SETUP_ENVTEST)
224+
$(SETUP_ENVTEST): go.mod # Build setup-envtest from tools folder.
225+
GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-runtime/tools/setup-envtest@$(SETUP_ENVTEST_VER)

controllers/proxmoxcluster_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
package controller
17+
package controllers
1818

1919
import (
2020
"context"
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package controllers
2+
3+
import (
4+
"context"
5+
6+
. "github.com/onsi/ginkgo/v2"
7+
. "github.com/onsi/gomega"
8+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
9+
ctrl "sigs.k8s.io/controller-runtime"
10+
"sigs.k8s.io/controller-runtime/pkg/client"
11+
12+
infrav1 "github.com/sp-yduck/cluster-api-provider-proxmox/api/v1beta1"
13+
)
14+
15+
var _ = Describe("ProxmoxClusterReconciler", func() {
16+
BeforeEach(func() {})
17+
AfterEach(func() {})
18+
19+
Context("Reconcile ProxmoxCluster", func() {
20+
It("should not error and requeue the request with insufficient set up", func() {
21+
ctx := context.Background()
22+
23+
reconciler := &ProxmoxClusterReconciler{
24+
Client: k8sClient,
25+
}
26+
27+
instance := &infrav1.ProxmoxCluster{
28+
ObjectMeta: metav1.ObjectMeta{Name: "foo", Namespace: "default"},
29+
Spec: infrav1.ProxmoxClusterSpec{
30+
ServerRef: infrav1.ServerRef{
31+
Endpoint: "a.b.c.d:8006",
32+
SecretRef: &infrav1.ObjectReference{Name: "foo"},
33+
},
34+
},
35+
}
36+
37+
// Create the ProxmoxCluster object and expect the Reconcile and Deployment to be created
38+
Expect(k8sClient.Create(ctx, instance)).To(Succeed())
39+
defer func() {
40+
err := k8sClient.Delete(ctx, instance)
41+
Expect(err).NotTo(HaveOccurred())
42+
}()
43+
44+
result, err := reconciler.Reconcile(ctx, ctrl.Request{
45+
NamespacedName: client.ObjectKey{
46+
Namespace: instance.Namespace,
47+
Name: instance.Name,
48+
},
49+
})
50+
Expect(err).NotTo(HaveOccurred())
51+
Expect(result.RequeueAfter).To(BeZero())
52+
Expect(result.Requeue).To(BeFalse())
53+
})
54+
})
55+
})

controllers/proxmoxmachine_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
package controller
17+
package controllers
1818

1919
import (
2020
"context"
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package controllers
2+
3+
import (
4+
"context"
5+
6+
. "github.com/onsi/ginkgo/v2"
7+
. "github.com/onsi/gomega"
8+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
9+
ctrl "sigs.k8s.io/controller-runtime"
10+
"sigs.k8s.io/controller-runtime/pkg/client"
11+
12+
infrav1 "github.com/sp-yduck/cluster-api-provider-proxmox/api/v1beta1"
13+
)
14+
15+
var _ = Describe("ProxmoxMachineReconciler", func() {
16+
BeforeEach(func() {})
17+
AfterEach(func() {})
18+
19+
Context("Reconcile an ProxmoxMachine", func() {
20+
It("should not error with minimal set up", func() {
21+
reconciler := &ProxmoxMachineReconciler{
22+
Client: k8sClient,
23+
}
24+
By("Calling reconcile")
25+
ctx := context.Background()
26+
instance := &infrav1.ProxmoxMachine{ObjectMeta: metav1.ObjectMeta{Name: "foo", Namespace: "default"}}
27+
result, err := reconciler.Reconcile(ctx, ctrl.Request{
28+
NamespacedName: client.ObjectKey{
29+
Namespace: instance.Namespace,
30+
Name: instance.Name,
31+
},
32+
})
33+
Expect(err).NotTo(HaveOccurred())
34+
Expect(result.RequeueAfter).To(BeZero())
35+
Expect(result.Requeue).To(BeFalse())
36+
})
37+
})
38+
})

controllers/suite_test.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
package controller
17+
package controllers
1818

1919
import (
2020
"path/filepath"
@@ -25,12 +25,13 @@ import (
2525

2626
"k8s.io/client-go/kubernetes/scheme"
2727
"k8s.io/client-go/rest"
28+
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
2829
"sigs.k8s.io/controller-runtime/pkg/client"
2930
"sigs.k8s.io/controller-runtime/pkg/envtest"
3031
logf "sigs.k8s.io/controller-runtime/pkg/log"
3132
"sigs.k8s.io/controller-runtime/pkg/log/zap"
3233

33-
infrastructurev1beta1 "github.com/sp-yduck/cluster-api-provider-proxmox/api/v1beta1"
34+
infrav1 "github.com/sp-yduck/cluster-api-provider-proxmox/api/v1beta1"
3435
//+kubebuilder:scaffold:imports
3536
)
3637

@@ -52,7 +53,7 @@ var _ = BeforeSuite(func() {
5253

5354
By("bootstrapping test environment")
5455
testEnv = &envtest.Environment{
55-
CRDDirectoryPaths: []string{filepath.Join("..", "..", "config", "crd", "bases")},
56+
CRDDirectoryPaths: []string{filepath.Join("..", "config", "crd", "bases")},
5657
ErrorIfCRDPathMissing: true,
5758
}
5859

@@ -62,8 +63,8 @@ var _ = BeforeSuite(func() {
6263
Expect(err).NotTo(HaveOccurred())
6364
Expect(cfg).NotTo(BeNil())
6465

65-
err = infrastructurev1beta1.AddToScheme(scheme.Scheme)
66-
Expect(err).NotTo(HaveOccurred())
66+
Expect(clusterv1.AddToScheme(scheme.Scheme)).To(Succeed())
67+
Expect(infrav1.AddToScheme(scheme.Scheme)).To(Succeed())
6768

6869
//+kubebuilder:scaffold:scheme
6970

0 commit comments

Comments
 (0)