Skip to content

Commit

Permalink
Merge pull request #146 from telekom-mms/feature/remove-package-execs
Browse files Browse the repository at this point in the history
Remove package execs
  • Loading branch information
hwipl authored Feb 5, 2025
2 parents 0258b0b + 8c855c4 commit 99f52cd
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 133 deletions.
20 changes: 17 additions & 3 deletions internal/cmdtmpl/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ import (
"bytes"
"context"
"fmt"
"os/exec"
"strings"
"text/template"

"github.com/telekom-mms/oc-daemon/internal/execs"
)

// Command consists of a command line to be executed and an optional Stdin to
Expand Down Expand Up @@ -537,9 +536,24 @@ type Cmd struct {
Stdin string
}

// RunCmd runs the cmd with args and sets stdin to s, returns stdout and stderr.
var RunCmd = func(ctx context.Context, cmd string, s string, arg ...string) (stdout, stderr []byte, err error) {
c := exec.CommandContext(ctx, cmd, arg...)
if s != "" {
c.Stdin = bytes.NewBufferString(s)
}
var outbuf, errbuf bytes.Buffer
c.Stdout = &outbuf
c.Stderr = &errbuf
err = c.Run()
stdout = outbuf.Bytes()
stderr = errbuf.Bytes()
return
}

// Run runs the command.
func (c *Cmd) Run(ctx context.Context) (stdout, stderr []byte, err error) {
return execs.RunCmd(ctx, c.Cmd, c.Stdin, c.Args...)
return RunCmd(ctx, c.Cmd, c.Stdin, c.Args...)
}

// GetCmds returns a list of Cmds ready to run.
Expand Down
34 changes: 34 additions & 0 deletions internal/cmdtmpl/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmdtmpl

import (
"context"
"path/filepath"
"testing"
"text/template"

Expand Down Expand Up @@ -57,6 +58,39 @@ func TestGetCommandList(t *testing.T) {
}
}

// TestRunCmd tests RunCmd.
func TestRunCmd(t *testing.T) {
ctx := context.Background()

// test not existing
dir := t.TempDir()
if _, _, err := RunCmd(ctx, filepath.Join(dir, "does/not/exist"), ""); err == nil {
t.Errorf("running not existing command should fail: %v", err)
}

// test existing
if _, _, err := RunCmd(ctx, "echo", "", "this", "is", "a", "test"); err != nil {
t.Errorf("running echo failed: %v", err)
}

// test with stdin
if _, _, err := RunCmd(ctx, "echo", "this is a test"); err != nil {
t.Errorf("running echo failed: %v", err)
}

// test stdout
stdout, stderr, err := RunCmd(ctx, "cat", "this is a test")
if err != nil || string(stdout) != "this is a test" {
t.Errorf("running echo failed: %s, %s, %v", stdout, stderr, err)
}

// test stderr and error
stdout, stderr, err = RunCmd(ctx, "cat", "", "does/not/exist")
if err == nil || string(stderr) != "cat: does/not/exist: No such file or directory\n" {
t.Errorf("running echo failed: %s, %s, %v", stdout, stderr, err)
}
}

