15
15
package storageos_test
16
16
17
17
import (
18
+ "context"
19
+ "io/fs"
20
+ "os"
18
21
"path/filepath"
19
22
"testing"
20
23
@@ -37,6 +40,72 @@ func TestOS(t *testing.T) {
37
40
testWriteBucketToReadBucket ,
38
41
true ,
39
42
)
43
+
44
+ t .Run ("get_non_existent_file" , func (t * testing.T ) {
45
+ t .Parallel ()
46
+ ctx := context .Background ()
47
+ // Create a bucket at an absolute path.
48
+ tempDir := t .TempDir ()
49
+ tempDir , err := filepath .Abs (tempDir )
50
+ require .NoError (t , err )
51
+ bucket , err := storageos .NewProvider ().NewReadWriteBucket (tempDir )
52
+ require .NoError (t , err )
53
+
54
+ // Write a file to it.
55
+ writeObjectCloser , err := bucket .Put (ctx , "foo.txt" )
56
+ require .NoError (t , err )
57
+ written , err := writeObjectCloser .Write ([]byte (nil ))
58
+ require .NoError (t , err )
59
+ require .Zero (t , written )
60
+ require .NoError (t , writeObjectCloser .Close ())
61
+
62
+ // Try reading a file as if foo.txt is a directory.
63
+ _ , err = bucket .Get (ctx , "foo.txt/bar.txt" )
64
+ require .ErrorIs (t , err , fs .ErrNotExist )
65
+ _ , err = bucket .Get (ctx , "foo.txt/bar.txt/baz.txt" )
66
+ require .ErrorIs (t , err , fs .ErrNotExist )
67
+
68
+ // Read a file that does not exist at all.
69
+ _ , err = bucket .Get (ctx , "baz.txt" )
70
+ require .ErrorIs (t , err , fs .ErrNotExist )
71
+ })
72
+
73
+ t .Run ("get_non_existent_file_symlink" , func (t * testing.T ) {
74
+ t .Parallel ()
75
+ ctx := context .Background ()
76
+ // Create a bucket at an absolute path.
77
+ actualTempDir := t .TempDir ()
78
+ actualTempDir , err := filepath .Abs (actualTempDir )
79
+ require .NoError (t , err )
80
+ _ , err = os .Create (filepath .Join (actualTempDir , "foo.txt" ))
81
+ require .NoError (t , err )
82
+ tempDir := t .TempDir ()
83
+ tempDir , err = filepath .Abs (tempDir )
84
+ require .NoError (t , err )
85
+ tempDir = filepath .Join (tempDir , "sym" )
86
+ require .NoError (t , os .Symlink (actualTempDir , tempDir ))
87
+ t .Cleanup (func () {
88
+ if err := os .Remove (tempDir ); err != nil {
89
+ t .Error (err )
90
+ }
91
+ })
92
+ provider := storageos .NewProvider (storageos .ProviderWithSymlinks ())
93
+ bucket , err := provider .NewReadWriteBucket (tempDir , storageos .ReadWriteBucketWithSymlinksIfSupported ())
94
+ require .NoError (t , err )
95
+
96
+ _ , err = bucket .Get (ctx , "foo.txt" )
97
+ require .NoError (t , err )
98
+
99
+ // Try reading a file as if foo.txt is a directory.
100
+ _ , err = bucket .Get (ctx , "foo.txt/bar.txt" )
101
+ require .ErrorIs (t , err , fs .ErrNotExist )
102
+ _ , err = bucket .Get (ctx , "foo.txt/bar.txt/baz.txt" )
103
+ require .ErrorIs (t , err , fs .ErrNotExist )
104
+
105
+ // Read a file that does not exist at all.
106
+ _ , err = bucket .Get (ctx , "baz.txt" )
107
+ require .ErrorIs (t , err , fs .ErrNotExist )
108
+ })
40
109
}
41
110
42
111
func testNewReadBucket (t * testing.T , dirPath string , storageosProvider storageos.Provider ) (storage.ReadBucket , storagetesting.GetExternalPathFunc ) {
0 commit comments