Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implementation of the Starlark run_local() function
This patch implements the Go code for starlark builtin function run_local(). This function allows Crashd script to run arbitrary command from on the local machine. This patch does the followings: - Adds Go function to support starlark builtin func for run_local - Adds and updates tests for run_local Signed-off-by: Vladimir Vivien <vivienv@vmware.com>
- Loading branch information
1 parent
7ea48e8
commit a46c358
Showing
4 changed files
with
119 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Copyright (c) 2020 VMware, Inc. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package starlark | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/vladimirvivien/echo" | ||
"go.starlark.net/starlark" | ||
) | ||
|
||
// runLocalFunc is a built-in starlark function that runs a provided command on the local machine. | ||
// It returns the result of the command as struct containing information about the executed command. | ||
// Starlark format: run_local(<command string>) | ||
func runLocalFunc(thread *starlark.Thread, b *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) { | ||
var cmdStr string | ||
if args != nil && args.Len() == 1 { | ||
cmd, ok := args.Index(0).(starlark.String) | ||
if !ok { | ||
return starlark.None, fmt.Errorf("%s: command must be a string", identifiers.runLocal) | ||
} | ||
cmdStr = string(cmd) | ||
} | ||
|
||
p := echo.New().RunProc(cmdStr) | ||
if p.Err() != nil { | ||
return starlark.None, fmt.Errorf("%s: %s", identifiers.runLocal, p.Err()) | ||
} | ||
|
||
return starlark.String(p.Result()), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// Copyright (c) 2020 VMware, Inc. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package starlark | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
|
||
"go.starlark.net/starlark" | ||
) | ||
|
||
func TestRunLocalFunc(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
args func(t *testing.T) starlark.Tuple | ||
eval func(t *testing.T, args starlark.Tuple) | ||
}{ | ||
{ | ||
name: "simple command", | ||
args: func(t *testing.T) starlark.Tuple { return starlark.Tuple{starlark.String("echo 'Hello World!'")} }, | ||
eval: func(t *testing.T, args starlark.Tuple) { | ||
val, err := runLocalFunc(newTestThreadLocal(t), nil, args, nil) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
result := "" | ||
if r, ok := val.(starlark.String); ok { | ||
result = string(r) | ||
} | ||
if result != "Hello World!" { | ||
t.Errorf("unexpected result: %s", result) | ||
} | ||
}, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
test.eval(t, test.args(t)) | ||
}) | ||
} | ||
} | ||
|
||
func TestRunLocalScript(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
script string | ||
eval func(t *testing.T, script string) | ||
}{ | ||
{ | ||
name: "run local", | ||
script: ` | ||
result = run_local("""echo 'Hello World!'""") | ||
`, | ||
eval: func(t *testing.T, script string) { | ||
exe := New() | ||
if err := exe.Exec("test.star", strings.NewReader(script)); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
resultVal := exe.result["result"] | ||
if resultVal == nil { | ||
t.Fatal("run_local() should be assigned to a variable for test") | ||
} | ||
result, ok := resultVal.(starlark.String) | ||
if !ok { | ||
t.Fatal("run_local() should return a string") | ||
} | ||
|
||
if string(result) != "Hello World!" { | ||
t.Fatalf("uneexpected result %s", result) | ||
} | ||
|
||
}, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
test.eval(t, test.script) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters