Skip to content
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

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 116 additions & 0 deletions go/cmd/mysqlctl/command/position_test.go
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)
Copy link
Contributor

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)

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 \"\"",
Copy link
Contributor

Choose a reason for hiding this comment

The 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 mysqlctl command (i.e., that functionality should be tested elsewhere). we should remove these (and replace them with different tests if necessary/applicable)

},
{
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())
}
})
}
}
33 changes: 33 additions & 0 deletions go/cmd/mysqlctl/command/start_test.go
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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this called TestList when we are testing the Start command?

require.NotNil(t, Start)
require.Equal(t, "start", Start.Name())
Copy link
Contributor

Choose a reason for hiding this comment

The 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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nor this one


err := Start.RunE(&cobra.Command{}, []string{})
Copy link
Contributor

Choose a reason for hiding this comment

The 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")
}
Loading