2021-11-10 11:57:22 -06:00
|
|
|
package artifacts
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"testing/fstest"
|
|
|
|
|
|
|
|
"github.com/julienschmidt/httprouter"
|
|
|
|
"github.com/nektos/act/pkg/model"
|
|
|
|
"github.com/nektos/act/pkg/runner"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2023-01-16 15:01:54 -06:00
|
|
|
type writableMapFile struct {
|
|
|
|
fstest.MapFile
|
2021-11-10 11:57:22 -06:00
|
|
|
}
|
|
|
|
|
2023-01-16 15:01:54 -06:00
|
|
|
func (f *writableMapFile) Write(data []byte) (int, error) {
|
|
|
|
f.Data = data
|
|
|
|
return len(data), nil
|
2021-11-10 11:57:22 -06:00
|
|
|
}
|
|
|
|
|
2023-01-16 15:01:54 -06:00
|
|
|
func (f *writableMapFile) Close() error {
|
|
|
|
return nil
|
2021-11-10 11:57:22 -06:00
|
|
|
}
|
|
|
|
|
2023-01-16 15:01:54 -06:00
|
|
|
type writeMapFS struct {
|
|
|
|
fstest.MapFS
|
2021-11-10 11:57:22 -06:00
|
|
|
}
|
|
|
|
|
2023-01-16 15:01:54 -06:00
|
|
|
func (fsys writeMapFS) OpenWritable(name string) (WritableFile, error) {
|
|
|
|
var file = &writableMapFile{
|
|
|
|
MapFile: fstest.MapFile{
|
|
|
|
Data: []byte("content2"),
|
|
|
|
},
|
2021-11-10 11:57:22 -06:00
|
|
|
}
|
2023-01-16 15:01:54 -06:00
|
|
|
fsys.MapFS[name] = &file.MapFile
|
2021-11-10 11:57:22 -06:00
|
|
|
|
2023-01-16 15:01:54 -06:00
|
|
|
return file, nil
|
2021-11-10 11:57:22 -06:00
|
|
|
}
|
2022-06-20 17:06:55 -05:00
|
|
|
|
2023-01-16 15:01:54 -06:00
|
|
|
func (fsys writeMapFS) OpenAppendable(name string) (WritableFile, error) {
|
|
|
|
var file = &writableMapFile{
|
|
|
|
MapFile: fstest.MapFile{
|
|
|
|
Data: []byte("content2"),
|
|
|
|
},
|
2022-06-20 17:06:55 -05:00
|
|
|
}
|
2023-01-16 15:01:54 -06:00
|
|
|
fsys.MapFS[name] = &file.MapFile
|
2022-06-20 17:06:55 -05:00
|
|
|
|
2023-01-16 15:01:54 -06:00
|
|
|
return file, nil
|
2022-06-20 17:06:55 -05:00
|
|
|
}
|
2021-11-10 11:57:22 -06:00
|
|
|
|
|
|
|
func TestNewArtifactUploadPrepare(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
var memfs = fstest.MapFS(map[string]*fstest.MapFile{})
|
|
|
|
|
|
|
|
router := httprouter.New()
|
2023-01-16 15:01:54 -06:00
|
|
|
uploads(router, "artifact/server/path", writeMapFS{memfs})
|
2021-11-10 11:57:22 -06:00
|
|
|
|
|
|
|
req, _ := http.NewRequest("POST", "http://localhost/_apis/pipelines/workflows/1/artifacts", nil)
|
|
|
|
rr := httptest.NewRecorder()
|
|
|
|
|
|
|
|
router.ServeHTTP(rr, req)
|
|
|
|
|
|
|
|
if status := rr.Code; status != http.StatusOK {
|
|
|
|
assert.Fail("Wrong status")
|
|
|
|
}
|
|
|
|
|
|
|
|
response := FileContainerResourceURL{}
|
|
|
|
err := json.Unmarshal(rr.Body.Bytes(), &response)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal("http://localhost/upload/1", response.FileContainerResourceURL)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestArtifactUploadBlob(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
var memfs = fstest.MapFS(map[string]*fstest.MapFile{})
|
|
|
|
|
|
|
|
router := httprouter.New()
|
2023-01-16 15:01:54 -06:00
|
|
|
uploads(router, "artifact/server/path", writeMapFS{memfs})
|
2021-11-10 11:57:22 -06:00
|
|
|
|
|
|
|
req, _ := http.NewRequest("PUT", "http://localhost/upload/1?itemPath=some/file", strings.NewReader("content"))
|
|
|
|
rr := httptest.NewRecorder()
|
|
|
|
|
|
|
|
router.ServeHTTP(rr, req)
|
|
|
|
|
|
|
|
if status := rr.Code; status != http.StatusOK {
|
|
|
|
assert.Fail("Wrong status")
|
|
|
|
}
|
|
|
|
|
|
|
|
response := ResponseMessage{}
|
|
|
|
err := json.Unmarshal(rr.Body.Bytes(), &response)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal("success", response.Message)
|
2023-01-16 15:01:54 -06:00
|
|
|
assert.Equal("content", string(memfs["artifact/server/path/1/some/file"].Data))
|
2021-11-10 11:57:22 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestFinalizeArtifactUpload(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
var memfs = fstest.MapFS(map[string]*fstest.MapFile{})
|
|
|
|
|
|
|
|
router := httprouter.New()
|
2023-01-16 15:01:54 -06:00
|
|
|
uploads(router, "artifact/server/path", writeMapFS{memfs})
|
2021-11-10 11:57:22 -06:00
|
|
|
|
|
|
|
req, _ := http.NewRequest("PATCH", "http://localhost/_apis/pipelines/workflows/1/artifacts", nil)
|
|
|
|
rr := httptest.NewRecorder()
|
|
|
|
|
|
|
|
router.ServeHTTP(rr, req)
|
|
|
|
|
|
|
|
if status := rr.Code; status != http.StatusOK {
|
|
|
|
assert.Fail("Wrong status")
|
|
|
|
}
|
|
|
|
|
|
|
|
response := ResponseMessage{}
|
|
|
|
err := json.Unmarshal(rr.Body.Bytes(), &response)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal("success", response.Message)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestListArtifacts(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
var memfs = fstest.MapFS(map[string]*fstest.MapFile{
|
2023-01-16 15:01:54 -06:00
|
|
|
"artifact/server/path/1/file.txt": {
|
2021-11-10 11:57:22 -06:00
|
|
|
Data: []byte(""),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
router := httprouter.New()
|
2023-01-16 15:01:54 -06:00
|
|
|
downloads(router, "artifact/server/path", memfs)
|
2021-11-10 11:57:22 -06:00
|
|
|
|
|
|
|
req, _ := http.NewRequest("GET", "http://localhost/_apis/pipelines/workflows/1/artifacts", nil)
|
|
|
|
rr := httptest.NewRecorder()
|
|
|
|
|
|
|
|
router.ServeHTTP(rr, req)
|
|
|
|
|
|
|
|
if status := rr.Code; status != http.StatusOK {
|
|
|
|
assert.FailNow(fmt.Sprintf("Wrong status: %d", status))
|
|
|
|
}
|
|
|
|
|
|
|
|
response := NamedFileContainerResourceURLResponse{}
|
|
|
|
err := json.Unmarshal(rr.Body.Bytes(), &response)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(1, response.Count)
|
|
|
|
assert.Equal("file.txt", response.Value[0].Name)
|
|
|
|
assert.Equal("http://localhost/download/1", response.Value[0].FileContainerResourceURL)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestListArtifactContainer(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
var memfs = fstest.MapFS(map[string]*fstest.MapFile{
|
2023-01-16 15:01:54 -06:00
|
|
|
"artifact/server/path/1/some/file": {
|
2021-11-10 11:57:22 -06:00
|
|
|
Data: []byte(""),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
router := httprouter.New()
|
2023-01-16 15:01:54 -06:00
|
|
|
downloads(router, "artifact/server/path", memfs)
|
2021-11-10 11:57:22 -06:00
|
|
|
|
|
|
|
req, _ := http.NewRequest("GET", "http://localhost/download/1?itemPath=some/file", nil)
|
|
|
|
rr := httptest.NewRecorder()
|
|
|
|
|
|
|
|
router.ServeHTTP(rr, req)
|
|
|
|
|
|
|
|
if status := rr.Code; status != http.StatusOK {
|
|
|
|
assert.FailNow(fmt.Sprintf("Wrong status: %d", status))
|
|
|
|
}
|
|
|
|
|
|
|
|
response := ContainerItemResponse{}
|
|
|
|
err := json.Unmarshal(rr.Body.Bytes(), &response)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(1, len(response.Value))
|
2023-01-16 15:01:54 -06:00
|
|
|
assert.Equal("some/file", response.Value[0].Path)
|
2021-11-10 11:57:22 -06:00
|
|
|
assert.Equal("file", response.Value[0].ItemType)
|
|
|
|
assert.Equal("http://localhost/artifact/1/some/file/.", response.Value[0].ContentLocation)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDownloadArtifactFile(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
var memfs = fstest.MapFS(map[string]*fstest.MapFile{
|
2023-01-16 15:01:54 -06:00
|
|
|
"artifact/server/path/1/some/file": {
|
2021-11-10 11:57:22 -06:00
|
|
|
Data: []byte("content"),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
router := httprouter.New()
|
2023-01-16 15:01:54 -06:00
|
|
|
downloads(router, "artifact/server/path", memfs)
|
2021-11-10 11:57:22 -06:00
|
|
|
|
|
|
|
req, _ := http.NewRequest("GET", "http://localhost/artifact/1/some/file", nil)
|
|
|
|
rr := httptest.NewRecorder()
|
|
|
|
|
|
|
|
router.ServeHTTP(rr, req)
|
|
|
|
|
|
|
|
if status := rr.Code; status != http.StatusOK {
|
|
|
|
assert.FailNow(fmt.Sprintf("Wrong status: %d", status))
|
|
|
|
}
|
|
|
|
|
|
|
|
data := rr.Body.Bytes()
|
|
|
|
|
|
|
|
assert.Equal("content", string(data))
|
|
|
|
}
|
|
|
|
|
|
|
|
type TestJobFileInfo struct {
|
|
|
|
workdir string
|
|
|
|
workflowPath string
|
|
|
|
eventName string
|
|
|
|
errorMessage string
|
|
|
|
platforms map[string]string
|
|
|
|
containerArchitecture string
|
|
|
|
}
|
|
|
|
|
2023-01-16 08:12:20 -06:00
|
|
|
var artifactsPath = path.Join(os.TempDir(), "test-artifacts")
|
|
|
|
var artifactsAddr = "127.0.0.1"
|
2021-11-10 11:57:22 -06:00
|
|
|
var artifactsPort = "12345"
|
|
|
|
|
|
|
|
func TestArtifactFlow(t *testing.T) {
|
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("skipping integration test")
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
2023-01-16 08:12:20 -06:00
|
|
|
cancel := Serve(ctx, artifactsPath, artifactsAddr, artifactsPort)
|
2021-11-10 11:57:22 -06:00
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
platforms := map[string]string{
|
2021-12-22 13:34:18 -06:00
|
|
|
"ubuntu-latest": "node:16-buster-slim",
|
2021-11-10 11:57:22 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
tables := []TestJobFileInfo{
|
|
|
|
{"testdata", "upload-and-download", "push", "", platforms, ""},
|
2023-01-16 15:01:54 -06:00
|
|
|
{"testdata", "GHSL-2023-004", "push", "", platforms, ""},
|
2021-11-10 11:57:22 -06:00
|
|
|
}
|
|
|
|
log.SetLevel(log.DebugLevel)
|
|
|
|
|
|
|
|
for _, table := range tables {
|
|
|
|
runTestJobFile(ctx, t, table)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func runTestJobFile(ctx context.Context, t *testing.T, tjfi TestJobFileInfo) {
|
|
|
|
t.Run(tjfi.workflowPath, func(t *testing.T) {
|
2022-04-20 13:34:27 -05:00
|
|
|
fmt.Printf("::group::%s\n", tjfi.workflowPath)
|
|
|
|
|
2023-01-16 08:12:20 -06:00
|
|
|
if err := os.RemoveAll(artifactsPath); err != nil {
|
2021-11-10 11:57:22 -06:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
workdir, err := filepath.Abs(tjfi.workdir)
|
|
|
|
assert.Nil(t, err, workdir)
|
|
|
|
fullWorkflowPath := filepath.Join(workdir, tjfi.workflowPath)
|
|
|
|
runnerConfig := &runner.Config{
|
|
|
|
Workdir: workdir,
|
|
|
|
BindWorkdir: false,
|
|
|
|
EventName: tjfi.eventName,
|
|
|
|
Platforms: tjfi.platforms,
|
|
|
|
ReuseContainers: false,
|
|
|
|
ContainerArchitecture: tjfi.containerArchitecture,
|
|
|
|
GitHubInstance: "github.com",
|
2023-01-16 08:12:20 -06:00
|
|
|
ArtifactServerPath: artifactsPath,
|
|
|
|
ArtifactServerAddr: artifactsAddr,
|
2021-11-10 11:57:22 -06:00
|
|
|
ArtifactServerPort: artifactsPort,
|
|
|
|
}
|
|
|
|
|
|
|
|
runner, err := runner.New(runnerConfig)
|
|
|
|
assert.Nil(t, err, tjfi.workflowPath)
|
|
|
|
|
|
|
|
planner, err := model.NewWorkflowPlanner(fullWorkflowPath, true)
|
|
|
|
assert.Nil(t, err, fullWorkflowPath)
|
|
|
|
|
2023-02-16 10:41:59 -06:00
|
|
|
plan, err := planner.PlanEvent(tjfi.eventName)
|
|
|
|
if err == nil {
|
|
|
|
err = runner.NewPlanExecutor(plan)(ctx)
|
|
|
|
if tjfi.errorMessage == "" {
|
|
|
|
assert.Nil(t, err, fullWorkflowPath)
|
|
|
|
} else {
|
|
|
|
assert.Error(t, err, tjfi.errorMessage)
|
|
|
|
}
|
2021-11-10 11:57:22 -06:00
|
|
|
} else {
|
2023-02-16 10:41:59 -06:00
|
|
|
assert.Nil(t, plan)
|
2021-11-10 11:57:22 -06:00
|
|
|
}
|
2022-04-20 13:34:27 -05:00
|
|
|
|
|
|
|
fmt.Println("::endgroup::")
|
2021-11-10 11:57:22 -06:00
|
|
|
})
|
|
|
|
}
|
2023-01-16 15:01:54 -06:00
|
|
|
|
|
|
|
func TestMkdirFsImplSafeResolve(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
baseDir := "/foo/bar"
|
|
|
|
|
|
|
|
tests := map[string]struct {
|
|
|
|
input string
|
|
|
|
want string
|
|
|
|
}{
|
|
|
|
"simple": {input: "baz", want: "/foo/bar/baz"},
|
|
|
|
"nested": {input: "baz/blue", want: "/foo/bar/baz/blue"},
|
|
|
|
"dots in middle": {input: "baz/../../blue", want: "/foo/bar/blue"},
|
|
|
|
"leading dots": {input: "../../parent", want: "/foo/bar/parent"},
|
|
|
|
"root path": {input: "/root", want: "/foo/bar/root"},
|
|
|
|
"root": {input: "/", want: "/foo/bar"},
|
|
|
|
"empty": {input: "", want: "/foo/bar"},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range tests {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert.Equal(tc.want, safeResolve(baseDir, tc.input))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDownloadArtifactFileUnsafePath(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
var memfs = fstest.MapFS(map[string]*fstest.MapFile{
|
|
|
|
"artifact/server/path/some/file": {
|
|
|
|
Data: []byte("content"),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
router := httprouter.New()
|
|
|
|
downloads(router, "artifact/server/path", memfs)
|
|
|
|
|
|
|
|
req, _ := http.NewRequest("GET", "http://localhost/artifact/2/../../some/file", nil)
|
|
|
|
rr := httptest.NewRecorder()
|
|
|
|
|
|
|
|
router.ServeHTTP(rr, req)
|
|
|
|
|
|
|
|
if status := rr.Code; status != http.StatusOK {
|
|
|
|
assert.FailNow(fmt.Sprintf("Wrong status: %d", status))
|
|
|
|
}
|
|
|
|
|
|
|
|
data := rr.Body.Bytes()
|
|
|
|
|
|
|
|
assert.Equal("content", string(data))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestArtifactUploadBlobUnsafePath(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
var memfs = fstest.MapFS(map[string]*fstest.MapFile{})
|
|
|
|
|
|
|
|
router := httprouter.New()
|
|
|
|
uploads(router, "artifact/server/path", writeMapFS{memfs})
|
|
|
|
|
|
|
|
req, _ := http.NewRequest("PUT", "http://localhost/upload/1?itemPath=../../some/file", strings.NewReader("content"))
|
|
|
|
rr := httptest.NewRecorder()
|
|
|
|
|
|
|
|
router.ServeHTTP(rr, req)
|
|
|
|
|
|
|
|
if status := rr.Code; status != http.StatusOK {
|
|
|
|
assert.Fail("Wrong status")
|
|
|
|
}
|
|
|
|
|
|
|
|
response := ResponseMessage{}
|
|
|
|
err := json.Unmarshal(rr.Body.Bytes(), &response)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal("success", response.Message)
|
|
|
|
assert.Equal("content", string(memfs["artifact/server/path/1/some/file"].Data))
|
|
|
|
}
|