From a60ad8d53991ce32f80ee50151a6de1155dcbef5 Mon Sep 17 00:00:00 2001 From: Jairo Fernandez Date: Sun, 22 Oct 2023 00:57:13 -0500 Subject: [PATCH] test: k3d create - check if ports required are opened --- cmd/k3d/create.go | 4 +++- cmd/k3d/create_test.go | 34 ++++++++++++++++++++++++++++++---- 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/cmd/k3d/create.go b/cmd/k3d/create.go index 45f2b34c8..9a92edea6 100644 --- a/cmd/k3d/create.go +++ b/cmd/k3d/create.go @@ -69,6 +69,8 @@ var FlagsToValidate = []FlagConfig{ {"use-telemetry", "bool"}, } +var CheckPortsFn = k8s.CheckForExistingPortForwards + func runK3d(cmd *cobra.Command, args []string) error { flagValues := make(map[string]interface{}) @@ -121,7 +123,7 @@ func runK3d(cmd *cobra.Command, args []string) error { } // Check for existing port forwards before continuing - err = k8s.CheckForExistingPortForwards(8080, 8200, 9000, 9094) + err = CheckPortsFn(8080, 8200, 9000, 9094) if err != nil { return fmt.Errorf("%s - this port is required to set up your kubefirst environment - please close any existing port forwards before continuing", err.Error()) } diff --git a/cmd/k3d/create_test.go b/cmd/k3d/create_test.go index c1c7222cd..75f886c05 100644 --- a/cmd/k3d/create_test.go +++ b/cmd/k3d/create_test.go @@ -1,6 +1,7 @@ package k3d import ( + "errors" "testing" "github.com/spf13/cobra" @@ -11,9 +12,9 @@ func mockCommandComplete() *cobra.Command { cmd.Flags().Bool("ci", false, "ci flag") cmd.Flags().String("cluster-name", "default", "cluster-name flag") cmd.Flags().String("cluster-type", "default", "cluster-type flag") - cmd.Flags().String("github-org", "default", "github-org flag") - cmd.Flags().String("github-user", "default", "github-user flag") - cmd.Flags().String("gitlab-group", "default", "gitlab-group flag") + cmd.Flags().String("github-org", "", "github-org flag") + cmd.Flags().String("github-user", "", "github-user flag") + cmd.Flags().String("gitlab-group", "", "gitlab-group flag") cmd.Flags().String("git-provider", "default", "git-provider flag") cmd.Flags().String("git-protocol", "default", "git-protocol flag") cmd.Flags().String("gitops-template-url", "default", "gitops-template-url flag") @@ -28,6 +29,10 @@ func mockCommandIncomplete() *cobra.Command { return cmd } +func mockCheckForExistingPortForwards(ports ...int) error { + return errors.New("port 8080 is in use") +} + func TestRunK3dShouldReturnErrorIfSomeFlagIsNotPresent(t *testing.T) { cmd := mockCommandIncomplete() args := []string{"create"} @@ -41,7 +46,10 @@ func TestRunK3dShouldReturnErrorIfSomeFlagIsNotPresent(t *testing.T) { func TestRunK3dShouldReturnErrorIfSomeFlagAreNotValid(t *testing.T) { cmd := mockCommandComplete() - args := []string{"create", "--cluster-name", "test", "--cluster-type", "test"} + cmd.Flags().Set("github-user", "usertest") + cmd.Flags().Set("github-org", "orgtest") + + args := []string{"create"} err := runK3d(cmd, args) errorExpected := "only one of --github-user or --github-org can be supplied" @@ -49,3 +57,21 @@ func TestRunK3dShouldReturnErrorIfSomeFlagAreNotValid(t *testing.T) { t.Errorf("runK3d(%q) returned an error: %v", args, err) } } + +func TestShouldReturnErrorWhenSomePortsAreNotOpen(t *testing.T) { + cmd := mockCommandComplete() + cmd.Flags().Set("github-user", "usertest") + + originalCheckPortsFn := CheckPortsFn + defer func() { CheckPortsFn = originalCheckPortsFn }() + + CheckPortsFn = mockCheckForExistingPortForwards + + args := []string{"create"} + err := runK3d(cmd, args) + + errorExpected := "port 8080 is in use - this port is required to set up your kubefirst environment - please close any existing port forwards before continuing" + if errorExpected != err.Error() { + t.Errorf("runK3d(%q) returned an error: %v", args, err) + } +}