Skip to content

Commit 6b6441c

Browse files
authored
Fix/test validated link target tempdir (#3223)
* improve testlink_target * symlink copy error * linting issue * remove link
1 parent 3188eed commit 6b6441c

File tree

2 files changed

+42
-8
lines changed

2 files changed

+42
-8
lines changed

.gitignore

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,6 @@ __pycache__
4646
# https://github.com/knative/func/issues/3196
4747
/pkg/builders/testdata/go-fn-with-private-deps/.s2i
4848

49-
# TODO: Run this test from a temp directory instead:
50-
# https://github.com/knative/func/issues/3197
51-
pkg/oci/testdata/test-links/absoluteLink
52-
pkg/oci/testdata/test-links/absoluteLinkWindows
53-
5449
# TODO: update the test which creates this to run in a temp directory:
5550
# https://github.com/knative/func/issues/3158
5651
/pkg/creds/auth.json

pkg/oci/builder_test.go

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,40 @@ import (
2626

2727
var TestPlatforms = []fn.Platform{{OS: "linux", Architecture: runtime.GOARCH}}
2828

29+
func copyDir(src, dst string) error {
30+
return filepath.Walk(src, func(path string, info fs.FileInfo, err error) error {
31+
if err != nil {
32+
return err
33+
}
34+
35+
rel, err := filepath.Rel(src, path)
36+
if err != nil {
37+
return err
38+
}
39+
40+
target := filepath.Join(dst, rel)
41+
42+
if info.Mode()&os.ModeSymlink != 0 {
43+
linkTarget, err := os.Readlink(path)
44+
if err != nil {
45+
return err
46+
}
47+
return os.Symlink(linkTarget, target)
48+
}
49+
50+
if info.IsDir() {
51+
return os.MkdirAll(target, info.Mode())
52+
}
53+
54+
data, err := os.ReadFile(path)
55+
if err != nil {
56+
return err
57+
}
58+
59+
return os.WriteFile(target, data, info.Mode())
60+
})
61+
}
62+
2963
// TestBuilder_BuildGo ensures that, when given a Go Function, an OCI-compliant
3064
// directory structure is created on .Build in the expected path.
3165
func TestBuilder_BuildGo(t *testing.T) {
@@ -504,13 +538,18 @@ func (l *TestLanguageBuilder) Configure(job buildJob, p v1.Platform, c v1.Config
504538
return l.ConfigureFn(job, p, c)
505539
}
506540

507-
// Test_validatedLinkTaarget ensures that the function disallows
541+
// Test_validatedLinkTarget ensures that the function disallows
508542
// links which are absolute or refer to targets outside the given root, in
509543
// addition to the basic job of returning the value of reading the link.
510544
func Test_validatedLinkTarget(t *testing.T) {
511-
root := filepath.Join("testdata", "test-links")
545+
tmp := t.TempDir()
546+
root := filepath.Join(tmp, "test-links")
547+
err := copyDir("testdata/test-links", root)
512548

513-
err := os.Symlink("/var/example/absolute/link", filepath.Join(root, "absoluteLink"))
549+
if err != nil {
550+
t.Fatalf("failed to copy test data: %v", err)
551+
}
552+
err = os.Symlink("/var/example/absolute/link", filepath.Join(root, "absoluteLink"))
514553
if err != nil && !errors.Is(err, os.ErrExist) {
515554
t.Fatal(err)
516555
}

0 commit comments

Comments
 (0)