-
Notifications
You must be signed in to change notification settings - Fork 819
Avoid calling flag.Parse() twice. #1997
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
pracucci
merged 9 commits into
cortexproject:master
from
pstibrany:dont-call-flag-parse-twice
Jan 21, 2020
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
cdb6bce
Calling flag.Parse() twice confuses some flags.
pstibrany b6c7b59
Reverted unintended import order change.
pstibrany cb8a8df
Call RegisterFlags(&cfg) to set defaults before parsing config file.
pstibrany 44cfb20
Added some unit tests for parsing config.
pstibrany 9535e40
Test mode in main simply dumps the config YAML if no error occurs
pstibrany f742d9e
Separate stdout/stderr expected messages and checks.
pstibrany 85636f9
Use mutexProfileFraction only after flags are parsed.
pstibrany f0db5a0
Renamed default test name.
pstibrany 9efeaf0
Reset testMode to original value at the end of test.
pstibrany File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,163 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"flag" | ||
"io" | ||
"io/ioutil" | ||
"os" | ||
"sync" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestFlagParsing(t *testing.T) { | ||
for name, tc := range map[string]struct { | ||
arguments []string | ||
yaml string | ||
stdoutMessage string // string that must be included in stdout | ||
stderrMessage string // string that must be included in stderr | ||
}{ | ||
"help": { | ||
arguments: []string{"-h"}, | ||
stderrMessage: configFileOption, | ||
}, | ||
|
||
// check that config file is used | ||
"config with unknown target": { | ||
yaml: "target: unknown", | ||
stderrMessage: "unrecognised module name: unknown", | ||
}, | ||
|
||
"argument with unknown target": { | ||
arguments: []string{"-target=unknown"}, | ||
stderrMessage: "unrecognised module name: unknown", | ||
}, | ||
|
||
"unknown flag": { | ||
arguments: []string{"-unknown.flag"}, | ||
stderrMessage: "-unknown.flag", | ||
}, | ||
|
||
"config with wrong argument override": { | ||
yaml: "target: ingester", | ||
arguments: []string{"-target=unknown"}, | ||
stderrMessage: "unrecognised module name: unknown", | ||
}, | ||
|
||
"default values": { | ||
stdoutMessage: "target: all\n", | ||
}, | ||
|
||
"config": { | ||
yaml: "target: ingester", | ||
stdoutMessage: "target: ingester\n", | ||
}, | ||
|
||
"config with arguments override": { | ||
yaml: "target: ingester", | ||
arguments: []string{"-target=distributor"}, | ||
stdoutMessage: "target: distributor\n", | ||
}, | ||
|
||
// we cannot test the happy path, as cortex would then fully start | ||
} { | ||
t.Run(name, func(t *testing.T) { | ||
testSingle(t, tc.arguments, tc.yaml, []byte(tc.stdoutMessage), []byte(tc.stderrMessage)) | ||
}) | ||
} | ||
} | ||
|
||
func testSingle(t *testing.T, arguments []string, yaml string, stdoutMessage, stderrMessage []byte) { | ||
oldArgs, oldStdout, oldStderr, oldTestMode := os.Args, os.Stdout, os.Stderr, testMode | ||
defer func() { | ||
os.Stdout = oldStdout | ||
os.Stderr = oldStderr | ||
os.Args = oldArgs | ||
testMode = oldTestMode | ||
}() | ||
|
||
if yaml != "" { | ||
tempFile, err := ioutil.TempFile("", "test") | ||
require.NoError(t, err) | ||
|
||
defer func() { | ||
require.NoError(t, tempFile.Close()) | ||
require.NoError(t, os.Remove(tempFile.Name())) | ||
}() | ||
|
||
_, err = tempFile.WriteString(yaml) | ||
require.NoError(t, err) | ||
|
||
arguments = append([]string{"-" + configFileOption, tempFile.Name()}, arguments...) | ||
} | ||
|
||
arguments = append([]string{"./cortex"}, arguments...) | ||
|
||
testMode = true | ||
os.Args = arguments | ||
co := captureOutput(t) | ||
|
||
// reset default flags | ||
flag.CommandLine = flag.NewFlagSet(arguments[0], flag.ExitOnError) | ||
|
||
main() | ||
|
||
stdout, stderr := co.Done() | ||
if !bytes.Contains(stdout, stdoutMessage) { | ||
t.Errorf("Expected on stdout: %q, stdout: %s\n", stdoutMessage, stdout) | ||
} | ||
if !bytes.Contains(stderr, stderrMessage) { | ||
t.Errorf("Expected on stderr: %q, stderr: %s\n", stderrMessage, stderr) | ||
} | ||
} | ||
|
||
type capturedOutput struct { | ||
stdoutBuf bytes.Buffer | ||
stderrBuf bytes.Buffer | ||
|
||
wg sync.WaitGroup | ||
stdoutReader, stdoutWriter *os.File | ||
stderrReader, stderrWriter *os.File | ||
} | ||
|
||
func captureOutput(t *testing.T) *capturedOutput { | ||
stdoutR, stdoutW, err := os.Pipe() | ||
require.NoError(t, err) | ||
os.Stdout = stdoutW | ||
pracucci marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
stderrR, stderrW, err := os.Pipe() | ||
require.NoError(t, err) | ||
os.Stderr = stderrW | ||
|
||
co := &capturedOutput{ | ||
stdoutReader: stdoutR, | ||
stdoutWriter: stdoutW, | ||
stderrReader: stderrR, | ||
stderrWriter: stderrW, | ||
} | ||
co.wg.Add(1) | ||
go func() { | ||
defer co.wg.Done() | ||
_, _ = io.Copy(&co.stdoutBuf, stdoutR) | ||
}() | ||
|
||
co.wg.Add(1) | ||
go func() { | ||
defer co.wg.Done() | ||
_, _ = io.Copy(&co.stderrBuf, stderrR) | ||
}() | ||
|
||
return co | ||
} | ||
|
||
func (co *capturedOutput) Done() (stdout []byte, stderr []byte) { | ||
// we need to close writers for readers to stop | ||
_ = co.stdoutWriter.Close() | ||
_ = co.stderrWriter.Close() | ||
|
||
co.wg.Wait() | ||
|
||
return co.stdoutBuf.Bytes(), co.stderrBuf.Bytes() | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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,22 @@ | ||
package flagext | ||
|
||
import ( | ||
"flag" | ||
) | ||
|
||
type ignoredFlag struct { | ||
name string | ||
} | ||
|
||
func (ignoredFlag) String() string { | ||
return "ignored" | ||
} | ||
|
||
func (d ignoredFlag) Set(string) error { | ||
return nil | ||
} | ||
|
||
// IgnoredFlag ignores set value, without any warning | ||
func IgnoredFlag(f *flag.FlagSet, name, message string) { | ||
f.Var(ignoredFlag{name}, name, message) | ||
} |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.