Skip to content

Commit

Permalink
Update EventListener logs command
Browse files Browse the repository at this point in the history
This will update eventlistener logs cmd to
be compatible with both v1alpha1 and v1beta1

Signed-off-by: vinamra28 <[email protected]>
  • Loading branch information
vinamra28 committed Sep 30, 2021
1 parent f75438f commit 4afb94c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 24 deletions.
22 changes: 7 additions & 15 deletions pkg/cmd/eventlistener/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

"github.com/spf13/cobra"
"github.com/tektoncd/cli/pkg/cli"
"github.com/tektoncd/cli/pkg/eventlistener"
"github.com/tektoncd/cli/pkg/formatted"
"github.com/tektoncd/cli/pkg/options"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -56,7 +57,12 @@ Show 2 lines of most recent logs from all EventListener pods:
return fmt.Errorf("tail cannot be 0 or less than 0 unless -1 for all pods")
}

err := getEventListener(args[0], p)
cs, err := p.Clients()
if err != nil {
return fmt.Errorf("failed to create tekton client")
}

_, err = eventlistener.Get(cs, args[0], metav1.GetOptions{}, p.Namespace())
if err != nil {
return err
}
Expand All @@ -73,20 +79,6 @@ Show 2 lines of most recent logs from all EventListener pods:
return c
}

func getEventListener(elName string, p cli.Params) error {
cs, err := p.Clients()
if err != nil {
return fmt.Errorf("failed to create tekton client")
}

_, err = cs.Triggers.TriggersV1alpha1().EventListeners(p.Namespace()).Get(context.Background(), elName, metav1.GetOptions{})
if err != nil {
return fmt.Errorf("failed to get EventListener %s: %v", elName, err)
}

return nil
}

func logs(elName string, p cli.Params, s *cli.Stream, opts *options.LogOptions) error {
cs, err := p.Clients()
if err != nil {
Expand Down
28 changes: 19 additions & 9 deletions pkg/cmd/eventlistener/logs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,20 @@ import (
"github.com/jonboulle/clockwork"
"github.com/spf13/cobra"
"github.com/tektoncd/cli/pkg/test"
cb "github.com/tektoncd/cli/pkg/test/builder"
testDynamic "github.com/tektoncd/cli/pkg/test/dynamic"
"github.com/tektoncd/triggers/pkg/apis/triggers/v1beta1"
v1alpha1 "github.com/tektoncd/triggers/pkg/apis/triggers/v1beta1"
triggertest "github.com/tektoncd/triggers/test"
"gotest.tools/v3/golden"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
)

func TestLogsEventListener(t *testing.T) {
now := time.Now()

els := []*v1alpha1.EventListener{
els := []*v1beta1.EventListener{
{
ObjectMeta: metav1.ObjectMeta{
Name: "eventlistener-no-pods",
Expand All @@ -29,47 +33,41 @@ func TestLogsEventListener(t *testing.T) {

tests := []struct {
name string
command *cobra.Command
args []string
wantError bool
goldenFile bool
want string
}{
{
name: "No arguments passed",
command: commandLogs(t, els, now),
args: []string{"logs"},
wantError: true,
goldenFile: false,
want: "accepts 1 arg(s), received 0",
},
{
name: "No EventListener found",
command: commandLogs(t, els, now),
args: []string{"logs", "notFound"},
wantError: true,
goldenFile: false,
want: "failed to get EventListener notFound: eventlisteners.triggers.tekton.dev \"notFound\" not found",
},
{
name: "No EventListener pods",
command: commandLogs(t, els, now),
args: []string{"logs", "eventlistener-no-pods"},
wantError: false,
goldenFile: false,
want: "No pods available for EventListener eventlistener-no-pods\n",
},
{
name: "Tail option as 0 results in error",
command: commandLogs(t, els, now),
args: []string{"logs", "eventlistener-no-pods", "-t", "0"},
wantError: true,
goldenFile: false,
want: "tail cannot be 0 or less than 0 unless -1 for all pods",
},
{
name: "Tail option as -2 results in error",
command: commandLogs(t, els, now),
args: []string{"logs", "eventlistener-no-pods", "-t", "-2"},
wantError: true,
goldenFile: false,
Expand All @@ -79,7 +77,7 @@ func TestLogsEventListener(t *testing.T) {

for _, td := range tests {
t.Run(td.name, func(t *testing.T) {
got, err := test.ExecuteCommand(td.command, td.args...)
got, err := test.ExecuteCommand(commandLogs(t, els, now), td.args...)

if err != nil && !td.wantError {
t.Errorf("Unexpected error: %v", err)
Expand All @@ -100,6 +98,18 @@ func TestLogsEventListener(t *testing.T) {
func commandLogs(t *testing.T, els []*v1alpha1.EventListener, now time.Time) *cobra.Command {
clock := clockwork.NewFakeClockAt(now)
cs := test.SeedTestResources(t, triggertest.Resources{EventListeners: els})
p := &test.Params{Tekton: cs.Pipeline, Clock: clock, Kube: cs.Kube, Triggers: cs.Triggers}
cs.Triggers.Resources = cb.TriggersAPIResourceList("v1beta1", []string{"eventlistener"})
tdc := testDynamic.Options{}
var utts []runtime.Object
for _, el := range els {
utts = append(utts, cb.UnstructuredV1beta1EL(el, "v1beta1"))
}
dc, err := tdc.Client(utts...)
if err != nil {
t.Errorf("unable to create dynamic client: %v", err)
}

p := &test.Params{Tekton: cs.Pipeline, Clock: clock, Kube: cs.Kube, Triggers: cs.Triggers, Dynamic: dc}

return Command(p)
}

0 comments on commit 4afb94c

Please sign in to comment.