-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Added unit tests for the cmd/mysqlctl package #15041
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
/* | ||
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 command | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestPosition(t *testing.T) { | ||
cmd := Position | ||
require.NotNil(t, cmd) | ||
require.Equal(t, "position", cmd.Name()) | ||
|
||
tests := []struct { | ||
name string | ||
args []string | ||
expectedErr string | ||
}{ | ||
{ | ||
name: "empty args", | ||
args: []string{}, | ||
expectedErr: "accepts 3 arg(s), received 0", | ||
}, | ||
{ | ||
name: "non-empty args", | ||
args: []string{"equal", "at_least", "append"}, | ||
}, | ||
{ | ||
name: "non-empty args", | ||
args: []string{"temp", "at_least", "append"}, | ||
expectedErr: "invalid operation temp (choices are 'equal', 'at_least', 'append')", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
err := cmd.Args(&cobra.Command{}, tt.args) | ||
if tt.expectedErr == "" { | ||
require.NoError(t, err) | ||
} else { | ||
require.ErrorContains(t, err, tt.expectedErr) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestPositionRun(t *testing.T) { | ||
cmd := Position | ||
require.NotNil(t, cmd) | ||
require.Equal(t, "position", cmd.Name()) | ||
|
||
tests := []struct { | ||
name string | ||
args []string | ||
expectedErr string | ||
}{ | ||
{ | ||
name: "parse error", | ||
args: []string{"equal", "at_least", "append"}, | ||
expectedErr: "parse error: unknown GTIDSet flavor \"\"", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. reading these test cases, it seems like we are really only testing that GTIDSet flavor parsing is correct, which is orthogonal to the actual |
||
}, | ||
{ | ||
name: "only equal", | ||
args: []string{"equal", "", ""}, | ||
}, | ||
{ | ||
name: "only equal with error", | ||
args: []string{"equal", "", "error"}, | ||
expectedErr: "parse error: unknown GTIDSet flavor \"\"", | ||
}, | ||
{ | ||
name: "only append", | ||
args: []string{"append", "", ""}, | ||
}, | ||
{ | ||
name: "only append with error", | ||
args: []string{"append", "", "error"}, | ||
expectedErr: "parse error: unknown GTID flavor \"\"", | ||
}, | ||
{ | ||
name: "only at_least", | ||
args: []string{"at_least", "", ""}, | ||
}, | ||
{ | ||
name: "only at_least with error", | ||
args: []string{"at_least", "", "error"}, | ||
expectedErr: "parse error: unknown GTIDSet flavor \"\"", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
err := cmd.RunE(&cobra.Command{}, tt.args) | ||
if tt.expectedErr == "" { | ||
require.NoError(t, err) | ||
} else { | ||
require.Equal(t, tt.expectedErr, err.Error()) | ||
} | ||
}) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
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 command | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestList(t *testing.T) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this called |
||
require.NotNil(t, Start) | ||
require.Equal(t, "start", Start.Name()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i don't think this assertion is particularly valuable |
||
require.Equal(t, "Starts mysqld on an already 'init'-ed directory.", Start.Short) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nor this one |
||
|
||
err := Start.RunE(&cobra.Command{}, []string{}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we set up a more comprehensive test? this fails pretty early into the command's main method right? |
||
require.ErrorContains(t, err, "failed to find mysql config") | ||
} |
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.
nit: technically speaking (here it is Fine by coincidence, but in case anyone copies this pattern), this should be
cmd.Args(cmd, tt.args)