Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
k3s
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
k3s
Commits
2220c6df
Commit
2220c6df
authored
Jan 19, 2017
by
Maciej Kwiek
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
pkg/util/iptables missing unit tests
Added tests for Save, SaveAll, Restore and RestoreAll
parent
723fa087
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
221 additions
and
1 deletion
+221
-1
iptables.go
pkg/util/iptables/iptables.go
+0
-1
iptables_test.go
pkg/util/iptables/iptables_test.go
+221
-0
No files found.
pkg/util/iptables/iptables.go
View file @
2220c6df
...
@@ -54,7 +54,6 @@ type Interface interface {
...
@@ -54,7 +54,6 @@ type Interface interface {
DeleteRule
(
table
Table
,
chain
Chain
,
args
...
string
)
error
DeleteRule
(
table
Table
,
chain
Chain
,
args
...
string
)
error
// IsIpv6 returns true if this is managing ipv6 tables
// IsIpv6 returns true if this is managing ipv6 tables
IsIpv6
()
bool
IsIpv6
()
bool
// TODO: (BenTheElder) Unit-Test Save/SaveAll, Restore/RestoreAll
// Save calls `iptables-save` for table.
// Save calls `iptables-save` for table.
Save
(
table
Table
)
([]
byte
,
error
)
Save
(
table
Table
)
([]
byte
,
error
)
// SaveAll calls `iptables-save`.
// SaveAll calls `iptables-save`.
...
...
pkg/util/iptables/iptables_test.go
View file @
2220c6df
...
@@ -771,3 +771,224 @@ func TestReload(t *testing.T) {
...
@@ -771,3 +771,224 @@ func TestReload(t *testing.T) {
t
.
Errorf
(
"wrong CombinedOutput() log, got %s"
,
fcmd
.
CombinedOutputLog
[
6
])
t
.
Errorf
(
"wrong CombinedOutput() log, got %s"
,
fcmd
.
CombinedOutputLog
[
6
])
}
}
}
}
func
TestSave
(
t
*
testing
.
T
)
{
output
:=
`# Generated by iptables-save v1.6.0 on Thu Jan 19 11:38:09 2017
*filter
:INPUT ACCEPT [15079:38410730]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [11045:521562]
COMMIT
# Completed on Thu Jan 19 11:38:09 2017`
fcmd
:=
exec
.
FakeCmd
{
CombinedOutputScript
:
[]
exec
.
FakeCombinedOutputAction
{
// iptables version check
func
()
([]
byte
,
error
)
{
return
[]
byte
(
"iptables v1.9.22"
),
nil
},
func
()
([]
byte
,
error
)
{
return
[]
byte
(
output
),
nil
},
func
()
([]
byte
,
error
)
{
return
nil
,
&
exec
.
FakeExitError
{
Status
:
1
}
},
},
}
fexec
:=
exec
.
FakeExec
{
CommandScript
:
[]
exec
.
FakeCommandAction
{
func
(
cmd
string
,
args
...
string
)
exec
.
Cmd
{
return
exec
.
InitFakeCmd
(
&
fcmd
,
cmd
,
args
...
)
},
func
(
cmd
string
,
args
...
string
)
exec
.
Cmd
{
return
exec
.
InitFakeCmd
(
&
fcmd
,
cmd
,
args
...
)
},
func
(
cmd
string
,
args
...
string
)
exec
.
Cmd
{
return
exec
.
InitFakeCmd
(
&
fcmd
,
cmd
,
args
...
)
},
},
}
runner
:=
New
(
&
fexec
,
dbus
.
NewFake
(
nil
,
nil
),
ProtocolIpv4
)
defer
runner
.
Destroy
()
// Success.
o
,
err
:=
runner
.
Save
(
TableNAT
)
if
err
!=
nil
{
t
.
Fatalf
(
"expected success, got %v"
,
err
)
}
if
string
(
o
[
:
len
(
output
)])
!=
output
{
t
.
Errorf
(
"expected output to be equal to mocked one, got %v"
,
o
)
}
if
fcmd
.
CombinedOutputCalls
!=
2
{
t
.
Errorf
(
"expected 2 CombinedOutput() calls, got %d"
,
fcmd
.
CombinedOutputCalls
)
}
if
!
sets
.
NewString
(
fcmd
.
CombinedOutputLog
[
1
]
...
)
.
HasAll
(
"iptables-save"
,
"-t"
,
"nat"
)
{
t
.
Errorf
(
"wrong CombinedOutput() log, got %s"
,
fcmd
.
CombinedOutputLog
[
1
])
}
// Failure.
_
,
err
=
runner
.
Save
(
TableNAT
)
if
err
==
nil
{
t
.
Errorf
(
"expected failure"
)
}
}
func
TestSaveAll
(
t
*
testing
.
T
)
{
output
:=
`# Generated by iptables-save v1.6.0 on Thu Jan 19 11:38:09 2017
*filter
:INPUT ACCEPT [15079:38410730]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [11045:521562]
COMMIT
# Completed on Thu Jan 19 11:38:09 2017`
fcmd
:=
exec
.
FakeCmd
{
CombinedOutputScript
:
[]
exec
.
FakeCombinedOutputAction
{
// iptables version check
func
()
([]
byte
,
error
)
{
return
[]
byte
(
"iptables v1.9.22"
),
nil
},
func
()
([]
byte
,
error
)
{
return
[]
byte
(
output
),
nil
},
func
()
([]
byte
,
error
)
{
return
nil
,
&
exec
.
FakeExitError
{
Status
:
1
}
},
},
}
fexec
:=
exec
.
FakeExec
{
CommandScript
:
[]
exec
.
FakeCommandAction
{
func
(
cmd
string
,
args
...
string
)
exec
.
Cmd
{
return
exec
.
InitFakeCmd
(
&
fcmd
,
cmd
,
args
...
)
},
func
(
cmd
string
,
args
...
string
)
exec
.
Cmd
{
return
exec
.
InitFakeCmd
(
&
fcmd
,
cmd
,
args
...
)
},
func
(
cmd
string
,
args
...
string
)
exec
.
Cmd
{
return
exec
.
InitFakeCmd
(
&
fcmd
,
cmd
,
args
...
)
},
},
}
runner
:=
New
(
&
fexec
,
dbus
.
NewFake
(
nil
,
nil
),
ProtocolIpv4
)
defer
runner
.
Destroy
()
// Success.
o
,
err
:=
runner
.
SaveAll
()
if
err
!=
nil
{
t
.
Fatalf
(
"expected success, got %v"
,
err
)
}
if
string
(
o
[
:
len
(
output
)])
!=
output
{
t
.
Errorf
(
"expected output to be equal to mocked one, got %v"
,
o
)
}
if
fcmd
.
CombinedOutputCalls
!=
2
{
t
.
Errorf
(
"expected 2 CombinedOutput() calls, got %d"
,
fcmd
.
CombinedOutputCalls
)
}
if
!
sets
.
NewString
(
fcmd
.
CombinedOutputLog
[
1
]
...
)
.
HasAll
(
"iptables-save"
)
{
t
.
Errorf
(
"wrong CombinedOutput() log, got %s"
,
fcmd
.
CombinedOutputLog
[
1
])
}
// Failure.
_
,
err
=
runner
.
SaveAll
()
if
err
==
nil
{
t
.
Errorf
(
"expected failure"
)
}
}
func
TestRestore
(
t
*
testing
.
T
)
{
fcmd
:=
exec
.
FakeCmd
{
CombinedOutputScript
:
[]
exec
.
FakeCombinedOutputAction
{
// iptables version check
func
()
([]
byte
,
error
)
{
return
[]
byte
(
"iptables v1.9.22"
),
nil
},
func
()
([]
byte
,
error
)
{
return
[]
byte
{},
nil
},
func
()
([]
byte
,
error
)
{
return
[]
byte
{},
nil
},
func
()
([]
byte
,
error
)
{
return
[]
byte
{},
nil
},
func
()
([]
byte
,
error
)
{
return
[]
byte
{},
nil
},
func
()
([]
byte
,
error
)
{
return
nil
,
&
exec
.
FakeExitError
{
Status
:
1
}
},
},
}
fexec
:=
exec
.
FakeExec
{
CommandScript
:
[]
exec
.
FakeCommandAction
{
func
(
cmd
string
,
args
...
string
)
exec
.
Cmd
{
return
exec
.
InitFakeCmd
(
&
fcmd
,
cmd
,
args
...
)
},
func
(
cmd
string
,
args
...
string
)
exec
.
Cmd
{
return
exec
.
InitFakeCmd
(
&
fcmd
,
cmd
,
args
...
)
},
func
(
cmd
string
,
args
...
string
)
exec
.
Cmd
{
return
exec
.
InitFakeCmd
(
&
fcmd
,
cmd
,
args
...
)
},
func
(
cmd
string
,
args
...
string
)
exec
.
Cmd
{
return
exec
.
InitFakeCmd
(
&
fcmd
,
cmd
,
args
...
)
},
func
(
cmd
string
,
args
...
string
)
exec
.
Cmd
{
return
exec
.
InitFakeCmd
(
&
fcmd
,
cmd
,
args
...
)
},
func
(
cmd
string
,
args
...
string
)
exec
.
Cmd
{
return
exec
.
InitFakeCmd
(
&
fcmd
,
cmd
,
args
...
)
},
},
}
runner
:=
New
(
&
fexec
,
dbus
.
NewFake
(
nil
,
nil
),
ProtocolIpv4
)
defer
runner
.
Destroy
()
// both flags true
err
:=
runner
.
Restore
(
TableNAT
,
[]
byte
{},
FlushTables
,
RestoreCounters
)
if
err
!=
nil
{
t
.
Errorf
(
"expected success, got %v"
,
err
)
}
commandSet
:=
sets
.
NewString
(
fcmd
.
CombinedOutputLog
[
1
]
...
)
if
!
commandSet
.
HasAll
(
"iptables-restore"
,
"-T"
,
string
(
TableNAT
),
"--counters"
)
||
commandSet
.
HasAny
(
"--noflush"
)
{
t
.
Errorf
(
"wrong CombinedOutput() log, got %s"
,
fcmd
.
CombinedOutputLog
[
1
])
}
// FlushTables, NoRestoreCounters
err
=
runner
.
Restore
(
TableNAT
,
[]
byte
{},
FlushTables
,
NoRestoreCounters
)
if
err
!=
nil
{
t
.
Errorf
(
"expected success, got %v"
,
err
)
}
commandSet
=
sets
.
NewString
(
fcmd
.
CombinedOutputLog
[
2
]
...
)
if
!
commandSet
.
HasAll
(
"iptables-restore"
,
"-T"
,
string
(
TableNAT
))
||
commandSet
.
HasAny
(
"--noflush"
,
"--counters"
)
{
t
.
Errorf
(
"wrong CombinedOutput() log, got %s"
,
fcmd
.
CombinedOutputLog
[
2
])
}
// NoFlushTables, RestoreCounters
err
=
runner
.
Restore
(
TableNAT
,
[]
byte
{},
NoFlushTables
,
RestoreCounters
)
if
err
!=
nil
{
t
.
Errorf
(
"expected success, got %v"
,
err
)
}
commandSet
=
sets
.
NewString
(
fcmd
.
CombinedOutputLog
[
3
]
...
)
if
!
commandSet
.
HasAll
(
"iptables-restore"
,
"-T"
,
string
(
TableNAT
),
"--noflush"
,
"--counters"
)
{
t
.
Errorf
(
"wrong CombinedOutput() log, got %s"
,
fcmd
.
CombinedOutputLog
[
3
])
}
// NoFlushTables, NoRestoreCounters
err
=
runner
.
Restore
(
TableNAT
,
[]
byte
{},
NoFlushTables
,
NoRestoreCounters
)
if
err
!=
nil
{
t
.
Errorf
(
"expected success, got %v"
,
err
)
}
commandSet
=
sets
.
NewString
(
fcmd
.
CombinedOutputLog
[
4
]
...
)
if
!
commandSet
.
HasAll
(
"iptables-restore"
,
"-T"
,
string
(
TableNAT
),
"--noflush"
)
||
commandSet
.
HasAny
(
"--counters"
)
{
t
.
Errorf
(
"wrong CombinedOutput() log, got %s"
,
fcmd
.
CombinedOutputLog
[
4
])
}
if
fcmd
.
CombinedOutputCalls
!=
5
{
t
.
Errorf
(
"expected 5 CombinedOutput() calls, got %d"
,
fcmd
.
CombinedOutputCalls
)
}
// Failure.
err
=
runner
.
Restore
(
TableNAT
,
[]
byte
{},
FlushTables
,
RestoreCounters
)
if
err
==
nil
{
t
.
Errorf
(
"expected failure"
)
}
}
// TestRestoreAll tests only the simplest use case, as flag handling code is already tested in TestRestore
func
TestRestoreAll
(
t
*
testing
.
T
)
{
fcmd
:=
exec
.
FakeCmd
{
CombinedOutputScript
:
[]
exec
.
FakeCombinedOutputAction
{
// iptables version check
func
()
([]
byte
,
error
)
{
return
[]
byte
(
"iptables v1.9.22"
),
nil
},
func
()
([]
byte
,
error
)
{
return
[]
byte
{},
nil
},
func
()
([]
byte
,
error
)
{
return
nil
,
&
exec
.
FakeExitError
{
Status
:
1
}
},
},
}
fexec
:=
exec
.
FakeExec
{
CommandScript
:
[]
exec
.
FakeCommandAction
{
func
(
cmd
string
,
args
...
string
)
exec
.
Cmd
{
return
exec
.
InitFakeCmd
(
&
fcmd
,
cmd
,
args
...
)
},
func
(
cmd
string
,
args
...
string
)
exec
.
Cmd
{
return
exec
.
InitFakeCmd
(
&
fcmd
,
cmd
,
args
...
)
},
func
(
cmd
string
,
args
...
string
)
exec
.
Cmd
{
return
exec
.
InitFakeCmd
(
&
fcmd
,
cmd
,
args
...
)
},
},
}
runner
:=
New
(
&
fexec
,
dbus
.
NewFake
(
nil
,
nil
),
ProtocolIpv4
)
defer
runner
.
Destroy
()
err
:=
runner
.
RestoreAll
([]
byte
{},
NoFlushTables
,
RestoreCounters
)
if
err
!=
nil
{
t
.
Errorf
(
"expected success, got %v"
,
err
)
}
commandSet
:=
sets
.
NewString
(
fcmd
.
CombinedOutputLog
[
1
]
...
)
if
!
commandSet
.
HasAll
(
"iptables-restore"
,
"--counters"
,
"--noflush"
)
{
t
.
Errorf
(
"wrong CombinedOutput() log, got %s"
,
fcmd
.
CombinedOutputLog
[
1
])
}
if
fcmd
.
CombinedOutputCalls
!=
2
{
t
.
Errorf
(
"expected 2 CombinedOutput() calls, got %d"
,
fcmd
.
CombinedOutputCalls
)
}
// Failure.
err
=
runner
.
Restore
(
TableNAT
,
[]
byte
{},
FlushTables
,
RestoreCounters
)
if
err
==
nil
{
t
.
Errorf
(
"expected failure"
)
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment