Skip to content

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
haijianyang committed Apr 9, 2024
1 parent 7a77cc9 commit b66f9b5
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 12 deletions.
56 changes: 44 additions & 12 deletions controllers/elfmachine_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
capev1 "github.com/smartxworks/cluster-api-provider-elf/api/v1beta1"
capecontext "github.com/smartxworks/cluster-api-provider-elf/pkg/context"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -39,7 +38,6 @@ import (
ctrlutil "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"

"github.com/smartxworks/cluster-api-provider-elf-static-ip/pkg/config"
"github.com/smartxworks/cluster-api-provider-elf-static-ip/pkg/context"
"github.com/smartxworks/cluster-api-provider-elf-static-ip/pkg/ipam"
"github.com/smartxworks/cluster-api-provider-elf-static-ip/pkg/ipam/metal3io"
ipamutil "github.com/smartxworks/cluster-api-provider-elf-static-ip/pkg/ipam/util"
Expand Down Expand Up @@ -347,26 +345,60 @@ var _ = Describe("ElfMachineReconciler", func() {
}
ctrlContext := newCtrlContexts(elfCluster, cluster, elfMachine, machine, elfMachineTemplate, metal3IPPool, metal3IPClaim, metal3IPAddress)
fake.InitOwnerReferences(ctrlContext, elfCluster, cluster, elfMachine, machine)
machineContext := &context.MachineContext{
IPAMService: metal3io.NewIpam(ctrlContext.Client, ctrlContext.Logger),
MachineContext: &capecontext.MachineContext{
ControllerContext: ctrlContext,
Cluster: cluster,
Machine: machine,
ElfMachine: elfMachine,
Logger: ctrlContext.Logger,
},
}
machineContext := newMachineContext(ctrlContext, metal3io.NewIpam(ctrlContext.Client, ctrlContext.Logger), cluster, machine, elfMachine)
reconciler := &ElfMachineReconciler{ControllerContext: ctrlContext}
ipPool, err := reconciler.getIPPool(machineContext, elfMachine.Spec.Network.Devices[0])
Expect(err).ToNot(HaveOccurred())
Expect(ipPool.GetNamespace()).To(Equal(metal3IPPool.Namespace))
Expect(ipPool.GetName()).To(Equal(metal3IPPool.Name))

metal3IPPool.Namespace = ipam.DefaultIPPoolNamespace
ctrlContext = newCtrlContexts(elfCluster, cluster, elfMachine, machine, elfMachineTemplate, metal3IPPool, metal3IPClaim, metal3IPAddress)
fake.InitOwnerReferences(ctrlContext, elfCluster, cluster, elfMachine, machine)
machineContext = newMachineContext(ctrlContext, metal3io.NewIpam(ctrlContext.Client, ctrlContext.Logger), cluster, machine, elfMachine)
reconciler = &ElfMachineReconciler{ControllerContext: ctrlContext}
ipPool, err = reconciler.getIPPool(machineContext, elfMachine.Spec.Network.Devices[0])
Expect(err).ToNot(HaveOccurred())
Expect(ipPool.GetName()).To(Equal(metal3IPPool.Name))

elfMachine.Spec.Network.Devices[0].AddressesFromPools[0].Name = "notfound"
ipPool, err = reconciler.getIPPool(machineContext, elfMachine.Spec.Network.Devices[0])
Expect(err).ToNot(HaveOccurred())
Expect(ipPool).To(BeNil())
})

It("should get the default ip-pool", func() {
metal3IPPool.Namespace = cluster.Namespace
metal3IPPool.Labels = map[string]string{ipam.DefaultIPPoolKey: "true"}
ctrlContext := newCtrlContexts(elfCluster, cluster, elfMachine, machine, elfMachineTemplate, metal3IPPool, metal3IPClaim, metal3IPAddress)
fake.InitOwnerReferences(ctrlContext, elfCluster, cluster, elfMachine, machine)
machineContext := newMachineContext(ctrlContext, metal3io.NewIpam(ctrlContext.Client, ctrlContext.Logger), cluster, machine, elfMachine)
reconciler := &ElfMachineReconciler{ControllerContext: ctrlContext}
ipPool, err := reconciler.getIPPool(machineContext, elfMachine.Spec.Network.Devices[0])
Expect(err).ToNot(HaveOccurred())
Expect(ipPool.GetNamespace()).To(Equal(metal3IPPool.Namespace))
Expect(ipPool.GetName()).To(Equal(metal3IPPool.Name))

metal3IPPool.Namespace = ipam.DefaultIPPoolNamespace
metal3IPPool.Labels = map[string]string{ipam.DefaultIPPoolKey: "true"}
ctrlContext = newCtrlContexts(elfCluster, cluster, elfMachine, machine, elfMachineTemplate, metal3IPPool, metal3IPClaim, metal3IPAddress)
fake.InitOwnerReferences(ctrlContext, elfCluster, cluster, elfMachine, machine)
machineContext = newMachineContext(ctrlContext, metal3io.NewIpam(ctrlContext.Client, ctrlContext.Logger), cluster, machine, elfMachine)
reconciler = &ElfMachineReconciler{ControllerContext: ctrlContext}
ipPool, err = reconciler.getIPPool(machineContext, elfMachine.Spec.Network.Devices[0])
Expect(err).ToNot(HaveOccurred())
Expect(ipPool.GetNamespace()).To(Equal(metal3IPPool.Namespace))
Expect(ipPool.GetName()).To(Equal(metal3IPPool.Name))

metal3IPPool.Namespace = "notfoud"
metal3IPPool.Labels = map[string]string{ipam.DefaultIPPoolKey: "true"}
ctrlContext = newCtrlContexts(elfCluster, cluster, elfMachine, machine, elfMachineTemplate, metal3IPPool, metal3IPClaim, metal3IPAddress)
fake.InitOwnerReferences(ctrlContext, elfCluster, cluster, elfMachine, machine)
machineContext = newMachineContext(ctrlContext, metal3io.NewIpam(ctrlContext.Client, ctrlContext.Logger), cluster, machine, elfMachine)
reconciler = &ElfMachineReconciler{ControllerContext: ctrlContext}
ipPool, err = reconciler.getIPPool(machineContext, elfMachine.Spec.Network.Devices[0])
Expect(err).ToNot(HaveOccurred())
Expect(ipPool).To(BeNil())
})
})
})
20 changes: 20 additions & 0 deletions controllers/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,16 @@ import (
ipamv1 "github.com/metal3-io/ip-address-manager/api/v1alpha1"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
capev1 "github.com/smartxworks/cluster-api-provider-elf/api/v1beta1"
capecontext "github.com/smartxworks/cluster-api-provider-elf/pkg/context"
"k8s.io/klog/v2"
capiv1 "sigs.k8s.io/cluster-api/api/v1beta1"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
ctrllog "sigs.k8s.io/controller-runtime/pkg/log"

"github.com/smartxworks/cluster-api-provider-elf-static-ip/pkg/context"
"github.com/smartxworks/cluster-api-provider-elf-static-ip/pkg/ipam"
ipamutil "github.com/smartxworks/cluster-api-provider-elf-static-ip/pkg/ipam/util"
"github.com/smartxworks/cluster-api-provider-elf-static-ip/test/fake"
"github.com/smartxworks/cluster-api-provider-elf-static-ip/test/helpers"
Expand Down Expand Up @@ -111,3 +115,19 @@ func setMetal3IPForClaim(ipClaim *ipamv1.IPClaim, ip *ipamv1.IPAddress) {
ref := ipamutil.GetObjRef(ip)
ipClaim.Status.Address = &ref
}

func newMachineContext(
ctrlContext *capecontext.ControllerContext,
ipamService ipam.IPAddressManager,
cluster *capiv1.Cluster, machine *capiv1.Machine, elfMachine *capev1.ElfMachine) *context.MachineContext {
return &context.MachineContext{
IPAMService: ipamService,
MachineContext: &capecontext.MachineContext{
ControllerContext: ctrlContext,
Cluster: cluster,
Machine: machine,
ElfMachine: elfMachine,
Logger: ctrlContext.Logger,
},
}
}

0 comments on commit b66f9b5

Please sign in to comment.