chore: minimal integration test for the LXC backend (#1000)

Reviewed-on: https://code.forgejo.org/forgejo/runner/pulls/1000
Reviewed-by: Michael Kriese <michael.kriese@gmx.de>
Reviewed-by: Mathieu Fenniak <mfenniak@noreply.code.forgejo.org>
This commit is contained in:
earl-warren 2025-09-15 15:46:46 +00:00
commit 6576754256
No known key found for this signature in database
GPG key ID: F128CBE6AB3A7201
3 changed files with 78 additions and 3 deletions

View file

@ -185,6 +185,11 @@ jobs:
EOF
apt --quiet install --yes -qq docker.io make
- name: install LXC
run: |
act/runner/lxc-helpers.sh lxc_prepare_environment
act/runner/lxc-helpers.sh lxc_install_lxc_inside 10.39.28 fdb1
- run: apt-get -q install -qq -y gcc # required for `-race`
- run: make integration-test

View file

@ -13,6 +13,7 @@ import (
"path/filepath"
"runtime"
"strings"
"sync/atomic"
"time"
"github.com/go-git/go-billy/v5/helper/polyfill"
@ -191,12 +192,12 @@ func (e *HostEnvironment) Start(_ bool) common.Executor {
type ptyWriter struct {
Out io.Writer
AutoStop bool
AutoStop atomic.Bool
dirtyLine bool
}
func (w *ptyWriter) Write(buf []byte) (int, error) {
if w.AutoStop && len(buf) > 0 && buf[len(buf)-1] == 4 {
if w.AutoStop.Load() && len(buf) > 0 && buf[len(buf)-1] == 4 {
n, err := w.Out.Write(buf[:len(buf)-1])
if err != nil {
return n, err
@ -365,7 +366,7 @@ func (e *HostEnvironment) exec(ctx context.Context, commandparam []string, cmdli
return fmt.Errorf("RUN %w", err)
}
if tty != nil {
writer.AutoStop = true
writer.AutoStop.Store(true)
if _, err := tty.Write([]byte("\x04")); err != nil {
common.Logger(ctx).Debug("Failed to write EOT")
}

View file

@ -13,6 +13,7 @@ import (
"code.forgejo.org/forgejo/runner/v11/internal/pkg/labels"
"code.forgejo.org/forgejo/runner/v11/internal/pkg/report"
"connectrpc.com/connect"
log "github.com/sirupsen/logrus"
"google.golang.org/protobuf/types/known/structpb"
"github.com/stretchr/testify/assert"
@ -20,6 +21,10 @@ import (
"github.com/stretchr/testify/require"
)
func init() {
log.SetLevel(log.TraceLevel)
}
func TestExplainFailedGenerateWorkflow(t *testing.T) {
logged := ""
log := func(message string, args ...any) {
@ -337,3 +342,67 @@ jobs:
runWorkflow(ctx, cancel, checkKey2Yaml, "push", "refs/heads/main", "step 5: push cache should not be polluted by PR")
})
}
func TestRunnerLXC(t *testing.T) {
if testing.Short() {
t.Skip("skipping integration test")
}
forgejoClient := &forgejoClientMock{}
forgejoClient.On("Address").Return("https://127.0.0.1:8080") // not expected to be used in this test
forgejoClient.On("UpdateLog", mock.Anything, mock.Anything).Return(nil, nil)
forgejoClient.On("UpdateTask", mock.Anything, mock.Anything).
Return(connect.NewResponse(&runnerv1.UpdateTaskResponse{}), nil)
runner := NewRunner(
&config.Config{
Log: config.Log{
JobLevel: "trace",
},
Host: config.Host{
WorkdirParent: t.TempDir(),
},
},
&config.Registration{
Labels: []string{"lxc:lxc://debian:bookworm"},
},
forgejoClient)
require.NotNil(t, runner)
runWorkflow := func(ctx context.Context, cancel context.CancelFunc, yamlContent, eventName, ref, description string) {
task := &runnerv1.Task{
WorkflowPayload: []byte(yamlContent),
Context: &structpb.Struct{
Fields: map[string]*structpb.Value{
"token": structpb.NewStringValue("some token here"),
"forgejo_default_actions_url": structpb.NewStringValue("https://data.forgejo.org"),
"repository": structpb.NewStringValue("runner"),
"event_name": structpb.NewStringValue(eventName),
"ref": structpb.NewStringValue(ref),
},
},
}
reporter := report.NewReporter(ctx, cancel, forgejoClient, task, time.Second)
err := runner.run(ctx, task, reporter)
reporter.Close(nil)
require.NoError(t, err, description)
}
t.Run("OK", func(t *testing.T) {
ctx, cancel := context.WithCancel(t.Context())
defer cancel()
workflow := `
on:
push:
jobs:
job:
runs-on: lxc
steps:
- run: echo OK
`
runWorkflow(ctx, cancel, workflow, "push", "refs/heads/main", "OK")
})
}