Files
zeekay ed0990d4d2 Speak ZAP to the forge, not JSON shaped like it
The forge serves its runner surface as five typed zip operations. This is the
half that calls them: zip.Dial and zip.Call by op name, so the body carries the
same bytes both ends hold in memory, and the address is derived rather than
spelled — the forge registers POST /v1/runner/<name>, zip derives
post_runner_<name>, and TestOpNames pins our five literals against zip.ID so
the two cannot drift.

The types moved with the wire, and both moves were forced by it rather than
chosen. A map is refused at encode, so secrets, vars, needs and a job's outputs
cross as []Pair and are indexed once, at the boundary act wants a map at. A
time.Time is accepted and then arrives as the zero time with no error anywhere,
so every instant is an int64 of unix nanoseconds.

Falling out of it:

- Task.Context is a struct. As a map the forge wrote git_runtime_token while
  this runner read gitea_runtime_token and quietly fell back to the task token,
  so ACTIONS_RUNTIME_TOKEN was the wrong secret and the mask that keeps a
  runtime token out of a job's log covered nothing. A field is a compile error.
- A transport timeout is an error again. It was read as "the designed idle path
  for a long poll" — fiction, the forge has never blocked — which counted it as
  an empty response AND reset the error counter, so a forge answering in six
  seconds against a five second wait was indistinguishable from an idle one in
  every log line and every metric, with the counter that would have raised the
  alarm suppressed. There is no long poll: task answers immediately and
  TaskIn.Queue is what makes an idle ask cost nothing.
- fetch_timeout is now the ceiling on the wire. A zip.Call cannot be cancelled
  once it has left, so a per-request context bounds nothing and the wait has to
  sit on the transport, where it bounds every call.
- The credential is a field, so header.go is gone. client.Forge stamps it and
  no caller carries it.
- logs -> log, Row -> Line, NoMore -> Last, SentOutputs -> Stored. You append
  to a log, and the other four addresses are singular.
- Insecure() had no caller.

client.take copies every string and []byte out of a reply, and it is not
belt-and-braces. ZAP reads text zero-copy — zap.Object.Text is an unsafe.String
over the message — and zip.Call hands the response back to fasthttp's pool as
it returns, so a reply's strings point at memory the next call writes over. A
token registered as "runner-secret" read back "1.2.3r-secret" one call later,
the version string of the call in between laid over its first five bytes, and
the forge answered 401 to a runner that was registered. TestOwn fails without
the copy. Delete it when zip copies on decode.

client.dial declares its own transport scheme rather than taking http and
https: declaring a scheme REPLACES what is registered under that name, and
zip's own entries carry a Serve half this one does not — taking them left the
process able to dial plain HTTP through zip and no longer able to serve it.

.runner is untouched. Its uuid and token are exactly the credential the new
wire sends, so a registration made before the cutover keeps working after it,
and a runner only has to be rebuilt.

Requires github.com/hanzoai/git at 92b2e84dba or later. Nothing here has been
exercised against a live forge.
2026-09-06 13:50:41 -07:00

322 lines
7.1 KiB
Go

