Skip to content

Commit

Permalink
test: ipam initialize
Browse files Browse the repository at this point in the history
  • Loading branch information
fra98 committed Dec 5, 2024
1 parent c975f4e commit a5b75be
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 17 deletions.
114 changes: 114 additions & 0 deletions pkg/ipam/initialize_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
// Copyright 2019-2024 The Liqo Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package ipam

import (
"context"
"net/netip"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"k8s.io/client-go/kubernetes/scheme"
"sigs.k8s.io/controller-runtime/pkg/client/fake"

ipamcore "github.com/liqotech/liqo/pkg/ipam/core"
"github.com/liqotech/liqo/pkg/utils/testutil"
)

var _ = Describe("Initialize routine tests", func() {
const (
testNamespace = "test"
)

var (
ctx context.Context
fakeClientBuilder *fake.ClientBuilder

fakeIpamServer *LiqoIPAM
)

BeforeEach(func() {
ctx = context.Background()
fakeClientBuilder = fake.NewClientBuilder().WithScheme(scheme.Scheme)
})

Describe("Testing ipam initialization", func() {
Context("Initialize", func() {
BeforeEach(func() {
// Add in-cluster networks
client := fakeClientBuilder.WithObjects(
// First pool
testutil.FakeNetwork("net1", testNamespace, "10.0.0.0/16", nil),
testutil.FakeNetwork("net2", testNamespace, "10.2.0.0/16", nil),
testutil.FakeNetwork("net3", testNamespace, "10.4.0.0/24", nil),
testutil.FakeNetwork("net4", testNamespace, "10.3.0.0/16", nil), // network with some IPs
testutil.FakeIP("ip1", testNamespace, "10.3.0.0", "10.3.0.0/16", nil, nil, false),
testutil.FakeIP("ip2", testNamespace, "10.3.0.2", "10.3.0.0/16", nil, nil, false),

// Second pool
testutil.FakeNetwork("net5", testNamespace, "192.168.1.0/24", nil),

// Network with full pool
testutil.FakeNetwork("net6", testNamespace, "172.16.1.0/24", nil),
).Build()

ipamCore, err := ipamcore.NewIpam([]string{"10.0.0.0/8", "192.168.0.0/16", "172.16.1.0/24"})
Expect(err).To(BeNil())

// Init ipam server
fakeIpamServer = &LiqoIPAM{
Client: client,
IpamCore: ipamCore,
opts: &ServerOptions{
GraphvizEnabled: false,
},
}
})

It("should populate the cache", func() {
// Run initialize
Expect(fakeIpamServer.initialize(ctx)).To(Succeed())

// Check the cache:

// First pool
Expect(fakeIpamServer.networkIsAvailable(netip.MustParsePrefix("10.0.0.0/16"))).To(Equal(false))
Expect(fakeIpamServer.networkIsAvailable(netip.MustParsePrefix("10.1.0.0/16"))).To(Equal(true))
Expect(fakeIpamServer.networkIsAvailable(netip.MustParsePrefix("10.2.0.0/16"))).To(Equal(false))
Expect(fakeIpamServer.networkIsAvailable(netip.MustParsePrefix("10.4.0.0/24"))).To(Equal(false))
Expect(fakeIpamServer.networkIsAvailable(netip.MustParsePrefix("10.4.0.0/16"))).To(Equal(false))
Expect(fakeIpamServer.networkIsAvailable(netip.MustParsePrefix("10.4.0.0/30"))).To(Equal(false))
Expect(fakeIpamServer.networkIsAvailable(netip.MustParsePrefix("10.3.0.0/16"))).To(Equal(false))
Expect(fakeIpamServer.networkIsAvailable(netip.MustParsePrefix("172.16.1.0/24"))).To(Equal(false))
available, err := fakeIpamServer.isIPAvailable(netip.MustParseAddr("10.3.0.0"), netip.MustParsePrefix("10.3.0.0/16"))
Expect(err).To(BeNil())
Expect(available).To(Equal(false))
available, err = fakeIpamServer.isIPAvailable(netip.MustParseAddr("10.3.0.1"), netip.MustParsePrefix("10.3.0.0/16"))
Expect(err).To(BeNil())
Expect(available).To(Equal(true))
available, err = fakeIpamServer.isIPAvailable(netip.MustParseAddr("10.3.0.2"), netip.MustParsePrefix("10.3.0.0/16"))
Expect(err).To(BeNil())
Expect(available).To(Equal(false))

// Second pool
Expect(fakeIpamServer.networkIsAvailable(netip.MustParsePrefix("192.168.1.0/24"))).To(Equal(false))
Expect(fakeIpamServer.networkIsAvailable(netip.MustParsePrefix("192.168.2.0/24"))).To(Equal(true))

// Out of pools
Expect(fakeIpamServer.networkIsAvailable(netip.MustParsePrefix("1.1.1.1/24"))).To(Equal(false))
})
})
})
})
16 changes: 8 additions & 8 deletions pkg/ipam/sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ var _ = Describe("Sync routine tests", func() {
BeforeEach(func() {
// Add in-cluster networks
client := fakeClientBuilder.WithObjects(
testutil.FakeNetwork("net1", "10.0.0.0/16", map[string]string{}),
testutil.FakeNetwork("net2", "10.1.0.0/16", map[string]string{}),
testutil.FakeNetwork("net3", "10.2.0.0/16", map[string]string{}),
testutil.FakeNetwork("net4", "10.4.0.0/16", map[string]string{}),
testutil.FakeNetwork("net1", testNamespace, "10.0.0.0/16", nil),
testutil.FakeNetwork("net2", testNamespace, "10.1.0.0/16", nil),
testutil.FakeNetwork("net3", testNamespace, "10.2.0.0/16", nil),
testutil.FakeNetwork("net4", testNamespace, "10.4.0.0/16", nil),
).Build()

ipamCore, err := ipamcore.NewIpam([]string{"10.0.0.0/8"})
Expand Down Expand Up @@ -149,11 +149,11 @@ var _ = Describe("Sync routine tests", func() {
BeforeEach(func() {
// Add in-cluster IPs
client := fakeClientBuilder.WithObjects(
testutil.FakeNetwork("net1", "10.0.0.0/24", map[string]string{}),
testutil.FakeNetwork("net1", testNamespace, "10.0.0.0/24", nil),

testutil.FakeIP("ip1", "10.0.0.0", "10.0.0.0/24", nil, nil, false),
testutil.FakeIP("ip2", "10.0.0.1", "10.0.0.0/24", nil, nil, false),
testutil.FakeIP("ip3", "10.0.0.2", "10.0.0.0/24", nil, nil, false),
testutil.FakeIP("ip1", testNamespace, "10.0.0.0", "10.0.0.0/24", nil, nil, false),
testutil.FakeIP("ip2", testNamespace, "10.0.0.1", "10.0.0.0/24", nil, nil, false),
testutil.FakeIP("ip3", testNamespace, "10.0.0.2", "10.0.0.0/24", nil, nil, false),
).Build()

ipamCore, err := ipamcore.NewIpam([]string{"10.0.0.0/8"})
Expand Down
18 changes: 9 additions & 9 deletions pkg/utils/testutil/liqo.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,11 +177,11 @@ func FakeForgingOpts() *forge.ForgingOpts {
}

// FakeNetwork returns a fake Network.
func FakeNetwork(name, cidr string, labels map[string]string) *ipamv1alpha1.Network {
func FakeNetwork(name, namespace, cidr string, labels map[string]string) *ipamv1alpha1.Network {
return &ipamv1alpha1.Network{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: liqoconsts.DefaultLiqoNamespace,
Namespace: namespace,
Labels: labels,
},
Spec: ipamv1alpha1.NetworkSpec{
Expand All @@ -195,48 +195,48 @@ func FakeNetwork(name, cidr string, labels map[string]string) *ipamv1alpha1.Netw

// FakeNetworkPodCIDR returns a fake Network of type PodCIDR.
func FakeNetworkPodCIDR() *ipamv1alpha1.Network {
return FakeNetwork("pod-cidr", PodCIDR, map[string]string{
return FakeNetwork("pod-cidr", liqoconsts.DefaultLiqoNamespace, PodCIDR, map[string]string{
liqoconsts.NetworkNotRemappedLabelKey: liqoconsts.NetworkNotRemappedLabelValue,
liqoconsts.NetworkTypeLabelKey: string(liqoconsts.NetworkTypePodCIDR),
})
}

// FakeNetworkServiceCIDR returns a fake Network of type ServiceCIDR.
func FakeNetworkServiceCIDR() *ipamv1alpha1.Network {
return FakeNetwork("service-cidr", ServiceCIDR, map[string]string{
return FakeNetwork("service-cidr", liqoconsts.DefaultLiqoNamespace, ServiceCIDR, map[string]string{
liqoconsts.NetworkNotRemappedLabelKey: liqoconsts.NetworkNotRemappedLabelValue,
liqoconsts.NetworkTypeLabelKey: string(liqoconsts.NetworkTypeServiceCIDR),
})
}

// FakeNetworkExternalCIDR returns a fake Network of type ExternalCIDR.
func FakeNetworkExternalCIDR() *ipamv1alpha1.Network {
return FakeNetwork("external-cidr", ExternalCIDR, map[string]string{
return FakeNetwork("external-cidr", liqoconsts.DefaultLiqoNamespace, ExternalCIDR, map[string]string{
liqoconsts.NetworkTypeLabelKey: string(liqoconsts.NetworkTypeExternalCIDR),
})
}

// FakeNetworkInternalCIDR returns a fake Network of type InternalCIDR.
func FakeNetworkInternalCIDR() *ipamv1alpha1.Network {
return FakeNetwork("internal-cidr", InternalCIDR, map[string]string{
return FakeNetwork("internal-cidr", liqoconsts.DefaultLiqoNamespace, InternalCIDR, map[string]string{
liqoconsts.NetworkTypeLabelKey: string(liqoconsts.NetworkTypeInternalCIDR),
})
}

// FakeNetworkReservedSubnet returns a fake Network of type Reserved Subnet.
func FakeNetworkReservedSubnet(i int) *ipamv1alpha1.Network {
return FakeNetwork(ReservedSubnets[i], ReservedSubnets[i], map[string]string{
return FakeNetwork(ReservedSubnets[i], liqoconsts.DefaultLiqoNamespace, ReservedSubnets[i], map[string]string{
liqoconsts.NetworkTypeLabelKey: string(liqoconsts.NetworkTypeReserved),
liqoconsts.NetworkNotRemappedLabelKey: liqoconsts.NetworkNotRemappedLabelValue,
})
}

// FakeIP returns a fake IP.
func FakeIP(name, ip, cidr string, labels map[string]string, networkRef *corev1.ObjectReference, masquerade bool) *ipamv1alpha1.IP {
func FakeIP(name, namespace, ip, cidr string, labels map[string]string, networkRef *corev1.ObjectReference, masquerade bool) *ipamv1alpha1.IP {
return &ipamv1alpha1.IP{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: liqoconsts.DefaultLiqoNamespace,
Namespace: namespace,
Labels: labels,
},
Spec: ipamv1alpha1.IPSpec{
Expand Down

0 comments on commit a5b75be

Please sign in to comment.