Skip to content

Commit

Permalink
nodemap: Extend fake node map for use in tests
Browse files Browse the repository at this point in the history
The fake node ID map currently only defines the minimum set of functions
needed for the interface. This commit extends it to simulate the actual
behavior of the BPF map with an in-memory Golang map.

This will allow us to use check that the map content matches our
expectations for various events of the node object lifecycle.

Signed-off-by: Paul Chaignon <paul.chaignon@gmail.com>
  • Loading branch information
pchaigno committed Jul 27, 2023
1 parent e1b9063 commit 52b0430
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion pkg/maps/nodemap/fake/node_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,42 @@
package fake

import (
"fmt"
"net"

"github.com/cilium/cilium/pkg/maps/nodemap"
)

type fakeNodeMap struct {
ids map[string]uint16
}

var _ nodemap.Map = &fakeNodeMap{}

func NewFakeNodeMap() *fakeNodeMap {
return &fakeNodeMap{}
return &fakeNodeMap{
ids: map[string]uint16{},
}
}

func (f fakeNodeMap) Update(ip net.IP, nodeID uint16) error {
f.ids[ip.String()] = nodeID
return nil
}

func (f fakeNodeMap) Delete(ip net.IP) error {
delete(f.ids, ip.String())
return nil
}

// This function is used only for tests.
func (f fakeNodeMap) Lookup(ip net.IP) (uint16, error) {
if nodeID, exists := f.ids[ip.String()]; exists {
return nodeID, nil
}
return 0, fmt.Errorf("IP not found in node ID map")
}

func (f fakeNodeMap) IterateWithCallback(cb nodemap.NodeIterateCallback) error {
return nil
}

0 comments on commit 52b0430

Please sign in to comment.