// TestCmdRun tests Run of Cmd.
func TestCmdRun(t *testing.T) {
cmd := &Cmd{
Expand Down
4 changes: 0 additions & 4 deletions internal/daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"github.com/telekom-mms/oc-daemon/internal/daemoncfg"
"github.com/telekom-mms/oc-daemon/internal/dbusapi"
"github.com/telekom-mms/oc-daemon/internal/dnsproxy"
"github.com/telekom-mms/oc-daemon/internal/execs"
"github.com/telekom-mms/oc-daemon/internal/ocrunner"
"github.com/telekom-mms/oc-daemon/internal/profilemon"
"github.com/telekom-mms/oc-daemon/internal/sleepmon"
Expand Down Expand Up @@ -941,9 +940,6 @@ func (d *Daemon) Start() error {
// create context
ctx := context.Background()

// set executables
execs.SetExecutables(d.config.Executables)

// cleanup after a failed shutdown
d.cleanup(ctx)

Expand Down
41 changes: 0 additions & 41 deletions internal/execs/execs.go

This file was deleted.

63 changes: 0 additions & 63 deletions internal/execs/execs_test.go

This file was deleted.

8 changes: 4 additions & 4 deletions internal/trafpol/filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ import (
"net/netip"
"testing"

"github.com/telekom-mms/oc-daemon/internal/cmdtmpl"
"github.com/telekom-mms/oc-daemon/internal/daemoncfg"
"github.com/telekom-mms/oc-daemon/internal/execs"
)

// TestFilterFunctionsErrors tests filter functions, errors.
func TestFilterFunctionsErrors(_ *testing.T) {
oldRunCmd := execs.RunCmd
execs.RunCmd = func(context.Context, string, string, ...string) ([]byte, []byte, error) {
oldRunCmd := cmdtmpl.RunCmd
cmdtmpl.RunCmd = func(context.Context, string, string, ...string) ([]byte, []byte, error) {
return nil, nil, errors.New("test error")
}
defer func() { execs.RunCmd = oldRunCmd }()
defer func() { cmdtmpl.RunCmd = oldRunCmd }()

ctx := context.Background()

Expand Down
16 changes: 11 additions & 5 deletions internal/trafpol/trafpol_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import (
"sync"
"testing"

"github.com/telekom-mms/oc-daemon/internal/cmdtmpl"
"github.com/telekom-mms/oc-daemon/internal/cpd"
"github.com/telekom-mms/oc-daemon/internal/daemoncfg"
"github.com/telekom-mms/oc-daemon/internal/devmon"
"github.com/telekom-mms/oc-daemon/internal/execs"
"github.com/vishvananda/netlink"
)

Expand Down Expand Up @@ -56,14 +56,16 @@ func TestTrafPolHandleCPDReport(t *testing.T) {

var nftMutex sync.Mutex
nftCmds := []string{}
oldRunCmd := execs.RunCmd
execs.RunCmd = func(_ context.Context, cmd string, stdin string, args ...string) ([]byte, []byte, error) {
oldRunCmd := cmdtmpl.RunCmd
cmdtmpl.RunCmd = func(_ context.Context, cmd string, stdin string,
args ...string) ([]byte, []byte, error) {

nftMutex.Lock()
defer nftMutex.Unlock()
nftCmds = append(nftCmds, cmd+" "+strings.Join(args, " ")+" "+stdin)
return nil, nil, nil
}
defer func() { execs.RunCmd = oldRunCmd }()
defer func() { cmdtmpl.RunCmd = oldRunCmd }()

getNftCmds := func() []string {
nftMutex.Lock()
Expand Down Expand Up @@ -303,10 +305,14 @@ func TestCleanup(t *testing.T) {
"nft -f - delete table inet oc-daemon-filter",
}
got := []string{}
execs.RunCmd = func(_ context.Context, cmd string, _ string, args ...string) ([]byte, []byte, error) {

oldRunCmd := cmdtmpl.RunCmd
cmdtmpl.RunCmd = func(_ context.Context, cmd string, _ string, args ...string) ([]byte, []byte, error) {
got = append(got, cmd+" "+strings.Join(args, " "))
return nil, nil, nil
}
defer func() { cmdtmpl.RunCmd = oldRunCmd }()

Cleanup(context.Background(), daemoncfg.NewConfig())
if !reflect.DeepEqual(got, want) {
t.Errorf("got %v, want %v", got, want)
Expand Down
26 changes: 13 additions & 13 deletions internal/vpnsetup/vpnsetup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import (
"time"

"github.com/telekom-mms/oc-daemon/internal/addrmon"
"github.com/telekom-mms/oc-daemon/internal/cmdtmpl"
"github.com/telekom-mms/oc-daemon/internal/daemoncfg"
"github.com/telekom-mms/oc-daemon/internal/devmon"
"github.com/telekom-mms/oc-daemon/internal/dnsproxy"
"github.com/telekom-mms/oc-daemon/internal/execs"
"github.com/vishvananda/netlink"
)

Expand Down Expand Up @@ -95,8 +95,8 @@ func TestVPNSetupCheckDNSDomain(t *testing.T) {
// TestVPNSetupEnsureDNS tests ensureDNS of VPNSetup.
func TestVPNSetupEnsureDNS(t *testing.T) {
// clean up after tests
oldRunCmd := execs.RunCmd
defer func() { execs.RunCmd = oldRunCmd }()
oldRunCmd := cmdtmpl.RunCmd
defer func() { cmdtmpl.RunCmd = oldRunCmd }()

// test settings
conf := daemoncfg.NewConfig()
Expand All @@ -107,7 +107,7 @@ func TestVPNSetupEnsureDNS(t *testing.T) {
ctx := context.Background()

// test resolvectl error
execs.RunCmd = func(context.Context, string, string, ...string) ([]byte, []byte, error) {
cmdtmpl.RunCmd = func(context.Context, string, string, ...string) ([]byte, []byte, error) {
return nil, nil, errors.New("test error")
}

Expand All @@ -124,7 +124,7 @@ func TestVPNSetupEnsureDNS(t *testing.T) {
[]byte("header\nProtocols: +DefaultRoute\nDNS Servers: other\nDNS Domain: test.example.com ~.\n"),
[]byte("header\nProtocols: +DefaultRoute\nDNS Servers: 127.0.0.1:4253\nDNS Domain: other\n"),
} {
execs.RunCmd = func(context.Context, string, string, ...string) ([]byte, []byte, error) {
cmdtmpl.RunCmd = func(context.Context, string, string, ...string) ([]byte, []byte, error) {
return invalid, nil, nil
}

Expand All @@ -139,7 +139,7 @@ func TestVPNSetupEnsureDNS(t *testing.T) {
[]byte("header\n Protocols: +DefaultRoute \nother\n " +
"DNS Servers: 127.0.0.1:4253 \n DNS Domain: test.example.com ~.\nother\n"),
} {
execs.RunCmd = func(context.Context, string, string, ...string) ([]byte, []byte, error) {
cmdtmpl.RunCmd = func(context.Context, string, string, ...string) ([]byte, []byte, error) {
return valid, nil, nil
}

Expand All @@ -159,11 +159,11 @@ func TestVPNSetupStartStop(_ *testing.T) {
// TestVPNSetupSetupTeardown tests Setup and Teardown of VPNSetup.
func TestVPNSetupSetupTeardown(_ *testing.T) {
// override functions
oldCmd := execs.RunCmd
execs.RunCmd = func(context.Context, string, string, ...string) ([]byte, []byte, error) {
oldCmd := cmdtmpl.RunCmd
cmdtmpl.RunCmd = func(context.Context, string, string, ...string) ([]byte, []byte, error) {
return nil, nil, nil
}
defer func() { execs.RunCmd = oldCmd }()
defer func() { cmdtmpl.RunCmd = oldCmd }()

oldRegisterAddrUpdates := addrmon.RegisterAddrUpdates
addrmon.RegisterAddrUpdates = func(*addrmon.AddrMon) (chan netlink.AddrUpdate, error) {
Expand Down Expand Up @@ -205,11 +205,11 @@ func TestVPNSetupSetupTeardown(_ *testing.T) {
// TestVPNSetupGetState tests GetState of VPNSetup.
func TestVPNSetupGetState(t *testing.T) {
// override functions
oldCmd := execs.RunCmd
execs.RunCmd = func(context.Context, string, string, ...string) ([]byte, []byte, error) {
oldCmd := cmdtmpl.RunCmd
cmdtmpl.RunCmd = func(context.Context, string, string, ...string) ([]byte, []byte, error) {
return nil, nil, nil
}
defer func() { execs.RunCmd = oldCmd }()
defer func() { cmdtmpl.RunCmd = oldCmd }()

oldRegisterAddrUpdates := addrmon.RegisterAddrUpdates
addrmon.RegisterAddrUpdates = func(*addrmon.AddrMon) (chan netlink.AddrUpdate, error) {
Expand Down Expand Up @@ -268,7 +268,7 @@ func TestNewVPNSetup(t *testing.T) {
// TestCleanup tests Cleanup.
func TestCleanup(t *testing.T) {
got := []string{}
execs.RunCmd = func(_ context.Context, cmd string, s string, arg ...string) ([]byte, []byte, error) {
cmdtmpl.RunCmd = func(_ context.Context, cmd string, s string, arg ...string) ([]byte, []byte, error) {
if s == "" {
got = append(got, cmd+" "+strings.Join(arg, " "))
return nil, nil, nil
Expand Down

0 comments on commit 99f52cd

Please sign in to comment.