mirror of
https://github.com/hanzoai/forge.git
synced 2026-10-30 15:00:39 +00:00
Source files keep their SPDX-License-Identifier and Hanzo copyright line; the Gitea, Gogs and LFS Test Server copyrights they used to repeat now live once in NOTICE, alongside LICENSE. The header lint accepts any copyright line above the SPDX line, so it no longer demands an upstream one. Co-authored-by: Hanzo Dev <[email protected]>
178 lines
5.5 KiB
Go
178 lines
5.5 KiB
Go
// Copyright 2026 Hanzo AI, Inc. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package integration
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"net/url"
|
|
"strconv"
|
|
"testing"
|
|
"time"
|
|
|
|
actions_model "github.com/hanzoai/git/models/actions"
|
|
auth_model "github.com/hanzoai/git/models/auth"
|
|
"github.com/hanzoai/git/models/unittest"
|
|
user_model "github.com/hanzoai/git/models/user"
|
|
runner_module "github.com/hanzoai/forge/modules/actions/runner"
|
|
"github.com/hanzoai/git/modules/json"
|
|
"github.com/hanzoai/git/routers/web/repo/actions"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestActionsDeleteRun(t *testing.T) {
|
|
now := time.Now()
|
|
testCase := struct {
|
|
treePath string
|
|
fileContent string
|
|
outcomes map[string]*mockTaskOutcome
|
|
expectedStatuses map[string]string
|
|
}{
|
|
treePath: ".hanzo/workflows/test1.yml",
|
|
fileContent: `name: test1
|
|
on:
|
|
push:
|
|
paths:
|
|
- .hanzo/workflows/test1.yml
|
|
jobs:
|
|
job1:
|
|
runs-on: ubuntu-latest
|
|
steps:
|
|
- run: echo job1
|
|
job2:
|
|
runs-on: ubuntu-latest
|
|
steps:
|
|
- run: echo job2
|
|
job3:
|
|
runs-on: ubuntu-latest
|
|
steps:
|
|
- run: echo job3
|
|
`,
|
|
outcomes: map[string]*mockTaskOutcome{
|
|
"job1": {
|
|
result: runner_module.Success,
|
|
logLines: []runner_module.Line{
|
|
{
|
|
Time: now.Add(4 * time.Second).UnixNano(),
|
|
Content: " \U0001F433 docker create image",
|
|
},
|
|
{
|
|
Time: now.Add(5 * time.Second).UnixNano(),
|
|
Content: "job1",
|
|
},
|
|
{
|
|
Time: now.Add(6 * time.Second).UnixNano(),
|
|
Content: "\U0001F3C1 Job succeeded",
|
|
},
|
|
},
|
|
},
|
|
"job2": {
|
|
result: runner_module.Success,
|
|
logLines: []runner_module.Line{
|
|
{
|
|
Time: now.Add(4 * time.Second).UnixNano(),
|
|
Content: " \U0001F433 docker create image",
|
|
},
|
|
{
|
|
Time: now.Add(5 * time.Second).UnixNano(),
|
|
Content: "job2",
|
|
},
|
|
{
|
|
Time: now.Add(6 * time.Second).UnixNano(),
|
|
Content: "\U0001F3C1 Job succeeded",
|
|
},
|
|
},
|
|
},
|
|
"job3": {
|
|
result: runner_module.Success,
|
|
logLines: []runner_module.Line{
|
|
{
|
|
Time: now.Add(4 * time.Second).UnixNano(),
|
|
Content: " \U0001F433 docker create image",
|
|
},
|
|
{
|
|
Time: now.Add(5 * time.Second).UnixNano(),
|
|
Content: "job3",
|
|
},
|
|
{
|
|
Time: now.Add(6 * time.Second).UnixNano(),
|
|
Content: "\U0001F3C1 Job succeeded",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
expectedStatuses: map[string]string{
|
|
"job1": actions_model.StatusSuccess.String(),
|
|
"job2": actions_model.StatusSuccess.String(),
|
|
"job3": actions_model.StatusSuccess.String(),
|
|
},
|
|
}
|
|
onGitRun(t, func(t *testing.T, u *url.URL) {
|
|
user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
|
|
session := loginUser(t, user2.Name)
|
|
token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteRepository, auth_model.AccessTokenScopeWriteUser)
|
|
|
|
apiRepo := createActionsTestRepo(t, token, "actions-delete-run-test", false)
|
|
runner := newMockRunner()
|
|
runner.registerAsRepoRunner(t, user2.Name, apiRepo.Name, "mock-runner", []string{"ubuntu-latest"}, false)
|
|
|
|
opts := getWorkflowCreateFileOptions(user2, apiRepo.DefaultBranch, "create "+testCase.treePath, testCase.fileContent)
|
|
createWorkflowFile(t, token, user2.Name, apiRepo.Name, testCase.treePath, opts)
|
|
|
|
var runID int64
|
|
for i := 0; i < len(testCase.outcomes); i++ {
|
|
task := runner.fetchTask(t)
|
|
jobName := getTaskJobNameByTaskID(t, token, user2.Name, apiRepo.Name, task.ID)
|
|
outcome := testCase.outcomes[jobName]
|
|
assert.NotNil(t, outcome)
|
|
runner.execTask(t, task, outcome)
|
|
runIndex := task.Context.RunNumber
|
|
parsedRunIndex, err := strconv.ParseInt(runIndex, 10, 64)
|
|
assert.NoError(t, err)
|
|
run := unittest.AssertExistsAndLoadBean(t, &actions_model.ActionRun{RepoID: apiRepo.ID, Index: parsedRunIndex})
|
|
runID = run.ID
|
|
}
|
|
|
|
jobs, err := actions_model.GetLatestAttemptJobsByRepoAndRunID(t.Context(), apiRepo.ID, runID)
|
|
assert.NoError(t, err)
|
|
|
|
for i := 0; i < len(testCase.outcomes); i++ {
|
|
jobID := jobs[i].ID
|
|
req := NewRequest(t, "POST", fmt.Sprintf("/%s/%s/actions/runs/%d/jobs/%d", user2.Name, apiRepo.Name, runID, jobID))
|
|
resp := session.MakeRequest(t, req, http.StatusOK)
|
|
var listResp actions.ViewResponse
|
|
err := json.Unmarshal(resp.Body.Bytes(), &listResp)
|
|
assert.NoError(t, err)
|
|
assert.Len(t, listResp.State.Run.Jobs, 3)
|
|
|
|
req = NewRequest(t, "GET", fmt.Sprintf("/%s/%s/actions/runs/%d/jobs/%d/logs", user2.Name, apiRepo.Name, runID, jobID)).
|
|
AddTokenAuth(token)
|
|
MakeRequest(t, req, http.StatusOK)
|
|
}
|
|
|
|
req := NewRequest(t, "GET", fmt.Sprintf("/%s/%s/actions/runs/%d", user2.Name, apiRepo.Name, runID))
|
|
session.MakeRequest(t, req, http.StatusOK)
|
|
|
|
req = NewRequest(t, "POST", fmt.Sprintf("/%s/%s/actions/runs/%d/delete", user2.Name, apiRepo.Name, runID))
|
|
session.MakeRequest(t, req, http.StatusOK)
|
|
|
|
req = NewRequest(t, "POST", fmt.Sprintf("/%s/%s/actions/runs/%d/delete", user2.Name, apiRepo.Name, runID))
|
|
session.MakeRequest(t, req, http.StatusNotFound)
|
|
|
|
req = NewRequest(t, "GET", fmt.Sprintf("/%s/%s/actions/runs/%d", user2.Name, apiRepo.Name, runID))
|
|
session.MakeRequest(t, req, http.StatusNotFound)
|
|
|
|
for i := 0; i < len(testCase.outcomes); i++ {
|
|
jobID := jobs[i].ID
|
|
req := NewRequest(t, "POST", fmt.Sprintf("/%s/%s/actions/runs/%d/jobs/%d", user2.Name, apiRepo.Name, runID, jobID))
|
|
session.MakeRequest(t, req, http.StatusNotFound)
|
|
|
|
req = NewRequest(t, "GET", fmt.Sprintf("/%s/%s/actions/runs/%d/jobs/%d/logs", user2.Name, apiRepo.Name, runID, jobID)).
|
|
AddTokenAuth(token)
|
|
MakeRequest(t, req, http.StatusNotFound)
|
|
}
|
|
})
|
|
}
|