syncthing/lib/versioner/versioner_test.go
Rahmi Pruitt 5b953033c7
Created cleanup functionality for syncthing (#6884)
* Add clean up for Simple File Versioning pt.1

created test

* Add clean up for Simple File Versioning pt.2

Passing the test

* stuck on how javascript communicates with backend

* Add trash clean up for Simple File Versioning

Add trash clean up functionality of to allow the user to delete backups
after specified amount of days.

* Fixed html and js style

* Refactored cleanup test cases

Refactored cleanup test cases to one file and deleted duplicated code.

* Added copyright to test file

* Refactor folder cleanout to utility function

* change utility function to package private

* refactored utility function; fixed build errors

* Updated copyright year.

* refactor test and logging

* refactor html and js

* revert style change in html

* reverted changes in html and some js

* checkout origin head version edit...html

* checkout upstream master and correct file
2020-08-24 12:14:30 +01:00

92 lines
2.6 KiB
Go

// Copyright (C) 2020 The Syncthing Authors.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at https://mozilla.org/MPL/2.0/.
package versioner
import (
"context"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"
"time"
"github.com/syncthing/syncthing/lib/config"
"github.com/syncthing/syncthing/lib/fs"
)
func TestVersionerCleanOut(t *testing.T) {
cfg := config.FolderConfiguration{
FilesystemType: fs.FilesystemTypeBasic,
Path: "testdata",
Versioning: config.VersioningConfiguration{
Params: map[string]string{
"cleanoutDays": "7",
},
},
}
testCasesVersioner := map[string]Versioner{
"simple": newSimple(cfg),
"trashcan": newTrashcan(cfg),
}
var testcases = map[string]bool{
"testdata/.stversions/file1": false,
"testdata/.stversions/file2": true,
"testdata/.stversions/keep1/file1": false,
"testdata/.stversions/keep1/file2": false,
"testdata/.stversions/keep2/file1": false,
"testdata/.stversions/keep2/file2": true,
"testdata/.stversions/keep3/keepsubdir/file1": false,
"testdata/.stversions/remove/file1": true,
"testdata/.stversions/remove/file2": true,
"testdata/.stversions/remove/removesubdir/file1": true,
}
for versionerType, versioner := range testCasesVersioner {
t.Run(fmt.Sprintf("%v versioner trashcan clean up", versionerType), func(t *testing.T) {
os.RemoveAll("testdata")
defer os.RemoveAll("testdata")
oldTime := time.Now().Add(-8 * 24 * time.Hour)
for file, shouldRemove := range testcases {
os.MkdirAll(filepath.Dir(file), 0777)
if err := ioutil.WriteFile(file, []byte("data"), 0644); err != nil {
t.Fatal(err)
}
if shouldRemove {
if err := os.Chtimes(file, oldTime, oldTime); err != nil {
t.Fatal(err)
}
}
}
if err := versioner.Clean(context.Background()); err != nil {
t.Fatal(err)
}
for file, shouldRemove := range testcases {
_, err := os.Lstat(file)
if shouldRemove && !os.IsNotExist(err) {
t.Error(file, "should have been removed")
} else if !shouldRemove && err != nil {
t.Error(file, "should not have been removed")
}
}
if _, err := os.Lstat("testdata/.stversions/keep3"); os.IsNotExist(err) {
t.Error("directory with non empty subdirs should not be removed")
}
if _, err := os.Lstat("testdata/.stversions/remove"); !os.IsNotExist(err) {
t.Error("empty directory should have been removed")
}
})
}
}