2015-04-02 08:21:11 +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-04-02 08:21:11 +00:00
|
|
|
|
|
|
|
// +build integration
|
|
|
|
|
|
|
|
package integration
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2015-08-06 09:29:25 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/config"
|
|
|
|
"github.com/syncthing/syncthing/lib/osutil"
|
2015-09-22 17:38:46 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/protocol"
|
2015-08-06 09:29:25 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/rc"
|
2015-04-02 08:21:11 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestOverride(t *testing.T) {
|
|
|
|
// Enable "Master" on s1/default
|
|
|
|
id, _ := protocol.DeviceIDFromString(id1)
|
|
|
|
cfg, _ := config.Load("h1/config.xml", id)
|
|
|
|
fld := cfg.Folders()["default"]
|
2016-12-19 11:26:26 +00:00
|
|
|
fld.Type = config.FolderTypeSendOnly
|
2015-04-02 08:21:11 +00:00
|
|
|
cfg.SetFolder(fld)
|
|
|
|
os.Rename("h1/config.xml", "h1/config.xml.orig")
|
2015-07-30 16:03:00 +00:00
|
|
|
defer osutil.Rename("h1/config.xml.orig", "h1/config.xml")
|
2015-04-02 08:21:11 +00:00
|
|
|
cfg.Save()
|
|
|
|
|
|
|
|
log.Println("Cleaning...")
|
|
|
|
err := removeAll("s1", "s2", "h1/index*", "h2/index*")
|
|
|
|
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
|
|
|
master := startInstance(t, 1)
|
|
|
|
defer checkedStop(t, master)
|
2015-04-02 08:21:11 +00:00
|
|
|
|
2015-06-18 13:22:45 +00:00
|
|
|
slave := startInstance(t, 2)
|
|
|
|
defer checkedStop(t, slave)
|
2015-04-02 08:21:11 +00:00
|
|
|
|
|
|
|
log.Println("Syncing...")
|
|
|
|
|
2015-06-18 13:22:45 +00:00
|
|
|
rc.AwaitSync("default", master, slave)
|
2015-04-02 08:21:11 +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("Changing file on slave side...")
|
|
|
|
|
|
|
|
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-06-18 13:22:45 +00:00
|
|
|
if err := slave.Rescan("default"); err != nil {
|
2015-04-02 08:21:11 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-05-23 20:56:43 +00:00
|
|
|
log.Println("Waiting for index to send...")
|
2015-04-02 08:21:11 +00:00
|
|
|
|
2015-05-23 20:56:43 +00:00
|
|
|
time.Sleep(10 * time.Second)
|
2015-04-02 08:21:11 +00:00
|
|
|
|
|
|
|
log.Println("Hitting Override on master...")
|
|
|
|
|
2015-06-18 13:22:45 +00:00
|
|
|
if _, err := master.Post("/rest/db/override?folder=default", nil); err != nil {
|
2015-04-02 08:21:11 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Println("Syncing...")
|
|
|
|
|
2015-06-18 13:22:45 +00:00
|
|
|
rc.AwaitSync("default", master, slave)
|
2015-04-02 08:21:11 +00:00
|
|
|
|
|
|
|
// Verify that the override worked
|
|
|
|
|
|
|
|
fd, err = os.Open("s1/testfile.txt")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
bs, err := ioutil.ReadAll(fd)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
fd.Close()
|
|
|
|
|
|
|
|
if strings.Contains(string(bs), "added to s2") {
|
|
|
|
t.Error("Change should not have been synced to master")
|
|
|
|
}
|
|
|
|
|
|
|
|
fd, err = os.Open("s2/testfile.txt")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
bs, err = ioutil.ReadAll(fd)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
fd.Close()
|
|
|
|
|
|
|
|
if strings.Contains(string(bs), "added to s2") {
|
|
|
|
t.Error("Change should have been overridden on slave")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-23 20:56:43 +00:00
|
|
|
/* This doesn't currently work with detection completion, as we don't actually
|
|
|
|
get to completion when in master/slave mode. Needs fixing.
|
|
|
|
|
2015-05-10 09:23:29 +00:00
|
|
|
func TestOverrideIgnores(t *testing.T) {
|
|
|
|
// Enable "Master" on s1/default
|
|
|
|
id, _ := protocol.DeviceIDFromString(id1)
|
|
|
|
cfg, _ := config.Load("h1/config.xml", id)
|
|
|
|
fld := cfg.Folders()["default"]
|
|
|
|
fld.ReadOnly = true
|
|
|
|
cfg.SetFolder(fld)
|
|
|
|
os.Rename("h1/config.xml", "h1/config.xml.orig")
|
2015-07-30 16:03:00 +00:00
|
|
|
defer osutil.Rename("h1/config.xml.orig", "h1/config.xml")
|
2015-05-10 09:23:29 +00:00
|
|
|
cfg.Save()
|
|
|
|
|
|
|
|
log.Println("Cleaning...")
|
|
|
|
err := removeAll("s1", "s2", "h1/index*", "h2/index*")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Println("Generating files...")
|
|
|
|
err = generateFiles("s1", 10, 2, "../LICENSE")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fd, err := os.Create("s1/testfile.txt")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
_, err = fd.WriteString("original text\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)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Println("Starting master...")
|
|
|
|
master := syncthingProcess{ // id1
|
|
|
|
instance: "1",
|
|
|
|
argv: []string{"-home", "h1"},
|
|
|
|
port: 8081,
|
|
|
|
apiKey: apiKey,
|
|
|
|
}
|
|
|
|
err = master.start()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer master.stop()
|
|
|
|
|
|
|
|
log.Println("Starting slave...")
|
|
|
|
slave := syncthingProcess{ // id2
|
|
|
|
instance: "2",
|
|
|
|
argv: []string{"-home", "h2"},
|
|
|
|
port: 8082,
|
|
|
|
apiKey: apiKey,
|
|
|
|
}
|
|
|
|
err = slave.start()
|
|
|
|
if err != nil {
|
|
|
|
master.stop()
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer slave.stop()
|
|
|
|
|
|
|
|
log.Println("Syncing...")
|
|
|
|
|
2015-05-23 20:56:43 +00:00
|
|
|
err = awaitCompletion("default", master, slave)
|
|
|
|
if err != nil {
|
2015-05-10 09:23:29 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
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("Ignoring testfile.txt on master...")
|
|
|
|
|
|
|
|
fd, err = os.Create("s1/.stignore")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
_, err = fd.WriteString("testfile.txt\n")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
err = fd.Close()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Println("Modify testfile.txt on master...")
|
|
|
|
|
|
|
|
fd, err = os.Create("s1/testfile.txt")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
_, err = fd.WriteString("updated on master but ignored\n")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
err = fd.Close()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fd, err = os.Create("s1/testfile2.txt")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
_, err = fd.WriteString("sync me\n")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
err = fd.Close()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = master.rescan("default")
|
|
|
|
|
2015-05-23 20:56:43 +00:00
|
|
|
log.Println("Waiting for sync...")
|
|
|
|
time.Sleep(10 * time.Second)
|
2015-05-10 09:23:29 +00:00
|
|
|
|
|
|
|
// Verify that sync worked
|
|
|
|
|
|
|
|
fd, err = os.Open("s2/testfile.txt")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
bs, err := ioutil.ReadAll(fd)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
fd.Close()
|
|
|
|
|
|
|
|
if !strings.Contains(string(bs), "original text") {
|
|
|
|
t.Error("Changes should not have been synced to slave")
|
|
|
|
}
|
|
|
|
|
|
|
|
fd, err = os.Open("s2/testfile2.txt")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
bs, err = ioutil.ReadAll(fd)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
fd.Close()
|
|
|
|
|
|
|
|
if !strings.Contains(string(bs), "sync me") {
|
|
|
|
t.Error("Changes should have been synced to slave")
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Println("Removing file on slave side...")
|
|
|
|
|
|
|
|
os.Remove("s2/testfile.txt")
|
|
|
|
|
|
|
|
err = slave.rescan("default")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-05-23 20:56:43 +00:00
|
|
|
log.Println("Waiting for sync...")
|
|
|
|
time.Sleep(10 * time.Second)
|
2015-05-10 09:23:29 +00:00
|
|
|
|
|
|
|
// Verify that nothing changed
|
|
|
|
|
|
|
|
fd, err = os.Open("s1/testfile.txt")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
bs, err = ioutil.ReadAll(fd)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
fd.Close()
|
|
|
|
|
|
|
|
if !strings.Contains(string(bs), "updated on master but ignored") {
|
|
|
|
t.Error("Changes should not have been synced to master")
|
|
|
|
}
|
|
|
|
|
|
|
|
fd, err = os.Open("s2/testfile.txt")
|
|
|
|
if err == nil {
|
|
|
|
t.Error("File should not exist on the slave")
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Println("Creating file on slave...")
|
|
|
|
|
|
|
|
fd, err = os.Create("s2/testfile3.txt")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
_, err = fd.WriteString("created on slave, should be removed on override\n")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
err = fd.Close()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Println("Hitting Override on master...")
|
|
|
|
|
|
|
|
resp, err := master.post("/rest/db/override?folder=default", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if resp.StatusCode != 200 {
|
|
|
|
t.Fatal(resp.Status)
|
|
|
|
}
|
|
|
|
|
2015-05-23 20:56:43 +00:00
|
|
|
log.Println("Waiting for sync...")
|
|
|
|
time.Sleep(10 * time.Second)
|
2015-05-10 09:23:29 +00:00
|
|
|
|
|
|
|
fd, err = os.Open("s2/testfile.txt")
|
|
|
|
if err == nil {
|
|
|
|
t.Error("File should not exist on the slave")
|
|
|
|
}
|
|
|
|
fd, err = os.Open("s2/testfile2.txt")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
bs, err = ioutil.ReadAll(fd)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
fd.Close()
|
|
|
|
if !strings.Contains(string(bs), "sync me") {
|
|
|
|
t.Error("Changes should have been synced to slave")
|
|
|
|
}
|
|
|
|
fd, err = os.Open("s2/testfile3.txt")
|
|
|
|
if err != nil {
|
|
|
|
t.Error("File should still exist on the slave")
|
|
|
|
}
|
|
|
|
fd.Close()
|
|
|
|
|
|
|
|
log.Println("Hitting Override on master (again)...")
|
|
|
|
|
|
|
|
resp, err = master.post("/rest/db/override?folder=default", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if resp.StatusCode != 200 {
|
|
|
|
t.Fatal(resp.Status)
|
|
|
|
}
|
|
|
|
|
2015-05-23 20:56:43 +00:00
|
|
|
log.Println("Waiting for sync...")
|
|
|
|
time.Sleep(10 * time.Second)
|
2015-05-10 09:23:29 +00:00
|
|
|
|
|
|
|
fd, err = os.Open("s2/testfile.txt")
|
|
|
|
if err == nil {
|
|
|
|
t.Error("File should not exist on the slave")
|
|
|
|
}
|
|
|
|
fd, err = os.Open("s2/testfile2.txt")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
bs, err = ioutil.ReadAll(fd)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
fd.Close()
|
|
|
|
if !strings.Contains(string(bs), "sync me") {
|
|
|
|
t.Error("Changes should have been synced to slave")
|
|
|
|
}
|
|
|
|
fd, err = os.Open("s2/testfile3.txt")
|
|
|
|
if err != nil {
|
|
|
|
t.Error("File should still exist on the slave")
|
|
|
|
}
|
|
|
|
fd.Close()
|
|
|
|
|
|
|
|
}
|
2015-05-23 20:56:43 +00:00
|
|
|
*/
|