Skip to content

Commit

Permalink
Merge pull request #294 from alauda/test/node-test
Browse files Browse the repository at this point in the history
test: add node test
  • Loading branch information
oilbeater authored Apr 3, 2020
2 parents 3237008 + ae18715 commit b2d564d
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 22 deletions.
12 changes: 12 additions & 0 deletions pkg/util/net.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,18 @@ func CIDRConflict(a, b string) bool {
return aIpNet.Contains(bIp) || bIpNet.Contains(aIp)
}

func CIDRContainIP(cidrStr, ipStr string) bool {
_, cidr, err := net.ParseCIDR(cidrStr)
if err != nil {
return false
}
ip := net.ParseIP(ipStr)
if ip == nil {
return false
}
return cidr.Contains(ip)
}

func CheckProtocol(address string) string {
address = strings.Split(address, "/")[0]
ip := net.ParseIP(address)
Expand Down
29 changes: 7 additions & 22 deletions pkg/util/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,11 @@ import (
)

func ValidateSubnet(subnet kubeovnv1.Subnet) error {
cidrStr := subnet.Spec.CIDRBlock
if cidrStr == "" {
return fmt.Errorf("cidr is required for logical switch")
}
_, cidr, err := net.ParseCIDR(cidrStr)
if err != nil {
return fmt.Errorf("%s is a invalid cidr %v", cidrStr, err)
}

gatewayStr := subnet.Spec.Gateway
if gatewayStr == "" {
return fmt.Errorf("gateway is required for logical switch")
}
gateway := net.ParseIP(gatewayStr)
if gateway == nil {
return fmt.Errorf("%s is not a valid gateway", gatewayStr)
}

if !cidr.Contains(gateway) {
return fmt.Errorf("gateway address %s not in cidr range", gatewayStr)
if !CIDRContainIP(subnet.Spec.CIDRBlock, subnet.Spec.Gateway){
return fmt.Errorf(" gateway %s is not in cidr %s", subnet.Spec.Gateway, subnet.Spec.CIDRBlock)
}

excludeIps := subnet.Spec.ExcludeIps

for _, ipr := range excludeIps {
ips := strings.Split(ipr, "..")
if len(ips) > 2 {
Expand Down Expand Up @@ -85,7 +66,11 @@ func ValidatePodNetwork(annotations map[string]string) error {
return fmt.Errorf("%s is not a valid %s", ip, IpAddressAnnotation)
}
}

if cidr := annotations[CidrAnnotation]; cidr != "" {
if !CIDRContainIP(cidr, ip) {
return fmt.Errorf("%s not in cidr %s", ip, cidr)
}
}
}

mac := annotations[MacAddressAnnotation]
Expand Down
1 change: 1 addition & 0 deletions test/e2e/e2e_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

// tests to run
_ "github.com/alauda/kube-ovn/test/e2e/ip"
_ "github.com/alauda/kube-ovn/test/e2e/node"
_ "github.com/alauda/kube-ovn/test/e2e/subnet"
)

Expand Down
1 change: 1 addition & 0 deletions test/e2e/ip/static_ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ var _ = Describe("[IP Allocation]", func() {
pod, err = f.KubeClientSet.CoreV1().Pods(namespace).Get(name, metav1.GetOptions{})
Expect(err).NotTo(HaveOccurred())
Expect(pod.Annotations[util.AllocatedAnnotation]).To(Equal("true"))
Expect(pod.Annotations[util.RoutedAnnotation]).To(Equal("true"))

time.Sleep(1 * time.Second)
ip, err := f.OvnClientSet.KubeovnV1().IPs().Get(fmt.Sprintf("%s.%s", name, namespace), metav1.GetOptions{})
Expand Down
33 changes: 33 additions & 0 deletions test/e2e/node/node.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package node

import (
"fmt"
"github.com/alauda/kube-ovn/pkg/util"
"github.com/alauda/kube-ovn/test/e2e/framework"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"os"
)

var _ = Describe("[Node Init]", func() {
f := framework.NewFramework("ip allocation", fmt.Sprintf("%s/.kube/config", os.Getenv("HOME")))

It("node annotations", func() {
nodes, err := f.KubeClientSet.CoreV1().Nodes().List(metav1.ListOptions{})
Expect(err).NotTo(HaveOccurred())
subnet, err := f.OvnClientSet.KubeovnV1().Subnets().Get("join", metav1.GetOptions{})
Expect(err).NotTo(HaveOccurred())
for _, no := range nodes.Items {
annotations := no.Annotations
Expect(annotations[util.AllocatedAnnotation]).To(Equal("true"))
Expect(annotations[util.CidrAnnotation]).To(Equal(subnet.Spec.CIDRBlock))
Expect(annotations[util.GatewayAnnotation]).To(Equal(subnet.Spec.Gateway))
Expect(annotations[util.IpAddressAnnotation]).NotTo(BeEmpty())
Expect(util.CIDRContainIP(annotations[util.CidrAnnotation], annotations[util.IpAddressAnnotation])).To(BeTrue())
Expect(annotations[util.MacAddressAnnotation]).NotTo(BeEmpty())
Expect(annotations[util.PortNameAnnotation]).NotTo(BeEmpty())
Expect(annotations[util.LogicalSwitchAnnotation]).To(Equal(subnet.Name))
}
})
})

0 comments on commit b2d564d

Please sign in to comment.