Skip to content

Commit

Permalink
Merge pull request #132 from telekom-mms/feature/add-command-templates
Browse files Browse the repository at this point in the history
Add command templates
  • Loading branch information
hwipl authored Jan 9, 2025
2 parents 6a60133 + 3cf38fa commit 9e6a59a
Show file tree
Hide file tree
Showing 14 changed files with 1,238 additions and 1,110 deletions.
654 changes: 654 additions & 0 deletions internal/cmdtmpl/command.go

Large diffs are not rendered by default.

146 changes: 146 additions & 0 deletions internal/cmdtmpl/command_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
package cmdtmpl

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

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

// TestExecuteTemplateErrors tests executeTemplate of CommandList, parse error.
func TestExecuteTemplateParseError(t *testing.T) {
cl := &CommandList{
template: template.Must(template.New("test").Parse("test")),
}
if _, err := cl.executeTemplate("{{ invalid }}", nil); err == nil {
t.Error("invalid template should not parse correctly")
}
}

// TestGetCommandList tests getCommandList.
func TestGetCommandList(t *testing.T) {
// not existing
for _, name := range []string{
"SplitRoutingDoesNotExist",
"TrafPolDoesNotExist",
"VPNSetupDoesNotExist",
"DoesNotExist",
} {
cl := getCommandList(name)
if cl != nil {
t.Errorf("command list %s should not exists, got %s", name, cl.Name)
}
}

// existing
for _, name := range []string{
// Split Routing
"SplitRoutingSetupRouting",
"SplitRoutingTeardownRouting",
"SplitRoutingSetExcludes",
"SplitRoutingCleanup",

// Traffic Policing
"TrafPolSetFilterRules",
"TrafPolUnsetFilterRules",
"TrafPolAddAllowedDevice",
"TrafPolRemoveAllowedDevice",
"TrafPolFlushAllowedHosts",
"TrafPolAddAllowedHost",
"TrafPolAddPortalPorts",
"TrafPolRemovePortalPorts",
"TrafPolCleanup",

// VPN Setup
"VPNSetupSetupVPNDevice",
"VPNSetupTeardownVPNDevice",
"VPNSetupSetupDNSServer",
"VPNSetupSetupDNSDomains",
"VPNSetupSetupDNSDefaultRoute",
"VPNSetupSetupDNS",
"VPNSetupTeardownDNS",
"VPNSetupEnsureDNS",
"VPNSetupCleanup",
} {
cl := getCommandList(name)
if cl.Name != name {
t.Errorf("command list should be %s, got %s", name, cl.Name)
}
}
}

// TestCmdRun tests Run of Cmd.
func TestCmdRun(t *testing.T) {
cmd := &Cmd{
Cmd: "echo",
Args: []string{"this", "is", "a", "test"},
}
stdout, _, err := cmd.Run(context.Background())
if err != nil {
t.Errorf("unexpected error %s", err)
}
if string(stdout) != "this is a test\n" {
t.Errorf("unexpected stdout: %s", stdout)
}
}

