Add unit test for PersistentPreRunE

This commit is contained in:
Jiaming Hu
2020-12-01 22:49:03 -05:00
parent 5e4f270b1e
commit 014513d55e
6 changed files with 48 additions and 16 deletions
+11 -7
View File
@@ -8,16 +8,13 @@ import (
"testing"
"github.com/crossplane/crossplane-runtime/pkg/test"
"github.com/oam-dev/kubevela/pkg/utils/env"
"github.com/oam-dev/kubevela/apis/types"
"github.com/oam-dev/kubevela/pkg/utils/system"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
"github.com/oam-dev/kubevela/apis/types"
cmdutil "github.com/oam-dev/kubevela/pkg/commands/util"
"github.com/oam-dev/kubevela/pkg/utils/env"
"github.com/oam-dev/kubevela/pkg/utils/system"
)
func TestENV(t *testing.T) {
@@ -101,3 +98,10 @@ func TestENV(t *testing.T) {
err = SetEnv([]string{"default"}, ioStream)
assert.NoError(t, err)
}
func TestEnvInitCommandPersistentPreRunE(t *testing.T) {
io := cmdutil.IOStreams{In: os.Stdin, Out: os.Stdout, ErrOut: os.Stderr}
fakeC := types.Args{}
cmd := NewEnvInitCommand(fakeC, io)
assert.Nil(t, cmd.PersistentPreRunE(new(cobra.Command), []string{}))
}
+9 -2
View File
@@ -5,6 +5,7 @@ import (
"os"
"testing"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
corev1 "k8s.io/api/core/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -12,12 +13,11 @@ import (
"k8s.io/kubectl/pkg/cmd/exec"
cmdtesting "k8s.io/kubectl/pkg/cmd/testing"
"github.com/oam-dev/kubevela/pkg/oam"
"github.com/oam-dev/kubevela/apis/types"
"github.com/oam-dev/kubevela/pkg/appfile"
"github.com/oam-dev/kubevela/pkg/application"
cmdutil "github.com/oam-dev/kubevela/pkg/commands/util"
"github.com/oam-dev/kubevela/pkg/oam"
)
func TestExecCommand(t *testing.T) {
@@ -60,3 +60,10 @@ func TestExecCommand(t *testing.T) {
err = o.Complete()
assert.NoError(t, err)
}
func TestExecCommandPersistentPreRunE(t *testing.T) {
io := cmdutil.IOStreams{In: os.Stdin, Out: os.Stdout, ErrOut: os.Stderr}
fakeC := types.Args{}
cmd := NewExecCommand(fakeC, io)
assert.Nil(t, cmd.PersistentPreRunE(new(cobra.Command), []string{}))
}
+9 -2
View File
@@ -5,6 +5,7 @@ import (
"os"
"testing"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
corev1 "k8s.io/api/core/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -12,10 +13,9 @@ import (
"k8s.io/kubectl/pkg/cmd/portforward"
cmdtesting "k8s.io/kubectl/pkg/cmd/testing"
"github.com/oam-dev/kubevela/pkg/oam"
"github.com/oam-dev/kubevela/apis/types"
cmdutil "github.com/oam-dev/kubevela/pkg/commands/util"
"github.com/oam-dev/kubevela/pkg/oam"
)
func TestPortForwardCommand(t *testing.T) {
@@ -51,3 +51,10 @@ func TestPortForwardCommand(t *testing.T) {
err := o.Init(context.Background(), cmd, []string{"fakeApp", "8081:8080"})
assert.NoError(t, err)
}
func TestNewPortForwardCommandPersistentPreRunE(t *testing.T) {
io := cmdutil.IOStreams{In: os.Stdin, Out: os.Stdout, ErrOut: os.Stderr}
fakeC := types.Args{}
cmd := NewPortForwardCommand(fakeC, io)
assert.Nil(t, cmd.PersistentPreRunE(new(cobra.Command), []string{}))
}
+10 -2
View File
@@ -2,11 +2,12 @@ package commands
import (
"bytes"
"os"
"testing"
"github.com/stretchr/testify/assert"
"github.com/gosuri/uitable"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
"github.com/oam-dev/kubevela/apis/types"
cmdutil "github.com/oam-dev/kubevela/pkg/commands/util"
@@ -90,3 +91,10 @@ func Test_printTraitList(t *testing.T) {
assert.NoError(t, printTraitList(&nn, iostream))
}
}
func TestNewTraitsCommandPersistentPreRunE(t *testing.T) {
io := cmdutil.IOStreams{In: os.Stdin, Out: os.Stdout, ErrOut: os.Stderr}
fakeC := types.Args{}
cmd := NewTraitsCommand(fakeC, io)
assert.Nil(t, cmd.PersistentPreRunE(new(cobra.Command), []string{}))
}
+1 -2
View File
@@ -9,8 +9,6 @@ import (
"path/filepath"
"strings"
"github.com/oam-dev/kubevela/pkg/utils/common"
"github.com/pkg/errors"
"github.com/spf13/cobra"
apierrors "k8s.io/apimachinery/pkg/api/errors"
@@ -26,6 +24,7 @@ import (
"github.com/oam-dev/kubevela/pkg/application"
cmdutil "github.com/oam-dev/kubevela/pkg/commands/util"
"github.com/oam-dev/kubevela/pkg/oam"
"github.com/oam-dev/kubevela/pkg/utils/common"
)
var (
+8 -1
View File
@@ -5,13 +5,13 @@ import (
"os"
"testing"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes/scheme"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
"github.com/oam-dev/kubevela/apis/core.oam.dev/v1alpha2"
"github.com/oam-dev/kubevela/apis/types"
"github.com/oam-dev/kubevela/pkg/commands/util"
)
@@ -39,3 +39,10 @@ func TestUp(t *testing.T) {
assert.Contains(t, msg, "App has been deployed")
assert.Contains(t, msg, fmt.Sprintf("App status: vela status %s", appName))
}
func TestNewUpCommandPersistentPreRunE(t *testing.T) {
io := util.IOStreams{In: os.Stdin, Out: os.Stdout, ErrOut: os.Stderr}
fakeC := types.Args{}
cmd := NewUpCommand(fakeC, io)
assert.Nil(t, cmd.PersistentPreRunE(new(cobra.Command), []string{}))
}