Skip to content

Commit

Permalink
Merge pull request #267 from opendevstack/feature/expose-logs-in-tests
Browse files Browse the repository at this point in the history
Expose logs in tests
  • Loading branch information
michaelsauter authored Nov 2, 2021
2 parents 21d04e6 + 10b026e commit 7f96ec9
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 14 deletions.
7 changes: 5 additions & 2 deletions pkg/tasktesting/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func getEventsAndLogsOfPod(
ctx context.Context,
c kubernetes.Interface,
pod *corev1.Pod,
collectedLogsChan chan []byte,
errs chan error) {
quitEvents := make(chan bool)
podName := pod.Name
Expand All @@ -35,7 +36,7 @@ func getEventsAndLogsOfPod(

watchingEvents := true
for _, container := range pod.Spec.Containers {
err := streamContainerLogs(ctx, c, podNamespace, podName, container.Name)
err := streamContainerLogs(ctx, c, podNamespace, podName, container.Name, collectedLogsChan)
if err != nil {
fmt.Printf("failure while getting container logs: %s", err)
errs <- err
Expand All @@ -51,7 +52,7 @@ func getEventsAndLogsOfPod(
func streamContainerLogs(
ctx context.Context,
c kubernetes.Interface,
podNamespace, podName, containerName string) error {
podNamespace, podName, containerName string, collectedLogsChan chan []byte) error {
log.Printf("Waiting for container %s from pod %s to be ready...\n", containerName, podName)

w, err := c.CoreV1().Pods(podNamespace).Watch(ctx, metav1.SingleObject(metav1.ObjectMeta{
Expand Down Expand Up @@ -81,9 +82,11 @@ func streamContainerLogs(
for reader.Scan() {
select {
case <-ctx.Done():
collectedLogsChan <- reader.Bytes()
fmt.Println(reader.Text())
return nil
default:
collectedLogsChan <- reader.Bytes()
fmt.Println(reader.Text())
}
}
Expand Down
32 changes: 22 additions & 10 deletions pkg/tasktesting/run.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tasktesting

import (
"bytes"
"context"
"os"
"path/filepath"
Expand Down Expand Up @@ -38,12 +39,13 @@ type TestCase struct {
}

type TaskRunContext struct {
Namespace string
Clients *kubernetes.Clients
Workspaces map[string]string
Params map[string]string
ODS *pipelinectxt.ODSContext
Cleanup func()
Namespace string
Clients *kubernetes.Clients
Workspaces map[string]string
Params map[string]string
ODS *pipelinectxt.ODSContext
Cleanup func()
CollectedLogs []byte
}

func Run(t *testing.T, tc TestCase, testOpts TestOpts) {
Expand Down Expand Up @@ -89,11 +91,15 @@ func Run(t *testing.T, tc TestCase, testOpts TestOpts) {
t.Fatal(err)
}

taskRun, err := WatchTaskRunUntilDone(t, testOpts, tr)
taskRun, collectedLogsBuffer, err := WatchTaskRunUntilDone(t, testOpts, tr)
if err != nil {
t.Fatal(err)
}

if collectedLogsBuffer.Len() > 0 {
testCaseContext.CollectedLogs = collectedLogsBuffer.Bytes()
}

// Show info from Task result
CollectTaskResultInfo(taskRun, t.Logf)

Expand Down Expand Up @@ -130,10 +136,12 @@ func InitWorkspace(workspaceName, workspaceDir string) (string, error) {
)
}

func WatchTaskRunUntilDone(t *testing.T, testOpts TestOpts, tr *tekton.TaskRun) (*tekton.TaskRun, error) {
func WatchTaskRunUntilDone(t *testing.T, testOpts TestOpts, tr *tekton.TaskRun) (*tekton.TaskRun, bytes.Buffer, error) {
taskRunDone := make(chan *tekton.TaskRun)
podAdded := make(chan *v1.Pod)
errs := make(chan error)
collectedLogsChan := make(chan []byte)
var collectedLogsBuffer bytes.Buffer

ctx, cancel := context.WithTimeout(context.TODO(), testOpts.Timeout)
go waitForTaskRunDone(
Expand All @@ -159,7 +167,7 @@ func WatchTaskRunUntilDone(t *testing.T, testOpts TestOpts, tr *tekton.TaskRun)
case err := <-errs:
if err != nil {
cancel()
return nil, err
return nil, collectedLogsBuffer, err
}

case pod := <-podAdded:
Expand All @@ -168,13 +176,17 @@ func WatchTaskRunUntilDone(t *testing.T, testOpts TestOpts, tr *tekton.TaskRun)
ctx,
testOpts.Clients.KubernetesClientSet,
pod,
collectedLogsChan,
errs,
)
}

case b := <-collectedLogsChan:
collectedLogsBuffer.Write(b)

case tr := <-taskRunDone:
cancel()
return tr, nil
return tr, collectedLogsBuffer, nil
}
}
}
23 changes: 23 additions & 0 deletions test/tasks/ods-finish_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"log"
"path/filepath"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -84,6 +85,11 @@ func TestTaskODSFinish(t *testing.T) {
bitbucketClient := tasktesting.BitbucketClientOrFatal(t, ctxt.Clients.KubernetesClientSet, ctxt.Namespace)
checkBuildStatus(t, bitbucketClient, ctxt.ODS.GitCommitSHA, bitbucket.BuildStatusSuccessful)
checkArtifactsAreInNexus(t, ctxt, nexus.TemporaryRepositoryDefault)

wantLogMsg := "Artifact coverage.out is already present in Nexus repository"
if !strings.Contains(string(ctxt.CollectedLogs), wantLogMsg) {
t.Fatalf("Want:\n%s\n\nGot:\n%s", wantLogMsg, string(ctxt.CollectedLogs))
}
},
},
"set bitbucket build status to successful and upload artifacts to permanent Nexus repository": {
Expand All @@ -109,6 +115,23 @@ func TestTaskODSFinish(t *testing.T) {
checkArtifactsAreInNexus(t, ctxt, nexus.PermanentRepositoryDefault)
},
},
"stops gracefully when context cannot be read": {
WorkspaceDirMapping: map[string]string{"source": "empty"},
PreRunFunc: func(t *testing.T, ctxt *tasktesting.TaskRunContext) {
ctxt.Params = map[string]string{
"pipeline-run-name": "foo",
"aggregate-tasks-status": "Failed",
}
},
WantRunSuccess: false,
PostRunFunc: func(t *testing.T, ctxt *tasktesting.TaskRunContext) {
want := "Unable to continue as pipeline context cannot be read"

if !strings.Contains(string(ctxt.CollectedLogs), want) {
t.Fatalf("Want:\n%s\n\nGot:\n%s", want, string(ctxt.CollectedLogs))
}
},
},
},
)
}
Expand Down
11 changes: 9 additions & 2 deletions test/tasks/ods-start_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os"
"path/filepath"
"strings"
"testing"

"github.com/opendevstack/pipeline/internal/directory"
Expand Down Expand Up @@ -181,8 +182,14 @@ func TestTaskODSStart(t *testing.T) {
}
},
WantRunSuccess: false,
// TODO: check in post run func that failure is actually due to
// missing pipeline run artifact and not due to sth. else.
PostRunFunc: func(t *testing.T, ctxt *tasktesting.TaskRunContext) {
want := "Pipeline runs with subrepos require a successful pipeline run " +
"for all checked out subrepo commits, however no such run was found"

if !strings.Contains(string(ctxt.CollectedLogs), want) {
t.Fatalf("Want:\n%s\n\nGot:\n%s", want, string(ctxt.CollectedLogs))
}
},
},
"handles QA stage": {
WorkspaceDirMapping: map[string]string{"source": "hello-world-app"},
Expand Down
Empty file.

0 comments on commit 7f96ec9

Please sign in to comment.