Files
act_runner/pkg/runner/action_cache_test.go
silverwind 1670945af3 Fix all 93 lint-go errors
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-23 13:59:05 +01:00

145 lines
3.0 KiB
Go

package runner
import (
"archive/tar"
"bytes"
"context"
"io"
"os"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
//nolint:gosec
func TestActionCache(t *testing.T) {
a := assert.New(t)
cache := &GoGitActionCache{
Path: os.TempDir(),
}
ctx := context.Background()
cacheDir := "nektos/act-test-actions"
repo := "https://github.com/nektos/act-test-actions"
refs := []struct {
Name string
CacheDir string
Repo string
Ref string
}{
{
Name: "Fetch Branch Name",
CacheDir: cacheDir,
Repo: repo,
Ref: "main",
},
{
Name: "Fetch Branch Name Absolutely",
CacheDir: cacheDir,
Repo: repo,
Ref: "refs/heads/main",
},
{
Name: "Fetch HEAD",
CacheDir: cacheDir,
Repo: repo,
Ref: "HEAD",
},
{
Name: "Fetch Sha",
CacheDir: cacheDir,
Repo: repo,
Ref: "de984ca37e4df4cb9fd9256435a3b82c4a2662b1",
},
}
for _, c := range refs {
t.Run(c.Name, func(t *testing.T) {
sha, err := cache.Fetch(ctx, c.CacheDir, c.Repo, c.Ref, "")
require.NoError(t, err)
require.NotEmpty(t, sha)
atar, err := cache.GetTarArchive(ctx, c.CacheDir, sha, "js")
require.NoError(t, err)
require.NotEmpty(t, atar)
mytar := tar.NewReader(atar)
th, err := mytar.Next()
require.NoError(t, err)
require.NotEqual(t, 0, th.Size)
buf := &bytes.Buffer{}
// G110: Potential DoS vulnerability via decompression bomb (gosec)
_, err = io.Copy(buf, mytar)
require.NoError(t, err)
str := buf.String()
a.NotEmpty(str)
})
}
}
func TestActionCacheFailures(t *testing.T) {
a := assert.New(t)
cache := &GoGitActionCache{
Path: os.TempDir(),
}
ctx := context.Background()
cacheDir := "nektos/act-test-actions"
repo := "https://github.com/nektos/act-test-actions-not-exist"
repoExist := "https://github.com/nektos/act-test-actions"
refs := []struct {
Name string
CacheDir string
Repo string
Ref string
}{
{
Name: "Fetch Branch Name",
CacheDir: cacheDir,
Repo: repo,
Ref: "main",
},
{
Name: "Fetch Branch Name Absolutely",
CacheDir: cacheDir,
Repo: repo,
Ref: "refs/heads/main",
},
{
Name: "Fetch HEAD",
CacheDir: cacheDir,
Repo: repo,
Ref: "HEAD",
},
{
Name: "Fetch Sha",
CacheDir: cacheDir,
Repo: repo,
Ref: "de984ca37e4df4cb9fd9256435a3b82c4a2662b1",
},
{
Name: "Fetch Branch Name no existing",
CacheDir: cacheDir,
Repo: repoExist,
Ref: "main2",
},
{
Name: "Fetch Branch Name Absolutely no existing",
CacheDir: cacheDir,
Repo: repoExist,
Ref: "refs/heads/main2",
},
{
Name: "Fetch Sha no existing",
CacheDir: cacheDir,
Repo: repoExist,
Ref: "de984ca37e4df4cb9fd9256435a3b82c4a2662b2",
},
}
for _, c := range refs {
t.Run(c.Name, func(t *testing.T) {
_, err := cache.Fetch(ctx, c.CacheDir, c.Repo, c.Ref, "")
t.Logf("%s\n", err)
if !a.Error(err) {
return
}
})
}
}