Skip to content

Commit

Permalink
Adds unit tests for list-types command processing
Browse files Browse the repository at this point in the history
  • Loading branch information
navidshaikh committed Dec 9, 2019
1 parent 0f51038 commit f526899
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 7 deletions.
2 changes: 1 addition & 1 deletion pkg/kn/commands/source/human_readable_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func printSourceTypes(sourceType unstructured.Unstructured, options hprinters.Pr
}

if !found {
return nil, fmt.Errorf("can't find kind of CRD for %s", name)
return nil, fmt.Errorf("can't find specs.names.kind for %s", name)
}

row := metav1beta1.TableRow{
Expand Down
75 changes: 75 additions & 0 deletions pkg/kn/commands/source/list_types_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// Copyright © 2019 The Knative Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package source

import (
"gotest.tools/assert"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"knative.dev/client/pkg/kn/commands"
"knative.dev/client/pkg/util"

"strings"
"testing"
)

func newUnstructured(name string) *unstructured.Unstructured {
return &unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": "apiextensions.k8s.io/v1beta1",
"kind": "CustomResourceDefinition",
"metadata": map[string]interface{}{
"namespace": "current",
"name": name,
"labels": map[string]interface{}{
"duck.knative.dev/source": "true",
},
},
},
}
}

func newUnstructuredWithSpecNames(name string, value map[string]interface{}) *unstructured.Unstructured {
u := newUnstructured(name)
u.Object["spec"] = map[string]interface{}{"names": value}
return u
}

func fakeListTypes(args []string, objects ...runtime.Object) (output []string, err error) {
knParams := &commands.KnParams{}
// not using the fake dynamic client returned here
cmd, _, buf := commands.CreateDynamicTestKnCommand(NewSourceCommand(knParams), knParams, objects...)

cmd.SetArgs(args)
err = cmd.Execute()
if err != nil {
return
}

output = strings.Split(buf.String(), "\n")
return
}

func TestSourceListTypes(t *testing.T) {
output, err := fakeListTypes([]string{"source", "list-types"},
newUnstructuredWithSpecNames("foo.in", map[string]interface{}{"kind": "foo"}),
newUnstructuredWithSpecNames("bar.in", map[string]interface{}{"kind": "bar"}),
)

assert.NilError(t, err)
assert.Check(t, util.ContainsAll(output[0], "TYPE", "NAME", "DESCRIPTION"))
assert.Check(t, util.ContainsAll(output[1], "foo", "foo.in"))
assert.Check(t, util.ContainsAll(output[2], "bar", "bar.in"))
}
18 changes: 17 additions & 1 deletion pkg/kn/commands/testing_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/viper"
"gotest.tools/assert"
"k8s.io/apimachinery/pkg/runtime"
client_testing "k8s.io/client-go/testing"
"knative.dev/client/pkg/kn/flags"

"knative.dev/client/pkg/serving/v1alpha1"
"knative.dev/serving/pkg/client/clientset/versioned/typed/serving/v1alpha1/fake"

dynamic_fake "k8s.io/client-go/dynamic/fake"
dynamic_kn "knative.dev/client/pkg/dynamic"
sources_client "knative.dev/client/pkg/eventing/sources/v1alpha1"
sources_fake "knative.dev/eventing/pkg/client/clientset/versioned/typed/sources/v1alpha1/fake"
)
Expand Down Expand Up @@ -70,6 +72,20 @@ func CreateSourcesTestKnCommand(cmd *cobra.Command, knParams *KnParams) (*cobra.
return knCommand, fakeEventing, buf
}

func CreateDynamicTestKnCommand(cmd *cobra.Command, knParams *KnParams, objects ...runtime.Object) (*cobra.Command, *dynamic_fake.FakeDynamicClient, *bytes.Buffer) {
buf := new(bytes.Buffer)
fakeDynamic := dynamic_fake.NewSimpleDynamicClient(runtime.NewScheme(), objects...)
knParams.Output = buf
knParams.NewDynamicClient = func(namespace string) (dynamic_kn.KnDynamicClient, error) {
return dynamic_kn.NewKnDynamicClient(fakeDynamic, FakeNamespace), nil
}

knParams.fixedCurrentNamespace = FakeNamespace
knCommand := NewKnTestCommand(cmd, knParams)
return knCommand, fakeDynamic, buf

}

// CaptureStdout collects the current content of os.Stdout
func CaptureStdout(t *testing.T) {
oldStdout = os.Stdout
Expand Down
10 changes: 5 additions & 5 deletions vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -400,10 +400,10 @@ k8s.io/apimachinery/pkg/apis/meta/v1/unstructured
k8s.io/apimachinery/pkg/labels
k8s.io/apimachinery/pkg/runtime/schema
k8s.io/apimachinery/pkg/api/errors
k8s.io/apimachinery/pkg/runtime
k8s.io/apimachinery/pkg/util/duration
k8s.io/apimachinery/pkg/api/resource
k8s.io/apimachinery/pkg/apis/meta/v1beta1
k8s.io/apimachinery/pkg/runtime
k8s.io/apimachinery/pkg/util/sets
k8s.io/apimachinery/pkg/api/meta
k8s.io/apimachinery/pkg/util/runtime
Expand All @@ -420,13 +420,13 @@ k8s.io/apimachinery/pkg/runtime/serializer/json
k8s.io/apimachinery/pkg/runtime/serializer/streaming
k8s.io/apimachinery/pkg/runtime/serializer/versioning
k8s.io/apimachinery/pkg/util/validation/field
k8s.io/apimachinery/pkg/conversion/queryparams
k8s.io/apimachinery/pkg/util/errors
k8s.io/apimachinery/pkg/util/naming
k8s.io/apimachinery/pkg/util/net
k8s.io/apimachinery/pkg/util/strategicpatch
k8s.io/apimachinery/pkg/util/errors
k8s.io/apimachinery/pkg/api/equality
k8s.io/apimachinery/pkg/api/validation
k8s.io/apimachinery/pkg/conversion/queryparams
k8s.io/apimachinery/pkg/util/naming
k8s.io/apimachinery/pkg/util/yaml
k8s.io/apimachinery/pkg/util/wait
k8s.io/apimachinery/third_party/forked/golang/reflect
Expand Down Expand Up @@ -457,12 +457,12 @@ k8s.io/cli-runtime/pkg/kustomize/k8sdeps/transformer/patch
k8s.io/cli-runtime/pkg/kustomize/k8sdeps/kv
# k8s.io/client-go v0.0.0-20191016110837-54936ba21026
k8s.io/client-go/dynamic
k8s.io/client-go/dynamic/fake
k8s.io/client-go/rest
k8s.io/client-go/testing
k8s.io/client-go/tools/clientcmd
k8s.io/client-go/plugin/pkg/client/auth/gcp
k8s.io/client-go/plugin/pkg/client/auth/oidc
k8s.io/client-go/dynamic/fake
k8s.io/client-go/pkg/version
k8s.io/client-go/plugin/pkg/client/auth/exec
k8s.io/client-go/rest/watch
Expand Down

0 comments on commit f526899

Please sign in to comment.