Skip to content

Commit

Permalink
test: add vmnetcfg tests
Browse files Browse the repository at this point in the history
Signed-off-by: Zespre Chang <[email protected]>
  • Loading branch information
starbops committed Jan 25, 2024
1 parent ec9367c commit 6fe1ae0
Show file tree
Hide file tree
Showing 8 changed files with 623 additions and 102 deletions.
25 changes: 25 additions & 0 deletions pkg/cache/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package cache

type CacheAllocatorBuilder struct {
cacheAllocator *CacheAllocator
}

func NewCacheAllocatorBuilder() *CacheAllocatorBuilder {
return &CacheAllocatorBuilder{
cacheAllocator: New(),
}
}

func (b *CacheAllocatorBuilder) MACSet(name string) *CacheAllocatorBuilder {
_ = b.cacheAllocator.NewMACSet(name)
return b
}

func (b *CacheAllocatorBuilder) Add(name, macAddress, ipAddress string) *CacheAllocatorBuilder {
_ = b.cacheAllocator.AddMAC(name, macAddress, ipAddress)
return b
}

func (b *CacheAllocatorBuilder) Build() *CacheAllocator {
return b.cacheAllocator
}
101 changes: 26 additions & 75 deletions pkg/controller/ippool/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"net"
"time"

cniv1 "github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/apis/k8s.cni.cncf.io/v1"
"github.com/rancher/wrangler/pkg/kv"
Expand All @@ -13,9 +14,7 @@ import (

"github.com/harvester/vm-dhcp-controller/pkg/apis/network.harvesterhci.io"
networkv1 "github.com/harvester/vm-dhcp-controller/pkg/apis/network.harvesterhci.io/v1alpha1"
"github.com/harvester/vm-dhcp-controller/pkg/cache"
"github.com/harvester/vm-dhcp-controller/pkg/config"
"github.com/harvester/vm-dhcp-controller/pkg/ipam"
)

