Skip to content

Commit

Permalink
Added test for file delete after single access
Browse files Browse the repository at this point in the history
  • Loading branch information
akclace committed Dec 1, 2024
1 parent a179235 commit d7b5535
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 16 deletions.
2 changes: 1 addition & 1 deletion internal/app/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func (f *fsPlugin) Find(thread *starlark.Thread, builtin *starlark.Builtin, args
var minSize, limit starlark.Int
var ignoreError starlark.Bool

if err := starlark.UnpackArgs("find", args, kwargs, "path", &path, "name?", nameGlob, "limit?", &limit, "min_size?", &minSize, "ignore_errors", &ignoreError); err != nil {
if err := starlark.UnpackArgs("find", args, kwargs, "path", &path, "name?", &nameGlob, "limit?", &limit, "min_size?", &minSize, "ignore_errors", &ignoreError); err != nil {
return nil, err
}

Expand Down
43 changes: 28 additions & 15 deletions internal/app/fs_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,25 +53,38 @@ func InitFileStore(ctx context.Context, connectString string) error {
return nil
}

func backgroundCleanup(ctx context.Context, cleanupTicker *time.Ticker) {
for range cleanupTicker.C {
expired, err := listExpiredFile(ctx)
if err != nil {
fmt.Fprintf(os.Stderr, "error cleaning up expired files %s", err)
break
}
func fileCleanup(ctx context.Context) error {
expired, err := listExpiredFile(ctx)
if err != nil {
return fmt.Errorf("error cleaning up expired files %w", err)
}

for _, file := range expired {
if strings.HasPrefix(file.FilePath, "file://") {
err := os.Remove(strings.TrimPrefix(file.FilePath, "file://"))
if err != nil {
fmt.Fprintf(os.Stderr, "error deleting file %s: %s", file.FilePath, err)
}
for _, file := range expired {
if strings.HasPrefix(file.FilePath, "file://") {
err := os.Remove(strings.TrimPrefix(file.FilePath, "file://"))
if err != nil {
return fmt.Errorf("error deleting file %s: %w", file.FilePath, err)
}
}
err = deleteExpiredFiles(ctx)
}
err = deleteExpiredFiles(ctx)
if err != nil {
return fmt.Errorf("error deleting expired files %w", err)
}
return nil
}

func backgroundCleanup(ctx context.Context, cleanupTicker *time.Ticker) {
err := fileCleanup(ctx)
if err != nil {
fmt.Fprintf(os.Stderr, "error cleaning up expired files %s", err)
return
}

for range cleanupTicker.C {
err := fileCleanup(ctx)
if err != nil {
fmt.Fprintf(os.Stderr, "error deleting expired files %s", err)
fmt.Fprintf(os.Stderr, "error cleaning up expired files %s", err)
break
}
}
Expand Down
5 changes: 5 additions & 0 deletions tests/commander/test_misc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,8 @@ tests:
stdout: /numlines/_clace_app/file/usr_file_
actions0500:
command: rm action_inp.txt
actions1000: # Create app
command: ../clace app create --auth=none --approve ./file_app /file_app
actions1100:
command: curl localhost:25222/file_app
stdout: Success
45 changes: 45 additions & 0 deletions tests/file_app/app.star
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
load("fs.in", "fs")
load("exec.in", "exec")
load("http.in", "http")

def handler(req):
exec.run("sh", ["-c", "rm -rf /tmp/fileapptmp"])
exec.run("sh", ["-c", "mkdir /tmp/fileapptmp"])
ret1 = exec.run("sh", ["-c", "echo \"abc\" > /tmp/fileapptmp/testfileapp.txt"])
ret11 = exec.run("sh", ["-c", "echo \"abc\" > /tmp/fileapptmp/testfileapp2.txt"])
ret2 = fs.load_file("/tmp/fileapptmp/testfileapp.txt") # single_access=True is default
ret3 = fs.load_file("/tmp/fileapptmp/testfileapp2.txt", single_access=False)

# First attempt works
ret4 = http.get("http://localhost:25222" + ret2.value["url"])
if ret4.value.status_code != 200:
return "Error: %s" % ret4.value.status_code
ret5 = http.get("http://localhost:25222" + ret3.value["url"])
if ret5.value.status_code != 200:
return "Error: %d" % ret5.value.status_code

# Second attempt fails for file1 (it got deleted on first GET call), works for file2 as it is multi_access
ret6 = http.get("http://localhost:25222" + ret2.value["url"])
if ret6.value.status_code == 200:
return "Error: %d" % ret6.value.status_code
ret7 = http.get("http://localhost:25222" + ret3.value["url"])
if ret7.value.status_code != 200:
return "Error: %d" % ret7.value.status_code

ret8 = fs.find("/tmp/fileapptmp", "testfileapp.txt")
if len(ret8.value) != 0:
return "Error: expected no files on disk %s" % ret8.value
ret9 = fs.find("/tmp/fileapptmp", "testfileapp2.txt")
if len(ret9.value) == 0:
return "Error: expected files on disk"

return "Success"

app = ace.app("file test",
routes = [ace.api("/", type=ace.TEXT)],
permissions = [
ace.permission("fs.in", "load_file"),
ace.permission("fs.in", "find"),
ace.permission("exec.in", "run"),
ace.permission("http.in", "get")]
)

0 comments on commit d7b5535

Please sign in to comment.