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

Fix override-src-port and override-src-ip flag parsing in register command #56

Merged
merged 4 commits into from
Jul 16, 2024

Conversation

Miles-Garnsey
Copy link
Member

Fixes #55

@burmanm
Copy link
Collaborator

burmanm commented Jul 15, 2024

The bug crept in because there's no tests at all for the command parameters. The least it could do is check all the intended / required parameters, like:

package register

import (
	"testing"

	"github.com/spf13/cobra"
	"github.com/stretchr/testify/require"
	"k8s.io/cli-runtime/pkg/genericiooptions"
)

func TestInputParameters(t *testing.T) {
	require := require.New(t)

	RegisterClusterCmd.RunE = func(cmd *cobra.Command, args []string) error {
		return nil
	}
	cmd := &cobra.Command{
		RunE: func(cmd *cobra.Command, args []string) error {
			return nil
		},
	}
	SetupRegisterClusterCmd(cmd, genericiooptions.NewTestIOStreamsDiscard())
	RegisterClusterCmd.SetArgs([]string{"register", "--source-context", "source", "--dest-context", "dest", "--oride-src-ip", "127.0.0.2"})
	executor := NewRegistrationExecutorFromRegisterClusterCmd(*RegisterClusterCmd)
	require.NoError(RegisterClusterCmd.Execute())
	require.Equal("127.0.0.2", executor.OverrideSourceIP)
}

In this case, you'd notice from Execute() that not all the parameters were correctly named and the test would fail. Later, after fixing the implementation (but not the test) one would notice that the 127.0.0.2 was not parsed correctly. I'm not entirely sure why this implementation doesn't catch the incorrect parameter name itself in Execute() like the other cmds, but this would still catch the implementation issue.

@Miles-Garnsey
Copy link
Member Author

Copying from slack:

--- FAIL: TestInputParameters (0.00s)
    register_test.go:158: 
        	Error Trace:	/Users/milesgarnsey/projects/k8ssandra-client/cmd/kubectl-k8ssandra/register/register_test.go:158
        	Error:      	Received unexpected error:
        	            	unknown command "^TestInputParameters$" for ""
        	Test:       	TestInputParameters
FAIL

I think that what you're seeing is that argument one is picking up the test name, so this isn't going to work. I'm not sure how to work around the way that cobra picks up the first  CLI argument...

If we were going to implement a test like this we'd need to figure out how to work around the way that RegisterClusterCmd.SetArgs([]string{"register", "--source-context", "source", "--dest-context", "dest", "--oride-src-ip", "127.0.0.2"}) seems to pick up the arguments from the test command instead of the ones we want.

@burmanm burmanm changed the title Fix flag names as identified by Micke. Fix override-src-port and override-src-ip flag parsing in register command Jul 16, 2024
@burmanm burmanm merged commit 39a6ddd into k8ssandra:main Jul 16, 2024
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Fix mismatched flag names override vs oride
2 participants