// TestGetCmds tets GetCmds.
func TestGetCmds(t *testing.T) {
// not existing
if _, err := GetCmds("DoesNotExist", nil); err == nil {
t.Error("not existing command list should return error")
}

// existing, that only need daemon config as input data
for _, name := range []string{
// Split Routing
"SplitRoutingSetupRouting",
"SplitRoutingTeardownRouting",
// "SplitRoutingSetExcludes", // skip, requires excludes
"SplitRoutingCleanup",

// Traffic Policing
"TrafPolSetFilterRules",
"TrafPolUnsetFilterRules",
// TrafPolAddAllowedDevice", // skip, requires device
// "TrafPolRemoveAllowedDevice", // skip, requires device
"TrafPolFlushAllowedHosts",
// "TrafPolAddAllowedHost", // skip, requires host
"TrafPolAddPortalPorts",
"TrafPolRemovePortalPorts",
"TrafPolCleanup",

// VPN Setup
"VPNSetupSetupVPNDevice",
"VPNSetupTeardownVPNDevice",
"VPNSetupSetupDNSServer",
"VPNSetupSetupDNSDomains",
"VPNSetupSetupDNSDefaultRoute",
"VPNSetupSetupDNS",
"VPNSetupTeardownDNS",
"VPNSetupEnsureDNS",
"VPNSetupCleanup",
} {
if cmds, err := GetCmds(name, daemoncfg.NewConfig()); err != nil ||
len(cmds) == 0 {

t.Errorf("got invalid command list for name %s", name)
}
}

// existing, with insufficient input data
for _, name := range []string{
// Split Routing
"SplitRoutingSetExcludes",

// Traffic Policing
"TrafPolAddAllowedDevice",
"TrafPolRemoveAllowedDevice",
"TrafPolAddAllowedHost",
} {
if _, err := GetCmds(name, daemoncfg.NewConfig()); err == nil {
t.Errorf("insufficient data should return error for list %s", name)
}
}
}
2 changes: 1 addition & 1 deletion internal/daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,7 @@ func (d *Daemon) handleProfileUpdate() error {
func (d *Daemon) cleanup(ctx context.Context) {
ocrunner.CleanupConnect(d.config.OpenConnect)
vpnsetup.Cleanup(ctx, d.config)
trafpol.Cleanup(ctx)
trafpol.Cleanup(ctx, d.config)
}

// initToken creates the daemon token for client authentication.
Expand Down
56 changes: 0 additions & 56 deletions internal/execs/execs.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,62 +32,6 @@ var RunCmd = func(ctx context.Context, cmd string, s string, arg ...string) (std
return
}

// RunIP runs the "ip" command with args.
func RunIP(ctx context.Context, arg ...string) (stdout, stderr []byte, err error) {
return RunCmd(ctx, ip, "", arg...)
}

// RunIPLink runs the "ip link" command with args.
func RunIPLink(ctx context.Context, arg ...string) (stdout, stderr []byte, err error) {
a := append([]string{"link"}, arg...)
return RunIP(ctx, a...)
}

// RunIPAddress runs the "ip address" command with args.
func RunIPAddress(ctx context.Context, arg ...string) (stdout, stderr []byte, err error) {
a := append([]string{"address"}, arg...)
return RunIP(ctx, a...)
}

// RunIP4Route runs the "ip -4 route" command with args.
func RunIP4Route(ctx context.Context, arg ...string) (stdout, stderr []byte, err error) {
a := append([]string{"-4", "route"}, arg...)
return RunIP(ctx, a...)
}

// RunIP6Route runs the "ip -6 route" command with args.
func RunIP6Route(ctx context.Context, arg ...string) (stdout, stderr []byte, err error) {
a := append([]string{"-6", "route"}, arg...)
return RunIP(ctx, a...)
}

// RunIP4Rule runs the "ip -4 rule" command with args.
func RunIP4Rule(ctx context.Context, arg ...string) (stdout, stderr []byte, err error) {
a := append([]string{"-4", "rule"}, arg...)
return RunIP(ctx, a...)
}

// RunIP6Rule runs the "ip -6 rule" command with args.
func RunIP6Rule(ctx context.Context, arg ...string) (stdout, stderr []byte, err error) {
a := append([]string{"-6", "rule"}, arg...)
return RunIP(ctx, a...)
}

// RunSysctl runs the "sysctl" command with args.
func RunSysctl(ctx context.Context, arg ...string) (stdout, stderr []byte, err error) {
return RunCmd(ctx, sysctl, "", arg...)
}

// RunNft runs the "nft -f -" command and sets stdin to s.
func RunNft(ctx context.Context, s string) (stdout, stderr []byte, err error) {
return RunCmd(ctx, nft, s, "-f", "-")
}

// RunResolvectl runs the "resolvectl" command with args.
func RunResolvectl(ctx context.Context, arg ...string) (stdout, stderr []byte, err error) {
return RunCmd(ctx, resolvectl, "", arg...)
}

// SetExecutables configures all executables from config.
func SetExecutables(config *daemoncfg.Executables) {
ip = config.IP
Expand Down
Loading

0 comments on commit 9e6a59a

Please sign in to comment.