|
| 1 | +// Copyright 2025 The Gitea Authors. All rights reserved. |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +package integration |
| 5 | + |
| 6 | +import ( |
| 7 | + runnerv1 "code.gitea.io/actions-proto-go/runner/v1" |
| 8 | + actions_model "code.gitea.io/gitea/models/actions" |
| 9 | + auth_model "code.gitea.io/gitea/models/auth" |
| 10 | + "code.gitea.io/gitea/models/unittest" |
| 11 | + user_model "code.gitea.io/gitea/models/user" |
| 12 | + "code.gitea.io/gitea/modules/json" |
| 13 | + "code.gitea.io/gitea/routers/web/repo/actions" |
| 14 | + "fmt" |
| 15 | + "github.com/stretchr/testify/assert" |
| 16 | + "google.golang.org/protobuf/types/known/timestamppb" |
| 17 | + "net/http" |
| 18 | + "net/url" |
| 19 | + "testing" |
| 20 | + "time" |
| 21 | +) |
| 22 | + |
| 23 | +func TestActionsDeleteRun(t *testing.T) { |
| 24 | + now := time.Now() |
| 25 | + testCase := struct { |
| 26 | + treePath string |
| 27 | + fileContent string |
| 28 | + outcomes map[string]*mockTaskOutcome |
| 29 | + expectedStatuses map[string]string |
| 30 | + }{ |
| 31 | + treePath: ".gitea/workflows/test1.yml", |
| 32 | + fileContent: `name: test1 |
| 33 | +on: |
| 34 | + push: |
| 35 | + paths: |
| 36 | + - .gitea/workflows/test1.yml |
| 37 | +jobs: |
| 38 | + job1: |
| 39 | + runs-on: ubuntu-latest |
| 40 | + steps: |
| 41 | + - run: echo job1 |
| 42 | + job2: |
| 43 | + runs-on: ubuntu-latest |
| 44 | + steps: |
| 45 | + - run: echo job2 |
| 46 | + job3: |
| 47 | + runs-on: ubuntu-latest |
| 48 | + steps: |
| 49 | + - run: echo job3 |
| 50 | +`, |
| 51 | + outcomes: map[string]*mockTaskOutcome{ |
| 52 | + "job1": { |
| 53 | + result: runnerv1.Result_RESULT_SUCCESS, |
| 54 | + logRows: []*runnerv1.LogRow{ |
| 55 | + { |
| 56 | + Time: timestamppb.New(now.Add(4 * time.Second)), |
| 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(6 * time.Second)), |
| 65 | + Content: "\U0001F3C1 Job succeeded", |
| 66 | + }, |
| 67 | + }, |
| 68 | + }, |
| 69 | + "job2": { |
| 70 | + result: runnerv1.Result_RESULT_SUCCESS, |
| 71 | + logRows: []*runnerv1.LogRow{ |
| 72 | + { |
| 73 | + Time: timestamppb.New(now.Add(4 * time.Second)), |
| 74 | + Content: " \U0001F433 docker create image", |
| 75 | + }, |
| 76 | + { |
| 77 | + Time: timestamppb.New(now.Add(5 * time.Second)), |
| 78 | + Content: "job2", |
| 79 | + }, |
| 80 | + { |
| 81 | + Time: timestamppb.New(now.Add(6 * time.Second)), |
| 82 | + Content: "\U0001F3C1 Job succeeded", |
| 83 | + }, |
| 84 | + }, |
| 85 | + }, |
| 86 | + "job3": { |
| 87 | + result: runnerv1.Result_RESULT_SUCCESS, |
| 88 | + logRows: []*runnerv1.LogRow{ |
| 89 | + { |
| 90 | + Time: timestamppb.New(now.Add(4 * time.Second)), |
| 91 | + Content: " \U0001F433 docker create image", |
| 92 | + }, |
| 93 | + { |
| 94 | + Time: timestamppb.New(now.Add(5 * time.Second)), |
| 95 | + Content: "job3", |
| 96 | + }, |
| 97 | + { |
| 98 | + Time: timestamppb.New(now.Add(6 * time.Second)), |
| 99 | + Content: "\U0001F3C1 Job succeeded", |
| 100 | + }, |
| 101 | + }, |
| 102 | + }, |
| 103 | + }, |
| 104 | + expectedStatuses: map[string]string{ |
| 105 | + "job1": actions_model.StatusSuccess.String(), |
| 106 | + "job2": actions_model.StatusSuccess.String(), |
| 107 | + "job3": actions_model.StatusSuccess.String(), |
| 108 | + }, |
| 109 | + } |
| 110 | + onGiteaRun(t, func(t *testing.T, u *url.URL) { |
| 111 | + user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) |
| 112 | + session := loginUser(t, user2.Name) |
| 113 | + token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteRepository, auth_model.AccessTokenScopeWriteUser) |
| 114 | + |
| 115 | + apiRepo := createActionsTestRepo(t, token, "actions-delete-run-test", false) |
| 116 | + runner := newMockRunner() |
| 117 | + runner.registerAsRepoRunner(t, user2.Name, apiRepo.Name, "mock-runner", []string{"ubuntu-latest"}, false) |
| 118 | + |
| 119 | + opts := getWorkflowCreateFileOptions(user2, apiRepo.DefaultBranch, "create "+testCase.treePath, testCase.fileContent) |
| 120 | + createWorkflowFile(t, token, user2.Name, apiRepo.Name, testCase.treePath, opts) |
| 121 | + |
| 122 | + runIndex := "" |
| 123 | + for i := 0; i < len(testCase.outcomes); i++ { |
| 124 | + task := runner.fetchTask(t) |
| 125 | + jobName := getTaskJobNameByTaskID(t, token, user2.Name, apiRepo.Name, task.Id) |
| 126 | + outcome := testCase.outcomes[jobName] |
| 127 | + assert.NotNil(t, outcome) |
| 128 | + runner.execTask(t, task, outcome) |
| 129 | + runIndex = task.Context.GetFields()["run_number"].GetStringValue() |
| 130 | + assert.Equal(t, "1", runIndex) |
| 131 | + } |
| 132 | + |
| 133 | + for i := 0; i < len(testCase.outcomes); i++ { |
| 134 | + req := NewRequestWithValues(t, "POST", fmt.Sprintf("/%s/%s/actions/runs/%s/jobs/%d", user2.Name, apiRepo.Name, runIndex, i), map[string]string{ |
| 135 | + "_csrf": GetUserCSRFToken(t, session), |
| 136 | + }) |
| 137 | + resp := session.MakeRequest(t, req, http.StatusOK) |
| 138 | + var listResp actions.ViewResponse |
| 139 | + err := json.Unmarshal(resp.Body.Bytes(), &listResp) |
| 140 | + assert.NoError(t, err) |
| 141 | + assert.Len(t, listResp.State.Run.Jobs, 3) |
| 142 | + |
| 143 | + req = NewRequest(t, "GET", fmt.Sprintf("/%s/%s/actions/runs/%s/jobs/%d/logs", user2.Name, apiRepo.Name, runIndex, i)). |
| 144 | + AddTokenAuth(token) |
| 145 | + MakeRequest(t, req, http.StatusOK) |
| 146 | + } |
| 147 | + |
| 148 | + req := NewRequestWithValues(t, "GET", fmt.Sprintf("/%s/%s/actions/runs/%s", user2.Name, apiRepo.Name, runIndex), map[string]string{ |
| 149 | + "_csrf": GetUserCSRFToken(t, session), |
| 150 | + }) |
| 151 | + session.MakeRequest(t, req, http.StatusOK) |
| 152 | + |
| 153 | + req = NewRequestWithValues(t, "DELETE", fmt.Sprintf("/%s/%s/actions/runs/%s/delete", user2.Name, apiRepo.Name, runIndex), map[string]string{ |
| 154 | + "_csrf": GetUserCSRFToken(t, session), |
| 155 | + }) |
| 156 | + session.MakeRequest(t, req, http.StatusOK) |
| 157 | + |
| 158 | + req = NewRequestWithValues(t, "DELETE", fmt.Sprintf("/%s/%s/actions/runs/%s/delete", user2.Name, apiRepo.Name, runIndex), map[string]string{ |
| 159 | + "_csrf": GetUserCSRFToken(t, session), |
| 160 | + }) |
| 161 | + session.MakeRequest(t, req, http.StatusNotFound) |
| 162 | + |
| 163 | + req = NewRequestWithValues(t, "GET", fmt.Sprintf("/%s/%s/actions/runs/%s", user2.Name, apiRepo.Name, runIndex), map[string]string{ |
| 164 | + "_csrf": GetUserCSRFToken(t, session), |
| 165 | + }) |
| 166 | + session.MakeRequest(t, req, http.StatusNotFound) |
| 167 | + |
| 168 | + for i := 0; i < len(testCase.outcomes); i++ { |
| 169 | + req := NewRequestWithValues(t, "POST", fmt.Sprintf("/%s/%s/actions/runs/%s/jobs/%d", user2.Name, apiRepo.Name, runIndex, i), map[string]string{ |
| 170 | + "_csrf": GetUserCSRFToken(t, session), |
| 171 | + }) |
| 172 | + session.MakeRequest(t, req, http.StatusNotFound) |
| 173 | + |
| 174 | + req = NewRequest(t, "GET", fmt.Sprintf("/%s/%s/actions/runs/%s/jobs/%d/logs", user2.Name, apiRepo.Name, runIndex, i)). |
| 175 | + AddTokenAuth(token) |
| 176 | + MakeRequest(t, req, http.StatusNotFound) |
| 177 | + } |
| 178 | + }) |
| 179 | +} |
0 commit comments