Skip to content

Commit

Permalink
test: k3d create - check if ports required are opened
Browse files Browse the repository at this point in the history
  • Loading branch information
jairoFernandez committed Oct 22, 2023
1 parent 3042f57 commit a60ad8d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
4 changes: 3 additions & 1 deletion cmd/k3d/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{})
Expand Down Expand Up @@ -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())
}
Expand Down
34 changes: 30 additions & 4 deletions cmd/k3d/create_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package k3d

import (
"errors"
"testing"

"github.com/spf13/cobra"
Expand All @@ -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")
Expand All @@ -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"}
Expand All @@ -41,11 +46,32 @@ 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"
if errorExpected != err.Error() {
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)
}
}

0 comments on commit a60ad8d

Please sign in to comment.