Skip to content

Commit

Permalink
Fix Contiv UTs.
Browse files Browse the repository at this point in the history
  • Loading branch information
Milan Lenco committed Nov 27, 2018
1 parent 6f14404 commit 2a3d4bf
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 29 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ clean:
test:
@echo "# running unit tests"
go test ./cmd/contiv-cni -tags="${GO_BUILD_TAGS}"
#go test ./plugins/contiv -tags="${GO_BUILD_TAGS}"
go test ./plugins/contiv -tags="${GO_BUILD_TAGS}"
go test ./plugins/contiv/ipam -tags="${GO_BUILD_TAGS}"
go test ./plugins/ksr -tags="${GO_BUILD_TAGS}"
go test ./plugins/policy/configurator -tags="${GO_BUILD_TAGS}"
Expand Down
30 changes: 30 additions & 0 deletions mock/eventloop/eventloop_mock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright (c) 2018 Cisco and/or its affiliates.
//
// 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 eventloop

import (
controller "github.com/contiv/vpp/plugins/controller/api"
)

// MockEventLoop is a mock implementation of the main event loop.
type MockEventLoop struct {
EventQueue []controller.Event
}

// PushEvent adds the given event into the mock event queue.
func (m *MockEventLoop) PushEvent(event controller.Event) error {
m.EventQueue = append(m.EventQueue, event)
return nil
}
70 changes: 70 additions & 0 deletions mock/nodesync/nodesync_mock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package nodesync

import (
"github.com/contiv/vpp/plugins/nodesync"
)

// MockNodeSync is a mock implementation of nodesync plugin.
type MockNodeSync struct {
thisNodeName string

nodes nodesync.Nodes
thisNode *nodesync.Node
}

// NewMockNodeSync is a constructor for MockNodeSync.
func NewMockNodeSync(thisNodeName string) *MockNodeSync {
return &MockNodeSync{
thisNodeName: thisNodeName,
nodes: make(nodesync.Nodes),
}
}

// GetNodeID return this node ID as set via UpdateNode() method.
func (m *MockNodeSync) GetNodeID() uint32 {
if m.thisNode == nil {
return 0
}
return m.thisNode.ID
}

// PublishNodeIPs does nothing here.
func (m *MockNodeSync) PublishNodeIPs(addresses []*nodesync.IPWithNetwork, version nodesync.IPVersion) error {
return nil
}

// GetAllNodes returns mock node data as set via UpdateNode() method.
func (m *MockNodeSync) GetAllNodes() nodesync.Nodes {
return m.nodes
}

// UpdateNode allows to set mock node data to test against.
func (m *MockNodeSync) UpdateNode(node *nodesync.Node) *nodesync.NodeUpdate {
if node.Name == m.thisNodeName {
m.thisNode = node
}
prev, _ := m.nodes[node.Name]
m.nodes[node.Name] = node
return &nodesync.NodeUpdate{
NodeName: node.Name,
PrevState: prev,
NewState: node,
}
}

// DeleteNode allows to delete node data.
func (m *MockNodeSync) DeleteNode(nodeName string) *nodesync.NodeUpdate {
if nodeName == m.thisNodeName {
m.thisNode = nil
}
prev, hasPrev := m.nodes[nodeName]
if !hasPrev {
return nil
}
delete(m.nodes, nodeName)
return &nodesync.NodeUpdate{
NodeName: nodeName,
PrevState: prev,
NewState: nil,
}
}
74 changes: 46 additions & 28 deletions plugins/contiv/remote_cni_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"context"
"fmt"
"net"
"strconv"
"strings"
"testing"