func prepareAgentPod(
Expand Down Expand Up @@ -169,12 +168,12 @@ func setDisabledCondition(ipPool *networkv1.IPPool, status corev1.ConditionStatu
networkv1.Disabled.Message(ipPool, message)
}

type ipPoolBuilder struct {
type IPPoolBuilder struct {
ipPool *networkv1.IPPool
}

func newIPPoolBuilder(namespace, name string) *ipPoolBuilder {
return &ipPoolBuilder{
func NewIPPoolBuilder(namespace, name string) *IPPoolBuilder {
return &IPPoolBuilder{
ipPool: &networkv1.IPPool{
ObjectMeta: metav1.ObjectMeta{
Namespace: namespace,
Expand All @@ -184,45 +183,45 @@ func newIPPoolBuilder(namespace, name string) *ipPoolBuilder {
}
}

func (b *ipPoolBuilder) NetworkName(networkName string) *ipPoolBuilder {
func (b *IPPoolBuilder) NetworkName(networkName string) *IPPoolBuilder {
b.ipPool.Spec.NetworkName = networkName
return b
}

func (b *ipPoolBuilder) Paused() *ipPoolBuilder {
func (b *IPPoolBuilder) Paused() *IPPoolBuilder {
paused := true
b.ipPool.Spec.Paused = &paused
return b
}

func (b *ipPoolBuilder) UnPaused() *ipPoolBuilder {
func (b *IPPoolBuilder) UnPaused() *IPPoolBuilder {
paused := false
b.ipPool.Spec.Paused = &paused
return b
}

func (b *ipPoolBuilder) ServerIP(serverIP string) *ipPoolBuilder {
func (b *IPPoolBuilder) ServerIP(serverIP string) *IPPoolBuilder {
b.ipPool.Spec.IPv4Config.ServerIP = serverIP
return b
}

func (b *ipPoolBuilder) CIDR(cidr string) *ipPoolBuilder {
func (b *IPPoolBuilder) CIDR(cidr string) *IPPoolBuilder {
b.ipPool.Spec.IPv4Config.CIDR = cidr
return b
}

func (b *ipPoolBuilder) PoolRange(start, end string) *ipPoolBuilder {
func (b *IPPoolBuilder) PoolRange(start, end string) *IPPoolBuilder {
b.ipPool.Spec.IPv4Config.Pool.Start = start
b.ipPool.Spec.IPv4Config.Pool.End = end
return b
}

func (b *ipPoolBuilder) Exclude(ipAddressList ...string) *ipPoolBuilder {
func (b *IPPoolBuilder) Exclude(ipAddressList ...string) *IPPoolBuilder {
b.ipPool.Spec.IPv4Config.Pool.Exclude = append(b.ipPool.Spec.IPv4Config.Pool.Exclude, ipAddressList...)
return b
}

func (b *ipPoolBuilder) AgentPodRef(namespace, name string) *ipPoolBuilder {
func (b *IPPoolBuilder) AgentPodRef(namespace, name string) *IPPoolBuilder {
if b.ipPool.Status.AgentPodRef == nil {
b.ipPool.Status.AgentPodRef = new(networkv1.PodReference)
}
Expand All @@ -231,7 +230,7 @@ func (b *ipPoolBuilder) AgentPodRef(namespace, name string) *ipPoolBuilder {
return b
}

func (b *ipPoolBuilder) Allocated(ipAddress, macAddress string) *ipPoolBuilder {
func (b *IPPoolBuilder) Allocated(ipAddress, macAddress string) *IPPoolBuilder {
if b.ipPool.Status.IPv4 == nil {
b.ipPool.Status.IPv4 = new(networkv1.IPv4Status)
}
Expand All @@ -242,43 +241,43 @@ func (b *ipPoolBuilder) Allocated(ipAddress, macAddress string) *ipPoolBuilder {
return b
}

func (b *ipPoolBuilder) Available(count int) *ipPoolBuilder {
func (b *IPPoolBuilder) Available(count int) *IPPoolBuilder {
if b.ipPool.Status.IPv4 == nil {
b.ipPool.Status.IPv4 = new(networkv1.IPv4Status)
}
b.ipPool.Status.IPv4.Available = count
return b
}

func (b *ipPoolBuilder) Used(count int) *ipPoolBuilder {
func (b *IPPoolBuilder) Used(count int) *IPPoolBuilder {
if b.ipPool.Status.IPv4 == nil {
b.ipPool.Status.IPv4 = new(networkv1.IPv4Status)
}
b.ipPool.Status.IPv4.Used = count
return b
}

func (b *ipPoolBuilder) RegisteredCondition(status corev1.ConditionStatus, reason, message string) *ipPoolBuilder {
func (b *IPPoolBuilder) RegisteredCondition(status corev1.ConditionStatus, reason, message string) *IPPoolBuilder {
setRegisteredCondition(b.ipPool, status, reason, message)
return b
}

func (b *ipPoolBuilder) CacheReadyCondition(status corev1.ConditionStatus, reason, message string) *ipPoolBuilder {
func (b *IPPoolBuilder) CacheReadyCondition(status corev1.ConditionStatus, reason, message string) *IPPoolBuilder {
setCacheReadyCondition(b.ipPool, status, reason, message)
return b
}

func (b *ipPoolBuilder) AgentReadyCondition(status corev1.ConditionStatus, reason, message string) *ipPoolBuilder {
func (b *IPPoolBuilder) AgentReadyCondition(status corev1.ConditionStatus, reason, message string) *IPPoolBuilder {
setAgentReadyCondition(b.ipPool, status, reason, message)
return b
}

func (b *ipPoolBuilder) DisabledCondition(status corev1.ConditionStatus, reason, message string) *ipPoolBuilder {
func (b *IPPoolBuilder) DisabledCondition(status corev1.ConditionStatus, reason, message string) *IPPoolBuilder {
setDisabledCondition(b.ipPool, status, reason, message)
return b
}

func (b *ipPoolBuilder) Build() *networkv1.IPPool {
func (b *IPPoolBuilder) Build() *networkv1.IPPool {
return b.ipPool
}

Expand Down Expand Up @@ -399,59 +398,11 @@ func (b *networkAttachmentDefinitionBuilder) Build() *cniv1.NetworkAttachmentDef
return b.nad
}

type cacheAllocatorBuilder struct {
cacheAllocator *cache.CacheAllocator
}

func newCacheAllocatorBuilder() *cacheAllocatorBuilder {
return &cacheAllocatorBuilder{
cacheAllocator: cache.New(),
}
}

func (b *cacheAllocatorBuilder) MACSet(name string) *cacheAllocatorBuilder {
_ = b.cacheAllocator.NewMACSet(name)
return b
}

func (b *cacheAllocatorBuilder) Add(name, macAddress, ipAddress string) *cacheAllocatorBuilder {
_ = b.cacheAllocator.AddMAC(name, macAddress, ipAddress)
return b
}

func (b *cacheAllocatorBuilder) Build() *cache.CacheAllocator {
return b.cacheAllocator
}

type ipAllocatorBuilder struct {
ipAllocator *ipam.IPAllocator
}

func newIPAllocatorBuilder() *ipAllocatorBuilder {
return &ipAllocatorBuilder{
ipAllocator: ipam.New(),
}
}

func (b *ipAllocatorBuilder) IPSubnet(name, cidr, start, end string) *ipAllocatorBuilder {
_ = b.ipAllocator.NewIPSubnet(name, cidr, start, end)
return b
}

func (b *ipAllocatorBuilder) Revoke(name string, ipAddressList ...string) *ipAllocatorBuilder {
for _, ip := range ipAddressList {
_ = b.ipAllocator.RevokeIP(name, ip)
func SanitizeStatus(status *networkv1.IPPoolStatus) {
now := time.Time{}
status.LastUpdate = metav1.NewTime(now)
for i := range status.Conditions {
status.Conditions[i].LastTransitionTime = ""
status.Conditions[i].LastUpdateTime = ""
}
return b
}

func (b *ipAllocatorBuilder) Allocate(name string, ipAddressList ...string) *ipAllocatorBuilder {
for _, ip := range ipAddressList {
_, _ = b.ipAllocator.AllocateIP(name, ip)
}
return b
}

func (b *ipAllocatorBuilder) Build() *ipam.IPAllocator {
return b.ipAllocator
}
42 changes: 16 additions & 26 deletions pkg/controller/ippool/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package ippool
import (
"fmt"
"testing"
"time"

"github.com/stretchr/testify/assert"
corev1 "k8s.io/api/core/v1"
Expand All @@ -22,13 +21,13 @@ import (
)

const (
testIPPoolNamespace = "default"
testIPPoolName = "ippool-1"
testKey = testIPPoolNamespace + "/" + testIPPoolName
testPodNamespace = "harvester-system"
testPodName = "default-ippool-1-agent"
testNADNamespace = "default"
testNADName = "net-1"
testIPPoolNamespace = testNADNamespace
testIPPoolName = testNADName
testKey = testIPPoolNamespace + "/" + testIPPoolName
testPodNamespace = "harvester-system"
testPodName = testNADNamespace + "-" + testNADName + "-agent"
testClusterNetwork = "provider"
testServerIP = "192.168.0.2"
testNetworkName = testNADNamespace + "/" + testNADName
Expand All @@ -50,16 +49,16 @@ const (
testMAC2 = "22:33:44:55:66:77"
)

func newTestCacheAllocatorBuilder() *cacheAllocatorBuilder {
return newCacheAllocatorBuilder()
func newTestCacheAllocatorBuilder() *cache.CacheAllocatorBuilder {
return cache.NewCacheAllocatorBuilder()
}

func newTestIPAllocatorBuilder() *ipAllocatorBuilder {
return newIPAllocatorBuilder()
func newTestIPAllocatorBuilder() *ipam.IPAllocatorBuilder {
return ipam.NewIPAllocatorBuilder()
}

func newTestIPPoolBuilder() *ipPoolBuilder {
return newIPPoolBuilder(testIPPoolNamespace, testIPPoolName)
func newTestIPPoolBuilder() *IPPoolBuilder {
return NewIPPoolBuilder(testIPPoolNamespace, testIPPoolName)
}

func newTestPodBuilder() *podBuilder {
Expand Down Expand Up @@ -207,8 +206,8 @@ func TestHandler_OnChange(t *testing.T) {
actual.ipPool, actual.err = handler.OnChange(tc.given.key, tc.given.ipPool)
assert.Nil(t, actual.err)

sanitizeStatus(&tc.expected.ipPool.Status)
sanitizeStatus(&actual.ipPool.Status)
SanitizeStatus(&tc.expected.ipPool.Status)
SanitizeStatus(&actual.ipPool.Status)

assert.Equal(t, tc.expected.ipPool, actual.ipPool, tc.name)

Expand All @@ -228,7 +227,7 @@ func TestHandler_DeployAgent(t *testing.T) {
expectedStatus := newTestIPPoolStatusBuilder().
AgentPodRef(testPodNamespace, testPodName).Build()
expectedPod := prepareAgentPod(
newIPPoolBuilder(testIPPoolNamespace, testIPPoolName).
NewIPPoolBuilder(testIPPoolNamespace, testIPPoolName).
ServerIP(testServerIP).
CIDR(testCIDR).
NetworkName(testNetworkName).Build(),
Expand Down Expand Up @@ -324,7 +323,7 @@ func TestHandler_DeployAgent(t *testing.T) {
givenNAD := newTestNetworkAttachmentDefinitionBuilder().
Label(clusterNetworkLabelKey, testClusterNetwork).Build()
givenPod := prepareAgentPod(
newIPPoolBuilder(testIPPoolNamespace, testIPPoolName).
NewIPPoolBuilder(testIPPoolNamespace, testIPPoolName).
ServerIP(testServerIP).
CIDR(testCIDR).
NetworkName(testNetworkName).Build(),
Expand All @@ -341,7 +340,7 @@ func TestHandler_DeployAgent(t *testing.T) {
expectedStatus := newTestIPPoolStatusBuilder().
AgentPodRef(testPodNamespace, testPodName).Build()
expectedPod := prepareAgentPod(
newIPPoolBuilder(testIPPoolNamespace, testIPPoolName).
NewIPPoolBuilder(testIPPoolNamespace, testIPPoolName).
ServerIP(testServerIP).
CIDR(testCIDR).
NetworkName(testNetworkName).Build(),
Expand Down Expand Up @@ -599,12 +598,3 @@ func TestHandler_MonitorAgent(t *testing.T) {
assert.Equal(t, fmt.Sprintf("agent for ippool %s is not deployed", testIPPoolNamespace+"/"+testIPPoolName), err.Error())
})
}

func sanitizeStatus(status *networkv1.IPPoolStatus) {
now := time.Time{}
status.LastUpdate = metav1.NewTime(now)
for i := range status.Conditions {
status.Conditions[i].LastTransitionTime = ""
status.Conditions[i].LastUpdateTime = ""
}
}
Loading

0 comments on commit 6fe1ae0

Please sign in to comment.