11package tasktesting
22
33import (
4+ "bytes"
45 "context"
56 "os"
67 "path/filepath"
@@ -38,12 +39,13 @@ type TestCase struct {
3839}
3940
4041type 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
4951func 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