|
| 1 | +package integration |
| 2 | + |
| 3 | +import ( |
| 4 | + runnerv1 "code.gitea.io/actions-proto-go/runner/v1" |
| 5 | + auth_model "code.gitea.io/gitea/models/auth" |
| 6 | + "code.gitea.io/gitea/models/unittest" |
| 7 | + user_model "code.gitea.io/gitea/models/user" |
| 8 | + "code.gitea.io/gitea/tests" |
| 9 | + "fmt" |
| 10 | + "github.com/PuerkitoBio/goquery" |
| 11 | + "net/http" |
| 12 | + "net/url" |
| 13 | + "strconv" |
| 14 | + "testing" |
| 15 | + |
| 16 | + "github.com/stretchr/testify/assert" |
| 17 | +) |
| 18 | + |
| 19 | +func TestRepoActionDelete(t *testing.T) { |
| 20 | + defer tests.PrepareTestEnv(t)() |
| 21 | + |
| 22 | + testCases := []struct { |
| 23 | + treePath string |
| 24 | + fileContent string |
| 25 | + outcomes map[string]*mockTaskOutcome |
| 26 | + expectedTaskNeeds map[string]*runnerv1.TaskNeed // jobID => TaskNeed |
| 27 | + }{ |
| 28 | + { |
| 29 | + treePath: ".gitea/workflows/jobs-outputs-with-matrix.yml", |
| 30 | + fileContent: `name: jobs-outputs-with-matrix |
| 31 | +on: |
| 32 | + push: |
| 33 | + paths: |
| 34 | + - '.gitea/workflows/jobs-outputs-with-matrix.yml' |
| 35 | +jobs: |
| 36 | + job1: |
| 37 | + runs-on: ubuntu-latest |
| 38 | + outputs: |
| 39 | + output_1: ${{ steps.gen_output.outputs.output_1 }} |
| 40 | + output_2: ${{ steps.gen_output.outputs.output_2 }} |
| 41 | + output_3: ${{ steps.gen_output.outputs.output_3 }} |
| 42 | + strategy: |
| 43 | + matrix: |
| 44 | + version: [1, 2, 3] |
| 45 | + steps: |
| 46 | + - name: Generate output |
| 47 | + id: gen_output |
| 48 | + run: | |
| 49 | + version="${{ matrix.version }}" |
| 50 | + echo "output_${version}=${version}" >> "$GITHUB_OUTPUT" |
| 51 | + job2: |
| 52 | + runs-on: ubuntu-latest |
| 53 | + needs: [job1] |
| 54 | + steps: |
| 55 | + - run: echo '${{ toJSON(needs.job1.outputs) }}' |
| 56 | +`, |
| 57 | + outcomes: map[string]*mockTaskOutcome{ |
| 58 | + "job1 (1)": { |
| 59 | + result: runnerv1.Result_RESULT_SUCCESS, |
| 60 | + outputs: map[string]string{ |
| 61 | + "output_1": "1", |
| 62 | + "output_2": "", |
| 63 | + "output_3": "", |
| 64 | + }, |
| 65 | + }, |
| 66 | + "job1 (2)": { |
| 67 | + result: runnerv1.Result_RESULT_SUCCESS, |
| 68 | + outputs: map[string]string{ |
| 69 | + "output_1": "", |
| 70 | + "output_2": "2", |
| 71 | + "output_3": "", |
| 72 | + }, |
| 73 | + }, |
| 74 | + "job1 (3)": { |
| 75 | + result: runnerv1.Result_RESULT_SUCCESS, |
| 76 | + outputs: map[string]string{ |
| 77 | + "output_1": "", |
| 78 | + "output_2": "", |
| 79 | + "output_3": "3", |
| 80 | + }, |
| 81 | + }, |
| 82 | + }, |
| 83 | + expectedTaskNeeds: map[string]*runnerv1.TaskNeed{ |
| 84 | + "job1": { |
| 85 | + Result: runnerv1.Result_RESULT_SUCCESS, |
| 86 | + Outputs: map[string]string{ |
| 87 | + "output_1": "1", |
| 88 | + "output_2": "2", |
| 89 | + "output_3": "3", |
| 90 | + }, |
| 91 | + }, |
| 92 | + }, |
| 93 | + }, |
| 94 | + { |
| 95 | + treePath: ".gitea/workflows/jobs-outputs-with-matrix-failure.yml", |
| 96 | + fileContent: `name: jobs-outputs-with-matrix-failure |
| 97 | +on: |
| 98 | + push: |
| 99 | + paths: |
| 100 | + - '.gitea/workflows/jobs-outputs-with-matrix-failure.yml' |
| 101 | +jobs: |
| 102 | + job1: |
| 103 | + runs-on: ubuntu-latest |
| 104 | + outputs: |
| 105 | + output_1: ${{ steps.gen_output.outputs.output_1 }} |
| 106 | + output_2: ${{ steps.gen_output.outputs.output_2 }} |
| 107 | + output_3: ${{ steps.gen_output.outputs.output_3 }} |
| 108 | + strategy: |
| 109 | + matrix: |
| 110 | + version: [1, 2, 3] |
| 111 | + steps: |
| 112 | + - name: Generate output |
| 113 | + id: gen_output |
| 114 | + run: | |
| 115 | + version="${{ matrix.version }}" |
| 116 | + echo "output_${version}=${version}" >> "$GITHUB_OUTPUT" |
| 117 | + job2: |
| 118 | + runs-on: ubuntu-latest |
| 119 | + if: ${{ always() }} |
| 120 | + needs: [job1] |
| 121 | + steps: |
| 122 | + - run: echo '${{ toJSON(needs.job1.outputs) }}' |
| 123 | +`, |
| 124 | + outcomes: map[string]*mockTaskOutcome{ |
| 125 | + "job1 (1)": { |
| 126 | + result: runnerv1.Result_RESULT_SUCCESS, |
| 127 | + outputs: map[string]string{ |
| 128 | + "output_1": "1", |
| 129 | + "output_2": "", |
| 130 | + "output_3": "", |
| 131 | + }, |
| 132 | + }, |
| 133 | + "job1 (2)": { |
| 134 | + result: runnerv1.Result_RESULT_FAILURE, |
| 135 | + outputs: map[string]string{ |
| 136 | + "output_1": "", |
| 137 | + "output_2": "", |
| 138 | + "output_3": "", |
| 139 | + }, |
| 140 | + }, |
| 141 | + "job1 (3)": { |
| 142 | + result: runnerv1.Result_RESULT_SUCCESS, |
| 143 | + outputs: map[string]string{ |
| 144 | + "output_1": "", |
| 145 | + "output_2": "", |
| 146 | + "output_3": "3", |
| 147 | + }, |
| 148 | + }, |
| 149 | + }, |
| 150 | + expectedTaskNeeds: map[string]*runnerv1.TaskNeed{ |
| 151 | + "job1": { |
| 152 | + Result: runnerv1.Result_RESULT_FAILURE, |
| 153 | + Outputs: map[string]string{ |
| 154 | + "output_1": "1", |
| 155 | + "output_2": "", |
| 156 | + "output_3": "3", |
| 157 | + }, |
| 158 | + }, |
| 159 | + }, |
| 160 | + }, |
| 161 | + } |
| 162 | + onGiteaRun(t, func(t *testing.T, u *url.URL) { |
| 163 | + user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) |
| 164 | + session := loginUser(t, user2.Name) |
| 165 | + token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteRepository, auth_model.AccessTokenScopeWriteUser) |
| 166 | + |
| 167 | + apiRepo := createActionsTestRepo(t, token, "actions-jobs-outputs-with-matrix", false) |
| 168 | + runner := newMockRunner() |
| 169 | + runner.registerAsRepoRunner(t, user2.Name, apiRepo.Name, "mock-runner", []string{"ubuntu-latest"}) |
| 170 | + |
| 171 | + for _, tc := range testCases { |
| 172 | + t.Run(fmt.Sprintf("test %s", tc.treePath), func(t *testing.T) { |
| 173 | + opts := getWorkflowCreateFileOptions(user2, apiRepo.DefaultBranch, fmt.Sprintf("create %s", tc.treePath), tc.fileContent) |
| 174 | + createWorkflowFile(t, token, user2.Name, apiRepo.Name, tc.treePath, opts) |
| 175 | + |
| 176 | + for i := 0; i < len(tc.outcomes); i++ { |
| 177 | + task := runner.fetchTask(t) |
| 178 | + jobName := getTaskJobNameByTaskID(t, token, user2.Name, apiRepo.Name, task.Id) |
| 179 | + outcome := tc.outcomes[jobName] |
| 180 | + assert.NotNil(t, outcome) |
| 181 | + runner.execTask(t, task, outcome) |
| 182 | + } |
| 183 | + |
| 184 | + task := runner.fetchTask(t) |
| 185 | + actualTaskNeeds := task.Needs |
| 186 | + assert.Len(t, actualTaskNeeds, len(tc.expectedTaskNeeds)) |
| 187 | + for jobID, tn := range tc.expectedTaskNeeds { |
| 188 | + actualNeed := actualTaskNeeds[jobID] |
| 189 | + assert.Equal(t, tn.Result, actualNeed.Result) |
| 190 | + assert.Len(t, actualNeed.Outputs, len(tn.Outputs)) |
| 191 | + for outputKey, outputValue := range tn.Outputs { |
| 192 | + assert.Equal(t, outputValue, actualNeed.Outputs[outputKey]) |
| 193 | + } |
| 194 | + } |
| 195 | + }) |
| 196 | + } |
| 197 | + httpContext := NewAPITestContext(t, user2.Name, apiRepo.Name, auth_model.AccessTokenScopeWriteRepository) |
| 198 | + // check result |
| 199 | + req := NewRequest(t, "GET", fmt.Sprintf("/%s/%s/actions", httpContext.Username, httpContext.Reponame)) |
| 200 | + resp := session.MakeRequest(t, req, http.StatusOK) |
| 201 | + htmlDoc := NewHTMLParser(t, resp.Body) |
| 202 | + |
| 203 | + var runIDs []int64 |
| 204 | + list := htmlDoc.doc.Find("#action-actions input.action-checkbox:not(:disabled)") |
| 205 | + list.Each(func(i int, s *goquery.Selection) { |
| 206 | + idStr, exists := s.Attr("data-action-id") |
| 207 | + if exists { |
| 208 | + runID, err := strconv.ParseInt(idStr, 10, 64) |
| 209 | + assert.NoError(t, err) |
| 210 | + runIDs = append(runIDs, runID) |
| 211 | + } |
| 212 | + }) |
| 213 | + |
| 214 | + assert.NotEmpty(t, runIDs) |
| 215 | + csrf := GetUserCSRFToken(t, session) |
| 216 | + reqDelete := NewRequestWithJSON(t, http.MethodPost, fmt.Sprintf("/%s/%s/actions/runs/delete", httpContext.Username, httpContext.Reponame), map[string]interface{}{ |
| 217 | + "actionIds": runIDs, |
| 218 | + "_csrf": csrf, |
| 219 | + }).AddTokenAuth(token). |
| 220 | + SetHeader("X-Csrf-Token", csrf) |
| 221 | + session.MakeRequest(t, reqDelete, http.StatusNoContent) |
| 222 | + doAPIDeleteRepository(httpContext) |
| 223 | + }) |
| 224 | + |
| 225 | +} |
0 commit comments