github.com/cilium/cilium@v1.16.2/pkg/testutils/mockmaps/ctmap.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright Authors of Cilium
     3  
     4  package mockmaps
     5  
     6  import (
     7  	"github.com/cilium/cilium/pkg/bpf"
     8  	"github.com/cilium/cilium/pkg/maps/ctmap"
     9  )
    10  
    11  // CtMockMap implements the CtMap interface and can be used for unit tests.
    12  type CtMockMap struct {
    13  	Entries []ctmap.CtMapRecord
    14  }
    15  
    16  // NewCtMockMap is a constructor for a CtMockMap.
    17  func NewCtMockMap(records []ctmap.CtMapRecord) *CtMockMap {
    18  	m := &CtMockMap{}
    19  	m.Entries = records
    20  	return m
    21  }
    22  
    23  // Open does nothing, mock maps need not be opened.
    24  func (m *CtMockMap) Open() error {
    25  	return nil
    26  }
    27  
    28  // Close does nothing, mock maps need not be closed either.
    29  func (m *CtMockMap) Close() error {
    30  	return nil
    31  }
    32  
    33  // Path returns a mock path for the mock map.
    34  func (m *CtMockMap) Path() (string, error) {
    35  	return "/this/is/a/mock/map", nil
    36  }
    37  
    38  // DumpEntries iterates through Map m and writes the values of the ct entries
    39  // in m to a string.
    40  func (m *CtMockMap) DumpEntries() (string, error) {
    41  	return ctmap.DoDumpEntries(m)
    42  }
    43  
    44  // DumpWithCallback runs the callback on each entry of the mock map.
    45  func (m *CtMockMap) DumpWithCallback(cb bpf.DumpCallback) error {
    46  	if cb == nil {
    47  		return nil
    48  	}
    49  	for _, e := range m.Entries {
    50  		cb(e.Key, &e.Value)
    51  	}
    52  	return nil
    53  }
    54  
    55  // Count returns the length of the map entries.
    56  func (m *CtMockMap) Count() (int, error) {
    57  	return len(m.Entries), nil
    58  }
    59  
    60  func (m *CtMockMap) Update(key bpf.MapKey, value bpf.MapValue) error {
    61  	return nil
    62  }