mirror of https://github.com/knative/client.git
Adding tests for commands package (#1377)
This commit is contained in:
parent
2164243458
commit
e4f18fa7f8
|
|
@ -16,12 +16,15 @@ package commands
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"fmt"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
"gotest.tools/v3/assert"
|
"gotest.tools/v3/assert"
|
||||||
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
"knative.dev/client/pkg/printers"
|
"knative.dev/client/pkg/printers"
|
||||||
"knative.dev/pkg/apis"
|
"knative.dev/pkg/apis"
|
||||||
)
|
)
|
||||||
|
|
@ -108,3 +111,43 @@ OK TYPE AGE REASON
|
||||||
I Ccc Eh.`))
|
I Ccc Eh.`))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestWriteSliceDesc(t *testing.T) {
|
||||||
|
var out bytes.Buffer
|
||||||
|
pw := printers.NewBarePrefixWriter(&out)
|
||||||
|
WriteSliceDesc(pw, nil, "", false)
|
||||||
|
assert.Equal(t, "", out.String())
|
||||||
|
|
||||||
|
slice := []string{"val1", "val2", "val3"}
|
||||||
|
WriteSliceDesc(pw, slice, "mockLabel", false)
|
||||||
|
expected := "mockLabel:\tval1, val2, val3\n"
|
||||||
|
assert.Equal(t, expected, out.String())
|
||||||
|
|
||||||
|
out.Reset()
|
||||||
|
WriteSliceDesc(pw, slice, "mockLabel", true)
|
||||||
|
expected = "mockLabel:\tval1\n\tval2\n\tval3\n"
|
||||||
|
assert.Equal(t, expected, out.String())
|
||||||
|
|
||||||
|
for i := 4; i <= TruncateAt; i++ {
|
||||||
|
slice = append(slice, fmt.Sprintf("val%d", i))
|
||||||
|
}
|
||||||
|
out.Reset()
|
||||||
|
joined := strings.Join(slice, ", ")[:TruncateAt-4]
|
||||||
|
expected = fmt.Sprintf("mockLabel:\t%s ...\n", joined)
|
||||||
|
WriteSliceDesc(pw, slice, "mockLabel", false)
|
||||||
|
assert.Equal(t, expected, out.String())
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestWriteMetadata(t *testing.T) {
|
||||||
|
var out bytes.Buffer
|
||||||
|
pw := printers.NewBarePrefixWriter(&out)
|
||||||
|
m := &metav1.ObjectMeta{
|
||||||
|
Name: "mockName",
|
||||||
|
Namespace: "mockNamespace",
|
||||||
|
CreationTimestamp: metav1.NewTime(time.Now().Add(-5 * time.Second)),
|
||||||
|
}
|
||||||
|
WriteMetadata(pw, m, false)
|
||||||
|
expected := "Name:\tmockName\nNamespace:\tmockNamespace\nAge:\t5s\n"
|
||||||
|
assert.Equal(t, expected, out.String())
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -18,6 +18,7 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
"gotest.tools/v3/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
// testCommandGenerator generates a test cobra command
|
// testCommandGenerator generates a test cobra command
|
||||||
|
|
@ -45,6 +46,11 @@ func TestGetNamespaceSample(t *testing.T) {
|
||||||
if actualNamespace != expectedNamespace {
|
if actualNamespace != expectedNamespace {
|
||||||
t.Fatalf("Incorrect namespace retrieved: %v, expected: %v", actualNamespace, expectedNamespace)
|
t.Fatalf("Incorrect namespace retrieved: %v, expected: %v", actualNamespace, expectedNamespace)
|
||||||
}
|
}
|
||||||
|
kp = &KnParams{}
|
||||||
|
testCmd = testCommandGenerator(false)
|
||||||
|
actualNamespace, err = kp.GetNamespace(testCmd)
|
||||||
|
assert.NilError(t, err)
|
||||||
|
assert.Equal(t, "default", actualNamespace)
|
||||||
}
|
}
|
||||||
|
|
||||||
// test current namespace without setting any namespace
|
// test current namespace without setting any namespace
|
||||||
|
|
|
||||||
|
|
@ -96,6 +96,9 @@ func TestPrepareConfig(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
_, err = (&KnParams{}).RestConfig()
|
||||||
|
assert.ErrorContains(t, err, "no kubeconfig")
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type typeTestCase struct {
|
type typeTestCase struct {
|
||||||
|
|
@ -408,3 +411,42 @@ func TestNewMessagingClient(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestInitialize(t *testing.T) {
|
||||||
|
params := &KnParams{}
|
||||||
|
params.Initialize()
|
||||||
|
assert.Assert(t, params.NewServingClient != nil)
|
||||||
|
assert.Assert(t, params.NewServingV1alpha1Client != nil)
|
||||||
|
assert.Assert(t, params.NewGitopsServingClient != nil)
|
||||||
|
assert.Assert(t, params.NewSourcesClient != nil)
|
||||||
|
assert.Assert(t, params.NewEventingClient != nil)
|
||||||
|
assert.Assert(t, params.NewMessagingClient != nil)
|
||||||
|
assert.Assert(t, params.NewDynamicClient != nil)
|
||||||
|
|
||||||
|
basic, err := clientcmd.NewClientConfigFromBytes([]byte(BASIC_KUBECONFIG))
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test all clients are not nil
|
||||||
|
params.ClientConfig = basic
|
||||||
|
servingClient, err := params.NewServingClient("mockNamespace")
|
||||||
|
assert.NilError(t, err)
|
||||||
|
assert.Assert(t, servingClient != nil)
|
||||||
|
|
||||||
|
eventingClient, err := params.NewEventingClient("mockNamespace")
|
||||||
|
assert.NilError(t, err)
|
||||||
|
assert.Assert(t, eventingClient != nil)
|
||||||
|
|
||||||
|
gitOpsClient, err := params.NewGitopsServingClient("mockNamespace", "mockDir")
|
||||||
|
assert.NilError(t, err)
|
||||||
|
assert.Assert(t, gitOpsClient != nil)
|
||||||
|
|
||||||
|
messagingClient, err := params.NewMessagingClient("mockNamespace")
|
||||||
|
assert.NilError(t, err)
|
||||||
|
assert.Assert(t, messagingClient != nil)
|
||||||
|
|
||||||
|
sourcesClient, err := params.NewSourcesClient("mockNamespace")
|
||||||
|
assert.NilError(t, err)
|
||||||
|
assert.Assert(t, sourcesClient != nil)
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue