Skip to content

Commit 618878a

Browse files
authored
Merge pull request #71 from sp-yduck/testing
add test some cases
2 parents 9082c6f + 01263fa commit 618878a

File tree

13 files changed

+192
-30
lines changed

13 files changed

+192
-30
lines changed

api/v1beta1/type.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ type Hardware struct {
8484
// SCSIHardWare SCSIHardWare `json:"scsiHardWare,omitempty"`
8585

8686
// hard disk size
87+
// +kubebuilder:validation:Pattern:=\+?\d+(\.\d+)?[KMGT]?
8788
// +kubebuilder:default:="50G"
8889
Disk string `json:"disk,omitempty"`
8990
}

cloud/cloudinit/common.go

Lines changed: 0 additions & 10 deletions
This file was deleted.

cloud/cloudinit/common_test.go

Lines changed: 0 additions & 17 deletions
This file was deleted.

cloud/cloudinit/user_test.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,20 @@ runcmd:
2424
`
2525
_, err := cloudinit.ParseUser(testYaml)
2626
if err != nil {
27-
t.Fatalf("failed to parse user: %v", err)
27+
t.Errorf("failed to parse user: %v", err)
28+
}
29+
30+
testYaml = `
31+
write_files:
32+
- path: /run/kubeadm/kubeadm.yaml
33+
owner: root:root
34+
permissions: '0640'
35+
content: |
36+
asdfasdfasdf
37+
`
38+
user, err := cloudinit.ParseUser(testYaml)
39+
if err == nil {
40+
t.Errorf("should returns an error. user=%v", *user)
2841
}
2942
}
3043

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package providerid
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestNew(t *testing.T) {
8+
uuid := ""
9+
providerID, err := New(uuid)
10+
if err == nil {
11+
t.Errorf("err should not be nil. providerID=%s", providerID)
12+
}
13+
14+
uuid = "asdf"
15+
providerID, err = New(uuid)
16+
if err != nil {
17+
t.Errorf("failed to create providerID: %v", err)
18+
}
19+
}
20+
21+
func TestUUID(t *testing.T) {
22+
pid := providerID{
23+
uuid: "asdf",
24+
}
25+
uuid := pid.UUID()
26+
if uuid != "asdf" {
27+
t.Errorf("should be asdf")
28+
}
29+
}
30+
31+
func TestString(t *testing.T) {
32+
pid := providerID{
33+
uuid: "asdf",
34+
}
35+
uuid := pid.String()
36+
if uuid != Prefix+"asdf" {
37+
t.Errorf("should be %sasdf", Prefix)
38+
}
39+
}

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/machine.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,6 @@ func (m *MachineScope) GetHardware() infrav1.Hardware {
178178
if m.ProxmoxMachine.Spec.Hardware.Memory == 0 {
179179
m.ProxmoxMachine.Spec.Hardware.Memory = 4096
180180
}
181-
// to do: validation
182181
if m.ProxmoxMachine.Spec.Hardware.Disk == "" {
183182
m.ProxmoxMachine.Spec.Hardware.Disk = "50G"
184183
}

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+
})

config/crd/bases/infrastructure.cluster.x-k8s.io_proxmoxmachines.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ spec:
119119
disk:
120120
default: 50G
121121
description: hard disk size
122+
pattern: \+?\d+(\.\d+)?[KMGT]?
122123
type: string
123124
memory:
124125
default: 4096

0 commit comments

Comments
 (0)