-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added unit tests for
go/cmd/rulesctl/
package (#15028)
Signed-off-by: VaibhavMalik4187 <[email protected]> Signed-off-by: Manan Gupta <[email protected]> Co-authored-by: Manan Gupta <[email protected]>
- Loading branch information
1 parent
9a00730
commit 9cd8ffc
Showing
8 changed files
with
583 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,133 @@ | ||
/* | ||
Copyright 2024 The Vitess Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package cmd | ||
|
||
import ( | ||
"io" | ||
"os" | ||
"testing" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestAdd(t *testing.T) { | ||
cmd := Add() | ||
require.NotNil(t, cmd) | ||
require.Equal(t, "add-rule", cmd.Name()) | ||
configFile = "./testdata/rules.json" | ||
|
||
tests := []struct { | ||
name string | ||
args []string | ||
expectedOutput string | ||
}{ | ||
{ | ||
name: "Action fail", | ||
args: []string{"--dry-run=true", "--name=Rule", `--description="New rules that will be added to the file"`, "--action=fail", "--plan=Select"}, | ||
expectedOutput: `[ | ||
{ | ||
"Description": "Some value", | ||
"Name": "Name", | ||
"Action": "FAIL" | ||
}, | ||
{ | ||
"Description": "\"New rules that will be added to the file\"", | ||
"Name": "Rule", | ||
"Plans": [ | ||
"Select" | ||
], | ||
"Action": "FAIL" | ||
} | ||
] | ||
`, | ||
}, | ||
{ | ||
name: "Action fail_retry", | ||
args: []string{"--dry-run=true", "--name=Rule", `--description="New rules that will be added to the file"`, "--action=fail_retry", "--plan=Select"}, | ||
expectedOutput: `[ | ||
{ | ||
"Description": "Some value", | ||
"Name": "Name", | ||
"Action": "FAIL" | ||
}, | ||
{ | ||
"Description": "\"New rules that will be added to the file\"", | ||
"Name": "Rule", | ||
"Plans": [ | ||
"Select", | ||
"Select" | ||
], | ||
"Action": "FAIL_RETRY" | ||
} | ||
] | ||
`, | ||
}, | ||
{ | ||
name: "Action continue with query", | ||
args: []string{"--dry-run=true", "--name=Rule", `--description="New rules that will be added to the file"`, "--action=continue", "--plan=Select", "--query=secret", "--leading-comment=None", "--trailing-comment=Yoho", "--table=Temp"}, | ||
expectedOutput: `[ | ||
{ | ||
"Description": "Some value", | ||
"Name": "Name", | ||
"Action": "FAIL" | ||
}, | ||
{ | ||
"Description": "\"New rules that will be added to the file\"", | ||
"Name": "Rule", | ||
"Query": "secret", | ||
"LeadingComment": "None", | ||
"TrailingComment": "Yoho", | ||
"Plans": [ | ||
"Select", | ||
"Select", | ||
"Select" | ||
], | ||
"TableNames": [ | ||
"Temp" | ||
] | ||
} | ||
] | ||
`, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if tt.args != nil { | ||
cmd.SetArgs(tt.args) | ||
err := cmd.Execute() | ||
require.NoError(t, err) | ||
} | ||
|
||
originalStdOut := os.Stdout | ||
defer func() { | ||
os.Stdout = originalStdOut | ||
}() | ||
// Redirect stdout to a buffer | ||
r, w, _ := os.Pipe() | ||
os.Stdout = w | ||
|
||
cmd.Run(&cobra.Command{}, []string{}) | ||
|
||
err := w.Close() | ||
require.NoError(t, err) | ||
got, err := io.ReadAll(r) | ||
require.NoError(t, err) | ||
require.EqualValues(t, tt.expectedOutput, string(got)) | ||
}) | ||
} | ||
} |
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,77 @@ | ||
/* | ||
Copyright 2024 The Vitess Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package cmd | ||
|
||
import ( | ||
"io" | ||
"os" | ||
"testing" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestExplainWithQueryPlanArguement(t *testing.T) { | ||
explainCmd := Explain() | ||
|
||
require.NotNil(t, explainCmd) | ||
require.Equal(t, "explain", explainCmd.Name()) | ||
|
||
originalStdOut := os.Stdout | ||
defer func() { | ||
os.Stdout = originalStdOut | ||
}() | ||
// Redirect stdout to a buffer | ||
r, w, _ := os.Pipe() | ||
os.Stdout = w | ||
|
||
explainCmd.Run(&cobra.Command{}, []string{"query-plans"}) | ||
|
||
err := w.Close() | ||
require.NoError(t, err) | ||
got, err := io.ReadAll(r) | ||
require.NoError(t, err) | ||
|
||
expected := "Query Plans!" | ||
require.Contains(t, string(got), expected) | ||
} | ||
|
||
func TestExplainWithRandomArguement(t *testing.T) { | ||
explainCmd := Explain() | ||
|
||
require.NotNil(t, explainCmd) | ||
require.Equal(t, "explain", explainCmd.Name()) | ||
|
||
// Redirect stdout to a buffer | ||
originalStdOut := os.Stdout | ||
defer func() { | ||
os.Stdout = originalStdOut | ||
}() | ||
// Redirect stdout to a buffer | ||
r, w, _ := os.Pipe() | ||
os.Stdout = w | ||
|
||
explainCmd.Run(&cobra.Command{}, []string{"random"}) | ||
|
||
err := w.Close() | ||
require.NoError(t, err) | ||
got, err := io.ReadAll(r) | ||
require.NoError(t, err) | ||
|
||
expected := "I don't know anything about" | ||
require.Contains(t, string(got), expected) | ||
} |
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,103 @@ | ||
/* | ||
Copyright 2024 The Vitess Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package cmd | ||
|
||
import ( | ||
"io" | ||
"os" | ||
"testing" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestList(t *testing.T) { | ||
cmd := List() | ||
require.NotNil(t, cmd) | ||
require.Equal(t, "list", cmd.Name()) | ||
configFile = "./testdata/rules.json" | ||
|
||
tests := []struct { | ||
name string | ||
args []string | ||
expectedOutput string | ||
}{ | ||
{ | ||
name: "No args", | ||
expectedOutput: `[ | ||
{ | ||
"Description": "Some value", | ||
"Name": "Name", | ||
"Action": "FAIL" | ||
} | ||
] | ||
`, | ||
}, | ||
{ | ||
name: "Name only", | ||
args: []string{"--names-only=true"}, | ||
expectedOutput: `[ | ||
"Name" | ||
] | ||
`, | ||
}, | ||
{ | ||
name: "Name flag set", | ||
args: []string{"--name=Name"}, | ||
expectedOutput: `"Name" | ||
`, | ||
}, | ||
{ | ||
name: "Random name in name flag", | ||
args: []string{"--name=Random"}, | ||
expectedOutput: `"" | ||
`, | ||
}, | ||
{ | ||
name: "Random name in name flag and names-only false", | ||
args: []string{"--name=Random", "--names-only=false"}, | ||
expectedOutput: `null | ||
`, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if tt.args != nil { | ||
cmd.SetArgs(tt.args) | ||
err := cmd.Execute() | ||
require.NoError(t, err) | ||
} | ||
|
||
originalStdOut := os.Stdout | ||
defer func() { | ||
os.Stdout = originalStdOut | ||
}() | ||
// Redirect stdout to a buffer | ||
r, w, _ := os.Pipe() | ||
os.Stdout = w | ||
|
||
cmd.Run(&cobra.Command{}, []string{}) | ||
|
||
err := w.Close() | ||
require.NoError(t, err) | ||
got, err := io.ReadAll(r) | ||
require.NoError(t, err) | ||
|
||
require.EqualValues(t, tt.expectedOutput, string(got)) | ||
}) | ||
} | ||
} |
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,60 @@ | ||
/* | ||
Copyright 2024 The Vitess Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package cmd | ||
|
||
import ( | ||
"io" | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestMainFunction(t *testing.T) { | ||
rootCmd := Main() | ||
require.NotNil(t, rootCmd) | ||
require.Equal(t, "rulesctl", rootCmd.Name()) | ||
|
||
originalStdOut := os.Stdout | ||
defer func() { | ||
os.Stdout = originalStdOut | ||
}() | ||
// Redirect stdout to a buffer | ||
r, w, _ := os.Pipe() | ||
os.Stdout = w | ||
|
||
args := os.Args | ||
t.Cleanup(func() { os.Args = args }) | ||
os.Args = []string{"rulesctl", "-f=testdata/rules.json", "list"} | ||
err := rootCmd.Execute() | ||
require.NoError(t, err) | ||
|
||
err = w.Close() | ||
require.NoError(t, err) | ||
got, err := io.ReadAll(r) | ||
require.NoError(t, err) | ||
|
||
expected := `[ | ||
{ | ||
"Description": "Some value", | ||
"Name": "Name", | ||
"Action": "FAIL" | ||
} | ||
] | ||
` | ||
require.EqualValues(t, expected, string(got)) | ||
} |
Oops, something went wrong.