-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstorage_test.go
More file actions
95 lines (79 loc) · 2.15 KB
/
Copy pathstorage_test.go
File metadata and controls
95 lines (79 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package artifact
import (
"context"
"os"
"path/filepath"
"testing"
)
func TestFilesystemStorage_PutAndGet(t *testing.T) {
dir := t.TempDir()
fs := &FilesystemStorage{BasePath: dir}
ctx := context.Background()
key := "test-workflow/exec-123/my-artifact/data.tar.gz"
data := []byte("fake tar content")
// Write source file first.
srcPath := filepath.Join(dir, "source.tar.gz")
if err := os.WriteFile(srcPath, data, 0644); err != nil {
t.Fatal(err)
}
url, err := fs.Put(ctx, key, srcPath)
if err != nil {
t.Fatalf("Put: %v", err)
}
if url == "" {
t.Fatal("url should not be empty")
}
// Verify file exists at the stored location.
stored, err := os.ReadFile(url)
if err != nil {
t.Fatalf("reading stored file: %v", err)
}
if string(stored) != string(data) {
t.Errorf("stored content = %q, want %q", stored, data)
}
}
func TestFilesystemStorage_Delete(t *testing.T) {
dir := t.TempDir()
fs := &FilesystemStorage{BasePath: dir}
ctx := context.Background()
// Write a file
key := "test-workflow/exec-123/artifact/file.txt"
srcPath := filepath.Join(dir, "src.txt")
if err := os.WriteFile(srcPath, []byte("data"), 0644); err != nil {
t.Fatal(err)
}
url, err := fs.Put(ctx, key, srcPath)
if err != nil {
t.Fatalf("Put: %v", err)
}
// Delete by prefix
err = fs.DeleteByPrefix(ctx, "test-workflow/exec-123/")
if err != nil {
t.Fatalf("DeleteByPrefix: %v", err)
}
// File should be gone
if _, err := os.Stat(url); !os.IsNotExist(err) {
t.Error("file should have been deleted")
}
}
func TestFilesystemStorage_DeleteEscapeProtection(t *testing.T) {
dir := t.TempDir()
fs := &FilesystemStorage{BasePath: dir}
ctx := context.Background()
err := fs.DeleteByPrefix(ctx, "../../etc")
if err == nil {
t.Error("expected error for path traversal")
}
}
func TestArtifactsDirContext(t *testing.T) {
ctx := context.Background()
// Empty by default
if dir := ArtifactsDirFromContext(ctx); dir != "" {
t.Errorf("expected empty, got %q", dir)
}
// Set and retrieve
ctx = WithArtifactsDir(ctx, "/tmp/artifacts")
if dir := ArtifactsDirFromContext(ctx); dir != "/tmp/artifacts" {
t.Errorf("expected '/tmp/artifacts', got %q", dir)
}
}