Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test: add some unit-test #57

Merged
merged 1 commit into from
Jul 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ jobs:
${{ runner.os }}-buildx-
- name: Unit test
run: go test -v -short ./pkg/... ./cmd/... -coverprofile cover.out
run: sudo go test -v -short ./pkg/... ./cmd/... -coverprofile cover.out

- name: Publish Unit Test Coverage
uses: codecov/codecov-action@v3
Expand Down Expand Up @@ -97,7 +97,7 @@ jobs:
${{ runner.os }}-buildx-
- name: Unit test
run: go test ./...
run: sudo go test -v -short ./pkg/... ./cmd/...

- name: Docker meta
id: meta
Expand Down
96 changes: 96 additions & 0 deletions pkg/networkengine/routedriver/vxlan/vxlan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ import (
"net"
"testing"

"github.com/openyurtio/raven-controller-manager/pkg/ravencontroller/apis/raven/v1alpha1"
"github.com/stretchr/testify/assert"
"github.com/vishvananda/netlink"

networkutil "github.com/openyurtio/raven/pkg/networkengine/util"
netlinkutil "github.com/openyurtio/raven/pkg/networkengine/util/netlink"
"github.com/openyurtio/raven/pkg/types"
)

func Test_applyRoutes(t *testing.T) {
Expand Down Expand Up @@ -319,3 +321,97 @@ func Test_applyRule(t *testing.T) {
})
}
}

func TestVxlan_Apply(t *testing.T) {
network := &types.Network{
LocalEndpoint: &types.Endpoint{
GatewayName: "gw-1",
NodeName: "node-1",
Subnets: []string{
"10.10.1.0/24",
},
PrivateIP: "192.168.1.1",
},
LocalNodeInfo: map[types.NodeName]*v1alpha1.NodeInfo{
"node-1": {
NodeName: "node-1",
PrivateIP: "192.168.1.1",
Subnets: []string{
"10.10.1.0/24",
},
},
"node-4": {
NodeName: "node-4",
PrivateIP: "192.168.1.4",
Subnets: []string{
"10.10.4.0/24",
},
},
},
RemoteEndpoints: map[types.GatewayName]*types.Endpoint{
"gw-2": {
GatewayName: "gw-2",
NodeName: "node-2",
Subnets: []string{
"10.10.2.0/24",
},
PrivateIP: "192.168.1.2",
},
"gw-3": {
GatewayName: "gw-3",
NodeName: "remoteGwNode12",
Subnets: []string{
"10.10.3.0/24",
},
PrivateIP: "192.168.1.3",
},
},
RemoteNodeInfo: map[types.NodeName]*v1alpha1.NodeInfo{
"node-2": {
NodeName: "node-2",
PrivateIP: "192.168.1.2",
Subnets: []string{
"10.10.2.0/24",
},
},
"node-3": {
NodeName: "node-3",
PrivateIP: "192.168.1.3",
Subnets: []string{
"10.10.3.0/24",
},
},
},
}

testcases := []struct {
name string
nodeName types.NodeName
network *types.Network
}{
{
name: "test-gateway-vxlan-apply",
nodeName: "node-1",
network: network,
},
{
name: "test-non-gateway-vxlan-apply",
nodeName: "node-4",
network: network,
},
}

for _, v := range testcases {
t.Run(v.name, func(t *testing.T) {
vx := vxlan{
nodeName: v.nodeName,
}
a := assert.New(t)
a.NoError(vx.Init())
a.NoError(vx.Apply(v.network, func() (int, error) {
return 1500, nil
}))
a.NoError(vx.Cleanup())
})
}
}
17 changes: 9 additions & 8 deletions pkg/networkengine/vpndriver/libreswan/libreswan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ func TestLibreswan_Apply(t *testing.T) {
},
},
},
findCentralGw: func(network *types.Network) *types.Endpoint {
return network.LocalEndpoint
},
}, {
name: "all-NATed-gateway",
nodeName: "localGwNode",
Expand Down Expand Up @@ -155,6 +158,9 @@ func TestLibreswan_Apply(t *testing.T) {
},
},
},
findCentralGw: func(network *types.Network) *types.Endpoint {
return nil
},
}, {
name: "NATed-gateway-connect-to-central-gateway",
nodeName: "localGwNode",
Expand Down Expand Up @@ -198,6 +204,9 @@ func TestLibreswan_Apply(t *testing.T) {
},
},
},
findCentralGw: func(network *types.Network) *types.Endpoint {
return network.RemoteEndpoints["centralGw"]
},
},
{
name: "NATed-gateway-connect-to-central-gateway-and-not-NATed-gateway",
Expand Down Expand Up @@ -353,14 +362,6 @@ func TestLibreswan_Apply(t *testing.T) {
}
for _, v := range testcases {
t.Run(v.name, func(t *testing.T) {
if v.name != "central-gateway-connect-to-NATed-gateways-and-not-NATed-gateway" {
return
}
if v.findCentralGw != nil {
findCentralGw = func(network *types.Network) *types.Endpoint {
return network.LocalEndpoint
}
}
var cleanup bool
netlinkutil.XfrmPolicyFlush = func() error {
cleanup = true
Expand Down