12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316 |
- // +build linux
- /*
- Copyright 2014 The Kubernetes Authors.
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- */
- package iptables
- import (
- "bytes"
- "fmt"
- "net"
- "os"
- "reflect"
- "strings"
- "testing"
- "time"
- "k8s.io/apimachinery/pkg/util/sets"
- "k8s.io/kubernetes/pkg/util/dbus"
- "k8s.io/utils/exec"
- fakeexec "k8s.io/utils/exec/testing"
- )
- const TestLockfilePath = "xtables.lock"
- func protocolStr(protocol Protocol) string {
- if protocol == ProtocolIpv4 {
- return "IPv4"
- }
- return "IPv6"
- }
- func testIPTablesVersionCmds(t *testing.T, protocol Protocol) {
- version := " v1.9.22"
- iptablesCmd := iptablesCommand(protocol)
- iptablesRestoreCmd := iptablesRestoreCommand(protocol)
- protoStr := protocolStr(protocol)
- fcmd := fakeexec.FakeCmd{
- CombinedOutputScript: []fakeexec.FakeCombinedOutputAction{
- // iptables version response (for runner instantiation)
- func() ([]byte, error) { return []byte(iptablesCmd + version), nil },
- // iptables-restore version response (for runner instantiation)
- func() ([]byte, error) { return []byte(iptablesRestoreCmd + version), nil },
- // iptables version response (for call to runner.GetVersion())
- func() ([]byte, error) { return []byte(iptablesCmd + version), nil },
- },
- }
- fexec := fakeexec.FakeExec{
- CommandScript: []fakeexec.FakeCommandAction{
- func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
- func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
- func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
- },
- }
- runner := New(&fexec, dbus.NewFake(nil, nil), protocol)
- defer runner.Destroy()
- // Check that proper iptables version command was used during runner instantiation
- if !sets.NewString(fcmd.CombinedOutputLog[0]...).HasAll(iptablesCmd, "--version") {
- t.Errorf("%s runner instantiate: Expected cmd '%s --version', Got '%s'", protoStr, iptablesCmd, fcmd.CombinedOutputLog[0])
- }
- // Check that proper iptables restore version command was used during runner instantiation
- if !sets.NewString(fcmd.CombinedOutputLog[1]...).HasAll(iptablesRestoreCmd, "--version") {
- t.Errorf("%s runner instantiate: Expected cmd '%s --version', Got '%s'", protoStr, iptablesRestoreCmd, fcmd.CombinedOutputLog[1])
- }
- _, err := runner.GetVersion()
- if err != nil {
- t.Errorf("%s GetVersion: Expected success, got %v", protoStr, err)
- }
- // Check that proper iptables version command was used for runner.GetVersion
- if !sets.NewString(fcmd.CombinedOutputLog[2]...).HasAll(iptablesCmd, "--version") {
- t.Errorf("%s GetVersion: Expected cmd '%s --version', Got '%s'", protoStr, iptablesCmd, fcmd.CombinedOutputLog[2])
- }
- }
- func TestIPTablesVersionCmdsIPv4(t *testing.T) {
- testIPTablesVersionCmds(t, ProtocolIpv4)
- }
- func TestIPTablesVersionCmdsIPv6(t *testing.T) {
- testIPTablesVersionCmds(t, ProtocolIpv6)
- }
- func testEnsureChain(t *testing.T, protocol Protocol) {
- protoStr := protocolStr(protocol)
- fcmd := fakeexec.FakeCmd{
- CombinedOutputScript: []fakeexec.FakeCombinedOutputAction{
- // iptables version check
- func() ([]byte, error) { return []byte("iptables v1.9.22"), nil },
- // iptables-restore version check
- func() ([]byte, error) { return []byte("iptables-restore v1.9.22"), nil },
- // Success.
- func() ([]byte, error) { return []byte{}, nil },
- // Exists.
- func() ([]byte, error) { return nil, &fakeexec.FakeExitError{Status: 1} },
- // Failure.
- func() ([]byte, error) { return nil, &fakeexec.FakeExitError{Status: 2} },
- },
- }
- fexec := fakeexec.FakeExec{
- CommandScript: []fakeexec.FakeCommandAction{
- func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
- func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
- func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
- func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
- func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
- },
- }
- runner := New(&fexec, dbus.NewFake(nil, nil), protocol)
- defer runner.Destroy()
- // Success.
- exists, err := runner.EnsureChain(TableNAT, Chain("FOOBAR"))
- if err != nil {
- t.Errorf("%s new chain: Expected success, got %v", protoStr, err)
- }
- if exists {
- t.Errorf("%s new chain: Expected exists = false", protoStr)
- }
- if fcmd.CombinedOutputCalls != 3 {
- t.Errorf("%s new chain: Expected 3 CombinedOutput() calls, got %d", protoStr, fcmd.CombinedOutputCalls)
- }
- cmd := iptablesCommand(protocol)
- if !sets.NewString(fcmd.CombinedOutputLog[2]...).HasAll(cmd, "-t", "nat", "-N", "FOOBAR") {
- t.Errorf("%s new chain: Expected cmd containing '%s -t nat -N FOOBAR', got %s", protoStr, cmd, fcmd.CombinedOutputLog[2])
- }
- // Exists.
- exists, err = runner.EnsureChain(TableNAT, Chain("FOOBAR"))
- if err != nil {
- t.Errorf("%s existing chain: Expected success, got %v", protoStr, err)
- }
- if !exists {
- t.Errorf("%s existing chain: Expected exists = true", protoStr)
- }
- // Simulate failure.
- _, err = runner.EnsureChain(TableNAT, Chain("FOOBAR"))
- if err == nil {
- t.Errorf("%s: Expected failure", protoStr)
- }
- }
- func TestEnsureChainIpv4(t *testing.T) {
- testEnsureChain(t, ProtocolIpv4)
- }
- func TestEnsureChainIpv6(t *testing.T) {
- testEnsureChain(t, ProtocolIpv6)
- }
- func TestFlushChain(t *testing.T) {
- fcmd := fakeexec.FakeCmd{
- CombinedOutputScript: []fakeexec.FakeCombinedOutputAction{
- // iptables version check
- func() ([]byte, error) { return []byte("iptables v1.9.22"), nil },
- // iptables-restore version check
- func() ([]byte, error) { return []byte("iptables-restore v1.9.22"), nil },
- // Success.
- func() ([]byte, error) { return []byte{}, nil },
- // Failure.
- func() ([]byte, error) { return nil, &fakeexec.FakeExitError{Status: 1} },
- },
- }
- fexec := fakeexec.FakeExec{
- CommandScript: []fakeexec.FakeCommandAction{
- func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
- func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
- func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
- func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
- },
- }
- runner := New(&fexec, dbus.NewFake(nil, nil), ProtocolIpv4)
- defer runner.Destroy()
- // Success.
- err := runner.FlushChain(TableNAT, Chain("FOOBAR"))
- if err != nil {
- t.Errorf("expected success, got %v", err)
- }
- if fcmd.CombinedOutputCalls != 3 {
- t.Errorf("expected 3 CombinedOutput() calls, got %d", fcmd.CombinedOutputCalls)
- }
- if !sets.NewString(fcmd.CombinedOutputLog[2]...).HasAll("iptables", "-t", "nat", "-F", "FOOBAR") {
- t.Errorf("wrong CombinedOutput() log, got %s", fcmd.CombinedOutputLog[2])
- }
- // Failure.
- err = runner.FlushChain(TableNAT, Chain("FOOBAR"))
- if err == nil {
- t.Errorf("expected failure")
- }
- }
- func TestDeleteChain(t *testing.T) {
- fcmd := fakeexec.FakeCmd{
- CombinedOutputScript: []fakeexec.FakeCombinedOutputAction{
- // iptables version check
- func() ([]byte, error) { return []byte("iptables v1.9.22"), nil },
- // iptables-restore version check
- func() ([]byte, error) { return []byte("iptables-restore v1.9.22"), nil },
- // Success.
- func() ([]byte, error) { return []byte{}, nil },
- // Failure.
- func() ([]byte, error) { return nil, &fakeexec.FakeExitError{Status: 1} },
- },
- }
- fexec := fakeexec.FakeExec{
- CommandScript: []fakeexec.FakeCommandAction{
- func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
- func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
- func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
- func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
- },
- }
- runner := New(&fexec, dbus.NewFake(nil, nil), ProtocolIpv4)
- defer runner.Destroy()
- // Success.
- err := runner.DeleteChain(TableNAT, Chain("FOOBAR"))
- if err != nil {
- t.Errorf("expected success, got %v", err)
- }
- if fcmd.CombinedOutputCalls != 3 {
- t.Errorf("expected 3 CombinedOutput() calls, got %d", fcmd.CombinedOutputCalls)
- }
- if !sets.NewString(fcmd.CombinedOutputLog[2]...).HasAll("iptables", "-t", "nat", "-X", "FOOBAR") {
- t.Errorf("wrong CombinedOutput() log, got %s", fcmd.CombinedOutputLog[2])
- }
- // Failure.
- err = runner.DeleteChain(TableNAT, Chain("FOOBAR"))
- if err == nil {
- t.Errorf("expected failure")
- }
- }
- func TestEnsureRuleAlreadyExists(t *testing.T) {
- fcmd := fakeexec.FakeCmd{
- CombinedOutputScript: []fakeexec.FakeCombinedOutputAction{
- // iptables version check
- func() ([]byte, error) { return []byte("iptables v1.9.22"), nil },
- // iptables-restore version check
- func() ([]byte, error) { return []byte("iptables-restore v1.9.22"), nil },
- // Success.
- func() ([]byte, error) { return []byte{}, nil },
- },
- }
- fexec := fakeexec.FakeExec{
- CommandScript: []fakeexec.FakeCommandAction{
- // iptables version check
- func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
- // iptables-restore version check
- func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
- // The second Command() call is checking the rule. Success of that exec means "done".
- func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
- },
- }
- runner := New(&fexec, dbus.NewFake(nil, nil), ProtocolIpv4)
- defer runner.Destroy()
- exists, err := runner.EnsureRule(Append, TableNAT, ChainOutput, "abc", "123")
- if err != nil {
- t.Errorf("expected success, got %v", err)
- }
- if !exists {
- t.Errorf("expected exists = true")
- }
- if fcmd.CombinedOutputCalls != 3 {
- t.Errorf("expected 3 CombinedOutput() calls, got %d", fcmd.CombinedOutputCalls)
- }
- if !sets.NewString(fcmd.CombinedOutputLog[2]...).HasAll("iptables", "-t", "nat", "-C", "OUTPUT", "abc", "123") {
- t.Errorf("wrong CombinedOutput() log, got %s", fcmd.CombinedOutputLog[2])
- }
- }
- func TestEnsureRuleNew(t *testing.T) {
- fcmd := fakeexec.FakeCmd{
- CombinedOutputScript: []fakeexec.FakeCombinedOutputAction{
- // iptables version check
- func() ([]byte, error) { return []byte("iptables v1.9.22"), nil },
- // iptables-restore version check
- func() ([]byte, error) { return []byte("iptables-restore v1.9.22"), nil },
- // Status 1 on the first call.
- func() ([]byte, error) { return nil, &fakeexec.FakeExitError{Status: 1} },
- // Success on the second call.
- func() ([]byte, error) { return []byte{}, nil },
- },
- }
- fexec := fakeexec.FakeExec{
- CommandScript: []fakeexec.FakeCommandAction{
- // iptables version check
- func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
- // iptables-restore version check
- func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
- // The second Command() call is checking the rule. Failure of that means create it.
- func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
- func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
- },
- }
- runner := New(&fexec, dbus.NewFake(nil, nil), ProtocolIpv4)
- defer runner.Destroy()
- exists, err := runner.EnsureRule(Append, TableNAT, ChainOutput, "abc", "123")
- if err != nil {
- t.Errorf("expected success, got %v", err)
- }
- if exists {
- t.Errorf("expected exists = false")
- }
- if fcmd.CombinedOutputCalls != 4 {
- t.Errorf("expected 4 CombinedOutput() calls, got %d", fcmd.CombinedOutputCalls)
- }
- if !sets.NewString(fcmd.CombinedOutputLog[3]...).HasAll("iptables", "-t", "nat", "-A", "OUTPUT", "abc", "123") {
- t.Errorf("wrong CombinedOutput() log, got %s", fcmd.CombinedOutputLog[3])
- }
- }
- func TestEnsureRuleErrorChecking(t *testing.T) {
- fcmd := fakeexec.FakeCmd{
- CombinedOutputScript: []fakeexec.FakeCombinedOutputAction{
- // iptables version check
- func() ([]byte, error) { return []byte("iptables v1.9.22"), nil },
- // iptables-restore version check
- func() ([]byte, error) { return []byte("iptables-restore v1.9.22"), nil },
- // Status 2 on the first call.
- func() ([]byte, error) { return nil, &fakeexec.FakeExitError{Status: 2} },
- },
- }
- fexec := fakeexec.FakeExec{
- CommandScript: []fakeexec.FakeCommandAction{
- // iptables version check
- func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
- // iptables-restore version check
- func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
- // The second Command() call is checking the rule. Failure of that means create it.
- func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
- },
- }
- runner := New(&fexec, dbus.NewFake(nil, nil), ProtocolIpv4)
- defer runner.Destroy()
- _, err := runner.EnsureRule(Append, TableNAT, ChainOutput, "abc", "123")
- if err == nil {
- t.Errorf("expected failure")
- }
- if fcmd.CombinedOutputCalls != 3 {
- t.Errorf("expected 3 CombinedOutput() calls, got %d", fcmd.CombinedOutputCalls)
- }
- }
- func TestEnsureRuleErrorCreating(t *testing.T) {
- fcmd := fakeexec.FakeCmd{
- CombinedOutputScript: []fakeexec.FakeCombinedOutputAction{
- // iptables version check
- func() ([]byte, error) { return []byte("iptables v1.9.22"), nil },
- // iptables-restore version check
- func() ([]byte, error) { return []byte("iptables-restore v1.9.22"), nil },
- // Status 1 on the first call.
- func() ([]byte, error) { return nil, &fakeexec.FakeExitError{Status: 1} },
- // Status 1 on the second call.
- func() ([]byte, error) { return nil, &fakeexec.FakeExitError{Status: 1} },
- },
- }
- fexec := fakeexec.FakeExec{
- CommandScript: []fakeexec.FakeCommandAction{
- // iptables version check
- func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
- // iptables-restore version check
- func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
- // The second Command() call is checking the rule. Failure of that means create it.
- func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
- func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
- },
- }
- runner := New(&fexec, dbus.NewFake(nil, nil), ProtocolIpv4)
- defer runner.Destroy()
- _, err := runner.EnsureRule(Append, TableNAT, ChainOutput, "abc", "123")
- if err == nil {
- t.Errorf("expected failure")
- }
- if fcmd.CombinedOutputCalls != 4 {
- t.Errorf("expected 4 CombinedOutput() calls, got %d", fcmd.CombinedOutputCalls)
- }
- }
- func TestDeleteRuleDoesNotExist(t *testing.T) {
- fcmd := fakeexec.FakeCmd{
- CombinedOutputScript: []fakeexec.FakeCombinedOutputAction{
- // iptables version check
- func() ([]byte, error) { return []byte("iptables v1.9.22"), nil },
- // iptables-restore version check
- func() ([]byte, error) { return []byte("iptables-restore v1.9.22"), nil },
- // Status 1 on the first call.
- func() ([]byte, error) { return nil, &fakeexec.FakeExitError{Status: 1} },
- },
- }
- fexec := fakeexec.FakeExec{
- CommandScript: []fakeexec.FakeCommandAction{
- // iptables version check
- func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
- // iptables-restore version check
- func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
- // The second Command() call is checking the rule. Failure of that exec means "does not exist".
- func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
- },
- }
- runner := New(&fexec, dbus.NewFake(nil, nil), ProtocolIpv4)
- defer runner.Destroy()
- err := runner.DeleteRule(TableNAT, ChainOutput, "abc", "123")
- if err != nil {
- t.Errorf("expected success, got %v", err)
- }
- if fcmd.CombinedOutputCalls != 3 {
- t.Errorf("expected 3 CombinedOutput() calls, got %d", fcmd.CombinedOutputCalls)
- }
- if !sets.NewString(fcmd.CombinedOutputLog[2]...).HasAll("iptables", "-t", "nat", "-C", "OUTPUT", "abc", "123") {
- t.Errorf("wrong CombinedOutput() log, got %s", fcmd.CombinedOutputLog[2])
- }
- }
- func TestDeleteRuleExists(t *testing.T) {
- fcmd := fakeexec.FakeCmd{
- CombinedOutputScript: []fakeexec.FakeCombinedOutputAction{
- // iptables version check
- func() ([]byte, error) { return []byte("iptables v1.9.22"), nil },
- // iptables-restore version check
- func() ([]byte, error) { return []byte("iptables-restore v1.9.22"), nil },
- // Success on the first call.
- func() ([]byte, error) { return []byte{}, nil },
- // Success on the second call.
- func() ([]byte, error) { return []byte{}, nil },
- },
- }
- fexec := fakeexec.FakeExec{
- CommandScript: []fakeexec.FakeCommandAction{
- // iptables version check
- func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
- // iptables-restore version check
- func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
- // The second Command() call is checking the rule. Success of that means delete it.
- func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
- func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
- },
- }
- runner := New(&fexec, dbus.NewFake(nil, nil), ProtocolIpv4)
- defer runner.Destroy()
- err := runner.DeleteRule(TableNAT, ChainOutput, "abc", "123")
- if err != nil {
- t.Errorf("expected success, got %v", err)
- }
- if fcmd.CombinedOutputCalls != 4 {
- t.Errorf("expected 4 CombinedOutput() calls, got %d", fcmd.CombinedOutputCalls)
- }
- if !sets.NewString(fcmd.CombinedOutputLog[3]...).HasAll("iptables", "-t", "nat", "-D", "OUTPUT", "abc", "123") {
- t.Errorf("wrong CombinedOutput() log, got %s", fcmd.CombinedOutputLog[3])
- }
- }
- func TestDeleteRuleErrorChecking(t *testing.T) {
- fcmd := fakeexec.FakeCmd{
- CombinedOutputScript: []fakeexec.FakeCombinedOutputAction{
- // iptables version check
- func() ([]byte, error) { return []byte("iptables v1.9.22"), nil },
- // iptables-restore version check
- func() ([]byte, error) { return []byte("iptables-restore v1.9.22"), nil },
- // Status 2 on the first call.
- func() ([]byte, error) { return nil, &fakeexec.FakeExitError{Status: 2} },
- },
- }
- fexec := fakeexec.FakeExec{
- CommandScript: []fakeexec.FakeCommandAction{
- // iptables version check
- func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
- // iptables-restore version check
- func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
- // The second Command() call is checking the rule. Failure of that means create it.
- func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
- },
- }
- runner := New(&fexec, dbus.NewFake(nil, nil), ProtocolIpv4)
- defer runner.Destroy()
- err := runner.DeleteRule(TableNAT, ChainOutput, "abc", "123")
- if err == nil {
- t.Errorf("expected failure")
- }
- if fcmd.CombinedOutputCalls != 3 {
- t.Errorf("expected 3 CombinedOutput() calls, got %d", fcmd.CombinedOutputCalls)
- }
- }
- func TestDeleteRuleErrorDeleting(t *testing.T) {
- fcmd := fakeexec.FakeCmd{
- CombinedOutputScript: []fakeexec.FakeCombinedOutputAction{
- // iptables version check
- func() ([]byte, error) { return []byte("iptables v1.9.22"), nil },
- // iptables-restore version check
- func() ([]byte, error) { return []byte("iptables-restore v1.9.22"), nil },
- // Success on the first call.
- func() ([]byte, error) { return []byte{}, nil },
- // Status 1 on the second call.
- func() ([]byte, error) { return nil, &fakeexec.FakeExitError{Status: 1} },
- },
- }
- fexec := fakeexec.FakeExec{
- CommandScript: []fakeexec.FakeCommandAction{
- // iptables version check
- func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
- // iptables-restore version check
- func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
- // The second Command() call is checking the rule. Success of that means delete it.
- func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
- func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
- },
- }
- runner := New(&fexec, dbus.NewFake(nil, nil), ProtocolIpv4)
- defer runner.Destroy()
- err := runner.DeleteRule(TableNAT, ChainOutput, "abc", "123")
- if err == nil {
- t.Errorf("expected failure")
- }
- if fcmd.CombinedOutputCalls != 4 {
- t.Errorf("expected 4 CombinedOutput() calls, got %d", fcmd.CombinedOutputCalls)
- }
- }
- func TestGetIPTablesHasCheckCommand(t *testing.T) {
- testCases := []struct {
- Version string
- Err bool
- Expected bool
- }{
- {"iptables v1.4.7", false, false},
- {"iptables v1.4.11", false, true},
- {"iptables v1.4.19.1", false, true},
- {"iptables v2.0.0", false, true},
- {"total junk", true, false},
- }
- for _, testCase := range testCases {
- fcmd := fakeexec.FakeCmd{
- CombinedOutputScript: []fakeexec.FakeCombinedOutputAction{
- func() ([]byte, error) { return []byte(testCase.Version), nil },
- },
- }
- fexec := fakeexec.FakeExec{
- CommandScript: []fakeexec.FakeCommandAction{
- func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
- },
- }
- version, err := getIPTablesVersionString(&fexec, ProtocolIpv4)
- if (err != nil) != testCase.Err {
- t.Errorf("Expected error: %v, Got error: %v", testCase.Err, err)
- }
- if err == nil {
- check := getIPTablesHasCheckCommand(version)
- if testCase.Expected != check {
- t.Errorf("Expected result: %v, Got result: %v", testCase.Expected, check)
- }
- }
- }
- }
- func TestIPTablesCommands(t *testing.T) {
- testCases := []struct {
- funcName string
- protocol Protocol
- expectedCmd string
- }{
- {"iptablesCommand", ProtocolIpv4, cmdIPTables},
- {"iptablesCommand", ProtocolIpv6, cmdIP6Tables},
- {"iptablesSaveCommand", ProtocolIpv4, cmdIPTablesSave},
- {"iptablesSaveCommand", ProtocolIpv6, cmdIP6TablesSave},
- {"iptablesRestoreCommand", ProtocolIpv4, cmdIPTablesRestore},
- {"iptablesRestoreCommand", ProtocolIpv6, cmdIP6TablesRestore},
- }
- for _, testCase := range testCases {
- var cmd string
- switch testCase.funcName {
- case "iptablesCommand":
- cmd = iptablesCommand(testCase.protocol)
- case "iptablesSaveCommand":
- cmd = iptablesSaveCommand(testCase.protocol)
- case "iptablesRestoreCommand":
- cmd = iptablesRestoreCommand(testCase.protocol)
- }
- if cmd != testCase.expectedCmd {
- t.Errorf("Function: %s, Expected result: %s, Actual result: %s", testCase.funcName, testCase.expectedCmd, cmd)
- }
- }
- }
- func TestCheckRuleWithoutCheckPresent(t *testing.T) {
- iptablesSaveOutput := `# Generated by iptables-save v1.4.7 on Wed Oct 29 14:56:01 2014
- *nat
- :PREROUTING ACCEPT [2136997:197881818]
- :POSTROUTING ACCEPT [4284525:258542680]
- :OUTPUT ACCEPT [5901660:357267963]
- -A PREROUTING -m addrtype --dst-type LOCAL -m mark --mark 0x00004000/0x00004000 -j DOCKER
- COMMIT
- # Completed on Wed Oct 29 14:56:01 2014`
- fcmd := fakeexec.FakeCmd{
- CombinedOutputScript: []fakeexec.FakeCombinedOutputAction{
- // Success.
- func() ([]byte, error) { return []byte(iptablesSaveOutput), nil },
- },
- }
- fexec := fakeexec.FakeExec{
- CommandScript: []fakeexec.FakeCommandAction{
- // The first Command() call is checking the rule. Success of that exec means "done".
- func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
- },
- }
- runner := &runner{exec: &fexec}
- exists, err := runner.checkRuleWithoutCheck(
- TableNAT, ChainPrerouting,
- "-m", "addrtype",
- "-m", "mark", "--mark", "0x4000/0x4000",
- "-j", "DOCKER",
- "--dst-type", "LOCAL")
- if err != nil {
- t.Errorf("expected success, got %v", err)
- }
- if !exists {
- t.Errorf("expected exists = true")
- }
- if fcmd.CombinedOutputCalls != 1 {
- t.Errorf("expected 1 CombinedOutput() call, got %d", fcmd.CombinedOutputCalls)
- }
- if !sets.NewString(fcmd.CombinedOutputLog[0]...).HasAll("iptables-save", "-t", "nat") {
- t.Errorf("wrong CombinedOutput() log, got %s", fcmd.CombinedOutputLog[0])
- }
- }
- func TestCheckRuleWithoutCheckAbsent(t *testing.T) {
- iptablesSaveOutput := `# Generated by iptables-save v1.4.7 on Wed Oct 29 14:56:01 2014
- *nat
- :PREROUTING ACCEPT [2136997:197881818]
- :POSTROUTING ACCEPT [4284525:258542680]
- :OUTPUT ACCEPT [5901660:357267963]
- -A PREROUTING -m addrtype --dst-type LOCAL -j DOCKER
- COMMIT
- # Completed on Wed Oct 29 14:56:01 2014`
- fcmd := fakeexec.FakeCmd{
- CombinedOutputScript: []fakeexec.FakeCombinedOutputAction{
- // Success.
- func() ([]byte, error) { return []byte(iptablesSaveOutput), nil },
- },
- }
- fexec := fakeexec.FakeExec{
- CommandScript: []fakeexec.FakeCommandAction{
- // The first Command() call is checking the rule. Success of that exec means "done".
- func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
- },
- }
- runner := &runner{exec: &fexec}
- exists, err := runner.checkRuleWithoutCheck(TableNAT, ChainPrerouting, "-m", "addrtype", "-j", "DOCKER")
- if err != nil {
- t.Errorf("expected success, got %v", err)
- }
- if exists {
- t.Errorf("expected exists = false")
- }
- if fcmd.CombinedOutputCalls != 1 {
- t.Errorf("expected 1 CombinedOutput() call, got %d", fcmd.CombinedOutputCalls)
- }
- if !sets.NewString(fcmd.CombinedOutputLog[0]...).HasAll("iptables-save", "-t", "nat") {
- t.Errorf("wrong CombinedOutput() log, got %s", fcmd.CombinedOutputLog[0])
- }
- }
- func TestIPTablesWaitFlag(t *testing.T) {
- testCases := []struct {
- Version string
- Result []string
- }{
- {"0.55.55", nil},
- {"1.0.55", nil},
- {"1.4.19", nil},
- {"1.4.20", []string{WaitString}},
- {"1.4.21", []string{WaitString}},
- {"1.4.22", []string{WaitString, WaitSecondsValue}},
- {"1.5.0", []string{WaitString, WaitSecondsValue}},
- {"2.0.0", []string{WaitString, WaitSecondsValue}},
- }
- for _, testCase := range testCases {
- result := getIPTablesWaitFlag(testCase.Version)
- if !reflect.DeepEqual(result, testCase.Result) {
- t.Errorf("For %s expected %v got %v", testCase.Version, testCase.Result, result)
- }
- }
- }
- func TestWaitFlagUnavailable(t *testing.T) {
- fcmd := fakeexec.FakeCmd{
- CombinedOutputScript: []fakeexec.FakeCombinedOutputAction{
- // iptables version check
- func() ([]byte, error) { return []byte("iptables v1.4.19"), nil },
- // iptables-restore version check
- func() ([]byte, error) { return []byte("iptables-restore v1.9.22"), nil },
- // Success.
- func() ([]byte, error) { return []byte{}, nil },
- },
- }
- fexec := fakeexec.FakeExec{
- CommandScript: []fakeexec.FakeCommandAction{
- // iptables version check
- func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
- // iptables-restore version check
- func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
- func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
- },
- }
- runner := New(&fexec, dbus.NewFake(nil, nil), ProtocolIpv4)
- defer runner.Destroy()
- err := runner.DeleteChain(TableNAT, Chain("FOOBAR"))
- if err != nil {
- t.Errorf("expected success, got %v", err)
- }
- if fcmd.CombinedOutputCalls != 3 {
- t.Errorf("expected 3 CombinedOutput() calls, got %d", fcmd.CombinedOutputCalls)
- }
- if sets.NewString(fcmd.CombinedOutputLog[2]...).Has(WaitString) {
- t.Errorf("wrong CombinedOutput() log, got %s", fcmd.CombinedOutputLog[2])
- }
- }
- func TestWaitFlagOld(t *testing.T) {
- fcmd := fakeexec.FakeCmd{
- CombinedOutputScript: []fakeexec.FakeCombinedOutputAction{
- // iptables version check
- func() ([]byte, error) { return []byte("iptables v1.4.20"), nil },
- // iptables-restore version check
- func() ([]byte, error) { return []byte("iptables-restore v1.9.22"), nil },
- // Success.
- func() ([]byte, error) { return []byte{}, nil },
- },
- }
- fexec := fakeexec.FakeExec{
- CommandScript: []fakeexec.FakeCommandAction{
- func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
- func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
- func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
- },
- }
- runner := New(&fexec, dbus.NewFake(nil, nil), ProtocolIpv4)
- defer runner.Destroy()
- err := runner.DeleteChain(TableNAT, Chain("FOOBAR"))
- if err != nil {
- t.Errorf("expected success, got %v", err)
- }
- if fcmd.CombinedOutputCalls != 3 {
- t.Errorf("expected 3 CombinedOutput() calls, got %d", fcmd.CombinedOutputCalls)
- }
- if !sets.NewString(fcmd.CombinedOutputLog[2]...).HasAll("iptables", WaitString) {
- t.Errorf("wrong CombinedOutput() log, got %s", fcmd.CombinedOutputLog[2])
- }
- if sets.NewString(fcmd.CombinedOutputLog[2]...).Has(WaitSecondsValue) {
- t.Errorf("wrong CombinedOutput() log, got %s", fcmd.CombinedOutputLog[2])
- }
- }
- func TestWaitFlagNew(t *testing.T) {
- fcmd := fakeexec.FakeCmd{
- CombinedOutputScript: []fakeexec.FakeCombinedOutputAction{
- // iptables version check
- func() ([]byte, error) { return []byte("iptables v1.4.22"), nil },
- // iptables-restore version check
- func() ([]byte, error) { return []byte("iptables-restore v1.9.22"), nil },
- // Success.
- func() ([]byte, error) { return []byte{}, nil },
- },
- }
- fexec := fakeexec.FakeExec{
- CommandScript: []fakeexec.FakeCommandAction{
- func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
- func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
- func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
- },
- }
- runner := New(&fexec, dbus.NewFake(nil, nil), ProtocolIpv4)
- defer runner.Destroy()
- err := runner.DeleteChain(TableNAT, Chain("FOOBAR"))
- if err != nil {
- t.Errorf("expected success, got %v", err)
- }
- if fcmd.CombinedOutputCalls != 3 {
- t.Errorf("expected 3 CombinedOutput() calls, got %d", fcmd.CombinedOutputCalls)
- }
- if !sets.NewString(fcmd.CombinedOutputLog[2]...).HasAll("iptables", WaitString, WaitSecondsValue) {
- t.Errorf("wrong CombinedOutput() log, got %s", fcmd.CombinedOutputLog[2])
- }
- }
- func TestReload(t *testing.T) {
- dbusConn := dbus.NewFakeConnection()
- dbusConn.SetBusObject(func(method string, args ...interface{}) ([]interface{}, error) { return nil, nil })
- dbusConn.AddObject(firewalldName, firewalldPath, func(method string, args ...interface{}) ([]interface{}, error) { return nil, nil })
- fdbus := dbus.NewFake(dbusConn, nil)
- reloaded := make(chan bool, 2)
- fcmd := fakeexec.FakeCmd{
- CombinedOutputScript: []fakeexec.FakeCombinedOutputAction{
- // iptables version check
- func() ([]byte, error) { return []byte("iptables v1.4.22"), nil },
- // iptables-restore version check
- func() ([]byte, error) { return []byte("iptables-restore v1.9.22"), nil },
- // first reload
- // EnsureChain
- func() ([]byte, error) { return []byte{}, nil },
- // EnsureRule abc check
- func() ([]byte, error) { return []byte{}, &fakeexec.FakeExitError{Status: 1} },
- // EnsureRule abc
- func() ([]byte, error) { return []byte{}, nil },
- // second reload
- // EnsureChain
- func() ([]byte, error) { return []byte{}, nil },
- // EnsureRule abc check
- func() ([]byte, error) { return []byte{}, &fakeexec.FakeExitError{Status: 1} },
- // EnsureRule abc
- func() ([]byte, error) { return []byte{}, nil },
- },
- }
- fexec := fakeexec.FakeExec{
- CommandScript: []fakeexec.FakeCommandAction{
- func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
- func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
- func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
- func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
- func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
- func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
- func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
- func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
- },
- }
- runner := New(&fexec, fdbus, ProtocolIpv4)
- defer runner.Destroy()
- runner.AddReloadFunc(func() {
- exists, err := runner.EnsureChain(TableNAT, Chain("FOOBAR"))
- if err != nil {
- t.Errorf("expected success, got %v", err)
- }
- if exists {
- t.Errorf("expected exists = false")
- }
- reloaded <- true
- })
- runner.AddReloadFunc(func() {
- exists, err := runner.EnsureRule(Append, TableNAT, ChainOutput, "abc", "123")
- if err != nil {
- t.Errorf("expected success, got %v", err)
- }
- if exists {
- t.Errorf("expected exists = false")
- }
- reloaded <- true
- })
- dbusConn.EmitSignal("org.freedesktop.DBus", "/org/freedesktop/DBus", "org.freedesktop.DBus", "NameOwnerChanged", firewalldName, "", ":1.1")
- <-reloaded
- <-reloaded
- if fcmd.CombinedOutputCalls != 5 {
- t.Errorf("expected 5 CombinedOutput() calls total, got %d", fcmd.CombinedOutputCalls)
- }
- if !sets.NewString(fcmd.CombinedOutputLog[2]...).HasAll("iptables", "-t", "nat", "-N", "FOOBAR") {
- t.Errorf("wrong CombinedOutput() log, got %s", fcmd.CombinedOutputLog[2])
- }
- if !sets.NewString(fcmd.CombinedOutputLog[3]...).HasAll("iptables", "-t", "nat", "-C", "OUTPUT", "abc", "123") {
- t.Errorf("wrong CombinedOutput() log, got %s", fcmd.CombinedOutputLog[3])
- }
- if !sets.NewString(fcmd.CombinedOutputLog[4]...).HasAll("iptables", "-t", "nat", "-A", "OUTPUT", "abc", "123") {
- t.Errorf("wrong CombinedOutput() log, got %s", fcmd.CombinedOutputLog[4])
- }
- go func() { time.Sleep(time.Second / 100); reloaded <- true }()
- dbusConn.EmitSignal(firewalldName, firewalldPath, firewalldInterface, "DefaultZoneChanged", "public")
- dbusConn.EmitSignal("org.freedesktop.DBus", "/org/freedesktop/DBus", "org.freedesktop.DBus", "NameOwnerChanged", "io.k8s.Something", "", ":1.1")
- <-reloaded
- if fcmd.CombinedOutputCalls != 5 {
- t.Errorf("Incorrect signal caused a reload")
- }
- dbusConn.EmitSignal(firewalldName, firewalldPath, firewalldInterface, "Reloaded")
- <-reloaded
- <-reloaded
- if fcmd.CombinedOutputCalls != 8 {
- t.Errorf("expected 8 CombinedOutput() calls total, got %d", fcmd.CombinedOutputCalls)
- }
- if !sets.NewString(fcmd.CombinedOutputLog[5]...).HasAll("iptables", "-t", "nat", "-N", "FOOBAR") {
- t.Errorf("wrong CombinedOutput() log, got %s", fcmd.CombinedOutputLog[5])
- }
- if !sets.NewString(fcmd.CombinedOutputLog[6]...).HasAll("iptables", "-t", "nat", "-C", "OUTPUT", "abc", "123") {
- t.Errorf("wrong CombinedOutput() log, got %s", fcmd.CombinedOutputLog[6])
- }
- if !sets.NewString(fcmd.CombinedOutputLog[7]...).HasAll("iptables", "-t", "nat", "-A", "OUTPUT", "abc", "123") {
- t.Errorf("wrong CombinedOutput() log, got %s", fcmd.CombinedOutputLog[7])
- }
- }
- func testSaveInto(t *testing.T, protocol Protocol) {
- version := " v1.9.22"
- iptablesCmd := iptablesCommand(protocol)
- iptablesSaveCmd := iptablesSaveCommand(protocol)
- iptablesRestoreCmd := iptablesRestoreCommand(protocol)
- protoStr := protocolStr(protocol)
- output := fmt.Sprintf(`# Generated by %s 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`, iptablesSaveCmd+version)
- stderrOutput := "#STDERR OUTPUT" // SaveInto() should should NOT capture stderr into the buffer
- fcmd := fakeexec.FakeCmd{
- CombinedOutputScript: []fakeexec.FakeCombinedOutputAction{
- // iptables version check
- func() ([]byte, error) { return []byte(iptablesCmd + version), nil },
- // iptables-restore version check
- func() ([]byte, error) { return []byte(iptablesRestoreCmd + version), nil },
- },
- RunScript: []fakeexec.FakeRunAction{
- func() ([]byte, []byte, error) { return []byte(output), []byte(stderrOutput), nil },
- func() ([]byte, []byte, error) { return nil, []byte(stderrOutput), &fakeexec.FakeExitError{Status: 1} },
- },
- }
- fexec := fakeexec.FakeExec{
- CommandScript: []fakeexec.FakeCommandAction{
- func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
- func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
- func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
- func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
- },
- }
- runner := New(&fexec, dbus.NewFake(nil, nil), protocol)
- defer runner.Destroy()
- buffer := bytes.NewBuffer(nil)
- // Success.
- err := runner.SaveInto(TableNAT, buffer)
- if err != nil {
- t.Fatalf("%s: Expected success, got %v", protoStr, err)
- }
- if string(buffer.Bytes()) != output {
- t.Errorf("%s: Expected output '%s', got '%v'", protoStr, output, string(buffer.Bytes()))
- }
- if fcmd.CombinedOutputCalls != 2 {
- t.Errorf("%s: Expected 2 CombinedOutput() calls, got %d", protoStr, fcmd.CombinedOutputCalls)
- }
- if fcmd.RunCalls != 1 {
- t.Errorf("%s: Expected 1 Run() call, got %d", protoStr, fcmd.RunCalls)
- }
- if !sets.NewString(fcmd.RunLog[0]...).HasAll(iptablesSaveCmd, "-t", "nat") {
- t.Errorf("%s: Expected cmd containing '%s -t nat', got '%s'", protoStr, iptablesSaveCmd, fcmd.RunLog[0])
- }
- // Failure.
- buffer.Reset()
- err = runner.SaveInto(TableNAT, buffer)
- if err == nil {
- t.Errorf("%s: Expected failure", protoStr)
- }
- if string(buffer.Bytes()) != stderrOutput {
- t.Errorf("%s: Expected output '%s', got '%v'", protoStr, stderrOutput, string(buffer.Bytes()))
- }
- }
- func TestSaveIntoIPv4(t *testing.T) {
- testSaveInto(t, ProtocolIpv4)
- }
- func TestSaveIntoIPv6(t *testing.T) {
- testSaveInto(t, ProtocolIpv6)
- }
- func testRestore(t *testing.T, protocol Protocol) {
- version := " v1.9.22"
- iptablesCmd := iptablesCommand(protocol)
- iptablesRestoreCmd := iptablesRestoreCommand(protocol)
- protoStr := protocolStr(protocol)
- fcmd := fakeexec.FakeCmd{
- CombinedOutputScript: []fakeexec.FakeCombinedOutputAction{
- // iptables version check
- func() ([]byte, error) { return []byte(iptablesCmd + version), nil },
- // iptables-restore version check
- func() ([]byte, error) { return []byte(iptablesRestoreCmd + version), 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, &fakeexec.FakeExitError{Status: 1} },
- },
- }
- fexec := fakeexec.FakeExec{
- CommandScript: []fakeexec.FakeCommandAction{
- func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
- func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
- func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
- func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
- func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
- func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
- func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
- },
- }
- runner := New(&fexec, dbus.NewFake(nil, nil), protocol)
- defer runner.Destroy()
- // both flags true
- err := runner.Restore(TableNAT, []byte{}, FlushTables, RestoreCounters)
- if err != nil {
- t.Errorf("%s flush,restore: Expected success, got %v", protoStr, err)
- }
- commandSet := sets.NewString(fcmd.CombinedOutputLog[2]...)
- if !commandSet.HasAll(iptablesRestoreCmd, "-T", string(TableNAT), "--counters") || commandSet.HasAny("--noflush") {
- t.Errorf("%s flush, restore: Expected cmd containing '%s -T %s --counters', got '%s'", protoStr, iptablesRestoreCmd, string(TableNAT), fcmd.CombinedOutputLog[2])
- }
- // FlushTables, NoRestoreCounters
- err = runner.Restore(TableNAT, []byte{}, FlushTables, NoRestoreCounters)
- if err != nil {
- t.Errorf("%s flush, no restore: Expected success, got %v", protoStr, err)
- }
- commandSet = sets.NewString(fcmd.CombinedOutputLog[3]...)
- if !commandSet.HasAll(iptablesRestoreCmd, "-T", string(TableNAT)) || commandSet.HasAny("--noflush", "--counters") {
- t.Errorf("%s flush, no restore: Expected cmd containing '--noflush' or '--counters', got '%s'", protoStr, fcmd.CombinedOutputLog[3])
- }
- // NoFlushTables, RestoreCounters
- err = runner.Restore(TableNAT, []byte{}, NoFlushTables, RestoreCounters)
- if err != nil {
- t.Errorf("%s no flush, restore: Expected success, got %v", protoStr, err)
- }
- commandSet = sets.NewString(fcmd.CombinedOutputLog[4]...)
- if !commandSet.HasAll(iptablesRestoreCmd, "-T", string(TableNAT), "--noflush", "--counters") {
- t.Errorf("%s no flush, restore: Expected cmd containing '--noflush' and '--counters', got '%s'", protoStr, fcmd.CombinedOutputLog[4])
- }
- // NoFlushTables, NoRestoreCounters
- err = runner.Restore(TableNAT, []byte{}, NoFlushTables, NoRestoreCounters)
- if err != nil {
- t.Errorf("%s no flush, no restore: Expected success, got %v", protoStr, err)
- }
- commandSet = sets.NewString(fcmd.CombinedOutputLog[5]...)
- if !commandSet.HasAll(iptablesRestoreCmd, "-T", string(TableNAT), "--noflush") || commandSet.HasAny("--counters") {
- t.Errorf("%s no flush, no restore: Expected cmd containing '%s -T %s --noflush', got '%s'", protoStr, iptablesRestoreCmd, string(TableNAT), fcmd.CombinedOutputLog[5])
- }
- if fcmd.CombinedOutputCalls != 6 {
- t.Errorf("%s: Expected 6 total CombinedOutput() calls, got %d", protoStr, fcmd.CombinedOutputCalls)
- }
- // Failure.
- err = runner.Restore(TableNAT, []byte{}, FlushTables, RestoreCounters)
- if err == nil {
- t.Errorf("%s Expected a failure", protoStr)
- }
- }
- func TestRestoreIPv4(t *testing.T) {
- testRestore(t, ProtocolIpv4)
- }
- func TestRestoreIPv6(t *testing.T) {
- testRestore(t, ProtocolIpv6)
- }
- // TestRestoreAll tests only the simplest use case, as flag handling code is already tested in TestRestore
- func TestRestoreAll(t *testing.T) {
- fcmd := fakeexec.FakeCmd{
- CombinedOutputScript: []fakeexec.FakeCombinedOutputAction{
- // iptables version check
- func() ([]byte, error) { return []byte("iptables v1.9.22"), nil },
- // iptables-restore version check
- func() ([]byte, error) { return []byte("iptables-restore v1.9.22"), nil },
- func() ([]byte, error) { return []byte{}, nil },
- func() ([]byte, error) { return nil, &fakeexec.FakeExitError{Status: 1} },
- },
- }
- fexec := fakeexec.FakeExec{
- CommandScript: []fakeexec.FakeCommandAction{
- func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
- func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
- func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
- func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
- },
- }
- runner := newInternal(&fexec, dbus.NewFake(nil, nil), ProtocolIpv4, TestLockfilePath)
- defer os.Remove(TestLockfilePath)
- defer runner.Destroy()
- err := runner.RestoreAll([]byte{}, NoFlushTables, RestoreCounters)
- if err != nil {
- t.Fatalf("expected success, got %v", err)
- }
- commandSet := sets.NewString(fcmd.CombinedOutputLog[2]...)
- if !commandSet.HasAll("iptables-restore", "--counters", "--noflush") {
- t.Errorf("wrong CombinedOutput() log, got %s", fcmd.CombinedOutputLog[2])
- }
- if fcmd.CombinedOutputCalls != 3 {
- t.Errorf("expected 3 CombinedOutput() calls, got %d", fcmd.CombinedOutputCalls)
- }
- // Failure.
- err = runner.Restore(TableNAT, []byte{}, FlushTables, RestoreCounters)
- if err == nil {
- t.Errorf("expected failure")
- }
- }
- // TestRestoreAllWait tests that the "wait" flag is passed to a compatible iptables-restore
- func TestRestoreAllWait(t *testing.T) {
- fcmd := fakeexec.FakeCmd{
- CombinedOutputScript: []fakeexec.FakeCombinedOutputAction{
- // iptables version check
- func() ([]byte, error) { return []byte("iptables v1.9.22"), nil },
- // iptables-restore version check
- func() ([]byte, error) { return []byte("iptables-restore v1.9.22"), nil },
- func() ([]byte, error) { return []byte{}, nil },
- func() ([]byte, error) { return nil, &fakeexec.FakeExitError{Status: 1} },
- },
- }
- fexec := fakeexec.FakeExec{
- CommandScript: []fakeexec.FakeCommandAction{
- func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
- func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
- func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
- func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
- },
- }
- runner := newInternal(&fexec, dbus.NewFake(nil, nil), ProtocolIpv4, TestLockfilePath)
- defer os.Remove(TestLockfilePath)
- defer runner.Destroy()
- err := runner.RestoreAll([]byte{}, NoFlushTables, RestoreCounters)
- if err != nil {
- t.Fatalf("expected success, got %v", err)
- }
- commandSet := sets.NewString(fcmd.CombinedOutputLog[2]...)
- if !commandSet.HasAll("iptables-restore", WaitString, WaitSecondsValue, "--counters", "--noflush") {
- t.Errorf("wrong CombinedOutput() log, got %s", fcmd.CombinedOutputLog[2])
- }
- if fcmd.CombinedOutputCalls != 3 {
- t.Errorf("expected 3 CombinedOutput() calls, got %d", fcmd.CombinedOutputCalls)
- }
- // Failure.
- err = runner.Restore(TableNAT, []byte{}, FlushTables, RestoreCounters)
- if err == nil {
- t.Errorf("expected failure")
- }
- }
- // TestRestoreAllWaitOldIptablesRestore tests that the "wait" flag is not passed
- // to a in-compatible iptables-restore
- func TestRestoreAllWaitOldIptablesRestore(t *testing.T) {
- fcmd := fakeexec.FakeCmd{
- CombinedOutputScript: []fakeexec.FakeCombinedOutputAction{
- // iptables version check
- func() ([]byte, error) { return []byte("iptables v1.9.22"), nil },
- // iptables-restore version check
- func() ([]byte, error) { return []byte("unrecognized option: --version"), nil },
- func() ([]byte, error) { return []byte{}, nil },
- func() ([]byte, error) { return nil, &fakeexec.FakeExitError{Status: 1} },
- },
- }
- fexec := fakeexec.FakeExec{
- CommandScript: []fakeexec.FakeCommandAction{
- func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
- func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
- func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
- func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
- },
- }
- runner := newInternal(&fexec, dbus.NewFake(nil, nil), ProtocolIpv4, TestLockfilePath)
- defer os.Remove(TestLockfilePath)
- defer runner.Destroy()
- err := runner.RestoreAll([]byte{}, NoFlushTables, RestoreCounters)
- if err != nil {
- t.Fatalf("expected success, got %v", err)
- }
- commandSet := sets.NewString(fcmd.CombinedOutputLog[2]...)
- if !commandSet.HasAll("iptables-restore", "--counters", "--noflush") {
- t.Errorf("wrong CombinedOutput() log, got %s", fcmd.CombinedOutputLog[2])
- }
- if commandSet.HasAll(WaitString, WaitSecondsValue) {
- t.Errorf("wrong CombinedOutput() log (unexpected %s option), got %s", WaitString, fcmd.CombinedOutputLog[2])
- }
- if fcmd.CombinedOutputCalls != 3 {
- t.Errorf("expected 3 CombinedOutput() calls, got %d", fcmd.CombinedOutputCalls)
- }
- // Failure.
- err = runner.Restore(TableNAT, []byte{}, FlushTables, RestoreCounters)
- if err == nil {
- t.Errorf("expected failure")
- }
- }
- // TestRestoreAllGrabNewLock tests that the iptables code will grab the
- // iptables /run lock when using an iptables-restore version that does not
- // support the --wait argument
- func TestRestoreAllGrabNewLock(t *testing.T) {
- fcmd := fakeexec.FakeCmd{
- CombinedOutputScript: []fakeexec.FakeCombinedOutputAction{
- // iptables version check
- func() ([]byte, error) { return []byte("iptables v1.9.22"), nil },
- // iptables-restore version check
- func() ([]byte, error) { return []byte("unrecognized option: --version"), nil },
- },
- }
- fexec := fakeexec.FakeExec{
- CommandScript: []fakeexec.FakeCommandAction{
- func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
- func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
- },
- }
- runner := newInternal(&fexec, dbus.NewFake(nil, nil), ProtocolIpv4, TestLockfilePath)
- defer os.Remove(TestLockfilePath)
- defer runner.Destroy()
- // Grab the /run lock and ensure the RestoreAll fails
- runLock, err := os.OpenFile(TestLockfilePath, os.O_CREATE, 0600)
- if err != nil {
- t.Fatalf("expected to open %s, got %v", TestLockfilePath, err)
- }
- defer runLock.Close()
- if err := grabIptablesFileLock(runLock); err != nil {
- t.Errorf("expected to lock %s, got %v", TestLockfilePath, err)
- }
- err = runner.RestoreAll([]byte{}, NoFlushTables, RestoreCounters)
- if err == nil {
- t.Errorf("expected failure, got success instead")
- }
- if !strings.Contains(err.Error(), "failed to acquire new iptables lock: timed out waiting for the condition") {
- t.Errorf("expected timeout error, got %v", err)
- }
- }
- // TestRestoreAllGrabOldLock tests that the iptables code will grab the
- // iptables @xtables abstract unix socket lock when using an iptables-restore
- // version that does not support the --wait argument
- func TestRestoreAllGrabOldLock(t *testing.T) {
- fcmd := fakeexec.FakeCmd{
- CombinedOutputScript: []fakeexec.FakeCombinedOutputAction{
- // iptables version check
- func() ([]byte, error) { return []byte("iptables v1.9.22"), nil },
- // iptables-restore version check
- func() ([]byte, error) { return []byte("unrecognized option: --version"), nil },
- },
- }
- fexec := fakeexec.FakeExec{
- CommandScript: []fakeexec.FakeCommandAction{
- func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
- func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
- },
- }
- runner := newInternal(&fexec, dbus.NewFake(nil, nil), ProtocolIpv4, TestLockfilePath)
- defer os.Remove(TestLockfilePath)
- defer runner.Destroy()
- // Grab the abstract @xtables socket
- runLock, err := net.ListenUnix("unix", &net.UnixAddr{Name: "@xtables", Net: "unix"})
- if err != nil {
- t.Fatalf("expected to lock @xtables, got %v", err)
- }
- defer runLock.Close()
- err = runner.RestoreAll([]byte{}, NoFlushTables, RestoreCounters)
- if err == nil {
- t.Errorf("expected failure, got success instead")
- }
- if !strings.Contains(err.Error(), "failed to acquire old iptables lock: timed out waiting for the condition") {
- t.Errorf("expected timeout error, got %v", err)
- }
- }
|