提交 e7ee20ed 编写于 作者: E Evan Lezar

Refactor getArgs and fix edge cases

This change refactors the getArgs function that extracts the bundle path
and runc subcommand ("create") from the command line arguments.

The function is changed to accept the command line arguments as a slice
to allow for testing and addresses edge cases with regards to the number
of dashes and an optional equals sign for arguments that are supported by
runc.
Signed-off-by: NEvan Lezar <elezar@nvidia.com>
上级 9a53484c
......@@ -61,18 +61,47 @@ func getConfig() (*config, error) {
return cfg, nil
}
func getArgs() (*args, error) {
// getArgs checks the specified slice of strings (argv) for a 'bundle' flag and a 'create'
// command line argument as allowed by runc.
// The following are supported:
// --bundle{{SEP}}BUNDLE_PATH
// -bundle{{SEP}}BUNDLE_PATH
// -b{{SEP}}BUNDLE_PATH
// where {{SEP}} is either ' ' or '='
func getArgs(argv []string) (*args, error) {
args := &args{}
for i, param := range os.Args {
if param == "--bundle" || param == "-b" {
if len(os.Args)-i <= 1 {
return nil, fmt.Errorf("bundle option needs an argument")
}
args.bundleDirPath = os.Args[i+1]
} else if param == "create" {
for i := 0; i < len(argv); i++ {
param := argv[i]
if param == "create" {
args.cmd = param
continue
}
if !strings.HasPrefix(param, "-") {
continue
}
trimmed := strings.TrimLeft(param, "-")
if len(trimmed) == 0 {
continue
}
parts := strings.SplitN(trimmed, "=", 2)
if parts[0] != "bundle" && parts[0] != "b" {
continue
}
if len(parts) == 2 {
args.bundleDirPath = parts[1]
continue
}
if len(argv)-i <= 1 {
return nil, fmt.Errorf("bundle option needs an argument")
}
args.bundleDirPath = argv[i+1]
i++
}
return args, nil
......@@ -150,7 +179,7 @@ func main() {
logger.Printf("Running %s\n", os.Args[0])
args, err := getArgs()
args, err := getArgs(os.Args)
exitOnError(err, "fail to get args")
if args.cmd != "create" {
......
......@@ -225,7 +225,7 @@ func TestGetConfigWithCustomConfig(t *testing.T) {
require.Equal(t, cfg.debugFilePath, "/nvidia-container-toolkit.log")
}
func TestArgs(t *testing.T) {
func TestArgsGetConfigFilePath(t *testing.T) {
wd, err := os.Getwd()
require.NoError(t, err)
......@@ -253,5 +253,95 @@ func TestArgs(t *testing.T) {
require.NoErrorf(t, err, "%d: %v", i, tc)
require.Equalf(t, tc.configPath, cp, "%d: %v", i, tc)
}
}
func TestGetArgs(t *testing.T) {
testCases := []struct {
argv []string
expected *args
isError bool
}{
{
argv: []string{},
expected: &args{},
},
{
argv: []string{"create"},
expected: &args{
cmd: "create",
},
},
{
argv: []string{"--bundle"},
expected: nil,
isError: true,
},
{
argv: []string{"-b"},
expected: nil,
isError: true,
},
{
argv: []string{"--bundle", "/foo/bar"},
expected: &args{bundleDirPath: "/foo/bar"},
},
{
argv: []string{"-bundle", "/foo/bar"},
expected: &args{bundleDirPath: "/foo/bar"},
},
{
argv: []string{"--bundle=/foo/bar"},
expected: &args{bundleDirPath: "/foo/bar"},
},
{
argv: []string{"-b=/foo/bar"},
expected: &args{bundleDirPath: "/foo/bar"},
},
{
argv: []string{"-b=/foo/=bar"},
expected: &args{bundleDirPath: "/foo/=bar"},
},
{
argv: []string{"-b", "/foo/bar"},
expected: &args{bundleDirPath: "/foo/bar"},
},
{
argv: []string{"create", "-b", "/foo/bar"},
expected: &args{
cmd: "create",
bundleDirPath: "/foo/bar",
},
},
{
argv: []string{"-b", "create", "create"},
expected: &args{
cmd: "create",
bundleDirPath: "create",
},
},
{
argv: []string{"-b=create", "create"},
expected: &args{
cmd: "create",
bundleDirPath: "create",
},
},
{
argv: []string{"-b", "create"},
expected: &args{
bundleDirPath: "create",
},
},
}
for i, tc := range testCases {
args, err := getArgs(tc.argv)
if tc.isError {
require.Errorf(t, err, "%d: %v", i, tc)
} else {
require.NoErrorf(t, err, "%d: %v", i, tc)
}
require.EqualValuesf(t, tc.expected, args, "%d: %v", i, tc)
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册