Skip to content

Commit

Permalink
Nit (#4385)
Browse files Browse the repository at this point in the history
Signed-off-by: Francesco Guardiani <francescoguard@gmail.com>
  • Loading branch information
slinkydeveloper authored Oct 26, 2020
1 parent 8dca90e commit 3ceaad4
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 10 deletions.
28 changes: 23 additions & 5 deletions test/lib/recordevents/event_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,32 @@ type EventInfo struct {
Sequence uint64 `json:"sequence"`
}

// Pretty print the event. Meant for debugging. This formats the validation error
// or the full event as appropriate. This does NOT format the headers.
// Pretty print the event. Meant for debugging.
func (ei *EventInfo) String() string {
var sb strings.Builder
sb.WriteString("-- EventInfo --\n")
if ei.Event != nil {
return ei.Event.String()
} else {
return fmt.Sprintf("invalid event %q", ei.Error)
sb.WriteString("--- Event ---\n")
sb.WriteString(ei.Event.String())
sb.WriteRune('\n')
sb.WriteRune('\n')
}
if ei.Error != "" {
sb.WriteString("--- Error ---\n")
sb.WriteString(ei.Error)
sb.WriteRune('\n')
sb.WriteRune('\n')
}
sb.WriteString("--- HTTP headers ---\n")
for k, v := range ei.HTTPHeaders {
sb.WriteString(" " + k + ": " + v[0] + "\n")
}
sb.WriteRune('\n')
sb.WriteString("--- Origin: '" + ei.Origin + "' ---\n")
sb.WriteString("--- Observer: '" + ei.Observer + "' ---\n")
sb.WriteString("--- Time: " + ei.Time.String() + " ---\n")
sb.WriteString(fmt.Sprintf("--- Sequence: %d ---\n", ei.Sequence))
return sb.String()
}

// This is mainly used for providing better failure messages
Expand Down
6 changes: 5 additions & 1 deletion test/lib/recordevents/observer/observer.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ type Observer struct {

type envConfig struct {
// ObserverName is used to identify this instance of the observer.
ObserverName string `envconfig:"OBSERVER_NAME" default:"observer-default" required:"true"`
ObserverName string `envconfig:"POD_NAME" default:"observer-default" required:"true"`

// Reply is used to define if the observer should reply back
Reply bool `envconfig:"REPLY" default:"false" required:"false"`
Expand Down Expand Up @@ -120,6 +120,10 @@ func (o *Observer) ServeHTTP(writer http.ResponseWriter, request *http.Request)

event, eventErr := cloudeventsbindings.ToEvent(context.TODO(), m)
header := request.Header
// Host header is removed from the request.Header map by net/http
if request.Host != "" {
header.Set("Host", request.Host)
}

eventErrStr := ""
if eventErr != nil {
Expand Down
3 changes: 0 additions & 3 deletions test/lib/recordevents/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,6 @@ func recordEventsPod(imageName string, name string, serviceAccountName string) *
ValueFrom: &corev1.EnvVarSource{
FieldRef: &corev1.ObjectFieldSelector{FieldPath: "metadata.namespace"},
},
}, {
Name: "OBSERVER",
Value: "recorder-" + name,
}, {
Name: "POD_NAME",
ValueFrom: &corev1.EnvVarSource{
Expand Down
20 changes: 19 additions & 1 deletion test/lib/test_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"fmt"
"path/filepath"
"sort"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -193,7 +194,24 @@ func TearDown(client *Client) {
if err != nil {
client.T.Logf("Could not list events in the namespace %q: %v", client.Namespace, err)
} else {
for _, e := range el.Items {
// Elements has to be ordered first
items := el.Items
sort.SliceStable(items, func(i, j int) bool {
// Some events might not contain last timestamp, in that case we fallback to event time
iTime := items[i].LastTimestamp.Time
if iTime.IsZero() {
iTime = items[i].EventTime.Time
}

jTime := items[j].LastTimestamp.Time
if jTime.IsZero() {
jTime = items[j].EventTime.Time
}

return iTime.Before(jTime)
})

for _, e := range items {
client.T.Log(formatEvent(&e))
}
}
Expand Down

0 comments on commit 3ceaad4

Please sign in to comment.