mirror of
https://gitea.com/gitea/act_runner
synced 2026-09-21 19:37:07 +02:00
Replace the Tart-based prototype with a runner-oriented macOS VM executor backed by runner-executor-macos and Apple Virtualization. Assisted-by: Codet:MODEL_VERSION
204 lines
4.9 KiB
Go
204 lines
4.9 KiB
Go
// Copyright 2023 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package labels
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
"gotest.tools/v3/assert"
|
|
)
|
|
|
|
func TestParse(t *testing.T) {
|
|
tests := []struct {
|
|
args string
|
|
want *Label
|
|
wantErr bool
|
|
}{
|
|
{
|
|
args: "ubuntu:docker://node:18",
|
|
want: &Label{
|
|
Name: "ubuntu",
|
|
Schema: "docker",
|
|
Arg: "//node:18",
|
|
},
|
|
wantErr: false,
|
|
},
|
|
{
|
|
args: "macos-latest:macos-vm://ghcr.io/cirruslabs/macos-sonoma-base:latest",
|
|
want: &Label{
|
|
Name: "macos-latest",
|
|
Schema: "macos-vm",
|
|
Arg: "//ghcr.io/cirruslabs/macos-sonoma-base:latest",
|
|
},
|
|
wantErr: false,
|
|
},
|
|
{
|
|
args: "ubuntu:host",
|
|
want: &Label{
|
|
Name: "ubuntu",
|
|
Schema: "host",
|
|
Arg: "",
|
|
},
|
|
wantErr: false,
|
|
},
|
|
{
|
|
args: "ubuntu",
|
|
want: &Label{
|
|
Name: "ubuntu",
|
|
Schema: "host",
|
|
Arg: "",
|
|
},
|
|
wantErr: false,
|
|
},
|
|
{
|
|
args: "pool:e57e18d4-10d4-406f-93bf-60f127221bdd",
|
|
want: &Label{
|
|
Name: "pool:e57e18d4-10d4-406f-93bf-60f127221bdd",
|
|
Schema: "host",
|
|
Arg: "",
|
|
Opaque: true,
|
|
},
|
|
wantErr: false,
|
|
},
|
|
{
|
|
args: "ubuntu:vm:ubuntu-18.04",
|
|
want: &Label{
|
|
Name: "ubuntu:vm:ubuntu-18.04",
|
|
Schema: "host",
|
|
Arg: "",
|
|
Opaque: true,
|
|
},
|
|
wantErr: false,
|
|
},
|
|
{
|
|
args: "",
|
|
want: nil,
|
|
wantErr: true,
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.args, func(t *testing.T) {
|
|
got, err := Parse(tt.args)
|
|
if tt.wantErr {
|
|
require.Error(t, err)
|
|
return
|
|
}
|
|
require.NoError(t, err)
|
|
assert.DeepEqual(t, got, tt.want)
|
|
})
|
|
}
|
|
}
|
|
|
|
// mustParse parses the given label strings, failing the test on any error.
|
|
func mustParse(t *testing.T, strs ...string) Labels {
|
|
t.Helper()
|
|
ls := make(Labels, 0, len(strs))
|
|
for _, s := range strs {
|
|
l, err := Parse(s)
|
|
require.NoError(t, err)
|
|
ls = append(ls, l)
|
|
}
|
|
return ls
|
|
}
|
|
|
|
func TestRequireDocker(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
strs []string
|
|
want bool
|
|
}{
|
|
{"empty", nil, false},
|
|
{"only host", []string{"ubuntu:host", "self-hosted"}, false},
|
|
{"has docker", []string{"ubuntu:host", "ubuntu:docker://node:18"}, true},
|
|
{"macos-vm does not require docker", []string{"macos-latest:macos-vm://ghcr.io/cirruslabs/macos-sonoma-base:latest"}, false},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
require.Equal(t, tt.want, mustParse(t, tt.strs...).RequireDocker())
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestRequireMacOSVM(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
strs []string
|
|
want bool
|
|
}{
|
|
{"empty", nil, false},
|
|
{"only host", []string{"ubuntu:host", "self-hosted"}, false},
|
|
{"only docker", []string{"ubuntu:docker://node:18"}, false},
|
|
{"has macos-vm", []string{"macos-latest:macos-vm://ghcr.io/cirruslabs/macos-sonoma-base:latest"}, true},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
require.Equal(t, tt.want, mustParse(t, tt.strs...).RequireMacOSVM())
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestPickPlatform(t *testing.T) {
|
|
ls := mustParse(t,
|
|
"ubuntu:docker://node:18",
|
|
"self-hosted:host",
|
|
"macos-latest:macos-vm://ghcr.io/cirruslabs/macos-sonoma-base:latest",
|
|
)
|
|
|
|
tests := []struct {
|
|
name string
|
|
runsOn []string
|
|
want string
|
|
}{
|
|
{"docker strips leading slashes", []string{"ubuntu"}, "node:18"},
|
|
{"host maps to self-hosted marker", []string{"self-hosted"}, SelfHostedPlatform},
|
|
{"macos-vm prefixes platform marker", []string{"macos-latest"}, MacOSVMSchemePrefix + "ghcr.io/cirruslabs/macos-sonoma-base:latest"},
|
|
{"first match wins", []string{"self-hosted", "ubuntu"}, SelfHostedPlatform},
|
|
{"unknown label picks nothing", []string{"windows"}, ""},
|
|
{"no runsOn picks nothing", nil, ""},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
require.Equal(t, tt.want, ls.PickPlatform(tt.runsOn))
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestNames(t *testing.T) {
|
|
ls := mustParse(t, "ubuntu:docker://node:18", "self-hosted:host", "pool:e57e18d4")
|
|
require.Equal(t, []string{"ubuntu", "self-hosted", "pool:e57e18d4"}, ls.Names())
|
|
require.Empty(t, Labels{}.Names())
|
|
}
|
|
|
|
func TestToStrings(t *testing.T) {
|
|
ls := mustParse(t,
|
|
"ubuntu:docker://node:18",
|
|
"self-hosted:host",
|
|
"macos-latest:macos-vm://ghcr.io/cirruslabs/macos-sonoma-base:latest",
|
|
"bare",
|
|
"pool:e57e18d4",
|
|
)
|
|
require.Equal(t, []string{
|
|
"ubuntu:docker://node:18",
|
|
"self-hosted:host",
|
|
"macos-latest:macos-vm://ghcr.io/cirruslabs/macos-sonoma-base:latest",
|
|
"bare:host",
|
|
"pool:e57e18d4",
|
|
}, ls.ToStrings())
|
|
}
|
|
|
|
// a colon-containing name must survive a write to and read back from the .runner file
|
|
func TestOpaqueLabelRoundTrip(t *testing.T) {
|
|
const raw = "pool:e57e18d4-10d4-406f-93bf-60f127221bdd"
|
|
|
|
ls := mustParse(t, raw)
|
|
require.Equal(t, []string{raw}, ls.ToStrings())
|
|
|
|
again := mustParse(t, ls.ToStrings()...)
|
|
require.Equal(t, ls, again)
|
|
require.Equal(t, []string{raw}, again.Names())
|
|
require.False(t, again.RequireDocker())
|
|
require.Equal(t, SelfHostedPlatform, again.PickPlatform([]string{raw}))
|
|
}
|