Skip to content

Commit

Permalink
Dump all build-pipeline CRDs when a test fails
Browse files Browse the repository at this point in the history
This will cause the `teardown` function to dump all Knative CRD objects
for the currently configured namepsace to YAML. This will make debugging
failed builds easier.

Fixes #145.
  • Loading branch information
Tanner Bruce committed Oct 29, 2018
1 parent d700b36 commit 28780d2
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 17 deletions.
4 changes: 2 additions & 2 deletions test/helm_task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ func TestHelmDeployPipelineRun(t *testing.T) {
c, namespace := setup(t, logger)
setupClusterBindingForHelm(c, t, namespace, logger)

knativetest.CleanupOnInterrupt(func() { tearDown(logger, c.KubeClient, namespace) }, logger)
defer tearDown(logger, c.KubeClient, namespace)
knativetest.CleanupOnInterrupt(func() { tearDown(t, logger, c, namespace) }, logger)
defer tearDown(t, logger, c, namespace)

logger.Infof("Creating Git PipelineResource %s", sourceResourceName)
if _, err := c.PipelineResourceClient.Create(getGoHelloworldGitResource(namespace)); err != nil {
Expand Down
103 changes: 98 additions & 5 deletions test/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ package test

import (
"flag"
"fmt"
"github.com/ghodss/yaml"
"os"
"strings"
"testing"

knativetest "github.com/knative/pkg/test"
Expand All @@ -44,13 +47,34 @@ func setup(t *testing.T, logger *logging.BaseLogger) (*clients, string) {
return c, namespace
}

func tearDown(logger *logging.BaseLogger, kubeClient *knativetest.KubeClient, namespace string) {
if kubeClient != nil {
logger.Infof("Deleting namespace %s", namespace)
if err := kubeClient.Kube.CoreV1().Namespaces().Delete(namespace, &metav1.DeleteOptions{}); err != nil {
logger.Errorf("Failed to delete namespace %s: %s", namespace, err)
func header(logger *logging.BaseLogger, text string) {
left := "### "
right := " ###"
txt := left + text + right
bar := strings.Repeat("#", len(txt))
logger.Info(bar)
logger.Info(txt)
logger.Info(bar)
}

func tearDown(t *testing.T, logger *logging.BaseLogger, cs *clients, namespace string) {
if cs.KubeClient == nil {
return
}
if t.Failed() {
header(logger, fmt.Sprintf("Dumping objects from %s", namespace))
bs, err := getCRDYaml(cs)
if err != nil {
logger.Error(err)
} else {
logger.Info(string(bs))
}
}

logger.Infof("Deleting namespace %s", namespace)
if err := cs.KubeClient.Kube.CoreV1().Namespaces().Delete(namespace, &metav1.DeleteOptions{}); err != nil {
logger.Errorf("Failed to delete namespace %s: %s", namespace, err)
}
}

func initializeLogsAndMetrics() {
Expand Down Expand Up @@ -83,3 +107,72 @@ func TestMain(m *testing.M) {
c := m.Run()
os.Exit(c)
}

func getCRDYaml(cs *clients) ([]byte, error) {
var output []byte
printOrAdd := func(kind, name string, i interface{}) {
bs, err := yaml.Marshal(i)
if err != nil {
return
}
output = append(output, []byte("\n---\n")...)
output = append(output, bs...)
}

ps, err := cs.PipelineClient.List(metav1.ListOptions{})
if err != nil {
return nil, err
}
for _, i := range ps.Items {
printOrAdd("Pipeline", i.Name, i)
}

pps, err := cs.PipelineParamsClient.List(metav1.ListOptions{})
if err != nil {
return nil, err
}
for _, i := range pps.Items {
printOrAdd("PipelineParamses", i.Name, i)
}

bds, err := cs.BuildClient.List(metav1.ListOptions{})
if err != nil {
return nil, err
}
for _, i := range bds.Items {
printOrAdd("Build", i.Name, i)
}

prs, err := cs.PipelineResourceClient.List(metav1.ListOptions{})
if err != nil {
return nil, err
}
for _, i := range prs.Items {
printOrAdd("PipelineResource", i.Name, i)
}

prrs, err := cs.PipelineRunClient.List(metav1.ListOptions{})
if err != nil {
return nil, err
}
for _, i := range prrs.Items {
printOrAdd("PipelineRun", i.Name, i)
}

ts, err := cs.TaskClient.List(metav1.ListOptions{})
if err != nil {
return nil, err
}
for _, i := range ts.Items {
printOrAdd("Task", i.Name, i)
}
trs, err := cs.TaskRunClient.List(metav1.ListOptions{})
if err != nil {
return nil, err
}
for _, i := range trs.Items {
printOrAdd("TaskRun", i.Name, i)
}
return output, nil
}

4 changes: 2 additions & 2 deletions test/kaniko_task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,8 @@ func TestKanikoTaskRun(t *testing.T) {
t.Errorf("Expected to get docker repo")
}

knativetest.CleanupOnInterrupt(func() { tearDown(logger, c.KubeClient, namespace) }, logger)
defer tearDown(logger, c.KubeClient, namespace)
knativetest.CleanupOnInterrupt(func() { tearDown(t, logger, c, namespace) }, logger)
defer tearDown(t, logger, c, namespace)

hasSecretConfig, err := createSecret(c.KubeClient, namespace)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions test/pipeline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ func TestPipeline(t *testing.T) {
logger := logging.GetContextLogger(t.Name())
c, namespace := setup(t, logger)

knativetest.CleanupOnInterrupt(func() { tearDown(logger, c.KubeClient, namespace) }, logger)
defer tearDown(logger, c.KubeClient, namespace)
knativetest.CleanupOnInterrupt(func() { tearDown(t, logger, c, namespace) }, logger)
defer tearDown(t, logger, c, namespace)

p, err := c.PipelineClient.List(metav1.ListOptions{})
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions test/pipelinerun_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ func TestPipelineRun(t *testing.T) {
logger := logging.GetContextLogger(t.Name())
c, namespace := setup(t, logger)

knativetest.CleanupOnInterrupt(func() { tearDown(logger, c.KubeClient, namespace) }, logger)
defer tearDown(logger, c.KubeClient, namespace)
knativetest.CleanupOnInterrupt(func() { tearDown(t, logger, c, namespace) }, logger)
defer tearDown(t, logger, c, namespace)

logger.Infof("Creating Pipeline Resources in namespace %s", namespace)
if _, err := c.TaskClient.Create(getHelloWorldTask(namespace, []string{"echo", taskOutput})); err != nil {
Expand Down Expand Up @@ -102,8 +102,8 @@ func TestPipelineRun_WithServiceAccount(t *testing.T) {
logger := logging.GetContextLogger(t.Name())
c, namespace := setup(t, logger)

knativetest.CleanupOnInterrupt(func() { tearDown(logger, c.KubeClient, namespace) }, logger)
defer tearDown(logger, c.KubeClient, namespace)
knativetest.CleanupOnInterrupt(func() { tearDown(t, logger, c, namespace) }, logger)
defer tearDown(t, logger, c, namespace)

logger.Infof("Creating pipeline resources in namespace %s", namespace)

Expand Down
4 changes: 2 additions & 2 deletions test/taskrun_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ func TestTaskRun(t *testing.T) {
logger := logging.GetContextLogger(t.Name())
c, namespace := setup(t, logger)

knativetest.CleanupOnInterrupt(func() { tearDown(logger, c.KubeClient, namespace) }, logger)
defer tearDown(logger, c.KubeClient, namespace)
knativetest.CleanupOnInterrupt(func() { tearDown(t, logger, c, namespace) }, logger)
defer tearDown(t, logger, c, namespace)

logger.Infof("Creating Task and TaskRun in namespace %s", namespace)
if _, err := c.TaskClient.Create(getHelloWorldTask(namespace, []string{"/bin/sh", "-c", fmt.Sprintf("echo %s", taskOutput)})); err != nil {
Expand Down

0 comments on commit 28780d2

Please sign in to comment.