mirror of https://github.com/knative/func.git
feat: add labels to func describe (#2882)
return typed error on describe of uninitialized Function adds describe tests
This commit is contained in:
parent
f9bf9fe9cc
commit
58fb81a41a
|
@ -3,6 +3,7 @@ package cmd
|
|||
import (
|
||||
"encoding/json"
|
||||
"encoding/xml"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
|
@ -81,6 +82,9 @@ func runDescribe(cmd *cobra.Command, args []string, newClient ClientFactory) (er
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !f.Initialized() {
|
||||
return errors.New("function not found at this path and no name provided")
|
||||
}
|
||||
details, err = client.Describe(cmd.Context(), "", "", f)
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -153,6 +157,13 @@ func (i info) Human(w io.Writer) error {
|
|||
fmt.Fprintf(w, " %v %v %v\n", s.Source, s.Type, s.Broker)
|
||||
}
|
||||
}
|
||||
|
||||
if len(i.Labels) > 0 {
|
||||
fmt.Fprintln(w, "Labels:")
|
||||
for k, v := range i.Labels {
|
||||
fmt.Fprintf(w, " %v: %v\n", k, v)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -170,6 +181,12 @@ func (i info) Plain(w io.Writer) error {
|
|||
fmt.Fprintf(w, "Subscription %v %v %v\n", s.Source, s.Type, s.Broker)
|
||||
}
|
||||
}
|
||||
|
||||
if len(i.Labels) > 0 {
|
||||
for k, v := range i.Labels {
|
||||
fmt.Fprintf(w, "Label %v %v\n", k, v)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
|
@ -2,6 +2,7 @@ package cmd
|
|||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
fn "knative.dev/func/pkg/functions"
|
||||
|
@ -9,6 +10,58 @@ import (
|
|||
. "knative.dev/func/pkg/testing"
|
||||
)
|
||||
|
||||
// TestDescribe_Default ensures that running describe when there is no
|
||||
// function in the given directory fails correctly.
|
||||
func TestDescribe_Default(t *testing.T) {
|
||||
_ = FromTempDirectory(t)
|
||||
describer := mock.NewDescriber()
|
||||
|
||||
cmd := NewDescribeCmd(NewTestClient(fn.WithDescriber(describer)))
|
||||
cmd.SetArgs([]string{})
|
||||
err := cmd.Execute()
|
||||
|
||||
if err == nil {
|
||||
t.Fatal("describing a nonexistent function should error")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "function not found at this path and no name provided") {
|
||||
t.Fatalf("Unexpected error text returned: %v", err)
|
||||
}
|
||||
if describer.DescribeInvoked {
|
||||
t.Fatal("Describer incorrectly invoked")
|
||||
}
|
||||
}
|
||||
|
||||
// TestDescribe_Undeployed ensures that describing a function which exists,
|
||||
// but has not been deployed, does not error but rather delegates to the
|
||||
// deployer which will presumably describe it as being !deployed (See deployer
|
||||
// test suite)
|
||||
func TestDescribe_Undeployed(t *testing.T) {
|
||||
root := FromTempDirectory(t)
|
||||
|
||||
client := fn.New()
|
||||
_, err := client.Init(fn.Function{
|
||||
Name: "testfunc",
|
||||
Runtime: "go",
|
||||
Registry: TestRegistry,
|
||||
Root: root,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
describer := mock.NewDescriber()
|
||||
|
||||
cmd := NewDescribeCmd(NewTestClient(fn.WithDescriber(describer)))
|
||||
cmd.SetArgs([]string{})
|
||||
if err := cmd.Execute(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if !describer.DescribeInvoked {
|
||||
t.Fatal("Describer should have been invoked for any initialized function")
|
||||
}
|
||||
}
|
||||
|
||||
// TestDescribe_ByName ensures that describing a function by name invokes
|
||||
// the describer appropriately.
|
||||
func TestDescribe_ByName(t *testing.T) {
|
||||
|
|
|
@ -973,10 +973,10 @@ func (c *Client) Describe(ctx context.Context, name, namespace string, f Functio
|
|||
return c.describer.Describe(ctx, name, namespace)
|
||||
}
|
||||
|
||||
// If the function's not initialized, then we can save some time and
|
||||
// fail fast.
|
||||
// Desribe Current Function
|
||||
// ------------------------
|
||||
if !f.Initialized() {
|
||||
return d, fmt.Errorf("function not initialized: %v", f.Root)
|
||||
return d, NewErrNotInitialized(f.Root)
|
||||
}
|
||||
|
||||
// If the function is undeployed, we can't describe it either.
|
||||
|
@ -984,6 +984,9 @@ func (c *Client) Describe(ctx context.Context, name, namespace string, f Functio
|
|||
return d, fmt.Errorf("unable to describe without a name. %v", ErrNameRequired)
|
||||
}
|
||||
|
||||
// If it has a populated deployed namespace, we can presume it's deployed
|
||||
// and attempt to describe.
|
||||
|
||||
return c.describer.Describe(ctx, f.Name, f.Deploy.Namespace)
|
||||
}
|
||||
|
||||
|
|
|
@ -98,5 +98,10 @@ func (d *Describer) Describe(ctx context.Context, name, namespace string) (descr
|
|||
|
||||
description.Subscriptions = subscriptions
|
||||
|
||||
// Populate labels from the service
|
||||
if service.Labels != nil {
|
||||
description.Labels = service.Labels
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue