github.com/cilium/cilium@v1.16.2/pkg/testutils/mockmaps/nat.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/nat"
     9  )
    10  
    11  // NatMockMap implements the NatMap interface and can be used for unit tests.
    12  type NatMockMap struct {
    13  	Entries []nat.NatMapRecord
    14  }
    15  
    16  // NewNatMockMap is a constructor for a NatMockMap.
    17  func NewNatMockMap(records []nat.NatMapRecord) *NatMockMap {
    18  	m := &NatMockMap{}
    19  	m.Entries = records
    20  	return m
    21  }
    22  
    23  // Open does nothing, mock maps need not be opened.
    24  func (m *NatMockMap) Open() error {
    25  	return nil
    26  }
    27  
    28  // Close does nothing, mock maps need not be closed either.
    29  func (m *NatMockMap) Close() error {
    30  	return nil
    31  }
    32  
    33  // Path returns a mock path for the mock map.
    34  func (m *NatMockMap) 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 *NatMockMap) DumpEntries() (string, error) {
    41  	return nat.DoDumpEntries(m)
    42  }
    43  
    44  // DumpWithCallback runs the callback on each entry of the mock map.
    45  func (m *NatMockMap) 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  }