@@ -17,13 +17,86 @@ import (
1717 user_model "code.gitea.io/gitea/models/user"
1818 "code.gitea.io/gitea/modules/setting"
1919 "code.gitea.io/gitea/modules/storage"
20+ "code.gitea.io/gitea/modules/test"
2021
2122 runnerv1 "code.gitea.io/actions-proto-go/runner/v1"
2223 "github.com/stretchr/testify/assert"
2324 "google.golang.org/protobuf/types/known/timestamppb"
2425)
2526
2627func TestDownloadTaskLogs (t * testing.T ) {
28+ now := time .Now ()
29+ testCases := []struct {
30+ treePath string
31+ fileContent string
32+ outcome * mockTaskOutcome
33+ zstdEnabled bool
34+ }{
35+ {
36+ treePath : ".gitea/workflows/download-task-logs-zstd.yml" ,
37+ fileContent : `name: download-task-logs-zstd
38+ on:
39+ push:
40+ paths:
41+ - '.gitea/workflows/download-task-logs-zstd.yml'
42+ jobs:
43+ job1:
44+ runs-on: ubuntu-latest
45+ steps:
46+ - run: echo job1 with zstd enabled
47+ ` ,
48+ outcome : & mockTaskOutcome {
49+ result : runnerv1 .Result_RESULT_SUCCESS ,
50+ logRows : []* runnerv1.LogRow {
51+ {
52+ Time : timestamppb .New (now .Add (1 * time .Second )),
53+ Content : " \U0001F433 docker create image" ,
54+ },
55+ {
56+ Time : timestamppb .New (now .Add (2 * time .Second )),
57+ Content : "job1 zstd enabled" ,
58+ },
59+ {
60+ Time : timestamppb .New (now .Add (3 * time .Second )),
61+ Content : "\U0001F3C1 Job succeeded" ,
62+ },
63+ },
64+ },
65+ zstdEnabled : true ,
66+ },
67+ {
68+ treePath : ".gitea/workflows/download-task-logs-no-zstd.yml" ,
69+ fileContent : `name: download-task-logs-no-zstd
70+ on:
71+ push:
72+ paths:
73+ - '.gitea/workflows/download-task-logs-no-zstd.yml'
74+ jobs:
75+ job1:
76+ runs-on: ubuntu-latest
77+ steps:
78+ - run: echo job1 with zstd disabled
79+ ` ,
80+ outcome : & mockTaskOutcome {
81+ result : runnerv1 .Result_RESULT_SUCCESS ,
82+ logRows : []* runnerv1.LogRow {
83+ {
84+ Time : timestamppb .New (now .Add (4 * time .Second )),
85+ Content : " \U0001F433 docker create image" ,
86+ },
87+ {
88+ Time : timestamppb .New (now .Add (5 * time .Second )),
89+ Content : "job1 zstd disabled" ,
90+ },
91+ {
92+ Time : timestamppb .New (now .Add (6 * time .Second )),
93+ Content : "\U0001F3C1 Job succeeded" ,
94+ },
95+ },
96+ },
97+ zstdEnabled : false ,
98+ },
99+ }
27100 onGiteaRun (t , func (t * testing.T , u * url.URL ) {
28101 user2 := unittest .AssertExistsAndLoadBean (t , & user_model.User {ID : 2 })
29102 session := loginUser (t , user2 .Name )
@@ -34,64 +107,50 @@ func TestDownloadTaskLogs(t *testing.T) {
34107 runner := newMockRunner ()
35108 runner .registerAsRepoRunner (t , user2 .Name , repo .Name , "mock-runner" , []string {"ubuntu-latest" })
36109
37- treePath := ".gitea/workflows/download-task-logs.yml"
38- fileContent := `name: download-task-logs
39- on: push
40- jobs:
41- job1:
42- runs-on: ubuntu-latest
43- steps:
44- - run: echo job1
45- `
110+ for _ , tc := range testCases {
111+ t .Run (fmt .Sprintf ("test %s" , tc .treePath ), func (t * testing.T ) {
112+ var resetFunc func ()
113+ if tc .zstdEnabled {
114+ resetFunc = test .MockVariableValue (& setting .Actions .LogCompression , "zstd" )
115+ assert .True (t , setting .Actions .LogCompression .IsZstd ())
116+ } else {
117+ resetFunc = test .MockVariableValue (& setting .Actions .LogCompression , "none" )
118+ assert .False (t , setting .Actions .LogCompression .IsZstd ())
119+ }
46120
47- // create the workflow file
48- opts := getWorkflowCreateFileOptions (user2 , repo .DefaultBranch , fmt .Sprintf ("create %s" , treePath ), fileContent )
49- createWorkflowFile (t , token , user2 .Name , repo .Name , treePath , opts )
121+ // create the workflow file
122+ opts := getWorkflowCreateFileOptions (user2 , repo .DefaultBranch , fmt .Sprintf ("create %s" , tc . treePath ), tc . fileContent )
123+ createWorkflowFile (t , token , user2 .Name , repo .Name , tc . treePath , opts )
50124
51- now := time .Now ()
52- outcome := & mockTaskOutcome {
53- result : runnerv1 .Result_RESULT_SUCCESS ,
54- logRows : []* runnerv1.LogRow {
55- {
56- Time : timestamppb .New (now ),
57- Content : " \U0001F433 docker create image" ,
58- },
59- {
60- Time : timestamppb .New (now .Add (5 * time .Second )),
61- Content : "job1" ,
62- },
63- {
64- Time : timestamppb .New (now .Add (8 * time .Second )),
65- Content : "\U0001F3C1 Job succeeded" ,
66- },
67- },
68- }
125+ // fetch and execute task
126+ task := runner .fetchTask (t )
127+ runner .execTask (t , task , tc .outcome )
69128
70- // fetch and execute task
71- task := runner .fetchTask (t )
72- runner .execTask (t , task , outcome )
129+ // check whether the log file exists
130+ logFileName := fmt .Sprintf ("%s/%02x/%d.log" , repo .FullName (), task .Id % 256 , task .Id )
131+ if setting .Actions .LogCompression .IsZstd () {
132+ logFileName += ".zst"
133+ }
134+ _ , err := storage .Actions .Stat (logFileName )
135+ assert .NoError (t , err )
73136
74- // check whether the log file exists
75- logFileName := fmt .Sprintf ("%s/%02x/%d.log" , repo .FullName (), task .Id % 256 , task .Id )
76- if setting .Actions .LogCompression .IsZstd () {
77- logFileName += ".zst"
78- }
79- _ , err := storage .Actions .Stat (logFileName )
80- assert .NoError (t , err )
137+ // download task logs and check content
138+ runIndex := task .Context .GetFields ()["run_number" ].GetStringValue ()
139+ req := NewRequest (t , "GET" , fmt .Sprintf ("/%s/%s/actions/runs/%s/jobs/0/logs" , user2 .Name , repo .Name , runIndex )).
140+ AddTokenAuth (token )
141+ resp := MakeRequest (t , req , http .StatusOK )
142+ logTextLines := strings .Split (strings .TrimSpace (resp .Body .String ()), "\n " )
143+ assert .Len (t , logTextLines , len (tc .outcome .logRows ))
144+ for idx , lr := range tc .outcome .logRows {
145+ assert .Equal (
146+ t ,
147+ fmt .Sprintf ("%s %s" , lr .Time .AsTime ().Format ("2006-01-02T15:04:05.0000000Z07:00" ), lr .Content ),
148+ logTextLines [idx ],
149+ )
150+ }
81151
82- // download task logs and check content
83- runIndex := task .Context .GetFields ()["run_number" ].GetStringValue ()
84- req := NewRequest (t , "GET" , fmt .Sprintf ("/%s/%s/actions/runs/%s/jobs/0/logs" , user2 .Name , repo .Name , runIndex )).
85- AddTokenAuth (token )
86- resp := MakeRequest (t , req , http .StatusOK )
87- logTextLines := strings .Split (strings .TrimSpace (resp .Body .String ()), "\n " )
88- assert .Len (t , logTextLines , len (outcome .logRows ))
89- for idx , lr := range outcome .logRows {
90- assert .Equal (
91- t ,
92- fmt .Sprintf ("%s %s" , lr .Time .AsTime ().Format ("2006-01-02T15:04:05.0000000Z07:00" ), lr .Content ),
93- logTextLines [idx ],
94- )
152+ resetFunc ()
153+ })
95154 }
96155
97156 httpContext := NewAPITestContext (t , user2 .Name , repo .Name , auth_model .AccessTokenScopeWriteRepository )
0 commit comments