Skip to content

Commit c38e79e

Browse files
committed
refactor: incorporated the review comments
Signed-off-by: gkarthiks <[email protected]>
1 parent a4abc1d commit c38e79e

File tree

1 file changed

+20
-23
lines changed

1 file changed

+20
-23
lines changed

pkg/util/iptables/testing/fake.go

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,19 @@ const (
3030
Destination = "-d "
3131
// Source represents the source address flag
3232
Source = "-s "
33-
// DPort represents the destination port
33+
// DPort represents the destination port flag
3434
DPort = "--dport "
35-
// Protocol represents the protocol flag which takes input by number of name
35+
// Protocol represents the protocol flag
3636
Protocol = "-p "
3737
// Jump represents jump flag specifies the jump target
3838
Jump = "-j "
3939
// Reject specifies the reject target
4040
Reject = "REJECT"
41-
// ToDest represents the --to-destination flag used to specify the destination address in DNAT
41+
// ToDest represents the flag used to specify the destination address in DNAT
4242
ToDest = "--to-destination "
4343
// Recent represents the sub-command recent that allows to dynamically create list of IP address to match against
4444
Recent = "recent "
45-
// MatchSet represents the --match-set flag which match packets against the specified set
45+
// MatchSet represents the flag which match packets against the specified set
4646
MatchSet = "--match-set "
4747
// SrcType represents the --src-type flag which matches if the source address is of given type
4848
SrcType = "--src-type "
@@ -53,79 +53,78 @@ const (
5353
// Rule holds a map of rules.
5454
type Rule map[string]string
5555

56-
// FakeIPTables no-op implementation of iptables Interface.
56+
// FakeIPTables is no-op implementation of iptables Interface.
5757
type FakeIPTables struct {
5858
hasRandomFully bool
5959
Lines []byte
6060
}
6161

62-
// NewFake returns a pointer for no-op implementation of iptables Interface.
62+
// NewFake returns a no-op iptables.Interface
6363
func NewFake() *FakeIPTables {
6464
return &FakeIPTables{}
6565
}
6666

67-
// SetHasRandomFully will enable the port maping fully randomized in the no-op implementation of iptables Interface.
67+
// SetHasRandomFully is part of iptables.Interface
6868
func (f *FakeIPTables) SetHasRandomFully(can bool) *FakeIPTables {
6969
f.hasRandomFully = can
7070
return f
7171
}
7272

73-
// EnsureChain will returns true and states the specified chain exists for testing.
73+
// EnsureChain is part of iptables.Interface
7474
func (*FakeIPTables) EnsureChain(table iptables.Table, chain iptables.Chain) (bool, error) {
7575
return true, nil
7676
}
7777

78-
// FlushChain returns nil and states that the specified chain is cleared.
78+
// FlushChain is part of iptables.Interface
7979
func (*FakeIPTables) FlushChain(table iptables.Table, chain iptables.Chain) error {
8080
return nil
8181
}
8282

83-
// DeleteChain returns nil and states that the specified chain exists and it is deleted.
83+
// DeleteChain is part of iptables.Interface
8484
func (*FakeIPTables) DeleteChain(table iptables.Table, chain iptables.Chain) error {
8585
return nil
8686
}
8787

88-
// EnsureRule return true and states that the specified rule is present.
88+
// EnsureRule is part of iptables.Interface
8989
func (*FakeIPTables) EnsureRule(position iptables.RulePosition, table iptables.Table, chain iptables.Chain, args ...string) (bool, error) {
9090
return true, nil
9191
}
9292

93-
// DeleteRule returns nil and states that the specified rule is present and is deleted.
93+
// DeleteRule is part of iptables.Interface
9494
func (*FakeIPTables) DeleteRule(table iptables.Table, chain iptables.Chain, args ...string) error {
9595
return nil
9696
}
9797

98-
// IsIpv6 returns false and states that it is managing only ipv4 tables.
98+
// IsIpv6 is part of iptables.Interface
9999
func (*FakeIPTables) IsIpv6() bool {
100100
return false
101101
}
102102

103-
// Save returns a copy of the iptables lines byte array.
103+
// Save is part of iptables.Interface
104104
func (f *FakeIPTables) Save(table iptables.Table) ([]byte, error) {
105105
lines := make([]byte, len(f.Lines))
106106
copy(lines, f.Lines)
107107
return lines, nil
108108
}
109109

110-
// SaveInto calls `iptables-save` command for table and stores result in a given buffer.
110+
// SaveInto is part of iptables.Interface
111111
func (f *FakeIPTables) SaveInto(table iptables.Table, buffer *bytes.Buffer) error {
112112
buffer.Write(f.Lines)
113113
return nil
114114
}
115115

116-
// Restore returns null and states that it ran `iptables-restore` successfully.
116+
// Restore is part of iptables.Interface
117117
func (*FakeIPTables) Restore(table iptables.Table, data []byte, flush iptables.FlushFlag, counters iptables.RestoreCountersFlag) error {
118118
return nil
119119
}
120120

121-
// RestoreAll is the same as Restore except that no table is specified.
121+
// RestoreAll is part of iptables.Interface
122122
func (f *FakeIPTables) RestoreAll(data []byte, flush iptables.FlushFlag, counters iptables.RestoreCountersFlag) error {
123123
f.Lines = data
124124
return nil
125125
}
126126

127-
// Monitor detects when the given iptables tables have been flushed by an external
128-
// tool (e.g. a firewall reload) by creating canary chains and polling to see if they have been deleted.
127+
// Monitor is part of iptables.Interface
129128
func (f *FakeIPTables) Monitor(canary iptables.Chain, tables []iptables.Table, reloadFunc func(), interval time.Duration, stopCh <-chan struct{}) {
130129
}
131130

@@ -137,9 +136,7 @@ func getToken(line, separator string) string {
137136
return ""
138137
}
139138

140-
// GetRules returns a list of rules for the given chain.
141-
// The chain name must match exactly.
142-
// The matching is pretty dumb, don't rely on it for anything but testing.
139+
// GetRules is part of iptables.Interface
143140
func (f *FakeIPTables) GetRules(chainName string) (rules []Rule) {
144141
for _, l := range strings.Split(string(f.Lines), "\n") {
145142
if strings.Contains(l, fmt.Sprintf("-A %v", chainName)) {
@@ -156,7 +153,7 @@ func (f *FakeIPTables) GetRules(chainName string) (rules []Rule) {
156153
return
157154
}
158155

159-
// HasRandomFully returns the value of the flag --random-fully
156+
// HasRandomFully is part of iptables.Interface
160157
func (f *FakeIPTables) HasRandomFully() bool {
161158
return f.hasRandomFully
162159
}

0 commit comments

Comments
 (0)