Skip to content

Commit

Permalink
Update egress agent test (#651)
Browse files Browse the repository at this point in the history
  • Loading branch information
lou-lan authored Aug 7, 2023
1 parent b01960b commit cdb441f
Show file tree
Hide file tree
Showing 26 changed files with 1,243 additions and 298 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ unitest_tests:
-@rm -rf $(UNITEST_OUTPUT)
-@mkdir -p $(UNITEST_OUTPUT)
@echo "run unitest tests"
$(ROOT_DIR)/tools/golang/ginkgo.sh \
sudo $(ROOT_DIR)/tools/golang/ginkgo.sh \
--cover --coverprofile=./coverage.out --covermode set \
--json-report unitestreport.json \
-randomize-suites -randomize-all --keep-going --timeout=1h -p --slow-spec-threshold=120s \
Expand Down
2 changes: 1 addition & 1 deletion charts/crds/egressgateway.spidernet.io_egresstunnels.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ spec:
group: egressgateway.spidernet.io
names:
categories:
- egressnode
- egresstunnel
kind: EgressTunnel
listKind: EgressTunnelList
plural: egresstunnels
Expand Down
10 changes: 0 additions & 10 deletions pkg/agent/police.go
Original file line number Diff line number Diff line change
Expand Up @@ -487,16 +487,6 @@ func parseMark(mark string) (uint32, error) {
return i32, nil
}

func parseMarkToInt(mark string) (int, error) {
tmp := strings.ReplaceAll(mark, "0x", "")
i64, err := strconv.ParseInt(tmp, 16, 32)
if err != nil {
return 0, err
}
i32 := int(i64)
return i32, nil
}

func (r *policeReconciler) buildPolicyRule(policyName string, mark uint32, version uint8, isIgnoreInternalCIDR bool) *iptables.Rule {
tmp := "v4-"
ignoreInternalCIDRName := EgressClusterCIDRIPv4
Expand Down
4 changes: 0 additions & 4 deletions pkg/agent/police_test.go

This file was deleted.

19 changes: 13 additions & 6 deletions pkg/agent/vxlan.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package agent
import (
"context"
"fmt"
"strconv"

"net"
"strings"
Expand Down Expand Up @@ -185,7 +186,7 @@ func (r *vxlanReconciler) updateEgressNodeStatus(node *egressv1.EgressTunnel, ve
ctx := context.Background()
err = r.client.Get(ctx, types.NamespacedName{Name: r.cfg.NodeName}, node)
if err != nil {
if !errors.IsNotFound(err) {
if errors.IsNotFound(err) {
return nil
}
return err
Expand Down Expand Up @@ -283,11 +284,7 @@ func (r *vxlanReconciler) parseVTEP(status egressv1.EgressNodeStatus) *vxlan.Pee
if !ready {
return nil
}
return &vxlan.Peer{
IPv4: ipv4,
IPv6: ipv6,
MAC: mac,
}
return &vxlan.Peer{IPv4: ipv4, IPv6: ipv6, MAC: mac}
}

func (r *vxlanReconciler) version() int {
Expand Down Expand Up @@ -423,6 +420,16 @@ func (r *vxlanReconciler) ensureRoute() error {
return nil
}

func parseMarkToInt(mark string) (int, error) {
tmp := strings.ReplaceAll(mark, "0x", "")
i64, err := strconv.ParseInt(tmp, 16, 32)
if err != nil {
return 0, err
}
i32 := int(i64)
return i32, nil
}

func newEgressNodeController(mgr manager.Manager, cfg *config.Config, log logr.Logger) error {
ruleRoute := route.NewRuleRoute(log)

Expand Down
30 changes: 30 additions & 0 deletions pkg/agent/vxlan/vxlan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,33 @@ func TestDiffLink(t *testing.T) {
})
}
}

func TestVxlan(t *testing.T) {
device := New()
mac, err := net.ParseMAC("66:bf:c7:47:5c:14")
if err != nil {
t.Fatal(err)
}

ipv6, ipv6Net, err := net.ParseCIDR("fd01::1/120")
if err != nil {
t.Fatal(err)
}

ipv4Net := &net.IPNet{
IP: []byte{10, 6, 1, 21},
Mask: []byte{255, 255, 255, 0},
}

ipv6Net = &net.IPNet{
IP: ipv6,
Mask: ipv6Net.Mask,
}

err = device.EnsureLink("egress",
101, 3456, mac, 0,
ipv4Net, nil, true)
if err != nil {
t.Fatal(err)
}
}
121 changes: 0 additions & 121 deletions pkg/agent/vxlan_test.go

This file was deleted.

43 changes: 43 additions & 0 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright 2022 Authors of spidernet-io
// SPDX-License-Identifier: Apache-2.0

package config

import (
"fmt"
"os"
"testing"
)

func TestLoadConfig(t *testing.T) {
f, err := os.CreateTemp("", "example-")
if err != nil {
t.Fatal(err)
}
defer f.Close()

// 输出临时文件的文件名
fmt.Println("Created temp file:", f.Name())
defer os.Remove(f.Name())

os.Setenv("CONFIGMAP_PATH", f.Name())

kubefile, err := os.CreateTemp("", "")
if err != nil {
t.Fatal(err)
}
defer kubefile.Close()
_, err = kubefile.WriteString("apiVersion: v1\nclusters:\n- cluster:\n certificate-authority-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUMvakNDQWVhZ0F3SUJBZ0lCQURBTkJna3Foa2lHOXcwQkFRc0ZBREFWTVJNd0VRWURWUVFERXdwcmRXSmwKY201bGRHVnpNQjRYRFRJek1EY3hNakE0TXpZMU5Wb1hEVE16TURjd09UQTRNelkxTlZvd0ZURVRNQkVHQTFVRQpBeE1LYTNWaVpYSnVaWFJsY3pDQ0FTSXdEUVlKS29aSWh2Y05BUUVCQlFBRGdnRVBBRENDQVFvQ2dnRUJBTWh1CjM3bkdBek5TSGtOb1c0cW1RSzVScXp1VUlldkNjVWF4eWlZbkQwTE9yYkZVZ0lFVnRZZUEvN2psbjJidHVpVXYKVEsrRWliRTFNMUs0OC9IYk1ZVlh2WEtERDhzbTBmZ3lJVDhnc04rbFBwVVpZdFc2cGFXbWFUVnRuUWFQNU8vaAp6MEorcFIxeTkyQTFJL2ZmSVBEa2xZbXdwSldMa1BFU1IvRmRZMm9Bb2UwejFKTjZ4VTlNSGVvcVZnckc5d3dLCnRvTTNTTnFoWXZNa3VVRnFGN0Zrc0U1aUJWRmxEUXJLblZrM0p6Q25PR0tSU0FidVdPS2huZ0g0eUNVVk5ydWIKdWpDbU1iTUFSQ09uazBCUnVvcXZSdnNOeU9SdXpxdXdXSk5lQXNXeGZrT09zT1hZWmFBV0k1Zm4vLzFqbTArVwp4VVZkL0dSVjJ6TjRodW5QNEcwQ0F3RUFBYU5aTUZjd0RnWURWUjBQQVFIL0JBUURBZ0trTUE4R0ExVWRFd0VCCi93UUZNQU1CQWY4d0hRWURWUjBPQkJZRUZFa21rTmY0WGFXT0pZRHhlNWFFVUxMUlVJTVFNQlVHQTFVZEVRUU8KTUF5Q0NtdDFZbVZ5Ym1WMFpYTXdEUVlKS29aSWh2Y05BUUVMQlFBRGdnRUJBS0tNMnJJMkozb01icGkySjdTdwpxU3hTN1FDcGljQUQyUkFKelBjMStvUmdOZndxbjhZNHVyY2dQazFkNWh3R2k5WTgxU0Nzb2YybitURldyeWNHCkFZOEhBc045M0RyUSt0ZUxWK1QzZ0xpd3BxNEp6QzFLTE1IZ3lDcU1uQXhRYjVkUUN6cFVLNjhaTG1NaVNvVnUKZnd1VTd5WjJyZmtJUUU2MVdsRW03NHQ2VjhkOFpQaVNFTXdTUDlzcE43Q0FHTHNJcElKREg5bEZtYXhjNnNDdwp4UjNUOXhqakE0SjFqSmY5RFdpQWZNWkRFMXhEREd5blZKZGdzeHRiMlFUMHVuRjRTYXZsaDB6alg2NmhrRkErCnhJd0hkWHMrbStTbi9ReWd4YkJKeXB5K0FlNkhZQU0rWE1BUnJtdXlzeHc3SFF4d2ZXM3FsSHVibThDK3JiUEwKcEJRPQotLS0tLUVORCBDRVJUSUZJQ0FURS0tLS0tCg==\n server: https://10.6.1.21:6443\n name: kubernetes\ncontexts:\n- context:\n cluster: kubernetes\n user: kubernetes-admin\n name: kubernetes-admin@kubernetes\ncurrent-context: kubernetes-admin@kubernetes\nkind: Config\npreferences: {}\nusers:\n- name: kubernetes-admin\n user:\n client-certificate-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSURJVENDQWdtZ0F3SUJBZ0lJRkxqUkdDa0FndlV3RFFZSktvWklodmNOQVFFTEJRQXdGVEVUTUJFR0ExVUUKQXhNS2EzVmlaWEp1WlhSbGN6QWVGdzB5TXpBM01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c09SeE44Qlp2N0ZCUjM4c3crWU1nYjJrWHE5UDIrYnRhbgoyVVg5R2dFQU4zVkh0cnQ2QWlwNlo0TUo4azhWVlE0NU9NLzE1bmd0L0FWdkI3NmxuRjc5UkhOejdwQ2x6ZmZTCnhkR1BHZit4QW9HQUoyYmxNeWhYd1ltMitiOVJzTWxJeGNHOFVKMm9DYVdDMVM0ZGQ1bzI5blJTS1Y4UmlJTG8KZUZaSlpjcDRtMlpNZkxkUVg1clNwRStaVWlFV2xuVWNSZktLSzNQbW9wU3VEK3BVTC9TaWZSbzlCMjNKNGZ6dwovaWVYVkpoajJEemJZSDZHRUtGaUttS1QzbW14WlNBY3B4OGJUYVhlT0IrK3hhdHlMaTJuTWZrPQotLS0tLUVORCBSU0EgUFJJVkFURSBLRVktLS0tLQo=")
if err != nil {
t.Fatal(err)
}
defer os.Remove(kubefile.Name())

os.Setenv("KUBECONFIG", kubefile.Name())

cfg, err := LoadConfig(false)
if err != nil {
t.Fatal(err)
}
cfg.PrintPrettyConfig()
}
44 changes: 44 additions & 0 deletions pkg/controller/cluster_endpoint_slice_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright 2022 Authors of spidernet-io
// SPDX-License-Identifier: Apache-2.0

package controller

import (
"testing"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/event"
)

func TestNamespacePredicate(t *testing.T) {
p := nsPredicate{}
if !p.Create(event.CreateEvent{}) {
t.Fatal("got false")
}

if !p.Delete(event.DeleteEvent{}) {
t.Fatal("got false")
}

if !p.Update(event.UpdateEvent{
ObjectOld: &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: "default",
Labels: map[string]string{
"aa": "bb",
},
},
},
ObjectNew: &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: "default",
Labels: map[string]string{
"aa": "cc",
},
},
},
}) {
t.Fatal("got false")
}
}
9 changes: 5 additions & 4 deletions pkg/controller/egress_cluster_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package controller
import (
"context"
"fmt"
"github.com/spidernet-io/egressgateway/pkg/utils/ip"
"strings"
"sync"

Expand Down Expand Up @@ -144,11 +145,11 @@ func (r *eciReconciler) reconcileCalicoIPPool(ctx context.Context, req reconcile
log.Info("update event")

// check if cidr about ippools changed
isv4Cidr, err := utils.IsIPv4Cidr(ippool.Spec.CIDR)
isv4Cidr, err := ip.IsIPv4Cidr(ippool.Spec.CIDR)
if err != nil {
return reconcile.Result{Requeue: true}, err
}
isv6Cidr, err := utils.IsIPv6Cidr(ippool.Spec.CIDR)
isv6Cidr, err := ip.IsIPv6Cidr(ippool.Spec.CIDR)
if err != nil {
return reconcile.Result{Requeue: true}, err
}
Expand Down Expand Up @@ -547,11 +548,11 @@ func getCidr(pod *corev1.Pod, param string) (ipv4Range, ipv6Range []string, err
// get cidr
ipRanges := strings.Split(ipRange, ",")
if len(ipRanges) == 1 {
if isV4, _ := utils.IsIPv4Cidr(ipRanges[0]); isV4 {
if isV4, _ := ip.IsIPv4Cidr(ipRanges[0]); isV4 {
ipv4Range = ipRanges
ipv6Range = []string{}
}
if isV6, _ := utils.IsIPv6Cidr(ipRanges[0]); isV6 {
if isV6, _ := ip.IsIPv6Cidr(ipRanges[0]); isV6 {
ipv6Range = ipRanges
ipv4Range = []string{}

Expand Down
Loading

0 comments on commit cdb441f

Please sign in to comment.