@@ -2,14 +2,22 @@ package sidecarlogresults
22
33import (
44 "bytes"
5+ "context"
56 "encoding/json"
7+ "fmt"
68 "os"
79 "path/filepath"
810 "sort"
11+ "strings"
912 "testing"
1013
1114 "github.com/google/go-cmp/cmp"
15+ "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
1216 "github.com/tektoncd/pipeline/test/diff"
17+ corev1 "k8s.io/api/core/v1"
18+ v1 "k8s.io/api/core/v1"
19+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
20+ fakekubeclientset "k8s.io/client-go/kubernetes/fake"
1321)
1422
1523func TestLookForResults_FanOutAndWait (t * testing.T ) {
@@ -121,6 +129,184 @@ func TestLookForResults(t *testing.T) {
121129 }
122130}
123131
132+ func TestExtractResultsFromLogs (t * testing.T ) {
133+ inputResults := []SidecarLogResult {
134+ {
135+ Name : "result1" ,
136+ Value : "foo" ,
137+ }, {
138+ Name : "result2" ,
139+ Value : "bar" ,
140+ },
141+ }
142+ podLogs := ""
143+ for _ , r := range inputResults {
144+ res , _ := json .Marshal (& r )
145+ podLogs = fmt .Sprintf ("%s%s\n " , podLogs , string (res ))
146+ }
147+ logs := strings .NewReader (podLogs )
148+
149+ results , err := extractResultsFromLogs (logs , []v1beta1.PipelineResourceResult {}, 4096 )
150+ if err != nil {
151+ t .Error (err )
152+ }
153+ want := []v1beta1.PipelineResourceResult {
154+ {
155+ Key : "result1" ,
156+ Value : "foo" ,
157+ ResultType : v1beta1 .TaskRunResultType ,
158+ }, {
159+ Key : "result2" ,
160+ Value : "bar" ,
161+ ResultType : v1beta1 .TaskRunResultType ,
162+ },
163+ }
164+ if d := cmp .Diff (want , results ); d != "" {
165+ t .Fatal (diff .PrintWantGot (d ))
166+ }
167+ }
168+
169+ func TestExtractResultsFromLogs_Failure (t * testing.T ) {
170+ inputResults := []SidecarLogResult {
171+ {
172+ Name : "result1" ,
173+ Value : strings .Repeat ("v" , 4098 ),
174+ },
175+ }
176+ podLogs := ""
177+ for _ , r := range inputResults {
178+ res , _ := json .Marshal (& r )
179+ podLogs = fmt .Sprintf ("%s%s\n " , podLogs , string (res ))
180+ }
181+ logs := strings .NewReader (podLogs )
182+
183+ _ , err := extractResultsFromLogs (logs , []v1beta1.PipelineResourceResult {}, 4096 )
184+ if err != ErrSizeExceeded {
185+ t .Fatalf ("Expected error %v but got %v" , ErrSizeExceeded , err )
186+ }
187+ }
188+
189+ func TestParseResults (t * testing.T ) {
190+ results := []SidecarLogResult {
191+ {
192+ Name : "result1" ,
193+ Value : "foo" ,
194+ }, {
195+ Name : "result2" ,
196+ Value : `{"IMAGE_URL":"ar.com", "IMAGE_DIGEST":"sha234"}` ,
197+ }, {
198+ Name : "result3" ,
199+ Value : `["hello","world"]` ,
200+ },
201+ }
202+ podLogs := []string {}
203+ for _ , r := range results {
204+ res , _ := json .Marshal (& r )
205+ podLogs = append (podLogs , string (res ))
206+ }
207+ want := []v1beta1.PipelineResourceResult {{
208+ Key : "result1" ,
209+ Value : "foo" ,
210+ ResultType : v1beta1 .TaskRunResultType ,
211+ }, {
212+ Key : "result2" ,
213+ Value : `{"IMAGE_URL":"ar.com", "IMAGE_DIGEST":"sha234"}` ,
214+ ResultType : v1beta1 .TaskRunResultType ,
215+ }, {
216+ Key : "result3" ,
217+ Value : `["hello","world"]` ,
218+ ResultType : v1beta1 .TaskRunResultType ,
219+ }}
220+ stepResults := []v1beta1.PipelineResourceResult {}
221+ for _ , plog := range podLogs {
222+ res , err := parseResults ([]byte (plog ), 4096 )
223+ if err != nil {
224+ t .Error (err )
225+ }
226+ stepResults = append (stepResults , res )
227+ }
228+ if d := cmp .Diff (want , stepResults ); d != "" {
229+ t .Fatal (diff .PrintWantGot (d ))
230+ }
231+ }
232+
233+ func TestParseResults_Failure (t * testing.T ) {
234+ result := SidecarLogResult {
235+ Name : "result2" ,
236+ Value : strings .Repeat ("k" , 4098 ),
237+ }
238+ res1 , _ := json .Marshal ("result1 v1" )
239+ res2 , _ := json .Marshal (& result )
240+ podLogs := []string {string (res1 ), string (res2 )}
241+ want := []string {
242+ "Invalid result json: cannot unmarshal string into Go value of type sidecarlogresults.SidecarLogResult" ,
243+ ErrSizeExceeded .Error (),
244+ }
245+ got := []string {}
246+ for _ , plog := range podLogs {
247+ _ , err := parseResults ([]byte (plog ), 4096 )
248+ got = append (got , err .Error ())
249+ }
250+ if d := cmp .Diff (want , got ); d != "" {
251+ t .Fatal (diff .PrintWantGot (d ))
252+ }
253+ }
254+
255+ func TestGetResultsFromSidecarLogs (t * testing.T ) {
256+ for _ , c := range []struct {
257+ desc string
258+ podPhase v1.PodPhase
259+ wantError bool
260+ }{{
261+ desc : "pod pending to start" ,
262+ podPhase : corev1 .PodPending ,
263+ wantError : false ,
264+ }, {
265+ desc : "pod running extract logs" ,
266+ podPhase : corev1 .PodRunning ,
267+ wantError : true ,
268+ }} {
269+ t .Run (c .desc , func (t * testing.T ) {
270+ ctx := context .Background ()
271+ clientset := fakekubeclientset .NewSimpleClientset ()
272+ pod := & v1.Pod {
273+ TypeMeta : metav1.TypeMeta {
274+ Kind : "Pod" ,
275+ APIVersion : "v1" ,
276+ },
277+ ObjectMeta : metav1.ObjectMeta {
278+ Name : "pod" ,
279+ Namespace : "foo" ,
280+ },
281+ Spec : v1.PodSpec {
282+ Containers : []v1.Container {
283+ {
284+ Name : "container" ,
285+ Image : "image" ,
286+ },
287+ },
288+ },
289+ Status : v1.PodStatus {
290+ Phase : c .podPhase ,
291+ },
292+ }
293+ pod , err := clientset .CoreV1 ().Pods (pod .Namespace ).Create (context .TODO (), pod , metav1.CreateOptions {})
294+ if err != nil {
295+ t .Errorf ("Error occurred while creating pod %s: %s" , pod .Name , err .Error ())
296+ }
297+
298+ // Fake logs are not formatted properly so there will be an error
299+ _ , err = GetResultsFromSidecarLogs (ctx , clientset , "foo" , "pod" , "container" , pod .Status .Phase )
300+ if err != nil && ! c .wantError {
301+ t .Fatalf ("did not expect an error but got: %v" , err )
302+ }
303+ if c .wantError && err == nil {
304+ t .Fatal ("expected to get an error but did not" )
305+ }
306+ })
307+ }
308+ }
309+
124310func createResult (t * testing.T , dir string , resultName string , resultValue string ) {
125311 t .Helper ()
126312 resultFile := filepath .Join (dir , resultName )
0 commit comments