Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This patch adds a new flag `--args-file` to the run command which takes the path to a file containing key=value pairs of arguments which can be passed to the Starlark diagnostics script. The values for the keys passed via the `--args-file` flag can be overridden by passing a diff value for the same key via the `--args` flag. Signed-off-by: Sagar Muchhal <muchhals@vmware.com>
- Loading branch information
Showing
6 changed files
with
238 additions
and
8 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,16 @@ | ||
// Copyright (c) 2020 VMware, Inc. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package cmd | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func TestCmd(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "Cmd Suite") | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Copyright (c) 2020 VMware, Inc. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package cmd | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/ginkgo/extensions/table" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
var _ = Describe("Run", func() { | ||
|
||
Context("With args-file and args both", func() { | ||
|
||
DescribeTable("processScriptArguments", func(argsFileContent string, args map[string]string, size int) { | ||
f, err := ioutil.TempFile(os.TempDir(), "") | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
err = ioutil.WriteFile(f.Name(), []byte(argsFileContent), 0644) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
defer f.Close() | ||
|
||
flags := &runFlags{ | ||
args: args, | ||
argsFile: f.Name(), | ||
} | ||
scriptArgs, err := processScriptArguments(flags) | ||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(scriptArgs).To(HaveLen(size)) | ||
}, | ||
Entry("no overlapping keys", "key=value", map[string]string{"a": "b"}, 2), | ||
Entry("overlapping keys", "key=value", map[string]string{"key": "b"}, 1), | ||
Entry("file with no keys", "", map[string]string{"key": "b"}, 1), | ||
Entry("with file and without args", "key=value", map[string]string{}, 1), | ||
) | ||
}) | ||
}) |
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,43 @@ | ||
// Copyright (c) 2020 VMware, Inc. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package util | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"log" | ||
"os" | ||
"strings" | ||
|
||
"github.com/pkg/errors" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
func ReadArgsFile(path string) (map[string]string, error) { | ||
file, err := os.Open(path) | ||
if err != nil { | ||
return nil, errors.Wrap(err, fmt.Sprintf("args file not found: %s", path)) | ||
} | ||
defer file.Close() | ||
|
||
scanner := bufio.NewScanner(file) | ||
if err := scanner.Err(); err != nil { | ||
log.Fatal(err) | ||
return nil, err | ||
} | ||
|
||
args := map[string]string{} | ||
for scanner.Scan() { | ||
line := scanner.Text() | ||
if !strings.HasPrefix(line, "#") && len(strings.TrimSpace(line)) != 0 { | ||
if pair := strings.Split(line, "="); len(pair) == 2 { | ||
args[strings.TrimSpace(pair[0])] = strings.TrimSpace(pair[1]) | ||
} else { | ||
logrus.Warnf("unknown entry in args file: %s", line) | ||
} | ||
} | ||
} | ||
|
||
return args, 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,83 @@ | ||
// Copyright (c) 2020 VMware, Inc. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package util | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/ginkgo/extensions/table" | ||
. "github.com/onsi/gomega" | ||
"github.com/onsi/gomega/gbytes" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
var _ = Describe("ReadArgsFile", func() { | ||
|
||
It("returns an error when an invalid file name is passed", func() { | ||
_, err := ReadArgsFile("/foo/blah") | ||
Expect(err).To(HaveOccurred()) | ||
}) | ||
|
||
Context("with valid file", func() { | ||
DescribeTable("length of args map", func(input string, size int, warnMsgPresent bool) { | ||
f := writeContentToFile(input) | ||
defer f.Close() | ||
|
||
warnBuffer := gbytes.NewBuffer() | ||
logrus.SetOutput(warnBuffer) | ||
|
||
args, _ := ReadArgsFile(f.Name()) | ||
Expect(args).To(HaveLen(size)) | ||
|
||
if warnMsgPresent { | ||
Expect(warnBuffer).To(gbytes.Say("unknown entry in args file")) | ||
} | ||
}, | ||
Entry("valid with no spaces", ` | ||
key=value | ||
foo=bar | ||
`, 2, false), | ||
Entry("valid with spaces", ` | ||
# key represents earth is round | ||
key = value | ||
foo= bar | ||
bloop =blah | ||
`, 3, false), | ||
Entry("valid with empty values", ` | ||
key = | ||
foo= bar | ||
bloop= | ||
`, 3, false), | ||
Entry("invalid", ` | ||
key value | ||
foo | ||
bar | ||
`, 0, true)) | ||
}) | ||
|
||
It("accepts comments in the args file", func() { | ||
f := writeContentToFile(`# key represents A | ||
key = value | ||
# foo represents B | ||
foo= bar`) | ||
defer f.Close() | ||
|
||
args, err := ReadArgsFile(f.Name()) | ||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(args).To(HaveLen(2)) | ||
}) | ||
|
||
}) | ||
|
||
var writeContentToFile = func(content string) *os.File { | ||
f, err := ioutil.TempFile(os.TempDir(), "read_file_args") | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
err = ioutil.WriteFile(f.Name(), []byte(content), 0644) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
return f | ||
} |
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,16 @@ | ||
// Copyright (c) 2020 VMware, Inc. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package util | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func TestUtil(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "Util Suite") | ||
} |