Skip to content

Commit

Permalink
Update e2e tests to refer to exec log on failure
Browse files Browse the repository at this point in the history
  • Loading branch information
cardil committed Oct 24, 2024
1 parent 86f6767 commit 52f8f71
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 64 deletions.
1 change: 0 additions & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ jobs:
matrix:
go-version:
- '1.22'
- '1.23'

steps:

Expand Down
8 changes: 6 additions & 2 deletions internal/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,19 @@ func (a *App) Command() *cobra.Command {
}

func (a *App) setGlobalFlags(c *cobra.Command) {
c.PersistentFlags().BoolVarP(
fl := c.PersistentFlags()
fl.BoolVarP(
&a.Verbose, "verbose", "v",
false, "verbose output",
)
c.PersistentFlags().VarP(
fl.VarP(
enumflag.New(&a.OutputMode, "output", outputModeIDs(), enumflag.EnumCaseInsensitive),
"output", "o",
"OutputMode format. One of: human|json|yaml.",
)
// TODO: config.BootstrapConfig should allow to add bootstrap flags to command
_ = fl.String("config", "", "")
_ = fl.MarkHidden("config")
}

var _ commandline.CobraProvider = new(App)
Expand Down
1 change: 1 addition & 0 deletions test/e2e-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

set -Eeo pipefail

# shellcheck disable=SC1090
source "$(go run knative.dev/hack/cmd/script e2e-tests.sh)"

function start_latest_knative_serving() {
Expand Down
76 changes: 15 additions & 61 deletions test/e2e/ics_send.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,27 @@ package e2e
import (
"context"
"fmt"
"strings"
"os"
"path"

cloudevents "github.com/cloudevents/sdk-go/v2"
cetest "github.com/cloudevents/sdk-go/v2/test"
"github.com/stretchr/testify/assert"
"gotest.tools/v3/icmd"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
configdir "knative.dev/client/pkg/config/dir"
"knative.dev/kn-plugin-event/test"
kubeclient "knative.dev/pkg/client/injection/kube/client"
"knative.dev/pkg/logging"
"knative.dev/reconciler-test/pkg/environment"
"knative.dev/reconciler-test/pkg/eventshub"
eventshubassert "knative.dev/reconciler-test/pkg/eventshub/assert"
"knative.dev/reconciler-test/pkg/feature"
"sigs.k8s.io/yaml"
)

const (
issue228Warn = "child pods are preserved by default when jobs are deleted; " +
"set propagationPolicy=Background to remove them or set " +
"propagationPolicy=Orphan to suppress this warning"
dirPerm = 0o750
)

// SendEventFeature will create a feature.Feature that will test sending an
Expand Down Expand Up @@ -63,14 +63,24 @@ func sendEvent(ev cloudevents.Event, sink Sink) feature.StepFn {
"--to", sink.String(),
}
cmd := test.ResolveKnEventCommand(t).ToIcmd(args...)
artifacts := os.Getenv("ARTIFACTS")
if artifacts == "" {
artifacts = os.TempDir()
}
cacheDir := path.Join(artifacts, t.Name())
if err := os.MkdirAll(cacheDir, dirPerm); err != nil {
t.Fatal(err)
}
cmd.Env = append(os.Environ(), configdir.CacheDirEnvName+"="+cacheDir)
log = log.With(json("cmd", cmd))
log.Info("Running")
result := icmd.RunCmd(cmd)
if err := result.Compare(icmd.Expected{
ExitCode: 0,
Err: fmt.Sprintf("Event (ID: %s) have been sent.", ev.ID()),
}); err != nil {
handleSendErr(ctx, t, err, ev)
t.Fatal(err, "\n\nExecution log: "+
path.Join(cacheDir, "last-exec.log.jsonl"))
}
assert.NotContains(t, result.Stderr(), issue228Warn)
log.Info("Succeeded")
Expand All @@ -82,59 +92,3 @@ func receiveEvent(ev cloudevents.Event, sinkName string) feature.StepFn {
MatchEvent(cetest.HasId(ev.ID())).
Exact(1)
}

// handleSendErr will handle the error from sending event.
func handleSendErr(ctx context.Context, t feature.T, err error, ev cloudevents.Event) {
// TODO: most of this code should be moved to production CLI, so that in case
// of send error, a nice, report is produced.
// See: https://github.com/knative-extensions/kn-plugin-event/issues/129
if err == nil {
return
}
kube := kubeclient.Get(ctx)
ns := environment.FromContext(ctx).Namespace()
log := logging.FromContext(ctx)
jobs := kube.BatchV1().Jobs(ns)
pods := kube.CoreV1().Pods(ns)
events := kube.CoreV1().Events(ns)
jlist, kerr := jobs.List(ctx, metav1.ListOptions{
LabelSelector: "event-id=" + ev.ID(),
})
if kerr != nil {
log.Error(kerr)
}
if len(jlist.Items) != 1 {
t.Fatal(err)
}
jobName := jlist.Items[0].Name
plist, kerr := pods.List(ctx, metav1.ListOptions{
LabelSelector: "job-name=" + jobName,
})
if kerr != nil {
log.Error(kerr)
}
podLogs := make([]string, 0, len(plist.Items))
for _, item := range plist.Items {
var bytes []byte
bytes, kerr = pods.GetLogs(item.Name, nil).DoRaw(ctx)
if kerr != nil {
log.Error(kerr)
}
podLogs = append(podLogs, string(bytes))
}
podsYaml, merr := yaml.Marshal(plist.Items)
if merr != nil {
log.Error(merr)
}
elist, eerr := events.List(ctx, metav1.ListOptions{})
if eerr != nil {
log.Error(eerr)
}
eventsYaml, eerr := yaml.Marshal(elist.Items)
if eerr != nil {
log.Error(eerr)
}
t.Fatal(err, "\n\nJob logs (", len(plist.Items), "):\n",
strings.Join(podLogs, "\n---\n"), "\n\nPods:\n",
string(podsYaml), "\n\nEvents:\n", string(eventsYaml))
}

0 comments on commit 52f8f71

Please sign in to comment.