-
Notifications
You must be signed in to change notification settings - Fork 260
feat: [NPM] ipset save before restoring and fixing grep UTs #1085
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
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
de13661
use ipset save to update members and update error handling logic for …
huntergregory ad7ba7c
remove unused code
huntergregory 9ac9aa4
add comment block describing high level ipset restore logic
huntergregory ab6d90d
fix bug in piping to grep. need to add pipe errors for fexec UTs so t…
huntergregory 7ecb311
grep for npm sets working for ipsets save, but this breaks DP UTs
huntergregory d55cecd
VerifyCalls method for mock ioshim
huntergregory 6c17d5b
add ability to unit test piped commands
huntergregory ce0a3d8
update logging
huntergregory 9fc401c
UTs for piping a command to grep
huntergregory 2a51a5c
grep for npm sets in ipset save, verify number of calls in UTs, and u…
huntergregory d6889b1
update comments based on PR suggestions
huntergregory 5ba0616
addressing comments
huntergregory 2646c97
remove out-of-scope policy changes for this PR
huntergregory 3cc072d
rename restore file creator files
huntergregory 31148c3
FIXME: setting v2 controllers toggle to true to create an image in pi…
huntergregory 29d447a
Revert "FIXME: setting v2 controllers toggle to true to create an ima…
huntergregory 6f47ab0
wrap errors
huntergregory 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,35 @@ | ||
| package ioutil | ||
|
|
||
| import utilexec "k8s.io/utils/exec" | ||
|
|
||
| // Grep is the grep command string | ||
| const Grep = "grep" | ||
|
|
||
| func PipeCommandToGrep(command, grepCommand utilexec.Cmd) (searchResults []byte, gotMatches bool, commandError error) { | ||
| pipe, commandError := command.StdoutPipe() | ||
| if commandError != nil { | ||
| return | ||
| } | ||
| closePipe := func() { _ = pipe.Close() } // appease go lint | ||
| defer closePipe() | ||
|
|
||
| grepCommand.SetStdin(pipe) | ||
| commandError = command.Start() | ||
| if commandError != nil { | ||
| return | ||
| } | ||
|
|
||
| // Without this wait, defunct iptable child process are created | ||
| wait := func() { _ = command.Wait() } // appease go lint | ||
| defer wait() | ||
|
|
||
| output, err := grepCommand.CombinedOutput() | ||
| if err != nil { | ||
| // grep returns err status 1 if nothing is found | ||
| // but the other command's exit status gets propagated through this CombinedOutput, so we might have errors undetected | ||
| return | ||
| } | ||
| searchResults = output | ||
| gotMatches = true | ||
| return | ||
| } | ||
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,92 @@ | ||
| package ioutil | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| testutils "github.com/Azure/azure-container-networking/test/utils" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestGrepMatch(t *testing.T) { | ||
| calls := []testutils.TestCmd{ | ||
| { | ||
| Cmd: []string{"some", "command"}, | ||
| PipedToCommand: true, | ||
| }, | ||
| { | ||
| Cmd: []string{"grep", "pattern"}, | ||
| Stdout: "1: here's the pattern we're looking for", | ||
| }, | ||
| { | ||
| Cmd: []string{"some", "command"}, | ||
| PipedToCommand: true, | ||
| }, | ||
| { | ||
| Cmd: []string{"grep", "pattern"}, | ||
| Stdout: "2: here's the pattern we're looking for", | ||
| }, | ||
| } | ||
| fexec := testutils.GetFakeExecWithScripts(calls) | ||
| defer testutils.VerifyCalls(t, fexec, calls) | ||
|
|
||
| someCommand := fexec.Command("some", "command") | ||
| grepCommand := fexec.Command(Grep, "pattern") | ||
| output, gotMatches, err := PipeCommandToGrep(someCommand, grepCommand) | ||
| require.NoError(t, err) | ||
| require.True(t, gotMatches) | ||
| require.Equal(t, "1: here's the pattern we're looking for", string(output)) | ||
|
|
||
| someCommand = fexec.Command("some", "command") | ||
| grepCommand = fexec.Command(Grep, "pattern") | ||
| output, gotMatches, err = PipeCommandToGrep(someCommand, grepCommand) | ||
| require.NoError(t, err) | ||
| require.True(t, gotMatches) | ||
| require.Equal(t, "2: here's the pattern we're looking for", string(output)) | ||
| } | ||
|
|
||
| func TestGrepNoMatch(t *testing.T) { | ||
| calls := []testutils.TestCmd{ | ||
| { | ||
| Cmd: []string{"some", "command"}, | ||
| PipedToCommand: true, | ||
| ExitCode: 0, | ||
| }, | ||
| { | ||
| Cmd: []string{"grep", "pattern"}, | ||
| ExitCode: 1, | ||
| }, | ||
| } | ||
| fexec := testutils.GetFakeExecWithScripts(calls) | ||
| defer testutils.VerifyCalls(t, fexec, calls) | ||
|
|
||
| someCommand := fexec.Command("some", "command") | ||
| grepCommand := fexec.Command(Grep, "pattern") | ||
| output, gotMatches, err := PipeCommandToGrep(someCommand, grepCommand) | ||
| require.NoError(t, err) | ||
| require.False(t, gotMatches) | ||
| require.Nil(t, output) | ||
| } | ||
|
|
||
| func TestCommandStartError(t *testing.T) { | ||
| calls := []testutils.TestCmd{ | ||
| { | ||
| Cmd: []string{"some", "command"}, | ||
| HasStartError: true, | ||
| PipedToCommand: true, | ||
| ExitCode: 5, | ||
| }, | ||
| { | ||
| Cmd: []string{"grep", "pattern"}, | ||
| ExitCode: 0, | ||
| }, | ||
| } | ||
| fexec := testutils.GetFakeExecWithScripts(calls) | ||
| defer testutils.VerifyCalls(t, fexec, calls) | ||
|
|
||
| someCommand := fexec.Command("some", "command") | ||
| grepCommand := fexec.Command(Grep, "pattern") | ||
| output, gotMatches, err := PipeCommandToGrep(someCommand, grepCommand) | ||
| require.Error(t, err) | ||
| require.False(t, gotMatches) | ||
| require.Nil(t, output) | ||
| } |
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should
errbecommandError?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no actually since grep returns an error if we don't find anything. I'd like to return commandError nil for this case