From 6e9edd53b30f0743d7558b0e6011fccdd08cf688 Mon Sep 17 00:00:00 2001 From: Mahmood Ali Date: Wed, 30 Sep 2020 09:58:22 -0400 Subject: [PATCH] tests: failover to copying when symlinking fails Symlinking busybox may fail when the test code and the test temporary directory live on different volumes/partitions; so we should copy instead. This situation arises in the Vagrant setup, where the code repository live on special file sharing volume. Somewhat unrelated, remove `f.Sync()` invocation from a test copyFile helper function. Sync is useful only for crash recovery, and isn't necessary in our test setup. The sync invocation is a significant overhead as it requires the OS to flush any cached writes to disk. --- drivers/java/driver_test.go | 3 --- drivers/shared/executor/executor_test.go | 24 +++++++++++++++++++++++- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/drivers/java/driver_test.go b/drivers/java/driver_test.go index b4c2c9e40..f54d8c6da 100644 --- a/drivers/java/driver_test.go +++ b/drivers/java/driver_test.go @@ -332,9 +332,6 @@ func copyFile(src, dst string, t *testing.T) { if _, err = io.Copy(out, in); err != nil { t.Fatalf("copying %v -> %v failed: %v", src, dst, err) } - if err := out.Sync(); err != nil { - t.Fatalf("copying %v -> %v failed: %v", src, dst, err) - } } func TestConfig_ParseAllHCL(t *testing.T) { diff --git a/drivers/shared/executor/executor_test.go b/drivers/shared/executor/executor_test.go index 41b141762..200fb323e 100644 --- a/drivers/shared/executor/executor_test.go +++ b/drivers/shared/executor/executor_test.go @@ -482,7 +482,29 @@ func setupRootfsBinary(t *testing.T, rootfs, path string) { ) err = os.Link(src, dst) - require.NoError(t, err) + if err != nil { + // On failure, fallback to copying the file directly. + // Linking may fail if the test source code lives on a separate + // volume/partition from the temp dir used for testing + copyFile(t, src, dst) + } +} + +func copyFile(t *testing.T, src, dst string) { + in, err := os.Open(src) + require.NoErrorf(t, err, "copying %v -> %v", src, dst) + defer in.Close() + + out, err := os.Create(dst) + require.NoErrorf(t, err, "copying %v -> %v", src, dst) + defer func() { + if err := out.Close(); err != nil { + t.Fatalf("copying %v -> %v failed: %v", src, dst, err) + } + }() + + _, err = io.Copy(out, in) + require.NoErrorf(t, err, "copying %v -> %v", src, dst) } // TestExecutor_Start_Kill_Immediately_NoGrace asserts that executors shutdown