// Copyright 2023 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package run
import (
"testing"
"github.com/hanzoai/git-runner/act/model"
forge "github.com/hanzoai/git/modules/actions/runner"
"github.com/stretchr/testify/require"
"go.yaml.in/yaml/v4"
"gotest.tools/v3/assert"
)
func Test_generateWorkflow(t *testing.T) {
type args struct {
task *forge.Task
}
tests := []struct {
name string
args args
assert func(t *testing.T, wf *model.Workflow)
want1 string
wantErr bool
}{
{
name: "has needs",
args: args{
task: &forge.Task{
Workflow: []byte(`
name: Build and deploy
on: push
jobs:
job9:
needs: build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- run: ./deploy --build ${{ needs.job1.outputs.output1 }}
- run: ./deploy --build ${{ needs.job2.outputs.output2 }}
`),
Needs: []forge.Need{
{
Job: "job1",
Outputs: []forge.Pair{{Name: "output1", Value: "output1 value"}},
Result: forge.Success,
},
{
Job: "job2",
Outputs: []forge.Pair{{Name: "output2", Value: "output2 value"}},
Result: forge.Success,
},
},
},
},
assert: func(t *testing.T, wf *model.Workflow) {
assert.DeepEqual(t, wf.GetJob("job9").Needs(), []string{"job1", "job2"})
},
want1: "job9",
wantErr: false,
},
{
name: "no needs",
args: args{
task: &forge.Task{
Workflow: []byte(`
name: Simple workflow
on: push
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- run: echo "hello"
`),
Needs: nil,
},
},
assert: func(t *testing.T, wf *model.Workflow) {
job := wf.GetJob("test")
assert.DeepEqual(t, job.Needs(), []string{})
assert.Equal(t, len(job.Steps), 2)
},
want1: "test",
wantErr: false,
},
{
name: "needs list",
args: args{
task: &forge.Task{
Workflow: []byte(`
name: Workflow with list needs
on: push
jobs:
deploy:
needs: [build, test, lint]
runs-on: ubuntu-latest
steps:
- run: echo "deploying"
`),
Needs: []forge.Need{
{Job: "build", Result: forge.Success},
{
Job: "test",
Outputs: []forge.Pair{{Name: "coverage", Value: "80%"}},
Result: forge.Success,
},
{Job: "lint", Result: forge.Failure},
},
},
},
assert: func(t *testing.T, wf *model.Workflow) {
job := wf.GetJob("deploy")
needs := job.Needs()
assert.DeepEqual(t, needs, []string{"build", "lint", "test"})
assert.Equal(t, wf.Jobs["test"].Outputs["coverage"], "80%")
assert.Equal(t, wf.Jobs["lint"].Result, "failure")
},
want1: "deploy",
wantErr: false,
},
{
name: "workflow env and defaults",
args: args{
task: &forge.Task{
Workflow: []byte(`
name: Complex workflow
on:
push:
branches: [main, develop]
pull_request:
types: [opened, synchronize]
env:
NODE_ENV: production
CI: "true"
jobs:
build:
runs-on: ubuntu-latest
env:
BUILD_TYPE: release
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: "18"
- run: npm ci
- run: npm run build
`),
Needs: nil,
},
},
assert: func(t *testing.T, wf *model.Workflow) {
assert.Equal(t, wf.Name, "Complex workflow")
assert.Equal(t, wf.Env["NODE_ENV"], "production")
assert.Equal(t, wf.Env["CI"], "true")
job := wf.GetJob("build")
assert.Equal(t, len(job.Steps), 4)
},
want1: "build",
wantErr: false,
},
{
name: "job with container and services",
args: args{
task: &forge.Task{
Workflow: []byte(`
name: Integration tests
on: push
jobs:
integration:
runs-on: ubuntu-latest
container:
image: node:18
services:
postgres:
image: postgres:15
steps:
- uses: actions/checkout@v3
- run: npm test
`),
Needs: nil,
},
},
assert: func(t *testing.T, wf *model.Workflow) {
job := wf.GetJob("integration")
container := job.Container()
assert.Equal(t, container.Image, "node:18")
assert.Equal(t, job.Services["postgres"].Image, "postgres:15")
},
want1: "integration",
wantErr: false,
},
{
name: "job with matrix strategy",
args: args{
task: &forge.Task{
Workflow: []byte(`
name: Matrix build
on: push
jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
go-version: ["1.21", "1.22"]
steps:
- uses: actions/checkout@v3
- run: go test ./...
`),
Needs: nil,
},
},
assert: func(t *testing.T, wf *model.Workflow) {
job := wf.GetJob("test")
matrixes, err := job.GetMatrixes()
require.NoError(t, err)
assert.Equal(t, len(matrixes), 2)
},
want1: "test",
wantErr: false,
},
{
name: "special yaml characters in values",
args: args{
task: &forge.Task{
Workflow: []byte("name: \"Special: characters & test\"\non: push\n\njobs:\n test:\n runs-on: ubuntu-latest\n steps:\n - run: 'echo \"hello: world\"'\n - run: 'echo \"quotes & ampersands\"'\n - run: |\n echo \"multiline\"\n echo \"script\"\n"),
Needs: nil,
},
},
assert: func(t *testing.T, wf *model.Workflow) {
assert.Equal(t, wf.Name, "Special: characters & test")
job := wf.GetJob("test")
assert.Equal(t, len(job.Steps), 3)
},
want1: "test",
wantErr: false,
},
{
name: "invalid yaml",
args: args{
task: &forge.Task{
Workflow: []byte(`
name: Bad workflow
on: push
jobs:
test:
runs-on: ubuntu-latest
steps:
- run: echo "ok"
bad-indent: true
`),
Needs: nil,
},
},
assert: nil,
want1: "",
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, got1, err := generateWorkflow(tt.args.task)
if tt.wantErr {
require.Error(t, err)
return
}
require.NoError(t, err)
tt.assert(t, got)
assert.Equal(t, got1, tt.want1)
})
}
}
func Test_yamlV4NodeRoundTrip(t *testing.T) {
t.Run("marshal sequence node", func(t *testing.T) {
node := yaml.Node{
Kind: yaml.SequenceNode,
Content: []*yaml.Node{
{Kind: yaml.ScalarNode, Value: "a"},
{Kind: yaml.ScalarNode, Value: "b"},
{Kind: yaml.ScalarNode, Value: "c"},
},
}
out, err := yaml.Marshal(&node)
require.NoError(t, err)
assert.Equal(t, string(out), "- a\n- b\n- c\n")
})
t.Run("unmarshal and re-marshal workflow", func(t *testing.T) {
input := []byte("name: test\non: push\njobs:\n build:\n runs-on: ubuntu-latest\n steps:\n - run: echo hello\n")
var wf map[string]any
err := yaml.Unmarshal(input, &wf)
require.NoError(t, err)
assert.Equal(t, wf["name"], "test")
out, err := yaml.Marshal(wf)
require.NoError(t, err)
var wf2 map[string]any
err = yaml.Unmarshal(out, &wf2)
require.NoError(t, err)
assert.Equal(t, wf2["name"], "test")
})
t.Run("node kind constants", func(t *testing.T) {
// Verify yaml/v4 node kind constants are usable (same API as v3)
require.NotEqual(t, yaml.ScalarNode, yaml.SequenceNode)
require.NotEqual(t, yaml.SequenceNode, yaml.MappingNode)
})
}