loading up the forgejo repo on tangled to test page performance
1// Copyright 2023 The Gitea Authors. All rights reserved.
2// Copyright 2024 The Forgejo Authors c/o Codeberg e.V.. All rights reserved.
3// SPDX-License-Identifier: MIT
4
5package integration
6
7import (
8 "fmt"
9 "net/http"
10 "net/url"
11 "strings"
12 "testing"
13
14 actions_model "forgejo.org/models/actions"
15 unit_model "forgejo.org/models/unit"
16 "forgejo.org/models/unittest"
17 user_model "forgejo.org/models/user"
18 files_service "forgejo.org/services/repository/files"
19 "forgejo.org/tests"
20
21 "github.com/stretchr/testify/assert"
22 "github.com/stretchr/testify/require"
23)
24
25func GetWorkflowRunRedirectURI(t *testing.T, repoURL, workflow string) string {
26 t.Helper()
27
28 req := NewRequest(t, "GET", fmt.Sprintf("%s/actions/workflows/%s/runs/latest", repoURL, workflow))
29 resp := MakeRequest(t, req, http.StatusTemporaryRedirect)
30
31 return resp.Header().Get("Location")
32}
33
34func TestActionsWebRouteLatestWorkflowRun(t *testing.T) {
35 onGiteaRun(t, func(t *testing.T, u *url.URL) {
36 user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
37
38 // create the repo
39 repo, _, f := tests.CreateDeclarativeRepo(t, user2, "actionsTestRepo",
40 []unit_model.Type{unit_model.TypeActions}, nil,
41 []*files_service.ChangeRepoFile{
42 {
43 Operation: "create",
44 TreePath: ".gitea/workflows/workflow-1.yml",
45 ContentReader: strings.NewReader("name: workflow-1\non:\n push:\njobs:\n job-1:\n runs-on: ubuntu-latest\n steps:\n - run: echo helloworld\n"),
46 },
47 {
48 Operation: "create",
49 TreePath: ".gitea/workflows/workflow-2.yml",
50 ContentReader: strings.NewReader("name: workflow-2\non:\n push:\njobs:\n job-2:\n runs-on: ubuntu-latest\n steps:\n - run: echo helloworld\n"),
51 },
52 },
53 )
54 defer f()
55
56 repoURL := repo.HTMLURL()
57
58 t.Run("valid workflows", func(t *testing.T) {
59 defer tests.PrintCurrentTest(t)()
60
61 // two runs have been created
62 assert.Equal(t, 2, unittest.GetCount(t, &actions_model.ActionRun{RepoID: repo.ID}))
63
64 // Get the redirect URIs for both workflows
65 workflowOneURI := GetWorkflowRunRedirectURI(t, repoURL, "workflow-1.yml")
66 workflowTwoURI := GetWorkflowRunRedirectURI(t, repoURL, "workflow-2.yml")
67
68 // Verify that the two are different.
69 assert.NotEqual(t, workflowOneURI, workflowTwoURI)
70
71 // Verify that each points to the correct workflow.
72 workflowOne := unittest.AssertExistsAndLoadBean(t, &actions_model.ActionRun{RepoID: repo.ID, Index: 1})
73 err := workflowOne.LoadAttributes(t.Context())
74 require.NoError(t, err)
75 assert.Equal(t, workflowOneURI, workflowOne.HTMLURL())
76
77 workflowTwo := unittest.AssertExistsAndLoadBean(t, &actions_model.ActionRun{RepoID: repo.ID, Index: 2})
78 err = workflowTwo.LoadAttributes(t.Context())
79 require.NoError(t, err)
80 assert.Equal(t, workflowTwoURI, workflowTwo.HTMLURL())
81 })
82
83 t.Run("check if workflow page shows file name", func(t *testing.T) {
84 defer tests.PrintCurrentTest(t)()
85
86 // Get the redirect URI
87 workflow := "workflow-1.yml"
88 workflowOneURI := GetWorkflowRunRedirectURI(t, repoURL, workflow)
89
90 // Fetch the page that shows information about the run initiated by "workflow-1.yml".
91 // routers/web/repo/actions/view.go: data-workflow-url is constructed using data-workflow-name.
92 req := NewRequest(t, "GET", workflowOneURI)
93 resp := MakeRequest(t, req, http.StatusOK)
94 htmlDoc := NewHTMLParser(t, resp.Body)
95
96 // Verify that URL of the workflow is shown correctly.
97 expectedURL := fmt.Sprintf("/user2/actionsTestRepo/actions?workflow=%s", workflow)
98 htmlDoc.AssertElement(t, fmt.Sprintf("#repo-action-view[data-workflow-url=\"%s\"]", expectedURL), true)
99 })
100
101 t.Run("existing workflow, non-existent branch", func(t *testing.T) {
102 defer tests.PrintCurrentTest(t)()
103
104 req := NewRequest(t, "GET", fmt.Sprintf("%s/actions/workflows/workflow-1.yml/runs/latest?branch=foobar", repoURL))
105 MakeRequest(t, req, http.StatusNotFound)
106 })
107
108 t.Run("non-existing workflow", func(t *testing.T) {
109 defer tests.PrintCurrentTest(t)()
110
111 req := NewRequest(t, "GET", fmt.Sprintf("%s/actions/workflows/workflow-3.yml/runs/latest", repoURL))
112 MakeRequest(t, req, http.StatusNotFound)
113 })
114 })
115}
116
117func TestActionsWebRouteLatestRun(t *testing.T) {
118 onGiteaRun(t, func(t *testing.T, u *url.URL) {
119 user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
120
121 // create the repo
122 repo, _, f := tests.CreateDeclarativeRepo(t, user2, "",
123 []unit_model.Type{unit_model.TypeActions}, nil,
124 []*files_service.ChangeRepoFile{
125 {
126 Operation: "create",
127 TreePath: ".gitea/workflows/pr.yml",
128 ContentReader: strings.NewReader("name: test\non:\n push:\njobs:\n test:\n runs-on: ubuntu-latest\n steps:\n - run: echo helloworld\n"),
129 },
130 },
131 )
132 defer f()
133
134 // a run has been created
135 assert.Equal(t, 1, unittest.GetCount(t, &actions_model.ActionRun{RepoID: repo.ID}))
136
137 // Hit the `/actions/runs/latest` route
138 req := NewRequest(t, "GET", fmt.Sprintf("%s/actions/runs/latest", repo.HTMLURL()))
139 resp := MakeRequest(t, req, http.StatusTemporaryRedirect)
140
141 // Verify that it redirects to the run we just created
142 workflow := unittest.AssertExistsAndLoadBean(t, &actions_model.ActionRun{RepoID: repo.ID})
143 err := workflow.LoadAttributes(t.Context())
144 require.NoError(t, err)
145
146 assert.Equal(t, workflow.HTMLURL(), resp.Header().Get("Location"))
147 })
148}
149
150func TestActionsArtifactDeletion(t *testing.T) {
151 onGiteaRun(t, func(t *testing.T, u *url.URL) {
152 user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
153
154 // create the repo
155 repo, _, f := tests.CreateDeclarativeRepo(t, user2, "",
156 []unit_model.Type{unit_model.TypeActions}, nil,
157 []*files_service.ChangeRepoFile{
158 {
159 Operation: "create",
160 TreePath: ".gitea/workflows/pr.yml",
161 ContentReader: strings.NewReader("name: test\non:\n push:\njobs:\n test:\n runs-on: ubuntu-latest\n steps:\n - run: echo helloworld\n"),
162 },
163 },
164 )
165 defer f()
166
167 // a run has been created
168 assert.Equal(t, 1, unittest.GetCount(t, &actions_model.ActionRun{RepoID: repo.ID}))
169
170 // Load the run we just created
171 run := unittest.AssertExistsAndLoadBean(t, &actions_model.ActionRun{RepoID: repo.ID})
172 err := run.LoadAttributes(t.Context())
173 require.NoError(t, err)
174
175 // Visit it's web view
176 req := NewRequest(t, "GET", run.HTMLURL())
177 resp := MakeRequest(t, req, http.StatusOK)
178 htmlDoc := NewHTMLParser(t, resp.Body)
179
180 // Assert that the artifact deletion markup exists
181 htmlDoc.AssertElement(t, "[data-locale-confirm-delete-artifact]", true)
182 })
183}