Skip to content

Commit 42faa1d

Browse files
Add Open() test for s3 backend
1 parent dcdae5c commit 42faa1d

File tree

5 files changed

+70
-14
lines changed

5 files changed

+70
-14
lines changed

backend/gs/location_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -365,12 +365,12 @@ func (lt *locationTestSuite) TestOpen() {
365365
// Test opening a non-existent file
366366
_, err = loc.Open("non-existent-file.txt")
367367
lt.Error(err, "Opening a non-existent file should return an error")
368-
lt.Contains(err.Error(), "ErrNotExist", "Error should indicate file does not exist")
368+
lt.Contains(err.Error(), "file does not exist", "Error should indicate file does not exist")
369369

370370
// Test opening with path traversal
371371
_, err = loc.Open("../outside.txt")
372372
lt.Error(err, "Opening a file with path traversal should return an error")
373-
lt.Contains(err.Error(), "ErrInvalid", "Error should indicate invalid path")
373+
lt.Contains(err.Error(), "invalid argument", "Error should indicate invalid path")
374374

375375
// Test opening with dot paths
376376
_, err = loc.Open(".")

backend/mem/file_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -770,14 +770,14 @@ func (s *memFileTest) TestStat() {
770770
fileInfo, err := s.testFile.Stat()
771771
s.NoError(err, "stat error not expected")
772772
s.NotNil(fileInfo, "FileInfo should not be nil")
773-
773+
774774
// Check file info properties
775775
s.Equal("test.txt", fileInfo.Name(), "FileInfo name should match file name")
776776
s.Equal(int64(len(expectedContent)), fileInfo.Size(), "FileInfo size should match content length")
777777
s.False(fileInfo.IsDir(), "FileInfo should indicate file is not a directory")
778778
s.NotNil(fileInfo.ModTime(), "ModTime should not be nil")
779779
s.Equal(0644, int(fileInfo.Mode()), "Mode should be 0644")
780-
780+
781781
// Test Stat on non-existent file
782782
nonExistentFile, err := s.fileSystem.NewFile("", "/non-existent-file.txt")
783783
s.NoError(err, "error creating reference to non-existent file")

backend/mem/location_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -350,48 +350,48 @@ func (s *memLocationTest) TestOpen() {
350350
testContent := "hello world"
351351
testFileName := "open_test_file.txt"
352352
testFilePath := "/test_files/" + testFileName
353-
353+
354354
// Create a file and write content
355355
file, err := s.fileSystem.NewFile("", testFilePath)
356356
s.NoError(err, "error not expected when creating a new file")
357357
_, err = file.Write([]byte(testContent))
358358
s.NoError(err, "write error not expected")
359359
s.NoError(file.Close(), "close error not expected")
360-
360+
361361
// Get the file's location
362362
loc := file.Location()
363-
363+
364364
// Test Opening the file
365365
opened, err := loc.Open(testFileName)
366366
s.NoError(err, "error not expected when opening existing file")
367367
s.NotNil(opened, "opened file should not be nil")
368-
368+
369369
// Read the content to verify
370370
data := make([]byte, len(testContent))
371371
n, err := opened.Read(data)
372372
s.NoError(err, "read error not expected")
373373
s.Equal(len(testContent), n, "should read all content")
374374
s.Equal(testContent, string(data), "content should match")
375-
375+
376376
// Test opening non-existent file
377377
_, err = loc.Open("non-existent-file.txt")
378378
s.Error(err, "error expected when opening non-existent file")
379379
var pathErr *fs.PathError
380380
s.True(errors.As(err, &pathErr), "error should be a fs.PathError")
381381
s.ErrorIs(pathErr.Err, fs.ErrNotExist, "underlying error should be fs.ErrNotExist")
382-
382+
383383
// Test opening with path traversal attempts (should be rejected)
384384
_, err = loc.Open("../outside.txt")
385385
s.Error(err, "error expected when path contains traversal")
386386
s.True(errors.As(err, &pathErr), "error should be a fs.PathError")
387387
s.ErrorIs(pathErr.Err, fs.ErrInvalid, "underlying error should be fs.ErrInvalid")
388-
388+
389389
_, err = loc.Open("./file.txt")
390390
s.Error(err, "error expected when path contains ./")
391-
391+
392392
_, err = loc.Open(".")
393393
s.Error(err, "error expected when path is .")
394-
394+
395395
_, err = loc.Open("..")
396396
s.Error(err, "error expected when path is ..")
397397
}

backend/os/file_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -755,7 +755,7 @@ func (s *osFileTest) TestStat() {
755755
s.NoError(err, "error creating reference to non-existent file")
756756
_, err = nonExistentFile.Stat()
757757
s.Error(err, "error expected when calling Stat on non-existent file")
758-
s.True(os.IsNotExist(err), "error should be os.IsNotExist")
758+
s.Contains(err.Error(), "no such file or directory", "error should indicate no such file")
759759
}
760760

761761
func TestOSFile(t *testing.T) {

backend/s3/location_test.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"path"
55
"regexp"
66
"testing"
7+
"time"
78

89
"github.com/aws/aws-sdk-go-v2/aws"
910
"github.com/aws/aws-sdk-go-v2/service/s3"
@@ -342,6 +343,61 @@ func (lt *locationTestSuite) TestDeleteFileWithAllVersionsOption() {
342343
lt.s3cliMock.AssertNumberOfCalls(lt.T(), "DeleteObject", 3)
343344
}
344345

346+
// TestOpen tests the Open method for the S3 location
347+
func (lt *locationTestSuite) TestOpen() {
348+
// Setup test values
349+
bucket := "test-bucket"
350+
locPath := "/test-dir/"
351+
filename := "test-file.txt"
352+
353+
// Create location
354+
loc, err := lt.fs.NewLocation(bucket, locPath)
355+
lt.NoError(err, "Creating location shouldn't return an error")
356+
357+
// Mock the HeadObject response for checking existence
358+
lt.s3cliMock.On("HeadObject", matchContext, mock.Anything).Return(&s3.HeadObjectOutput{
359+
ContentLength: aws.Int64(42),
360+
LastModified: aws.Time(time.Now()),
361+
}, nil).Once()
362+
363+
// Test opening an existing file
364+
file, err := loc.Open(filename)
365+
lt.NoError(err, "Opening an existing file should not return an error")
366+
lt.NotNil(file, "Returned file should not be nil")
367+
368+
// Verify mock expectations
369+
lt.s3cliMock.AssertExpectations(lt.T())
370+
371+
// Test opening a non-existent file
372+
// Setup mock to indicate file doesn't exist
373+
lt.s3cliMock.On("HeadObject", matchContext, mock.Anything).Return(nil, &types.NotFound{
374+
Message: aws.String("Not Found"),
375+
}).Once()
376+
377+
// Attempt to open a non-existent file
378+
_, err = loc.Open("non-existent.txt")
379+
lt.Error(err, "Opening a non-existent file should return an error")
380+
lt.Contains(err.Error(), "file does not exist", "Error should indicate file does not exist")
381+
382+
// Test opening with path traversal
383+
_, err = loc.Open("../outside.txt")
384+
lt.Error(err, "Opening a file with path traversal should return an error")
385+
lt.Contains(err.Error(), "invalid argument", "Error should indicate invalid path")
386+
387+
// Test opening with dot paths
388+
_, err = loc.Open(".")
389+
lt.Error(err, "Opening '.' should return an error")
390+
391+
_, err = loc.Open("..")
392+
lt.Error(err, "Opening '..' should return an error")
393+
394+
_, err = loc.Open("./file.txt")
395+
lt.Error(err, "Opening path with './' should return an error")
396+
397+
// Verify all mock expectations
398+
lt.s3cliMock.AssertExpectations(lt.T())
399+
}
400+
345401
func TestLocation(t *testing.T) {
346402
suite.Run(t, new(locationTestSuite))
347403
}

0 commit comments

Comments
 (0)