Skip to content
This repository was archived by the owner on Apr 3, 2018. It is now read-only.

Commit 43c9ed3

Browse files
author
Eric Ernst
committed
kata-agent: add initial test for kata-agent networking
These tests deal with hand-generated netlink structures and check the resulting agent protocol interface and route structures. Signed-off-by: Eric Ernst <[email protected]>
1 parent 082ec3f commit 43c9ed3

File tree

1 file changed

+105
-12
lines changed

1 file changed

+105
-12
lines changed

kata_agent_test.go

Lines changed: 105 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,14 @@ import (
2121
"io/ioutil"
2222
"net"
2323
"os"
24+
"reflect"
2425
"testing"
2526

2627
"github.com/containers/virtcontainers/pkg/mock"
2728
gpb "github.com/gogo/protobuf/types"
2829
pb "github.com/kata-containers/agent/protocols/grpc"
30+
"github.com/stretchr/testify/assert"
31+
"github.com/vishvananda/netlink"
2932
"golang.org/x/net/context"
3033
"google.golang.org/grpc"
3134
)
@@ -170,24 +173,20 @@ func (p *gRPCProxy) DestroySandbox(ctx context.Context, req *pb.DestroySandboxRe
170173
return emptyResp, nil
171174
}
172175

173-
func (p *gRPCProxy) AddInterface(ctx context.Context, req *pb.AddInterfaceRequest) (*gpb.Empty, error) {
174-
return emptyResp, nil
176+
func (p *gRPCProxy) AddInterface(ctx context.Context, req *pb.AddInterfaceRequest) (*pb.Interface, error) {
177+
return nil, nil
175178
}
176179

177-
func (p *gRPCProxy) RemoveInterface(ctx context.Context, req *pb.RemoveInterfaceRequest) (*gpb.Empty, error) {
178-
return emptyResp, nil
180+
func (p *gRPCProxy) RemoveInterface(ctx context.Context, req *pb.RemoveInterfaceRequest) (*pb.Interface, error) {
181+
return nil, nil
179182
}
180183

181-
func (p *gRPCProxy) UpdateInterface(ctx context.Context, req *pb.UpdateInterfaceRequest) (*gpb.Empty, error) {
182-
return emptyResp, nil
184+
func (p *gRPCProxy) UpdateInterface(ctx context.Context, req *pb.UpdateInterfaceRequest) (*pb.Interface, error) {
185+
return nil, nil
183186
}
184187

185-
func (p *gRPCProxy) AddRoute(ctx context.Context, req *pb.RouteRequest) (*gpb.Empty, error) {
186-
return emptyResp, nil
187-
}
188-
189-
func (p *gRPCProxy) RemoveRoute(ctx context.Context, req *pb.RouteRequest) (*gpb.Empty, error) {
190-
return emptyResp, nil
188+
func (p *gRPCProxy) UpdateRoutes(ctx context.Context, req *pb.UpdateRoutesRequest) (*pb.Routes, error) {
189+
return nil, nil
191190
}
192191

193192
func (p *gRPCProxy) OnlineCPUMem(ctx context.Context, req *pb.OnlineCPUMemRequest) (*gpb.Empty, error) {
@@ -243,3 +242,97 @@ func TestKataAgentSendReq(t *testing.T) {
243242
}
244243
}
245244
}
245+
246+
func TestGenerateInterfacesAndRoutes(t *testing.T) {
247+
248+
impl := &gRPCProxy{}
249+
250+
proxy := mock.ProxyGRPCMock{
251+
GRPCImplementer: impl,
252+
GRPCRegister: gRPCRegister,
253+
}
254+
255+
sockDir, err := testGenerateKataProxySockDir()
256+
if err != nil {
257+
t.Fatal(err)
258+
}
259+
defer os.RemoveAll(sockDir)
260+
261+
testKataProxyURL := fmt.Sprintf(testKataProxyURLTempl, sockDir)
262+
if err := proxy.Start(testKataProxyURL); err != nil {
263+
t.Fatal(err)
264+
}
265+
defer proxy.Stop()
266+
267+
k := &kataAgent{
268+
state: KataAgentState{
269+
URL: testKataProxyURL,
270+
},
271+
}
272+
273+
//
274+
//Create a couple of addresses
275+
//
276+
address1 := &net.IPNet{IP: net.IPv4(172, 17, 0, 2), Mask: net.CIDRMask(16, 32)}
277+
address2 := &net.IPNet{IP: net.IPv4(182, 17, 0, 2), Mask: net.CIDRMask(16, 32)}
278+
279+
addrs := []netlink.Addr{
280+
{IPNet: address1, Label: "phyaddr1"},
281+
{IPNet: address2, Label: "phyaddr2"},
282+
}
283+
284+
// Create a couple of routes:
285+
dst2 := &net.IPNet{IP: net.IPv4(172, 17, 0, 0), Mask: net.CIDRMask(16, 32)}
286+
src2 := net.IPv4(172, 17, 0, 2)
287+
gw2 := net.IPv4(172, 17, 0, 1)
288+
289+
routes := []netlink.Route{
290+
{LinkIndex: 329, Dst: nil, Src: nil, Gw: net.IPv4(172, 17, 0, 1), Scope: netlink.Scope(254)},
291+
{LinkIndex: 329, Dst: dst2, Src: src2, Gw: gw2},
292+
}
293+
294+
networkInfo := NetworkInfo{
295+
Iface: NetlinkIface{
296+
LinkAttrs: netlink.LinkAttrs{MTU: 1500},
297+
Type: "",
298+
},
299+
Addrs: addrs,
300+
Routes: routes,
301+
}
302+
303+
ep0 := &PhysicalEndpoint{
304+
IfaceName: "eth0",
305+
HardAddr: net.HardwareAddr{0x02, 0x00, 0xca, 0xfe, 0x00, 0x04}.String(),
306+
EndpointProperties: networkInfo,
307+
}
308+
309+
endpoints := []Endpoint{ep0}
310+
311+
nns := NetworkNamespace{NetNsPath: "foobar", NetNsCreated: true, Endpoints: endpoints}
312+
313+
resInterfaces, resRoutes, err := k.generateInterfacesAndRoutes(nns)
314+
315+
//
316+
// Build expected results:
317+
//
318+
expectedAddresses := []*pb.IPAddress{
319+
{Family: 0, Address: "172.17.0.2", Mask: "16"},
320+
{Family: 0, Address: "182.17.0.2", Mask: "16"},
321+
}
322+
323+
expectedInterfaces := []*pb.Interface{
324+
{Device: "eth0", Name: "eth0", IPAddresses: expectedAddresses, Mtu: 1500, HwAddr: "02:00:ca:fe:00:04"},
325+
}
326+
327+
expectedRoutes := []*pb.Route{
328+
{Dest: "", Gateway: "172.17.0.1", Device: "eth0", Source: "", Scope: uint32(254)},
329+
{Dest: "172.17.0.0/16", Gateway: "172.17.0.1", Device: "eth0", Source: "172.17.0.2"},
330+
}
331+
332+
assert.Nil(t, err, "unexpected failure when calling generateKataInterfacesAndRoutes")
333+
assert.True(t, reflect.DeepEqual(resInterfaces, expectedInterfaces),
334+
"Interfaces returned didn't match: got %+v, expecting %+v", resInterfaces, expectedInterfaces)
335+
assert.True(t, reflect.DeepEqual(resRoutes, expectedRoutes),
336+
"Routes returned didn't match: got %+v, expecting %+v", resRoutes, expectedRoutes)
337+
338+
}

0 commit comments

Comments
 (0)