Remove exec arg from utilipset.New

Historically it took an exec argument so you could pass a FakeExec to
mock its behavior in unit tests, but it has a fake implementation now
that is much more useful for unit tests than trying to use the real
implementation with a fake exec. (The unit tests still use fake execs,
but they don't need to use a public constructor.) So remove the exec
args from the public constructors.
This commit is contained in:
Dan Winship 2025-01-11 07:44:39 -05:00
parent 36f5820ad1
commit b5e9a8262e
3 changed files with 18 additions and 16 deletions

View File

@ -217,8 +217,7 @@ func (s *ProxyServer) createProxier(ctx context.Context, config *proxyconfigapi.
return nil, fmt.Errorf("unable to create proxier: %v", err)
}
} else if config.Mode == proxyconfigapi.ProxyModeIPVS {
execer := exec.New()
ipsetInterface := utilipset.New(execer)
ipsetInterface := utilipset.New()
ipvsInterface := utilipvs.New()
if err := ipvs.CanUseIPVSProxier(ctx, ipvsInterface, ipsetInterface, config.IPVS.Scheduler); err != nil {
return nil, fmt.Errorf("can't use the IPVS proxier: %v", err)
@ -511,8 +510,7 @@ func platformCleanup(ctx context.Context, mode proxyconfigapi.ProxyMode, cleanup
// Clean up iptables and ipvs rules if switching to nftables, or if cleanupAndExit
if !isIPTablesBased(mode) || cleanupAndExit {
ipts, _ := getIPTables(v1.IPFamilyUnknown)
execer := exec.New()
ipsetInterface := utilipset.New(execer)
ipsetInterface := utilipset.New()
ipvsInterface := utilipvs.New()
for _, ipt := range ipts {

View File

@ -278,9 +278,9 @@ type runner struct {
}
// New returns a new Interface which will exec ipset.
func New(exec utilexec.Interface) Interface {
func New() Interface {
return &runner{
exec: exec,
exec: utilexec.New(),
}
}

View File

@ -28,6 +28,10 @@ import (
fakeexec "k8s.io/utils/exec/testing"
)
func newInternal(fexec *fakeexec.FakeExec) Interface {
return &runner{fexec}
}
func TestCheckIPSetVersion(t *testing.T) {
testCases := []struct {
vstring string
@ -83,7 +87,7 @@ func TestFlushSet(t *testing.T) {
func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
},
}
runner := New(fexec)
runner := newInternal(fexec)
// Success.
err := runner.FlushSet("FOOBAR")
if err != nil {
@ -119,7 +123,7 @@ func TestDestroySet(t *testing.T) {
func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
},
}
runner := New(fexec)
runner := newInternal(fexec)
// Success
err := runner.DestroySet("FOOBAR")
if err != nil {
@ -153,7 +157,7 @@ func TestDestroyAllSets(t *testing.T) {
func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
},
}
runner := New(fexec)
runner := newInternal(fexec)
// Success
err := runner.DestroyAllSets()
if err != nil {
@ -198,7 +202,7 @@ func TestCreateSet(t *testing.T) {
func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
},
}
runner := New(fexec)
runner := newInternal(fexec)
// Create with ignoreExistErr = false, expect success
err := runner.CreateSet(&testSet, false)
if err != nil {
@ -388,7 +392,7 @@ func TestAddEntry(t *testing.T) {
func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
},
}
runner := New(fexec)
runner := newInternal(fexec)
// Create with ignoreExistErr = false, expect success
err := runner.AddEntry(testCases[i].entry.String(), testCases[i].set, false)
if err != nil {
@ -437,7 +441,7 @@ func TestDelEntry(t *testing.T) {
func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
},
}
runner := New(fexec)
runner := newInternal(fexec)
err := runner.DelEntry(testCases[i].entry.String(), testCases[i].set.Name)
if err != nil {
@ -482,7 +486,7 @@ func TestTestEntry(t *testing.T) {
func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
},
}
runner := New(fexec)
runner := newInternal(fexec)
// Success
ok, err := runner.TestEntry(testEntry.String(), setName)
if err != nil {
@ -530,7 +534,7 @@ func TestTestEntryIPv6(t *testing.T) {
func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
},
}
runner := New(fexec)
runner := newInternal(fexec)
// Success
ok, err := runner.TestEntry(testEntry.String(), setName)
if err != nil {
@ -604,7 +608,7 @@ Members:
},
},
}
runner := New(fexec)
runner := newInternal(fexec)
// Success
entries, err := runner.ListEntries("foobar")
if err != nil {
@ -643,7 +647,7 @@ baz`
func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
},
}
runner := New(fexec)
runner := newInternal(fexec)
// Success
list, err := runner.ListSets()
if err != nil {