...
1
2
3
4
19
20 package conntrack
21
22 import (
23 "fmt"
24
25 v1 "k8s.io/api/core/v1"
26 "k8s.io/apimachinery/pkg/util/sets"
27 )
28
29
30 type FakeInterface struct {
31 ClearedIPs sets.Set[string]
32 ClearedPorts sets.Set[int]
33 ClearedNATs map[string]string
34 ClearedPortNATs map[int]string
35 }
36
37 var _ Interface = &FakeInterface{}
38
39
40 func NewFake() *FakeInterface {
41 fake := &FakeInterface{}
42 fake.Reset()
43 return fake
44 }
45
46
47 func (fake *FakeInterface) Reset() {
48 fake.ClearedIPs = sets.New[string]()
49 fake.ClearedPorts = sets.New[int]()
50 fake.ClearedNATs = make(map[string]string)
51 fake.ClearedPortNATs = make(map[int]string)
52 }
53
54
55 func (fake *FakeInterface) ClearEntriesForIP(ip string, protocol v1.Protocol) error {
56 if protocol != v1.ProtocolUDP {
57 return fmt.Errorf("FakeInterface currently only supports UDP")
58 }
59
60 fake.ClearedIPs.Insert(ip)
61 return nil
62 }
63
64
65 func (fake *FakeInterface) ClearEntriesForPort(port int, isIPv6 bool, protocol v1.Protocol) error {
66 if protocol != v1.ProtocolUDP {
67 return fmt.Errorf("FakeInterface currently only supports UDP")
68 }
69
70 fake.ClearedPorts.Insert(port)
71 return nil
72 }
73
74
75 func (fake *FakeInterface) ClearEntriesForNAT(origin, dest string, protocol v1.Protocol) error {
76 if protocol != v1.ProtocolUDP {
77 return fmt.Errorf("FakeInterface currently only supports UDP")
78 }
79 if previous, exists := fake.ClearedNATs[origin]; exists && previous != dest {
80 return fmt.Errorf("ClearEntriesForNAT called with same origin (%s), different destination (%s / %s)", origin, previous, dest)
81 }
82
83 fake.ClearedNATs[origin] = dest
84 return nil
85 }
86
87
88 func (fake *FakeInterface) ClearEntriesForPortNAT(dest string, port int, protocol v1.Protocol) error {
89 if protocol != v1.ProtocolUDP {
90 return fmt.Errorf("FakeInterface currently only supports UDP")
91 }
92 if previous, exists := fake.ClearedPortNATs[port]; exists && previous != dest {
93 return fmt.Errorf("ClearEntriesForPortNAT called with same port (%d), different destination (%s / %s)", port, previous, dest)
94 }
95
96 fake.ClearedPortNATs[port] = dest
97 return nil
98 }
99
View as plain text