Skip to content

Commit

Permalink
Print logs and events on test failure
Browse files Browse the repository at this point in the history
Problem: When the tests fail we don't have any outputs from the
containers or the k8s events.

Solution: Print logs and outputs on failure.
  • Loading branch information
lucacome committed Aug 8, 2024
1 parent 6e26763 commit a7b7672
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 5 deletions.
6 changes: 3 additions & 3 deletions tests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,17 @@ ifeq ($(PLUS_ENABLED),true)
endif

.PHONY: setup-gcp-and-run-tests
setup-gcp-and-run-tests: create-gke-router create-and-setup-vm run-tests-on-vm ## Create and setup a GKE router and GCP VM for tests and run the functional tests
setup-gcp-and-run-tests: create-and-setup-vm run-tests-on-vm ## Create and setup a GCP VM for tests and run the functional tests

.PHONY: setup-gcp-and-run-nfr-tests
setup-gcp-and-run-nfr-tests: create-gke-router create-and-setup-vm nfr-test ## Create and setup a GKE router and GCP VM for tests and run the NFR tests
setup-gcp-and-run-nfr-tests: create-and-setup-vm nfr-test ## Create and setup a GCP VM for tests and run the NFR tests

.PHONY: create-gke-cluster
create-gke-cluster: ## Create a GKE cluster
./scripts/create-gke-cluster.sh $(CI)

.PHONY: create-and-setup-vm
create-and-setup-vm: ## Create and setup a GCP VM for tests
create-and-setup-vm: create-gke-router ## Create and setup a GKE router and GCP VM for tests
./scripts/create-and-setup-gcp-vm.sh

.PHONY: create-gke-router
Expand Down
52 changes: 52 additions & 0 deletions tests/framework/info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package framework

import (
"fmt"

core "k8s.io/api/core/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
)

func GetLogs(rm ResourceManager, namespace string, releaseName string) string {
var returnLogs string
pods, err := rm.GetPods(namespace, client.MatchingLabels{
"app.kubernetes.io/instance": releaseName,
})
if err != nil {
return fmt.Sprintf("failed to get pods: %v", err)
}
for _, pod := range pods {
for _, container := range pod.Spec.Containers {
returnLogs += fmt.Sprintf("Logs for container %s:\n", container.Name)
logs, err := rm.GetPodLogs(pod.Namespace, pod.Name, &core.PodLogOptions{
Container: container.Name,
})
if err != nil {
returnLogs += fmt.Sprintf(" failed to get logs: %v\n", err)
continue
}
returnLogs += fmt.Sprintf(" %s\n", logs)
}
}
return returnLogs
}

func GetEvents(rm ResourceManager, namespace string) string {
var returnEvents string
events, err := rm.GetEvents(namespace)
if err != nil {
return fmt.Sprintf("failed to get events: %v", err)
}
eventGroups := make(map[string][]core.Event)
for _, event := range events.Items {
eventGroups[event.InvolvedObject.Name] = append(eventGroups[event.InvolvedObject.Name], event)
}
for name, events := range eventGroups {
returnEvents += fmt.Sprintf("Events for %s:\n", name)
for _, event := range events {
returnEvents += fmt.Sprintf(" %s\n", event.Message)
}
returnEvents += "\n"
}
return returnEvents
}
17 changes: 17 additions & 0 deletions tests/framework/resourcemanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,23 @@ func (rm *ResourceManager) GetNGFDeployment(namespace, releaseName string) (*app
return &deployment, nil
}

// GetEvents returns all Events in the specified namespace.
func (rm *ResourceManager) GetEvents(namespace string) (*core.EventList, error) {
ctx, cancel := context.WithTimeout(context.Background(), rm.TimeoutConfig.GetTimeout)
defer cancel()

var eventList core.EventList
if err := rm.K8sClient.List(
ctx,
&eventList,
client.InNamespace(namespace),
); err != nil {
return &core.EventList{}, fmt.Errorf("error getting list of Events: %w", err)
}

return &eventList, nil
}

// ScaleDeployment scales the Deployment to the specified number of replicas.
func (rm *ResourceManager) ScaleDeployment(namespace, name string, replicas int32) error {
ctx, cancel := context.WithTimeout(context.Background(), rm.TimeoutConfig.UpdateTimeout)
Expand Down
20 changes: 18 additions & 2 deletions tests/suite/system_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
apiext "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
k8sRuntime "k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
k8sTypes "k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/kubernetes"
ctlr "sigs.k8s.io/controller-runtime"
Expand Down Expand Up @@ -76,6 +76,7 @@ var (
chartVersion string
clusterInfo framework.ClusterInfo
skipNFRTests bool
logs string
)

const (
Expand Down Expand Up @@ -235,7 +236,7 @@ func teardown(relName string) {
500*time.Millisecond,
true, /* poll immediately */
func(ctx context.Context) (bool, error) {
key := types.NamespacedName{Name: ngfNamespace}
key := k8sTypes.NamespacedName{Name: ngfNamespace}
if err := k8sClient.Get(ctx, key, &core.Namespace{}); err != nil && apierrors.IsNotFound(err) {
return true, nil
}
Expand Down Expand Up @@ -292,6 +293,11 @@ var _ = AfterSuite(func() {
if skipNFRTests {
Skip("")
}
events := framework.GetEvents(resourceManager, ngfNamespace)
AddReportEntry("Events", events, ReportEntryVisibilityNever)

logs = framework.GetLogs(resourceManager, ngfNamespace, releaseName)
AddReportEntry("Logs", logs, ReportEntryVisibilityNever)

labelFilter := GinkgoLabelFilter()
if !strings.Contains(labelFilter, "longevity-setup") {
Expand All @@ -311,3 +317,13 @@ func isNFR(labelFilter string) bool {
strings.Contains(labelFilter, "upgrade") ||
strings.Contains(labelFilter, "scale")
}

var _ = ReportAfterSuite("Print info on failure", func(report Report) {
if !report.SuiteSucceeded {
for _, specReport := range report.SpecReports {
for _, entry := range specReport.ReportEntries {
fmt.Println(entry.GetRawValue())
}
}
}
})

0 comments on commit a7b7672

Please sign in to comment.