Skip to content

Commit 5d306d3

Browse files
committed
add test cases for cloud/scope
1 parent 43f2661 commit 5d306d3

File tree

5 files changed

+136
-1
lines changed

5 files changed

+136
-1
lines changed

cloud/scope/clients.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func newComputeService(ctx context.Context, cluster *infrav1.ProxmoxCluster, crC
3838
serverRef := cluster.Spec.ServerRef
3939
secretRef := serverRef.SecretRef
4040
if secretRef == nil {
41-
return nil, errors.New("failed to get proxmox client form nil secretRef")
41+
return nil, errors.New("failed to get proxmox client from nil secretRef")
4242
}
4343

4444
var secret corev1.Secret

cloud/scope/clients_test.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package scope
2+
3+
import (
4+
"context"
5+
6+
. "github.com/onsi/ginkgo/v2"
7+
. "github.com/onsi/gomega"
8+
9+
corev1 "k8s.io/api/core/v1"
10+
"k8s.io/apimachinery/pkg/api/errors"
11+
12+
infrav1 "github.com/sp-yduck/cluster-api-provider-proxmox/api/v1beta1"
13+
)
14+
15+
var _ = Describe("newComputeService", func() {
16+
var (
17+
cluster *infrav1.ProxmoxCluster
18+
)
19+
20+
Context("When SecretRef is not present in ProxmoxCluster", func() {
21+
BeforeEach(func() {
22+
cluster = &infrav1.ProxmoxCluster{}
23+
})
24+
It("should return proper error", func() {
25+
svc, err := newComputeService(context.TODO(), cluster, k8sClient)
26+
Expect(err.Error()).To(Equal("failed to get proxmox client from nil secretRef"))
27+
Expect(svc).To(BeNil())
28+
})
29+
})
30+
31+
Context("When Secret is not present", func() {
32+
BeforeEach(func() {
33+
cluster = &infrav1.ProxmoxCluster{}
34+
cluster.Spec.ServerRef.SecretRef = &infrav1.ObjectReference{
35+
Namespace: "default",
36+
Name: "foo",
37+
}
38+
})
39+
40+
It("should return proper error", func() {
41+
svc, err := newComputeService(context.TODO(), cluster, k8sClient)
42+
Expect(errors.IsNotFound(err)).To(BeTrue())
43+
Expect(svc).To(BeNil())
44+
})
45+
})
46+
47+
Context("When Secret is empty", func() {
48+
BeforeEach(func() {
49+
cluster = &infrav1.ProxmoxCluster{}
50+
cluster.Spec.ServerRef.SecretRef = &infrav1.ObjectReference{
51+
Namespace: "default",
52+
Name: "foo",
53+
}
54+
cluster.SetName("foo")
55+
cluster.SetUID("bar")
56+
secret := &corev1.Secret{}
57+
secret.SetNamespace("default")
58+
secret.SetName("foo")
59+
k8sClient.Create(context.TODO(), secret)
60+
})
61+
62+
It("Should return proper error", func() {
63+
svc, err := newComputeService(context.TODO(), cluster, k8sClient)
64+
Expect(err.Error()).To(Equal("invalid authentication config"))
65+
Expect(svc).To(BeNil())
66+
})
67+
})
68+
})

cloud/scope/suite_test.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package scope
2+
3+
import (
4+
// "os"
5+
"path/filepath"
6+
"testing"
7+
8+
. "github.com/onsi/ginkgo/v2"
9+
. "github.com/onsi/gomega"
10+
11+
"k8s.io/client-go/kubernetes/scheme"
12+
"k8s.io/client-go/rest"
13+
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
14+
"sigs.k8s.io/controller-runtime/pkg/client"
15+
"sigs.k8s.io/controller-runtime/pkg/envtest"
16+
logf "sigs.k8s.io/controller-runtime/pkg/log"
17+
"sigs.k8s.io/controller-runtime/pkg/log/zap"
18+
19+
infrav1 "github.com/sp-yduck/cluster-api-provider-proxmox/api/v1beta1"
20+
)
21+
22+
var (
23+
cfg *rest.Config
24+
k8sClient client.Client
25+
testEnv *envtest.Environment
26+
proxmoxUser string
27+
proxmoxPassword string
28+
)
29+
30+
func TestScopes(t *testing.T) {
31+
RegisterFailHandler(Fail)
32+
RunSpecs(t, "Scope Suite")
33+
}
34+
35+
var _ = BeforeSuite(func() {
36+
logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true)))
37+
38+
By("bootstrapping test environment")
39+
testEnv = &envtest.Environment{
40+
CRDDirectoryPaths: []string{filepath.Join("..", "..", "config", "crd", "bases")},
41+
ErrorIfCRDPathMissing: true,
42+
}
43+
44+
var err error
45+
// cfg is defined in this file globally.
46+
cfg, err = testEnv.Start()
47+
Expect(err).NotTo(HaveOccurred())
48+
Expect(cfg).NotTo(BeNil())
49+
50+
Expect(clusterv1.AddToScheme(scheme.Scheme)).To(Succeed())
51+
Expect(infrav1.AddToScheme(scheme.Scheme)).To(Succeed())
52+
53+
//+kubebuilder:scaffold:scheme
54+
55+
k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme})
56+
Expect(err).NotTo(HaveOccurred())
57+
Expect(k8sClient).NotTo(BeNil())
58+
})
59+
60+
var _ = AfterSuite(func() {
61+
By("tearing down the test environment")
62+
err := testEnv.Stop()
63+
Expect(err).NotTo(HaveOccurred())
64+
})

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ require (
88
github.com/onsi/gomega v1.27.10
99
github.com/pkg/errors v0.9.1
1010
github.com/sp-yduck/proxmox-go v0.0.0-20230807140547-f03780ca1110
11+
github.com/stretchr/testify v1.8.4
1112
gopkg.in/yaml.v3 v3.0.1
1213
k8s.io/api v0.26.1
1314
k8s.io/apimachinery v0.26.1
@@ -48,6 +49,7 @@ require (
4849
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
4950
github.com/modern-go/reflect2 v1.0.2 // indirect
5051
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
52+
github.com/pmezard/go-difflib v1.0.0 // indirect
5153
github.com/prometheus/client_golang v1.14.0 // indirect
5254
github.com/prometheus/client_model v0.3.0 // indirect
5355
github.com/prometheus/common v0.37.0 // indirect

go.sum

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,7 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
312312
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
313313
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
314314
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
315+
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
315316
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU=
316317
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415/go.mod h1:GwrjFmJcFw6At/Gs6z4yjiIwzuJ1/+UwLxMQDVQXShQ=
317318
github.com/xeipuuv/gojsonschema v1.2.0/go.mod h1:anYRn/JVcOK2ZgGU+IjEV4nwlhoK5sQluxsYJ78Id3Y=

0 commit comments

Comments
 (0)