Skip to content
This repository was archived by the owner on Mar 21, 2025. It is now read-only.

Commit 7f96ec9

Browse files
author
Michael Sauter
authored
Merge pull request #267 from opendevstack/feature/expose-logs-in-tests
Expose logs in tests
2 parents 21d04e6 + 10b026e commit 7f96ec9

File tree

5 files changed

+59
-14
lines changed

5 files changed

+59
-14
lines changed

pkg/tasktesting/logs.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ func getEventsAndLogsOfPod(
1919
ctx context.Context,
2020
c kubernetes.Interface,
2121
pod *corev1.Pod,
22+
collectedLogsChan chan []byte,
2223
errs chan error) {
2324
quitEvents := make(chan bool)
2425
podName := pod.Name
@@ -35,7 +36,7 @@ func getEventsAndLogsOfPod(
3536

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

5758
w, err := c.CoreV1().Pods(podNamespace).Watch(ctx, metav1.SingleObject(metav1.ObjectMeta{
@@ -81,9 +82,11 @@ func streamContainerLogs(
8182
for reader.Scan() {
8283
select {
8384
case <-ctx.Done():
85+
collectedLogsChan <- reader.Bytes()
8486
fmt.Println(reader.Text())
8587
return nil
8688
default:
89+
collectedLogsChan <- reader.Bytes()
8790
fmt.Println(reader.Text())
8891
}
8992
}

pkg/tasktesting/run.go

+22-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package tasktesting
22

33
import (
4+
"bytes"
45
"context"
56
"os"
67
"path/filepath"
@@ -38,12 +39,13 @@ type TestCase struct {
3839
}
3940

4041
type TaskRunContext struct {
41-
Namespace string
42-
Clients *kubernetes.Clients
43-
Workspaces map[string]string
44-
Params map[string]string
45-
ODS *pipelinectxt.ODSContext
46-
Cleanup func()
42+
Namespace string
43+
Clients *kubernetes.Clients
44+
Workspaces map[string]string
45+
Params map[string]string
46+
ODS *pipelinectxt.ODSContext
47+
Cleanup func()
48+
CollectedLogs []byte
4749
}
4850

4951
func Run(t *testing.T, tc TestCase, testOpts TestOpts) {
@@ -89,11 +91,15 @@ func Run(t *testing.T, tc TestCase, testOpts TestOpts) {
8991
t.Fatal(err)
9092
}
9193

92-
taskRun, err := WatchTaskRunUntilDone(t, testOpts, tr)
94+
taskRun, collectedLogsBuffer, err := WatchTaskRunUntilDone(t, testOpts, tr)
9395
if err != nil {
9496
t.Fatal(err)
9597
}
9698

99+
if collectedLogsBuffer.Len() > 0 {
100+
testCaseContext.CollectedLogs = collectedLogsBuffer.Bytes()
101+
}
102+
97103
// Show info from Task result
98104
CollectTaskResultInfo(taskRun, t.Logf)
99105

@@ -130,10 +136,12 @@ func InitWorkspace(workspaceName, workspaceDir string) (string, error) {
130136
)
131137
}
132138

133-
func WatchTaskRunUntilDone(t *testing.T, testOpts TestOpts, tr *tekton.TaskRun) (*tekton.TaskRun, error) {
139+
func WatchTaskRunUntilDone(t *testing.T, testOpts TestOpts, tr *tekton.TaskRun) (*tekton.TaskRun, bytes.Buffer, error) {
134140
taskRunDone := make(chan *tekton.TaskRun)
135141
podAdded := make(chan *v1.Pod)
136142
errs := make(chan error)
143+
collectedLogsChan := make(chan []byte)
144+
var collectedLogsBuffer bytes.Buffer
137145

138146
ctx, cancel := context.WithTimeout(context.TODO(), testOpts.Timeout)
139147
go waitForTaskRunDone(
@@ -159,7 +167,7 @@ func WatchTaskRunUntilDone(t *testing.T, testOpts TestOpts, tr *tekton.TaskRun)
159167
case err := <-errs:
160168
if err != nil {
161169
cancel()
162-
return nil, err
170+
return nil, collectedLogsBuffer, err
163171
}
164172

165173
case pod := <-podAdded:
@@ -168,13 +176,17 @@ func WatchTaskRunUntilDone(t *testing.T, testOpts TestOpts, tr *tekton.TaskRun)
168176
ctx,
169177
testOpts.Clients.KubernetesClientSet,
170178
pod,
179+
collectedLogsChan,
171180
errs,
172181
)
173182
}
174183

184+
case b := <-collectedLogsChan:
185+
collectedLogsBuffer.Write(b)
186+
175187
case tr := <-taskRunDone:
176188
cancel()
177-
return tr, nil
189+
return tr, collectedLogsBuffer, nil
178190
}
179191
}
180192
}

test/tasks/ods-finish_test.go

+23
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"log"
66
"path/filepath"
7+
"strings"
78
"testing"
89
"time"
910

@@ -84,6 +85,11 @@ func TestTaskODSFinish(t *testing.T) {
8485
bitbucketClient := tasktesting.BitbucketClientOrFatal(t, ctxt.Clients.KubernetesClientSet, ctxt.Namespace)
8586
checkBuildStatus(t, bitbucketClient, ctxt.ODS.GitCommitSHA, bitbucket.BuildStatusSuccessful)
8687
checkArtifactsAreInNexus(t, ctxt, nexus.TemporaryRepositoryDefault)
88+
89+
wantLogMsg := "Artifact coverage.out is already present in Nexus repository"
90+
if !strings.Contains(string(ctxt.CollectedLogs), wantLogMsg) {
91+
t.Fatalf("Want:\n%s\n\nGot:\n%s", wantLogMsg, string(ctxt.CollectedLogs))
92+
}
8793
},
8894
},
8995
"set bitbucket build status to successful and upload artifacts to permanent Nexus repository": {
@@ -109,6 +115,23 @@ func TestTaskODSFinish(t *testing.T) {
109115
checkArtifactsAreInNexus(t, ctxt, nexus.PermanentRepositoryDefault)
110116
},
111117
},
118+
"stops gracefully when context cannot be read": {
119+
WorkspaceDirMapping: map[string]string{"source": "empty"},
120+
PreRunFunc: func(t *testing.T, ctxt *tasktesting.TaskRunContext) {
121+
ctxt.Params = map[string]string{
122+
"pipeline-run-name": "foo",
123+
"aggregate-tasks-status": "Failed",
124+
}
125+
},
126+
WantRunSuccess: false,
127+
PostRunFunc: func(t *testing.T, ctxt *tasktesting.TaskRunContext) {
128+
want := "Unable to continue as pipeline context cannot be read"
129+
130+
if !strings.Contains(string(ctxt.CollectedLogs), want) {
131+
t.Fatalf("Want:\n%s\n\nGot:\n%s", want, string(ctxt.CollectedLogs))
132+
}
133+
},
134+
},
112135
},
113136
)
114137
}

test/tasks/ods-start_test.go

+9-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"os"
66
"path/filepath"
7+
"strings"
78
"testing"
89

910
"github.com/opendevstack/pipeline/internal/directory"
@@ -181,8 +182,14 @@ func TestTaskODSStart(t *testing.T) {
181182
}
182183
},
183184
WantRunSuccess: false,
184-
// TODO: check in post run func that failure is actually due to
185-
// missing pipeline run artifact and not due to sth. else.
185+
PostRunFunc: func(t *testing.T, ctxt *tasktesting.TaskRunContext) {
186+
want := "Pipeline runs with subrepos require a successful pipeline run " +
187+
"for all checked out subrepo commits, however no such run was found"
188+
189+
if !strings.Contains(string(ctxt.CollectedLogs), want) {
190+
t.Fatalf("Want:\n%s\n\nGot:\n%s", want, string(ctxt.CollectedLogs))
191+
}
192+
},
186193
},
187194
"handles QA stage": {
188195
WorkspaceDirMapping: map[string]string{"source": "hello-world-app"},

test/testdata/workspaces/empty/.gitkeep

Whitespace-only changes.

0 commit comments

Comments
 (0)