Expand All @@ -33,14 +32,17 @@ import (

. "github.com/contiv/vpp/mock/datasync"
. "github.com/contiv/vpp/mock/dockerclient"
. "github.com/contiv/vpp/mock/eventloop"
"github.com/contiv/vpp/mock/localclient"
. "github.com/contiv/vpp/mock/nodesync"

stn_grpc "github.com/contiv/vpp/cmd/contiv-stn/model/stn"
"github.com/contiv/vpp/plugins/contiv/ipam"
"github.com/contiv/vpp/plugins/contiv/model/cni"
controller "github.com/contiv/vpp/plugins/controller/api"
nodeconfig "github.com/contiv/vpp/plugins/crd/pkg/apis/nodeconfig/v1"
k8sPod "github.com/contiv/vpp/plugins/ksr/model/pod"
"github.com/contiv/vpp/plugins/nodesync"
)

const (
Expand Down Expand Up @@ -153,16 +155,27 @@ func TestBasicStuff(t *testing.T) {
dockerClient := NewMockDockerClient()
dockerClient.Connect()

// event loop
eventLoop := &MockEventLoop{}

// datasync
datasync := NewMockDataSync()

// nodesync
nodeSync := NewMockNodeSync(node1)
nodeSync.UpdateNode(&nodesync.Node{
ID: node1ID,
Name: node1,
})

// transactions
txnTracker := localclient.NewTxnTracker(nil)

// Remote CNI Server init
args := &remoteCNIserverArgs{
Logger: logrus.DefaultLogger(),
nodeID: node1ID, // TODO: mock nodesync
Logger: logrus.DefaultLogger(),
eventLoop: eventLoop,
nodeSync: nodeSync,
txnFactory: func() controller.Transaction {
return txnTracker.NewControllerTxn(false)
},
Expand Down Expand Up @@ -192,7 +205,7 @@ func TestBasicStuff(t *testing.T) {
// resync against empty K8s state data
resyncEv, resyncCount := datasync.ResyncEvent(keyPrefixes...)
txn := txnTracker.NewControllerTxn(true)
err = server.Resync(resyncEv.KubeState, resyncCount, txn)
err = server.Resync(resyncEv, resyncEv.KubeState, resyncCount, txn)
Expect(err).To(BeNil())
err = commitTransaction(txn, true)
Expect(err).To(BeNil())
Expand All @@ -202,19 +215,28 @@ func TestBasicStuff(t *testing.T) {

// simulate DHCP event
dhcpIndexes.Put(Gbe8, &interfaces.DHCPLease{InterfaceName: Gbe8, HostIpAddress: Gbe8IP, RouterIpAddress: GwIPWithPrefix})
Eventually(eventLoop.EventQueue).Should(HaveLen(1))
event := eventLoop.EventQueue[0]
nodeIPv4Change, isNodeIPv4Change := event.(*NodeIPv4Change)
Expect(isNodeIPv4Change).To(BeTrue())
nodeIP := &net.IPNet{IP: nodeIPv4Change.NodeIP, Mask: nodeIPv4Change.NodeIPNet.Mask}
Expect(nodeIP.String()).To(Equal(Gbe8IP))
Expect(nodeIPv4Change.DefaultGw.String()).To(Equal(strings.Split(GwIPWithPrefix, "/")[0]))

fmt.Println("Add another node -----------------------------------------")

// add another node
node2 := &nodeinfo.NodeInfo{
Name: node2Name,
Id: node2ID,
IpAddress: node2IP,
ManagementIpAddress: node2MgmtIP,
addr, network, _ := net.ParseCIDR(node2IP)
mgmt := net.ParseIP(node2MgmtIP)
node2 := &nodesync.Node{
Name: node2Name,
ID: node2ID,
VppIPAddresses: []*nodesync.IPWithNetwork{{Address: addr, Network: network}},
MgmtIPAddresses: []net.IP{mgmt},
}
event := datasync.PutEvent(nodeIDKey(node2ID), node2)
nodeUpdateEvent := nodeSync.UpdateNode(node2)
txn = txnTracker.NewControllerTxn(false)
change, err := server.Update(event, txn)
change, err := server.Update(nodeUpdateEvent, txn)
Expect(err).To(BeNil())
Expect(change).To(Equal("connect node ID=2"))
err = commitTransaction(txn, false)
Expand All @@ -226,15 +248,16 @@ func TestBasicStuff(t *testing.T) {
fmt.Println("Other node Mgmt IP update --------------------------------")

// update another node
node2Update := &nodeinfo.NodeInfo{
Name: node2Name,
Id: node2ID,
IpAddress: node2IP,
ManagementIpAddress: node2MgmtIPUpdated,
mgmt = net.ParseIP(node2MgmtIPUpdated)
node2Update := &nodesync.Node{
Name: node2Name,
ID: node2ID,
VppIPAddresses: []*nodesync.IPWithNetwork{{Address: addr, Network: network}},
MgmtIPAddresses: []net.IP{mgmt},
}
event = datasync.PutEvent(nodeIDKey(node2ID), node2Update)
nodeUpdateEvent = nodeSync.UpdateNode(node2Update)
txn = txnTracker.NewControllerTxn(false)
change, err = server.Update(event, txn)
change, err = server.Update(nodeUpdateEvent, txn)
Expect(err).To(BeNil())
Expect(change).To(Equal("update node ID=2"))
err = commitTransaction(txn, false)
Expand Down Expand Up @@ -286,7 +309,7 @@ func TestBasicStuff(t *testing.T) {
// resync now with the IP from DHCP, new pod and the other node
resyncEv, resyncCount = datasync.ResyncEvent(keyPrefixes...)
txn = txnTracker.NewControllerTxn(true)
err = server.Resync(resyncEv.KubeState, resyncCount, txn)
err = server.Resync(resyncEv, resyncEv.KubeState, resyncCount, txn)
Expect(err).To(BeNil())
err = commitTransaction(txn, true)
Expect(err).To(BeNil())
Expand All @@ -304,7 +327,7 @@ func TestBasicStuff(t *testing.T) {
// resync
resyncEv, resyncCount = datasync.ResyncEvent(keyPrefixes...)
txn = txnTracker.NewControllerTxn(true)
err = server.Resync(resyncEv.KubeState, resyncCount, txn)
err = server.Resync(resyncEv, resyncEv.KubeState, resyncCount, txn)
Expect(err).To(BeNil())
err = commitTransaction(txn, true)
Expect(err).To(BeNil())
Expand All @@ -329,9 +352,9 @@ func TestBasicStuff(t *testing.T) {
fmt.Println("Delete node ----------------------------------------------")

// delete the other node
event = datasync.DeleteEvent(nodeIDKey(node2ID))
nodeUpdateEvent = nodeSync.DeleteNode(node2Name)
txn = txnTracker.NewControllerTxn(false)
change, err = server.Update(event, txn)
change, err = server.Update(nodeUpdateEvent, txn)
Expect(err).To(BeNil())
Expect(change).To(Equal("disconnect node ID=2"))
err = commitTransaction(txn, false)
Expand All @@ -344,7 +367,7 @@ func TestBasicStuff(t *testing.T) {

resyncEv, resyncCount = datasync.ResyncEvent(keyPrefixes...)
txn = txnTracker.NewControllerTxn(true)
err = server.Resync(resyncEv.KubeState, resyncCount, txn)
err = server.Resync(resyncEv, resyncEv.KubeState, resyncCount, txn)
Expect(err).To(BeNil())
err = commitTransaction(txn, true)
Expect(err).To(BeNil())
Expand Down Expand Up @@ -378,11 +401,6 @@ func stolenInterfaceInfo(expInterface string, reply *stn_grpc.STNReply) StolenIn
}
}

func nodeIDKey(index int) string {
str := strconv.FormatUint(uint64(index), 10)
return nodeinfo.AllocatedIDsKeyPrefix + str
}

func podIPFromCNIReply(reply *cni.CNIReply) net.IP {
Expect(reply).ToNot(BeNil())
Expect(reply.Interfaces).To(HaveLen(1))
Expand Down

0 comments on commit 2a3d4bf

Please sign in to comment.