@@ -15,11 +15,15 @@ package main
15
15
16
16
import (
17
17
"context"
18
+ "io/ioutil"
18
19
"os"
20
+ "path/filepath"
21
+ "syscall"
19
22
"testing"
20
23
21
24
"github.com/sirupsen/logrus"
22
25
"github.com/stretchr/testify/assert"
26
+ "github.com/stretchr/testify/require"
23
27
24
28
"github.com/firecracker-microvm/firecracker-containerd/proto"
25
29
)
@@ -36,6 +40,45 @@ func TestCopyFile_simple(t *testing.T) {
36
40
info , err := os .Stat (dstPath )
37
41
assert .NoError (t , err , "failed to stat file" )
38
42
assert .Equal (t , os .FileMode (expectedMode ), info .Mode ())
43
+ assert .NotEqual (t , 0 , int (info .Size ()))
44
+ }
45
+
46
+ func createSparseFile (path string , size int ) error {
47
+ f , err := os .Create (path )
48
+ if err != nil {
49
+ return err
50
+ }
51
+ defer f .Close ()
52
+
53
+ err = f .Truncate (int64 (size ))
54
+ if err != nil {
55
+ return err
56
+ }
57
+
58
+ return nil
59
+ }
60
+ func TestCopyFile_sparse (t * testing.T ) {
61
+ dir , err := ioutil .TempDir ("" , t .Name ())
62
+ require .NoError (t , err )
63
+ defer os .RemoveAll (dir )
64
+
65
+ expectedSize := 1024
66
+
67
+ src := filepath .Join (dir , "original-sparse-file" )
68
+ err = createSparseFile (src , expectedSize )
69
+ require .NoError (t , err )
70
+
71
+ dst := filepath .Join (dir , "copied-as-sparse" )
72
+ err = copyFile (src , dst , 0600 )
73
+ require .NoError (t , err , "failed to copy file" )
74
+
75
+ stat , err := os .Stat (dst )
76
+ require .NoError (t , err )
77
+ assert .Equal (t , expectedSize , int (stat .Size ()), "metadata-wise, the file is not empty" )
78
+
79
+ unixStat , ok := (stat .Sys ()).(* syscall.Stat_t )
80
+ require .True (t , ok )
81
+ assert .Equal (t , int64 (0 ), unixStat .Blocks , "it doesn't allocate any blocks, since the file is empty" )
39
82
}
40
83
41
84
func TestCopyFile_invalidPaths (t * testing.T ) {
0 commit comments