2014-11-20 08:23:58 -07:00
|
|
|
// Copyright (C) 2014 The Syncthing Authors.
|
|
|
|
//
|
2015-03-07 13:36:35 -07:00
|
|
|
// 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,
|
2017-02-08 23:52:18 -07:00
|
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
2014-11-20 08:23:58 -07:00
|
|
|
|
2021-08-17 01:10:41 -07:00
|
|
|
//go:build integration
|
2014-11-20 08:23:58 -07:00
|
|
|
// +build integration
|
|
|
|
|
2014-12-17 04:31:03 -07:00
|
|
|
package integration
|
2014-11-20 08:23:58 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"testing"
|
2014-12-03 03:03:44 -07:00
|
|
|
|
2015-08-06 02:29:25 -07:00
|
|
|
"github.com/syncthing/syncthing/lib/config"
|
2019-08-20 01:17:11 -07:00
|
|
|
"github.com/syncthing/syncthing/lib/events"
|
2015-09-22 10:38:46 -07:00
|
|
|
"github.com/syncthing/syncthing/lib/protocol"
|
2015-08-06 02:29:25 -07:00
|
|
|
"github.com/syncthing/syncthing/lib/rc"
|
2014-11-20 08:23:58 -07:00
|
|
|
)
|
|
|
|
|
2014-12-03 03:03:44 -07:00
|
|
|
func TestFileTypeChange(t *testing.T) {
|
|
|
|
// Use no versioning
|
|
|
|
id, _ := protocol.DeviceIDFromString(id2)
|
2020-09-07 00:35:37 -07:00
|
|
|
cfg, _, _ := config.Load("h2/config.xml", id, events.NoopLogger)
|
2014-12-03 03:03:44 -07:00
|
|
|
fld := cfg.Folders()["default"]
|
|
|
|
fld.Versioning = config.VersioningConfiguration{}
|
|
|
|
cfg.SetFolder(fld)
|
2018-12-21 03:50:28 -07:00
|
|
|
os.Rename("h2/config.xml", "h2/config.xml.orig")
|
|
|
|
defer os.Rename("h2/config.xml.orig", "h2/config.xml")
|
2014-12-03 03:03:44 -07:00
|
|
|
cfg.Save()
|
|
|
|
|
|
|
|
testFileTypeChange(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFileTypeChangeSimpleVersioning(t *testing.T) {
|
|
|
|
// Use simple versioning
|
|
|
|
id, _ := protocol.DeviceIDFromString(id2)
|
2020-09-07 00:35:37 -07:00
|
|
|
cfg, _, _ := config.Load("h2/config.xml", id, events.NoopLogger)
|
2014-12-03 03:03:44 -07:00
|
|
|
fld := cfg.Folders()["default"]
|
|
|
|
fld.Versioning = config.VersioningConfiguration{
|
|
|
|
Type: "simple",
|
|
|
|
Params: map[string]string{"keep": "5"},
|
|
|
|
}
|
|
|
|
cfg.SetFolder(fld)
|
2018-12-21 03:50:28 -07:00
|
|
|
os.Rename("h2/config.xml", "h2/config.xml.orig")
|
|
|
|
defer os.Rename("h2/config.xml.orig", "h2/config.xml")
|
2014-12-03 03:03:44 -07:00
|
|
|
cfg.Save()
|
|
|
|
|
|
|
|
testFileTypeChange(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFileTypeChangeStaggeredVersioning(t *testing.T) {
|
|
|
|
// Use staggered versioning
|
|
|
|
id, _ := protocol.DeviceIDFromString(id2)
|
2020-09-07 00:35:37 -07:00
|
|
|
cfg, _, _ := config.Load("h2/config.xml", id, events.NoopLogger)
|
2014-12-03 03:03:44 -07:00
|
|
|
fld := cfg.Folders()["default"]
|
|
|
|
fld.Versioning = config.VersioningConfiguration{
|
|
|
|
Type: "staggered",
|
|
|
|
}
|
|
|
|
cfg.SetFolder(fld)
|
2018-12-21 03:50:28 -07:00
|
|
|
os.Rename("h2/config.xml", "h2/config.xml.orig")
|
|
|
|
defer os.Rename("h2/config.xml.orig", "h2/config.xml")
|
2014-12-03 03:03:44 -07:00
|
|
|
cfg.Save()
|
|
|
|
|
|
|
|
testFileTypeChange(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testFileTypeChange(t *testing.T) {
|
2014-11-20 08:23:58 -07:00
|
|
|
log.Println("Cleaning...")
|
2015-03-29 03:55:27 -07:00
|
|
|
err := removeAll("s1", "s2", "h1/index*", "h2/index*")
|
2014-11-20 08:23:58 -07:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Println("Generating files...")
|
2014-11-29 15:41:06 -07:00
|
|
|
err = generateFiles("s1", 100, 20, "../LICENSE")
|
2014-11-20 08:23:58 -07:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// A file that we will replace with a directory later
|
|
|
|
|
2015-06-18 06:22:45 -07:00
|
|
|
if fd, err := os.Create("s1/fileToReplace"); err != nil {
|
2014-11-20 08:23:58 -07:00
|
|
|
t.Fatal(err)
|
2015-06-18 06:22:45 -07:00
|
|
|
} else {
|
|
|
|
fd.Close()
|
2014-11-20 08:23:58 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// A directory that we will replace with a file later
|
|
|
|
|
2015-02-21 16:35:19 -07:00
|
|
|
err = os.Mkdir("s1/emptyDirToReplace", 0755)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// A directory with files that we will replace with a file later
|
|
|
|
|
2014-11-20 08:23:58 -07:00
|
|
|
err = os.Mkdir("s1/dirToReplace", 0755)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-06-18 06:22:45 -07:00
|
|
|
if fd, err := os.Create("s1/dirToReplace/emptyFile"); err != nil {
|
2015-02-21 16:35:19 -07:00
|
|
|
t.Fatal(err)
|
2015-06-18 06:22:45 -07:00
|
|
|
} else {
|
|
|
|
fd.Close()
|
2015-02-21 16:35:19 -07:00
|
|
|
}
|
2014-11-20 08:23:58 -07:00
|
|
|
|
|
|
|
// Verify that the files and directories sync to the other side
|
|
|
|
|
2015-06-18 06:22:45 -07:00
|
|
|
sender := startInstance(t, 1)
|
|
|
|
defer checkedStop(t, sender)
|
2014-11-20 08:23:58 -07:00
|
|
|
|
2015-06-18 06:22:45 -07:00
|
|
|
receiver := startInstance(t, 2)
|
|
|
|
defer checkedStop(t, receiver)
|
2014-11-20 08:23:58 -07:00
|
|
|
|
2017-11-12 17:00:06 -07:00
|
|
|
sender.ResumeAll()
|
|
|
|
receiver.ResumeAll()
|
|
|
|
|
2015-06-18 06:22:45 -07:00
|
|
|
log.Println("Syncing...")
|
2014-11-20 08:23:58 -07:00
|
|
|
|
2015-06-18 06:22:45 -07:00
|
|
|
rc.AwaitSync("default", sender, receiver)
|
2014-11-20 08:23:58 -07:00
|
|
|
|
2015-06-18 06:22:45 -07:00
|
|
|
// Delay scans for the moment
|
|
|
|
if err := sender.RescanDelay("default", 86400); err != nil {
|
2014-11-29 14:17:54 -07:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-11-20 08:23:58 -07:00
|
|
|
|
|
|
|
log.Println("Comparing directories...")
|
|
|
|
err = compareDirectories("s1", "s2")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Println("Making some changes...")
|
|
|
|
|
|
|
|
// Replace file with directory
|
|
|
|
|
2015-02-21 16:35:19 -07:00
|
|
|
err = os.RemoveAll("s1/fileToReplace")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-11-20 08:23:58 -07:00
|
|
|
err = os.Mkdir("s1/fileToReplace", 0755)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-02-21 16:35:19 -07:00
|
|
|
// Replace empty directory with file
|
2014-11-20 08:23:58 -07:00
|
|
|
|
2015-02-21 16:35:19 -07:00
|
|
|
err = os.RemoveAll("s1/emptyDirToReplace")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-06-18 06:22:45 -07:00
|
|
|
if fd, err := os.Create("s1/emptyDirToReplace"); err != nil {
|
2015-02-21 16:35:19 -07:00
|
|
|
t.Fatal(err)
|
2015-06-18 06:22:45 -07:00
|
|
|
} else {
|
|
|
|
fd.Close()
|
2015-02-21 16:35:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Clear directory and replace with file
|
|
|
|
|
|
|
|
err = os.RemoveAll("s1/dirToReplace")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-06-18 06:22:45 -07:00
|
|
|
if fd, err := os.Create("s1/dirToReplace"); err != nil {
|
2014-11-20 08:23:58 -07:00
|
|
|
t.Fatal(err)
|
2015-06-18 06:22:45 -07:00
|
|
|
} else {
|
|
|
|
fd.Close()
|
2014-11-20 08:23:58 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Sync these changes and recheck
|
|
|
|
|
|
|
|
log.Println("Syncing...")
|
|
|
|
|
2015-06-18 06:22:45 -07:00
|
|
|
if err := sender.Rescan("default"); err != nil {
|
2014-11-20 08:23:58 -07:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-06-18 06:22:45 -07:00
|
|
|
rc.AwaitSync("default", sender, receiver)
|
2014-11-20 08:23:58 -07:00
|
|
|
|
|
|
|
log.Println("Comparing directories...")
|
|
|
|
err = compareDirectories("s1", "s2")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|