github.com/nektos/act@v0.2.83/pkg/runner/action_cache_test.go (about)

     1  package runner
     2  
     3  import (
     4  	"archive/tar"
     5  	"bytes"
     6  	"context"
     7  	"io"
     8  	"os"
     9  	"testing"
    10  
    11  	"github.com/stretchr/testify/assert"
    12  )
    13  
    14  //nolint:gosec
    15  func TestActionCache(t *testing.T) {
    16  	a := assert.New(t)
    17  	cache := &GoGitActionCache{
    18  		Path: os.TempDir(),
    19  	}
    20  	ctx := context.Background()
    21  	cacheDir := "nektos/act-test-actions"
    22  	repo := "https://github.com/nektos/act-test-actions"
    23  	refs := []struct {
    24  		Name     string
    25  		CacheDir string
    26  		Repo     string
    27  		Ref      string
    28  	}{
    29  		{
    30  			Name:     "Fetch Branch Name",
    31  			CacheDir: cacheDir,
    32  			Repo:     repo,
    33  			Ref:      "main",
    34  		},
    35  		{
    36  			Name:     "Fetch Branch Name Absolutely",
    37  			CacheDir: cacheDir,
    38  			Repo:     repo,
    39  			Ref:      "refs/heads/main",
    40  		},
    41  		{
    42  			Name:     "Fetch HEAD",
    43  			CacheDir: cacheDir,
    44  			Repo:     repo,
    45  			Ref:      "HEAD",
    46  		},
    47  		{
    48  			Name:     "Fetch Sha",
    49  			CacheDir: cacheDir,
    50  			Repo:     repo,
    51  			Ref:      "de984ca37e4df4cb9fd9256435a3b82c4a2662b1",
    52  		},
    53  	}
    54  	for _, c := range refs {
    55  		t.Run(c.Name, func(_ *testing.T) {
    56  			sha, err := cache.Fetch(ctx, c.CacheDir, c.Repo, c.Ref, "")
    57  			if !a.NoError(err) || !a.NotEmpty(sha) {
    58  				return
    59  			}
    60  			atar, err := cache.GetTarArchive(ctx, c.CacheDir, sha, "js")
    61  			if !a.NoError(err) || !a.NotEmpty(atar) {
    62  				return
    63  			}
    64  			mytar := tar.NewReader(atar)
    65  			th, err := mytar.Next()
    66  			if !a.NoError(err) || !a.NotEqual(0, th.Size) {
    67  				return
    68  			}
    69  			buf := &bytes.Buffer{}
    70  			// G110: Potential DoS vulnerability via decompression bomb (gosec)
    71  			_, err = io.Copy(buf, mytar)
    72  			a.NoError(err)
    73  			str := buf.String()
    74  			a.NotEmpty(str)
    75  		})
    76  	}
    77  }
    78  
    79  func TestActionCacheFailures(t *testing.T) {
    80  	a := assert.New(t)
    81  	cache := &GoGitActionCache{
    82  		Path: os.TempDir(),
    83  	}
    84  	ctx := context.Background()
    85  	cacheDir := "nektos/act-test-actions"
    86  	repo := "https://github.com/nektos/act-test-actions-not-exist"
    87  	repoExist := "https://github.com/nektos/act-test-actions"
    88  	refs := []struct {
    89  		Name     string
    90  		CacheDir string
    91  		Repo     string
    92  		Ref      string
    93  	}{
    94  		{
    95  			Name:     "Fetch Branch Name",
    96  			CacheDir: cacheDir,
    97  			Repo:     repo,
    98  			Ref:      "main",
    99  		},
   100  		{
   101  			Name:     "Fetch Branch Name Absolutely",
   102  			CacheDir: cacheDir,
   103  			Repo:     repo,
   104  			Ref:      "refs/heads/main",
   105  		},
   106  		{
   107  			Name:     "Fetch HEAD",
   108  			CacheDir: cacheDir,
   109  			Repo:     repo,
   110  			Ref:      "HEAD",
   111  		},
   112  		{
   113  			Name:     "Fetch Sha",
   114  			CacheDir: cacheDir,
   115  			Repo:     repo,
   116  			Ref:      "de984ca37e4df4cb9fd9256435a3b82c4a2662b1",
   117  		},
   118  		{
   119  			Name:     "Fetch Branch Name no existing",
   120  			CacheDir: cacheDir,
   121  			Repo:     repoExist,
   122  			Ref:      "main2",
   123  		},
   124  		{
   125  			Name:     "Fetch Branch Name Absolutely no existing",
   126  			CacheDir: cacheDir,
   127  			Repo:     repoExist,
   128  			Ref:      "refs/heads/main2",
   129  		},
   130  		{
   131  			Name:     "Fetch Sha no existing",
   132  			CacheDir: cacheDir,
   133  			Repo:     repoExist,
   134  			Ref:      "de984ca37e4df4cb9fd9256435a3b82c4a2662b2",
   135  		},
   136  	}
   137  	for _, c := range refs {
   138  		t.Run(c.Name, func(t *testing.T) {
   139  			_, err := cache.Fetch(ctx, c.CacheDir, c.Repo, c.Ref, "")
   140  			t.Logf("%s\n", err)
   141  			if !a.Error(err) {
   142  				return
   143  			}
   144  		})
   145  	}
   146  }