Skip to content

Commit

Permalink
dump events in sandbox namespace after each test run
Browse files Browse the repository at this point in the history
  • Loading branch information
freehan committed Jul 23, 2019
1 parent c557136 commit c814c8e
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pkg/e2e/framework.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,8 @@ func (f *Framework) RunWithSandbox(name string, t *testing.T, testFunc func(*tes
}

testFunc(t, sandbox)

sandbox.DumpSandboxInfo(t)
})
}

Expand Down
39 changes: 39 additions & 0 deletions pkg/e2e/sandbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import (
"k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/ingress-gce/pkg/fuzz"
"sort"
"testing"
)

// Sandbox represents a sandbox for running tests in a Kubernetes cluster.
Expand Down Expand Up @@ -93,3 +95,40 @@ func (s *Sandbox) MasterUpgraded() bool {
func (s *Sandbox) MasterUpgrading() bool {
return s.f.statusManager.masterUpgrading()
}

// DumpSandboxInfo dumps information about the sandbox into logs
func (s *Sandbox) DumpSandboxInfo(t *testing.T) {
s.dumpSandboxEvents(t)
}

// dumpSandboxEvents dumps the events happened in the sandbox namespace into logs
func (s *Sandbox) dumpSandboxEvents(t *testing.T) {
t.Logf("Collecting events from namespace %q.", s.Namespace)
events, err := s.f.Clientset.CoreV1().Events(s.Namespace).List(metav1.ListOptions{})
if err != nil {
t.Logf("Failed to list events in namespace %q", s.Namespace)
return
}
t.Logf("Found %d events.", len(events.Items))
// Sort events by their first timestamp
sortedEvents := events.Items
if len(sortedEvents) > 1 {
sort.Sort(byFirstTimestamp(sortedEvents))
}
for _, e := range sortedEvents {
t.Logf("At %v - event for %v/%v: %v %v: %v", e.FirstTimestamp, e.Namespace, e.InvolvedObject.Name, e.Source, e.Reason, e.Message)
}
}

// byFirstTimestamp sorts a slice of events by first timestamp, using their involvedObject's name as a tie breaker.
type byFirstTimestamp []v1.Event

func (o byFirstTimestamp) Len() int { return len(o) }
func (o byFirstTimestamp) Swap(i, j int) { o[i], o[j] = o[j], o[i] }

func (o byFirstTimestamp) Less(i, j int) bool {
if o[i].FirstTimestamp.Equal(&o[j].FirstTimestamp) {
return o[i].InvolvedObject.Name < o[j].InvolvedObject.Name
}
return o[i].FirstTimestamp.Before(&o[j].FirstTimestamp)
}

0 comments on commit c814c8e

Please sign in to comment.