Improve Actions test (#32883) (followup)

This commit is contained in:
Earl Warren 2024-12-28 19:38:11 +00:00
parent b18dcd69f2
commit 37f05617a5
No known key found for this signature in database
GPG key ID: 0579CB2928A78A00
3 changed files with 33 additions and 17 deletions

View file

@ -15,6 +15,7 @@ import (
auth_model "code.gitea.io/gitea/models/auth"
"code.gitea.io/gitea/models/unittest"
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/setting"
api "code.gitea.io/gitea/modules/structs"
runnerv1 "code.gitea.io/actions-proto-go/runner/v1"
@ -22,6 +23,9 @@ import (
)
func TestJobWithNeeds(t *testing.T) {
if !setting.Database.Type.IsSQLite3() {
t.Skip()
}
testCases := []struct {
treePath string
fileContent string
@ -31,7 +35,7 @@ func TestJobWithNeeds(t *testing.T) {
{
treePath: ".gitea/workflows/job-with-needs.yml",
fileContent: `name: job-with-needs
on:
on:
push:
paths:
- '.gitea/workflows/job-with-needs.yml'
@ -62,7 +66,7 @@ jobs:
{
treePath: ".gitea/workflows/job-with-needs-fail.yml",
fileContent: `name: job-with-needs-fail
on:
on:
push:
paths:
- '.gitea/workflows/job-with-needs-fail.yml'
@ -90,7 +94,7 @@ jobs:
{
treePath: ".gitea/workflows/job-with-needs-fail-if.yml",
fileContent: `name: job-with-needs-fail-if
on:
on:
push:
paths:
- '.gitea/workflows/job-with-needs-fail-if.yml'
@ -166,6 +170,9 @@ jobs:
}
func TestJobNeedsMatrix(t *testing.T) {
if !setting.Database.Type.IsSQLite3() {
t.Skip()
}
testCases := []struct {
treePath string
fileContent string
@ -175,7 +182,7 @@ func TestJobNeedsMatrix(t *testing.T) {
{
treePath: ".gitea/workflows/jobs-outputs-with-matrix.yml",
fileContent: `name: jobs-outputs-with-matrix
on:
on:
push:
paths:
- '.gitea/workflows/jobs-outputs-with-matrix.yml'
@ -194,7 +201,7 @@ jobs:
id: gen_output
run: |
version="${{ matrix.version }}"
echo "output_${version}=${version}" >> "$GITHUB_OUTPUT"
echo "output_${version}=${version}" >> "$GITHUB_OUTPUT"
job2:
runs-on: ubuntu-latest
needs: [job1]
@ -241,7 +248,7 @@ jobs:
{
treePath: ".gitea/workflows/jobs-outputs-with-matrix-failure.yml",
fileContent: `name: jobs-outputs-with-matrix-failure
on:
on:
push:
paths:
- '.gitea/workflows/jobs-outputs-with-matrix-failure.yml'
@ -260,7 +267,7 @@ jobs:
id: gen_output
run: |
version="${{ matrix.version }}"
echo "output_${version}=${version}" >> "$GITHUB_OUTPUT"
echo "output_${version}=${version}" >> "$GITHUB_OUTPUT"
job2:
runs-on: ubuntu-latest
if: ${{ always() }}

View file

@ -21,10 +21,14 @@ import (
runnerv1 "code.gitea.io/actions-proto-go/runner/v1"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/types/known/timestamppb"
)
func TestDownloadTaskLogs(t *testing.T) {
if !setting.Database.Type.IsSQLite3() {
t.Skip()
}
now := time.Now()
testCases := []struct {
treePath string
@ -35,7 +39,7 @@ func TestDownloadTaskLogs(t *testing.T) {
{
treePath: ".gitea/workflows/download-task-logs-zstd.yml",
fileContent: `name: download-task-logs-zstd
on:
on:
push:
paths:
- '.gitea/workflows/download-task-logs-zstd.yml'
@ -67,7 +71,7 @@ jobs:
{
treePath: ".gitea/workflows/download-task-logs-no-zstd.yml",
fileContent: `name: download-task-logs-no-zstd
on:
on:
push:
paths:
- '.gitea/workflows/download-task-logs-no-zstd.yml'
@ -132,7 +136,7 @@ jobs:
logFileName += ".zst"
}
_, err := storage.Actions.Stat(logFileName)
assert.NoError(t, err)
require.NoError(t, err)
// download task logs and check content
runIndex := task.Context.GetFields()["run_number"].GetStringValue()

View file

@ -19,6 +19,7 @@ import (
"code.gitea.io/actions-proto-go/runner/v1/runnerv1connect"
"connectrpc.com/connect"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/types/known/timestamppb"
)
@ -63,8 +64,8 @@ func (r *mockRunner) doPing(t *testing.T) {
resp, err := r.client.pingServiceClient.Ping(context.Background(), connect.NewRequest(&pingv1.PingRequest{
Data: "mock-runner",
}))
assert.NoError(t, err)
assert.Equal(t, "Hello, mock-runner!", resp.Msg.Data)
require.NoError(t, err)
require.Equal(t, "Hello, mock-runner!", resp.Msg.Data)
}
func (r *mockRunner) doRegister(t *testing.T, name, token string, labels []string) {
@ -75,11 +76,15 @@ func (r *mockRunner) doRegister(t *testing.T, name, token string, labels []strin
Version: "mock-runner-version",
Labels: labels,
}))
assert.NoError(t, err)
require.NoError(t, err)
r.client = newMockRunnerClient(resp.Msg.Runner.Uuid, resp.Msg.Runner.Token)
}
func (r *mockRunner) registerAsRepoRunner(t *testing.T, ownerName, repoName, runnerName string, labels []string) {
if !setting.Database.Type.IsSQLite3() {
// registering a mock runner when using a database other than SQLite leaves leftovers
t.FailNow()
}
session := loginUser(t, ownerName)
token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteRepository)
req := NewRequest(t, "GET", fmt.Sprintf("/api/v1/repos/%s/%s/actions/runners/registration-token", ownerName, repoName)).AddTokenAuth(token)
@ -102,7 +107,7 @@ func (r *mockRunner) fetchTask(t *testing.T, timeout ...time.Duration) *runnerv1
resp, err := r.client.runnerServiceClient.FetchTask(context.Background(), connect.NewRequest(&runnerv1.FetchTaskRequest{
TasksVersion: 0,
}))
assert.NoError(t, err)
require.NoError(t, err)
if resp.Msg.Task != nil {
task = resp.Msg.Task
break
@ -128,7 +133,7 @@ func (r *mockRunner) execTask(t *testing.T, task *runnerv1.Task, outcome *mockTa
Rows: []*runnerv1.LogRow{lr},
NoMore: idx == len(outcome.logRows)-1,
}))
assert.NoError(t, err)
require.NoError(t, err)
assert.EqualValues(t, idx+1, resp.Msg.AckIndex)
}
sentOutputKeys := make([]string, 0, len(outcome.outputs))
@ -140,7 +145,7 @@ func (r *mockRunner) execTask(t *testing.T, task *runnerv1.Task, outcome *mockTa
},
Outputs: map[string]string{outputKey: outputValue},
}))
assert.NoError(t, err)
require.NoError(t, err)
sentOutputKeys = append(sentOutputKeys, outputKey)
assert.ElementsMatch(t, sentOutputKeys, resp.Msg.SentOutputs)
}
@ -152,6 +157,6 @@ func (r *mockRunner) execTask(t *testing.T, task *runnerv1.Task, outcome *mockTa
StoppedAt: timestamppb.Now(),
},
}))
assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, outcome.result, resp.Msg.State.Result)
}