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

Commit c4eec8a

Browse files
author
Michael Sauter
committed
Expose log output in test framework
Relates to #127.
1 parent 21d04e6 commit c4eec8a

File tree

2 files changed

+27
-12
lines changed

2 files changed

+27
-12
lines changed

pkg/tasktesting/logs.go

Lines changed: 5 additions & 2 deletions
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

Lines changed: 22 additions & 10 deletions
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
}

0 commit comments

Comments
 (0)