2014-11-18 21:18:19 -07:00
|
|
|
// Copyright (C) 2014 The Syncthing Authors.
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify it
|
|
|
|
// under the terms of the GNU General Public License as published by the Free
|
|
|
|
// Software Foundation, either version 3 of the License, or (at your option)
|
|
|
|
// any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
// more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License along
|
|
|
|
// with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
// +build integration
|
|
|
|
|
2014-12-17 04:31:03 -07:00
|
|
|
package integration
|
2014-11-18 21:18:19 -07:00
|
|
|
|
|
|
|
import (
|
2014-12-08 14:15:10 -07:00
|
|
|
"io/ioutil"
|
2014-11-18 21:18:19 -07:00
|
|
|
"log"
|
|
|
|
"os"
|
2014-12-08 14:15:10 -07:00
|
|
|
"path/filepath"
|
2014-11-18 21:18:19 -07:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2014-12-03 03:03:44 -07:00
|
|
|
"github.com/syncthing/syncthing/internal/config"
|
|
|
|
"github.com/syncthing/syncthing/internal/protocol"
|
2014-11-18 21:18:19 -07:00
|
|
|
"github.com/syncthing/syncthing/internal/symlinks"
|
|
|
|
)
|
|
|
|
|
2014-12-08 14:15:10 -07:00
|
|
|
func symlinksSupported() bool {
|
|
|
|
tmp, err := ioutil.TempDir("", "symlink-test")
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmp)
|
|
|
|
err = os.Symlink("tmp", filepath.Join(tmp, "link"))
|
|
|
|
return err == nil
|
|
|
|
}
|
|
|
|
|
2014-11-18 21:18:19 -07:00
|
|
|
func TestSymlinks(t *testing.T) {
|
2014-12-08 14:15:10 -07:00
|
|
|
if !symlinksSupported() {
|
|
|
|
t.Skip("symlinks unsupported")
|
|
|
|
}
|
|
|
|
|
2014-12-03 03:03:44 -07:00
|
|
|
// Use no versioning
|
|
|
|
id, _ := protocol.DeviceIDFromString(id2)
|
|
|
|
cfg, _ := config.Load("h2/config.xml", id)
|
|
|
|
fld := cfg.Folders()["default"]
|
|
|
|
fld.Versioning = config.VersioningConfiguration{}
|
|
|
|
cfg.SetFolder(fld)
|
|
|
|
cfg.Save()
|
|
|
|
|
|
|
|
testSymlinks(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSymlinksSimpleVersioning(t *testing.T) {
|
2014-12-08 14:15:10 -07:00
|
|
|
if !symlinksSupported() {
|
|
|
|
t.Skip("symlinks unsupported")
|
|
|
|
}
|
|
|
|
|
2014-12-17 04:31:03 -07:00
|
|
|
// Use simple versioning
|
2014-12-03 03:03:44 -07:00
|
|
|
id, _ := protocol.DeviceIDFromString(id2)
|
|
|
|
cfg, _ := config.Load("h2/config.xml", id)
|
|
|
|
fld := cfg.Folders()["default"]
|
|
|
|
fld.Versioning = config.VersioningConfiguration{
|
|
|
|
Type: "simple",
|
|
|
|
Params: map[string]string{"keep": "5"},
|
|
|
|
}
|
|
|
|
cfg.SetFolder(fld)
|
|
|
|
cfg.Save()
|
|
|
|
|
|
|
|
testSymlinks(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSymlinksStaggeredVersioning(t *testing.T) {
|
2014-12-08 14:15:10 -07:00
|
|
|
if !symlinksSupported() {
|
|
|
|
t.Skip("symlinks unsupported")
|
|
|
|
}
|
|
|
|
|
2014-12-17 04:31:03 -07:00
|
|
|
// Use staggered versioning
|
2014-12-03 03:03:44 -07:00
|
|
|
id, _ := protocol.DeviceIDFromString(id2)
|
|
|
|
cfg, _ := config.Load("h2/config.xml", id)
|
|
|
|
fld := cfg.Folders()["default"]
|
|
|
|
fld.Versioning = config.VersioningConfiguration{
|
|
|
|
Type: "staggered",
|
|
|
|
}
|
|
|
|
cfg.SetFolder(fld)
|
|
|
|
cfg.Save()
|
|
|
|
|
|
|
|
testSymlinks(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testSymlinks(t *testing.T) {
|
2014-11-18 21:18:19 -07:00
|
|
|
log.Println("Cleaning...")
|
|
|
|
err := removeAll("s1", "s2", "h1/index", "h2/index")
|
|
|
|
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-18 21:18:19 -07:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// A file that we will replace with a symlink later
|
|
|
|
|
|
|
|
fd, err := os.Create("s1/fileToReplace")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
fd.Close()
|
|
|
|
|
|
|
|
// A directory that we will replace with a symlink later
|
|
|
|
|
|
|
|
err = os.Mkdir("s1/dirToReplace", 0755)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// A file and a symlink to that file
|
|
|
|
|
|
|
|
fd, err = os.Create("s1/file")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
fd.Close()
|
2014-12-04 14:24:49 -07:00
|
|
|
err = symlinks.Create("s1/fileLink", "file", 0)
|
2014-11-18 21:18:19 -07:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// A directory and a symlink to that directory
|
|
|
|
|
|
|
|
err = os.Mkdir("s1/dir", 0755)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-12-04 14:24:49 -07:00
|
|
|
err = symlinks.Create("s1/dirLink", "dir", 0)
|
2014-11-18 21:18:19 -07:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// A link to something in the repo that does not exist
|
|
|
|
|
2014-12-04 14:24:49 -07:00
|
|
|
err = symlinks.Create("s1/noneLink", "does/not/exist", 0)
|
2014-11-18 21:18:19 -07:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// A link we will replace with a file later
|
|
|
|
|
2014-12-04 14:24:49 -07:00
|
|
|
err = symlinks.Create("s1/repFileLink", "does/not/exist", 0)
|
2014-11-18 21:18:19 -07:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// A link we will replace with a directory later
|
|
|
|
|
2014-12-04 14:24:49 -07:00
|
|
|
err = symlinks.Create("s1/repDirLink", "does/not/exist", 0)
|
2014-11-18 21:18:19 -07:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2014-12-03 01:05:01 -07:00
|
|
|
// A link we will remove later
|
|
|
|
|
2014-12-04 14:24:49 -07:00
|
|
|
err = symlinks.Create("s1/removeLink", "does/not/exist", 0)
|
2014-12-03 01:05:01 -07:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2014-11-18 21:18:19 -07:00
|
|
|
// Verify that the files and symlinks sync to the other side
|
|
|
|
|
|
|
|
log.Println("Syncing...")
|
|
|
|
|
|
|
|
sender := syncthingProcess{ // id1
|
2015-01-11 01:26:20 -07:00
|
|
|
instance: "1",
|
|
|
|
argv: []string{"-home", "h1"},
|
|
|
|
port: 8081,
|
|
|
|
apiKey: apiKey,
|
2014-11-18 21:18:19 -07:00
|
|
|
}
|
|
|
|
err = sender.start()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
receiver := syncthingProcess{ // id2
|
2015-01-11 01:26:20 -07:00
|
|
|
instance: "2",
|
|
|
|
argv: []string{"-home", "h2"},
|
|
|
|
port: 8082,
|
|
|
|
apiKey: apiKey,
|
2014-11-18 21:18:19 -07:00
|
|
|
}
|
|
|
|
err = receiver.start()
|
|
|
|
if err != nil {
|
2014-11-29 14:17:54 -07:00
|
|
|
_ = sender.stop()
|
2014-11-18 21:18:19 -07:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
comp, err := sender.peerCompletion()
|
|
|
|
if err != nil {
|
2014-12-17 04:31:03 -07:00
|
|
|
if isTimeout(err) {
|
2014-11-18 21:18:19 -07:00
|
|
|
time.Sleep(time.Second)
|
|
|
|
continue
|
|
|
|
}
|
2014-11-29 14:17:54 -07:00
|
|
|
_ = sender.stop()
|
|
|
|
_ = receiver.stop()
|
2014-11-18 21:18:19 -07:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
curComp := comp[id2]
|
|
|
|
|
|
|
|
if curComp == 100 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
}
|
|
|
|
|
2014-11-29 14:17:54 -07:00
|
|
|
err = sender.stop()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
err = receiver.stop()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-11-18 21:18:19 -07:00
|
|
|
|
|
|
|
log.Println("Comparing directories...")
|
|
|
|
err = compareDirectories("s1", "s2")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Println("Making some changes...")
|
|
|
|
|
|
|
|
// Remove one symlink
|
|
|
|
|
|
|
|
err = os.Remove("s1/fileLink")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Change the target of another
|
|
|
|
|
|
|
|
err = os.Remove("s1/dirLink")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2014-12-04 14:24:49 -07:00
|
|
|
err = symlinks.Create("s1/dirLink", "file", 0)
|
2014-11-18 21:18:19 -07:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Replace one with a file
|
|
|
|
|
|
|
|
err = os.Remove("s1/repFileLink")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fd, err = os.Create("s1/repFileLink")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
fd.Close()
|
|
|
|
|
|
|
|
// Replace one with a directory
|
|
|
|
|
|
|
|
err = os.Remove("s1/repDirLink")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = os.Mkdir("s1/repDirLink", 0755)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Replace a file with a symlink
|
|
|
|
|
|
|
|
err = os.Remove("s1/fileToReplace")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2014-12-04 14:24:49 -07:00
|
|
|
err = symlinks.Create("s1/fileToReplace", "somewhere/non/existent", 0)
|
2014-11-18 21:18:19 -07:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Replace a directory with a symlink
|
|
|
|
|
|
|
|
err = os.RemoveAll("s1/dirToReplace")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2014-12-04 14:24:49 -07:00
|
|
|
err = symlinks.Create("s1/dirToReplace", "somewhere/non/existent", 0)
|
2014-11-18 21:18:19 -07:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2014-12-03 03:03:44 -07:00
|
|
|
// Remove a broken symlink
|
2014-12-03 01:05:01 -07:00
|
|
|
|
|
|
|
err = os.Remove("s1/removeLink")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2014-11-18 21:18:19 -07:00
|
|
|
// Sync these changes and recheck
|
|
|
|
|
|
|
|
log.Println("Syncing...")
|
|
|
|
|
|
|
|
err = sender.start()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = receiver.start()
|
|
|
|
if err != nil {
|
2014-11-29 14:17:54 -07:00
|
|
|
_ = sender.stop()
|
2014-11-18 21:18:19 -07:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
comp, err := sender.peerCompletion()
|
|
|
|
if err != nil {
|
2014-12-17 04:31:03 -07:00
|
|
|
if isTimeout(err) {
|
2014-11-18 21:18:19 -07:00
|
|
|
time.Sleep(time.Second)
|
|
|
|
continue
|
|
|
|
}
|
2014-11-29 14:17:54 -07:00
|
|
|
_ = sender.stop()
|
|
|
|
_ = receiver.stop()
|
2014-11-18 21:18:19 -07:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
curComp := comp[id2]
|
|
|
|
|
|
|
|
if curComp == 100 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
}
|
|
|
|
|
2014-11-29 14:17:54 -07:00
|
|
|
err = sender.stop()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
err = receiver.stop()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-11-18 21:18:19 -07:00
|
|
|
|
|
|
|
log.Println("Comparing directories...")
|
|
|
|
err = compareDirectories("s1", "s2")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|