2015-03-29 14:16:36 +00:00
|
|
|
// Copyright (C) 2015 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,
|
2017-02-09 06:52:18 +00:00
|
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
2015-03-29 14:16:36 +00:00
|
|
|
|
|
|
|
// +build integration
|
|
|
|
|
|
|
|
package integration
|
|
|
|
|
|
|
|
import (
|
2015-07-21 13:29:02 +00:00
|
|
|
"bytes"
|
2015-04-09 10:53:57 +00:00
|
|
|
"io/ioutil"
|
2015-03-29 14:16:36 +00:00
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
"time"
|
2015-04-27 23:09:44 +00:00
|
|
|
|
2015-08-06 09:29:25 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/osutil"
|
|
|
|
"github.com/syncthing/syncthing/lib/rc"
|
2015-03-29 14:16:36 +00:00
|
|
|
)
|
|
|
|
|
2015-06-18 13:22:45 +00:00
|
|
|
func TestConflictsDefault(t *testing.T) {
|
2015-03-29 14:16:36 +00:00
|
|
|
log.Println("Cleaning...")
|
2015-03-29 10:55:27 +00:00
|
|
|
err := removeAll("s1", "s2", "h1/index*", "h2/index*")
|
2015-03-29 14:16:36 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Println("Generating files...")
|
|
|
|
err = generateFiles("s1", 100, 20, "../LICENSE")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fd, err := os.Create("s1/testfile.txt")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
_, err = fd.WriteString("hello\n")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
err = fd.Close()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expected, err := directoryContents("s1")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-06-18 13:22:45 +00:00
|
|
|
sender := startInstance(t, 1)
|
|
|
|
defer checkedStop(t, sender)
|
|
|
|
receiver := startInstance(t, 2)
|
|
|
|
defer checkedStop(t, receiver)
|
2015-03-29 14:16:36 +00:00
|
|
|
|
2015-06-18 13:22:45 +00:00
|
|
|
// Rescan with a delay on the next one, so we are not surprised by a
|
|
|
|
// sudden rescan while we're trying to introduce conflicts.
|
|
|
|
|
|
|
|
if err := sender.RescanDelay("default", 86400); err != nil {
|
2015-03-29 14:16:36 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-06-18 13:22:45 +00:00
|
|
|
if err := receiver.RescanDelay("default", 86400); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
rc.AwaitSync("default", sender, receiver)
|
2015-03-29 14:16:36 +00:00
|
|
|
|
|
|
|
log.Println("Verifying...")
|
|
|
|
|
|
|
|
actual, err := directoryContents("s2")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
err = compareDirectoryContents(actual, expected)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Println("Introducing a conflict (simultaneous edit)...")
|
|
|
|
|
2015-11-17 12:32:57 +00:00
|
|
|
if err := sender.PauseDevice(receiver.ID()); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-03-29 14:16:36 +00:00
|
|
|
fd, err = os.OpenFile("s1/testfile.txt", os.O_WRONLY|os.O_APPEND, 0644)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
_, err = fd.WriteString("text added to s1\n")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
err = fd.Close()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fd, err = os.OpenFile("s2/testfile.txt", os.O_WRONLY|os.O_APPEND, 0644)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
_, err = fd.WriteString("text added to s2\n")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
err = fd.Close()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-11-17 12:32:57 +00:00
|
|
|
if err := sender.ResumeDevice(receiver.ID()); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-03-29 14:16:36 +00:00
|
|
|
log.Println("Syncing...")
|
|
|
|
|
2015-06-18 13:22:45 +00:00
|
|
|
if err := sender.RescanDelay("default", 86400); err != nil {
|
2015-03-29 14:16:36 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-06-18 13:22:45 +00:00
|
|
|
if err := receiver.RescanDelay("default", 86400); err != nil {
|
2015-03-29 14:16:36 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-06-18 13:22:45 +00:00
|
|
|
rc.AwaitSync("default", sender, receiver)
|
2015-03-29 14:16:36 +00:00
|
|
|
|
|
|
|
// The conflict is expected on the s2 side due to how we calculate which
|
|
|
|
// file is the winner (based on device ID)
|
|
|
|
|
2015-04-26 14:41:04 +00:00
|
|
|
files, err := osutil.Glob("s2/*sync-conflict*")
|
2015-03-29 14:16:36 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(files) != 1 {
|
|
|
|
t.Errorf("Expected 1 conflicted files instead of %d", len(files))
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Println("Introducing a conflict (edit plus delete)...")
|
|
|
|
|
2015-11-17 12:32:57 +00:00
|
|
|
if err := sender.PauseDevice(receiver.ID()); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-03-29 14:16:36 +00:00
|
|
|
err = os.Remove("s1/testfile.txt")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fd, err = os.OpenFile("s2/testfile.txt", os.O_WRONLY|os.O_APPEND, 0644)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
_, err = fd.WriteString("more text added to s2\n")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
err = fd.Close()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-11-17 12:32:57 +00:00
|
|
|
if err := sender.ResumeDevice(receiver.ID()); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-03-29 14:16:36 +00:00
|
|
|
log.Println("Syncing...")
|
|
|
|
|
2015-06-18 13:22:45 +00:00
|
|
|
if err := sender.RescanDelay("default", 86400); err != nil {
|
2015-03-29 14:16:36 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-06-18 13:22:45 +00:00
|
|
|
if err := receiver.RescanDelay("default", 86400); err != nil {
|
2015-03-29 14:16:36 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-06-18 13:22:45 +00:00
|
|
|
rc.AwaitSync("default", sender, receiver)
|
2015-03-29 14:16:36 +00:00
|
|
|
|
2015-07-21 13:29:02 +00:00
|
|
|
// The conflict is resolved to the advantage of the edit over the delete.
|
|
|
|
// As such, we get the edited content synced back to s1 where it was
|
|
|
|
// removed.
|
2015-03-29 14:16:36 +00:00
|
|
|
|
2015-04-26 14:41:04 +00:00
|
|
|
files, err = osutil.Glob("s2/*sync-conflict*")
|
2015-03-29 14:16:36 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-07-21 13:29:02 +00:00
|
|
|
if len(files) != 1 {
|
|
|
|
t.Errorf("Expected 1 conflicted files instead of %d", len(files))
|
|
|
|
}
|
|
|
|
bs, err := ioutil.ReadFile("s1/testfile.txt")
|
|
|
|
if err != nil {
|
|
|
|
t.Error("reading file:", err)
|
|
|
|
}
|
|
|
|
if !bytes.Contains(bs, []byte("more text added to s2")) {
|
|
|
|
t.Error("s1/testfile.txt should contain data added in s2")
|
2015-03-29 14:16:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-18 13:22:45 +00:00
|
|
|
func TestConflictsInitialMerge(t *testing.T) {
|
2015-04-09 10:53:57 +00:00
|
|
|
log.Println("Cleaning...")
|
|
|
|
err := removeAll("s1", "s2", "h1/index*", "h2/index*")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = os.Mkdir("s1", 0755)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
err = os.Mkdir("s2", 0755)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// File 1 is a conflict
|
|
|
|
|
|
|
|
err = ioutil.WriteFile("s1/file1", []byte("hello\n"), 0644)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = ioutil.WriteFile("s2/file1", []byte("goodbye\n"), 0644)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// File 2 exists on s1 only
|
|
|
|
|
|
|
|
err = ioutil.WriteFile("s1/file2", []byte("hello\n"), 0644)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// File 3 exists on s2 only
|
|
|
|
|
|
|
|
err = ioutil.WriteFile("s2/file3", []byte("goodbye\n"), 0644)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Let them sync
|
|
|
|
|
2015-06-18 13:22:45 +00:00
|
|
|
sender := startInstance(t, 1)
|
|
|
|
defer checkedStop(t, sender)
|
|
|
|
receiver := startInstance(t, 2)
|
|
|
|
defer checkedStop(t, receiver)
|
2015-04-09 10:53:57 +00:00
|
|
|
|
|
|
|
log.Println("Syncing...")
|
|
|
|
|
2015-06-18 13:22:45 +00:00
|
|
|
rc.AwaitSync("default", sender, receiver)
|
2015-04-09 10:53:57 +00:00
|
|
|
|
2015-06-18 13:22:45 +00:00
|
|
|
checkedStop(t, sender)
|
|
|
|
checkedStop(t, receiver)
|
2015-04-09 10:53:57 +00:00
|
|
|
|
|
|
|
log.Println("Verifying...")
|
|
|
|
|
|
|
|
// s1 should have three-four files (there's a conflict from s2 which may or may not have synced yet)
|
|
|
|
|
2015-04-26 14:41:04 +00:00
|
|
|
files, err := osutil.Glob("s1/file*")
|
2015-04-09 10:53:57 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(files) < 3 || len(files) > 4 {
|
|
|
|
t.Errorf("Expected 3-4 files in s1 instead of %d", len(files))
|
|
|
|
}
|
|
|
|
|
|
|
|
// s2 should have four files (there's a conflict)
|
|
|
|
|
2015-04-26 14:41:04 +00:00
|
|
|
files, err = osutil.Glob("s2/file*")
|
2015-04-09 10:53:57 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(files) != 4 {
|
|
|
|
t.Errorf("Expected 4 files in s2 instead of %d", len(files))
|
|
|
|
}
|
|
|
|
|
|
|
|
// file1 is in conflict, so there's two versions of that one
|
|
|
|
|
2015-04-26 14:41:04 +00:00
|
|
|
files, err = osutil.Glob("s2/file1*")
|
2015-04-09 10:53:57 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(files) != 2 {
|
|
|
|
t.Errorf("Expected 2 'file1' files in s2 instead of %d", len(files))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-18 13:22:45 +00:00
|
|
|
func TestConflictsIndexReset(t *testing.T) {
|
2015-04-09 10:53:57 +00:00
|
|
|
log.Println("Cleaning...")
|
|
|
|
err := removeAll("s1", "s2", "h1/index*", "h2/index*")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = os.Mkdir("s1", 0755)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
err = os.Mkdir("s2", 0755)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Three files on s1
|
|
|
|
|
|
|
|
err = ioutil.WriteFile("s1/file1", []byte("hello\n"), 0644)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
err = ioutil.WriteFile("s1/file2", []byte("hello\n"), 0644)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
err = ioutil.WriteFile("s2/file3", []byte("hello\n"), 0644)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Let them sync
|
|
|
|
|
2015-06-18 13:22:45 +00:00
|
|
|
sender := startInstance(t, 1)
|
|
|
|
defer checkedStop(t, sender)
|
|
|
|
receiver := startInstance(t, 2)
|
|
|
|
defer checkedStop(t, receiver)
|
2015-04-09 10:53:57 +00:00
|
|
|
|
|
|
|
log.Println("Syncing...")
|
|
|
|
|
2015-06-18 13:22:45 +00:00
|
|
|
rc.AwaitSync("default", sender, receiver)
|
2015-04-09 10:53:57 +00:00
|
|
|
|
|
|
|
log.Println("Verifying...")
|
|
|
|
|
|
|
|
// s1 should have three files
|
|
|
|
|
2015-04-26 14:41:04 +00:00
|
|
|
files, err := osutil.Glob("s1/file*")
|
2015-04-09 10:53:57 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(files) != 3 {
|
|
|
|
t.Errorf("Expected 3 files in s1 instead of %d", len(files))
|
|
|
|
}
|
|
|
|
|
|
|
|
// s2 should have three
|
|
|
|
|
2015-04-26 14:41:04 +00:00
|
|
|
files, err = osutil.Glob("s2/file*")
|
2015-04-09 10:53:57 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(files) != 3 {
|
|
|
|
t.Errorf("Expected 3 files in s2 instead of %d", len(files))
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Println("Updating...")
|
|
|
|
|
|
|
|
// change s2/file2 a few times, so that it's version counter increases.
|
|
|
|
// This will make the file on the cluster look newer than what we have
|
|
|
|
// locally after we rest the index, unless we have a fix for that.
|
|
|
|
|
2015-06-18 13:22:45 +00:00
|
|
|
for i := 0; i < 5; i++ {
|
|
|
|
err = ioutil.WriteFile("s2/file2", []byte("hello1\n"), 0644)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
err = receiver.Rescan("default")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
time.Sleep(time.Second)
|
2015-04-09 10:53:57 +00:00
|
|
|
}
|
|
|
|
|
2015-06-18 13:22:45 +00:00
|
|
|
rc.AwaitSync("default", sender, receiver)
|
2015-04-09 10:53:57 +00:00
|
|
|
|
|
|
|
// Now nuke the index
|
|
|
|
|
|
|
|
log.Println("Resetting...")
|
|
|
|
|
2015-06-18 13:22:45 +00:00
|
|
|
checkedStop(t, receiver)
|
2015-04-09 10:53:57 +00:00
|
|
|
removeAll("h2/index*")
|
|
|
|
|
|
|
|
// s1/file1 (remote) changes while receiver is down
|
|
|
|
|
|
|
|
err = ioutil.WriteFile("s1/file1", []byte("goodbye\n"), 0644)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// s1 must know about it
|
2015-06-18 13:22:45 +00:00
|
|
|
err = sender.Rescan("default")
|
2015-04-09 10:53:57 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// s2/file2 (local) changes while receiver is down
|
|
|
|
|
|
|
|
err = ioutil.WriteFile("s2/file2", []byte("goodbye\n"), 0644)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-06-18 13:22:45 +00:00
|
|
|
receiver = startInstance(t, 2)
|
|
|
|
defer checkedStop(t, receiver)
|
2015-04-09 10:53:57 +00:00
|
|
|
|
|
|
|
log.Println("Syncing...")
|
|
|
|
|
2015-06-18 13:22:45 +00:00
|
|
|
rc.AwaitSync("default", sender, receiver)
|
2015-04-09 10:53:57 +00:00
|
|
|
|
|
|
|
// s2 should have five files (three plus two conflicts)
|
|
|
|
|
2015-04-26 14:41:04 +00:00
|
|
|
files, err = osutil.Glob("s2/file*")
|
2015-04-09 10:53:57 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(files) != 5 {
|
|
|
|
t.Errorf("Expected 5 files in s2 instead of %d", len(files))
|
|
|
|
}
|
|
|
|
|
|
|
|
// file1 is in conflict, so there's two versions of that one
|
|
|
|
|
2015-04-26 14:41:04 +00:00
|
|
|
files, err = osutil.Glob("s2/file1*")
|
2015-04-09 10:53:57 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(files) != 2 {
|
|
|
|
t.Errorf("Expected 2 'file1' files in s2 instead of %d", len(files))
|
|
|
|
}
|
|
|
|
|
|
|
|
// file2 is in conflict, so there's two versions of that one
|
|
|
|
|
2015-04-26 14:41:04 +00:00
|
|
|
files, err = osutil.Glob("s2/file2*")
|
2015-04-09 10:53:57 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(files) != 2 {
|
|
|
|
t.Errorf("Expected 2 'file2' files in s2 instead of %d", len(files))
|
|
|
|
}
|
|
|
|
}
|