-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(feat): Add service.FileService test coverage
- Loading branch information
Showing
4 changed files
with
196 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
package service | ||
|
||
import ( | ||
"os" | ||
"path" | ||
"syscall" | ||
"testing" | ||
|
||
"github.com/reecetech/ebs-bootstrap/internal/model" | ||
"github.com/reecetech/ebs-bootstrap/internal/utils" | ||
) | ||
|
||
func TestGetFile(t *testing.T) { | ||
file, err := regularFile() | ||
defer os.Remove(file.Path) | ||
utils.CheckError("temporaryFile()", t, nil, err) | ||
|
||
dir, err := directory() | ||
defer os.RemoveAll(dir.Path) | ||
utils.CheckError("temporaryDirectory()", t, nil, err) | ||
|
||
spec, err := special() | ||
utils.CheckError("lstat()", t, nil, err) | ||
|
||
ufs := NewUnixFileService() | ||
|
||
subtests := []struct { | ||
Name string | ||
Path string | ||
ExpectedOutput *model.File | ||
ShouldExpectErr bool | ||
}{ | ||
{ | ||
Name: "Regular File", | ||
Path: file.Path, | ||
ExpectedOutput: file, | ||
ShouldExpectErr: false, | ||
}, | ||
{ | ||
Name: "Directory", | ||
Path: dir.Path, | ||
ExpectedOutput: dir, | ||
ShouldExpectErr: false, | ||
}, | ||
{ | ||
Name: "Special", | ||
Path: spec.Path, | ||
ExpectedOutput: spec, | ||
ShouldExpectErr: false, | ||
}, | ||
{ | ||
Name: "Non-Existing File", | ||
Path: "/dev/does-not-exist", | ||
ExpectedOutput: nil, | ||
ShouldExpectErr: true, | ||
}, | ||
} | ||
for _, subtest := range subtests { | ||
output, err := ufs.GetFile(subtest.Path) | ||
utils.ExpectErr("ufs.GetFile()", t, subtest.ShouldExpectErr, err) | ||
utils.CheckOutput("ufs.GetFile()", t, subtest.ExpectedOutput, output) | ||
} | ||
} | ||
|
||
func TestDirectoryModifications(t *testing.T) { | ||
ufs := NewUnixFileService() | ||
|
||
// Create Temporary Directory | ||
dir, err := directory() | ||
utils.ExpectErr("temporaryDirectory()", t, false, err) | ||
defer os.RemoveAll(dir.Path) | ||
|
||
// Create Nested Directory Inside Temporary Directory | ||
nestedDir := path.Join(dir.Path, "nested") | ||
err = ufs.CreateDirectory(nestedDir) | ||
utils.ExpectErr("ufs.CreateDirectory()", t, false, err) | ||
|
||
// Change Owner of Nested Directory to Match Temporary Directory | ||
err = ufs.ChangeOwner(nestedDir, dir.UserId, dir.GroupId) | ||
utils.ExpectErr("ufs.ChangeOwner()", t, false, err) | ||
|
||
// Change Permissions of Nested Directory to Match Temporary Directory | ||
err = ufs.ChangePermissions(nestedDir, dir.Permissions) | ||
utils.ExpectErr("ufs.ChangePermissions()", t, false, err) | ||
} | ||
|
||
// Create a temporary file | ||
func regularFile() (*model.File, error) { | ||
file, err := os.CreateTemp("", "temp_file") | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer file.Close() | ||
return lstat(file.Name(), model.RegularFile) | ||
} | ||
|
||
// Create a temporary directory | ||
func directory() (*model.File, error) { | ||
dir, err := os.MkdirTemp("", "temp_dir") | ||
if err != nil { | ||
return nil, err | ||
} | ||
return lstat(dir, model.Directory) | ||
} | ||
|
||
// Return lstat of /dev/null which is classified as a 'special' file in Unix | ||
func special() (*model.File, error) { | ||
return lstat("/dev/null", model.Special) | ||
} | ||
|
||
func lstat(path string, ft model.FileType) (*model.File, error) { | ||
info, err := os.Lstat(path) | ||
if err != nil { | ||
return nil, err | ||
} | ||
stat := info.Sys().(*syscall.Stat_t) | ||
|
||
return &model.File{ | ||
Path: path, | ||
Type: ft, | ||
DeviceId: stat.Dev, | ||
InodeNo: stat.Ino, | ||
UserId: model.UserId(stat.Uid), | ||
GroupId: model.GroupId(stat.Gid), | ||
Permissions: model.FilePermissions(info.Mode().Perm()), | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters