Skip to content

Commit

Permalink
Cleanup and add more unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lynnemorrison committed Oct 22, 2024
1 parent 261a052 commit af796c4
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 8 deletions.
10 changes: 8 additions & 2 deletions internal/cmd/skupper/connector/nonkube/connector_create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ func TestNonKubeCmdConnectorCreate_ValidateInput(t *testing.T) {
},
{
name: "Connector name is not valid",
args: []string{"my new Connector"},
args: []string{"my new Connector", "8080"},
flags: &common.CommandConnectorCreateFlags{},
expectedErrors: []string{"connector name and port must be configured"},
expectedErrors: []string{"connector name is not valid: value does not match this regular expression: ^[a-z0-9]([-a-z0-9]*[a-z0-9])*(\\.[a-z0-9]([-a-z0-9]*[a-z0-9])*)*$"},
},
{
name: "Connector name is empty",
Expand All @@ -54,6 +54,12 @@ func TestNonKubeCmdConnectorCreate_ValidateInput(t *testing.T) {
flags: &common.CommandConnectorCreateFlags{},
expectedErrors: []string{"connector port is not valid: strconv.Atoi: parsing \"abcd\": invalid syntax"},
},
{
name: "port not positive",
args: []string{"my-port-positive", "-45"},
flags: &common.CommandConnectorCreateFlags{},
expectedErrors: []string{"connector port is not valid: value is not positive"},
},
{
name: "more than two arguments was specified",
args: []string{"my", "Connector", "test"},
Expand Down
14 changes: 10 additions & 4 deletions internal/cmd/skupper/connector/nonkube/connector_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,12 @@ func (cmd *CmdConnectorStatus) Run() error {
fmt.Println("failed getting connector")
return err
}
status := "Not Ready"
if connector.IsConfigured() {
status = "Ok"
}
fmt.Fprintln(tw, fmt.Sprintf("%s\t%s\t%s\t%s\t%d",
connector.Name, connector.Status.Conditions[0].Type, connector.Spec.RoutingKey,
connector.Spec.Host, connector.Spec.Port))
connector.Name, status, connector.Spec.RoutingKey, connector.Spec.Host, connector.Spec.Port))
}
_ = tw.Flush()
}
Expand All @@ -124,10 +127,13 @@ func (cmd *CmdConnectorStatus) Run() error {
}
fmt.Println(encodedOutput)
} else {
status := "Not Ready"
if connector.IsConfigured() {
status = "Ok"
}
tw := tabwriter.NewWriter(os.Stdout, 8, 8, 1, '\t', tabwriter.TabIndent)
fmt.Fprintln(tw, fmt.Sprintf("Name:\t%s\nStatus:\t%s\nRouting key:\t%s\nHost:\t%s\nPort:\t%d\n",
connector.Name, connector.Status.Conditions[0].Type, connector.Spec.RoutingKey,
connector.Spec.Host, connector.Spec.Port))
connector.Name, status, connector.Spec.RoutingKey, connector.Spec.Host, connector.Spec.Port))
_ = tw.Flush()
}
}
Expand Down
40 changes: 39 additions & 1 deletion internal/cmd/skupper/connector/nonkube/connector_status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,46 @@ func TestCmdConnectorStatus_Run(t *testing.T) {
command.connectorName = test.connectorName
command.Flags = &test.flags
command.output = command.Flags.Output
command.namespace = "test"

t.Run(test.name, func(t *testing.T) {
err := command.Run()
if err != nil {
assert.Check(t, test.errorMessage == err.Error())
} else {
assert.Check(t, err == nil)
}
})
}
}

func TestCmdConnectorStatus_RunNoDirectory(t *testing.T) {
type test struct {
name string
connectorName string
flags common.CommandConnectorStatusFlags
k8sObjects []runtime.Object
skupperObjects []runtime.Object
skupperErrorMessage string
errorMessage string
}

homeDir, err := os.UserHomeDir()
assert.Check(t, err == nil)

testTable := []test{
{
name: "runs fails no directory",
errorMessage: "failed to read directory: open " + homeDir + "/.local/share/skupper/namespaces/default/runtime/state/connectors: no such file or directory",
},
}

for _, test := range testTable {
command := &CmdConnectorStatus{}
command.namespace = "default"
command.connectorHandler = fs2.NewConnectorHandler(command.namespace)
command.connectorName = test.connectorName
command.Flags = &test.flags
command.output = command.Flags.Output
t.Run(test.name, func(t *testing.T) {

err := command.Run()
Expand Down
14 changes: 13 additions & 1 deletion internal/cmd/skupper/connector/nonkube/connector_update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/skupperproject/skupper/internal/cmd/skupper/common/utils"
fs2 "github.com/skupperproject/skupper/internal/nonkube/client/fs"
"github.com/skupperproject/skupper/pkg/apis/skupper/v1alpha1"
"github.com/spf13/cobra"
"gotest.tools/assert"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
Expand Down Expand Up @@ -78,6 +79,16 @@ func TestCmdConnectorUpdate_ValidateInput(t *testing.T) {
flags: &common.CommandConnectorUpdateFlags{Output: "not-supported"},
expectedErrors: []string{"output type is not valid: value not-supported not allowed. It should be one of this options: [json yaml]"},
},
{
name: "kubernetes flags are not valid on this platform",
args: []string{"my-connector"},
flags: &common.CommandConnectorUpdateFlags{},
cobraGenericFlags: map[string]string{
common.FlagNameContext: "test",
common.FlagNameKubeconfig: "test",
},
expectedErrors: []string{},
},
{
name: "flags all valid",
args: []string{"my-connector"},
Expand All @@ -104,7 +115,8 @@ func TestCmdConnectorUpdate_ValidateInput(t *testing.T) {
},
}

command := &CmdConnectorUpdate{}
command := &CmdConnectorUpdate{Flags: &common.CommandConnectorUpdateFlags{}}
command.CobraCmd = &cobra.Command{Use: "test"}
command.namespace = "test"
command.connectorHandler = fs2.NewConnectorHandler(command.namespace)

Expand Down

0 comments on commit af796c4

Please sign in to comment.