2014-12-17 11:31:03 +00:00
|
|
|
// Copyright (C) 2014 The Syncthing Authors.
|
|
|
|
//
|
2015-03-07 20:36:35 +00: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,
|
|
|
|
// You can obtain one at http://mozilla.org/MPL/2.0/.
|
2014-12-17 11:31:03 +00:00
|
|
|
|
|
|
|
// +build integration
|
|
|
|
|
|
|
|
package integration
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2014-12-29 13:15:08 +00:00
|
|
|
"os"
|
2014-12-17 11:31:03 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2015-01-13 12:22:56 +00:00
|
|
|
"github.com/syncthing/protocol"
|
2014-12-17 11:31:03 +00:00
|
|
|
"github.com/syncthing/syncthing/internal/config"
|
|
|
|
)
|
|
|
|
|
2014-12-28 23:11:32 +00:00
|
|
|
func TestSyncClusterWithoutVersioning(t *testing.T) {
|
2014-12-17 11:31:03 +00: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()
|
|
|
|
|
|
|
|
testSyncCluster(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSyncClusterSimpleVersioning(t *testing.T) {
|
|
|
|
// Use simple versioning
|
|
|
|
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()
|
|
|
|
|
|
|
|
testSyncCluster(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSyncClusterStaggeredVersioning(t *testing.T) {
|
|
|
|
// Use staggered versioning
|
|
|
|
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()
|
|
|
|
|
|
|
|
testSyncCluster(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testSyncCluster(t *testing.T) {
|
2015-04-01 08:43:19 +00:00
|
|
|
// This tests syncing files back and forth between three cluster members.
|
|
|
|
// Their configs are in h1, h2 and h3. The folder "default" is shared
|
|
|
|
// between all and stored in s1, s2 and s3 respectively.
|
|
|
|
//
|
|
|
|
// Another folder is shared between 1 and 2 only, in s12-1 and s12-2. A
|
|
|
|
// third folders is shared between 2 and 3, in s23-2 and s23-3.
|
|
|
|
|
|
|
|
const (
|
|
|
|
numFiles = 100
|
|
|
|
fileSizeExp = 20
|
|
|
|
iterations = 3
|
|
|
|
)
|
|
|
|
log.Printf("Testing with numFiles=%d, fileSizeExp=%d, iterations=%d", numFiles, fileSizeExp, iterations)
|
2014-12-17 11:31:03 +00:00
|
|
|
|
|
|
|
log.Println("Cleaning...")
|
|
|
|
err := removeAll("s1", "s12-1",
|
|
|
|
"s2", "s12-2", "s23-2",
|
|
|
|
"s3", "s23-3",
|
2015-03-29 10:55:27 +00:00
|
|
|
"h1/index*", "h2/index*", "h3/index*")
|
2014-12-17 11:31:03 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create initial folder contents. All three devices have stuff in
|
|
|
|
// "default", which should be merged. The other two folders are initially
|
|
|
|
// empty on one side.
|
|
|
|
|
|
|
|
log.Println("Generating files...")
|
|
|
|
|
2015-04-01 08:43:19 +00:00
|
|
|
err = generateFiles("s1", numFiles, fileSizeExp, "../LICENSE")
|
2014-12-17 11:31:03 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-04-01 08:43:19 +00:00
|
|
|
err = generateFiles("s12-1", numFiles, fileSizeExp, "../LICENSE")
|
2014-12-17 11:31:03 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2014-12-29 13:15:08 +00:00
|
|
|
// We'll use this file for appending data without modifying the time stamp.
|
2014-12-20 23:12:12 +00:00
|
|
|
fd, err := os.Create("s1/test-appendfile")
|
2014-12-29 13:15:08 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2015-04-01 08:43:19 +00:00
|
|
|
err = generateFiles("s2", numFiles, fileSizeExp, "../LICENSE")
|
2014-12-17 11:31:03 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-04-01 08:43:19 +00:00
|
|
|
err = generateFiles("s23-2", numFiles, fileSizeExp, "../LICENSE")
|
2014-12-17 11:31:03 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-04-01 08:43:19 +00:00
|
|
|
err = generateFiles("s3", numFiles, fileSizeExp, "../LICENSE")
|
2014-12-17 11:31:03 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prepare the expected state of folders after the sync
|
2014-12-30 14:13:58 +00:00
|
|
|
c1, err := directoryContents("s1")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
c2, err := directoryContents("s2")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
c3, err := directoryContents("s3")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
e1 := mergeDirectoryContents(c1, c2, c3)
|
|
|
|
e2, err := directoryContents("s12-1")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
e3, err := directoryContents("s23-2")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-12-17 11:31:03 +00:00
|
|
|
expected := [][]fileInfo{e1, e2, e3}
|
|
|
|
|
2014-12-30 15:04:34 +00:00
|
|
|
// Start the syncers
|
|
|
|
p, err := scStartProcesses()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
for i := range p {
|
|
|
|
p[i].stop()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2015-04-01 08:43:19 +00:00
|
|
|
log.Println("Waiting for startup...")
|
2015-05-23 20:56:43 +00:00
|
|
|
for _, dev := range p {
|
|
|
|
waitForScan(dev)
|
2015-04-01 08:43:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for count := 0; count < iterations; count++ {
|
2014-12-17 11:31:03 +00:00
|
|
|
log.Println("Forcing rescan...")
|
|
|
|
|
|
|
|
// Force rescan of folders
|
2015-04-01 08:43:19 +00:00
|
|
|
for i, device := range p {
|
|
|
|
if err := device.rescan("default"); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if i < 2 {
|
|
|
|
if err := device.rescan("s12"); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-12-17 11:31:03 +00:00
|
|
|
}
|
|
|
|
if i > 1 {
|
2015-04-01 08:43:19 +00:00
|
|
|
if err := device.rescan("s23"); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-12-17 11:31:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sync stuff and verify it looks right
|
|
|
|
err = scSyncAndCompare(p, expected)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Println("Altering...")
|
|
|
|
|
|
|
|
// Alter the source files for another round
|
|
|
|
err = alterFiles("s1")
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
err = alterFiles("s12-1")
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
err = alterFiles("s23-2")
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2014-12-20 23:12:12 +00:00
|
|
|
// Alter the "test-appendfile" without changing it's modification time. Sneaky!
|
|
|
|
fi, err := os.Stat("s1/test-appendfile")
|
2014-12-29 13:15:08 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-12-20 23:12:12 +00:00
|
|
|
fd, err := os.OpenFile("s1/test-appendfile", os.O_APPEND|os.O_WRONLY, 0644)
|
2014-12-29 13:15:08 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
_, err = fd.Seek(0, os.SEEK_END)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
_, err = fd.WriteString("more data\n")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
err = fd.Close()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-12-20 23:12:12 +00:00
|
|
|
err = os.Chtimes("s1/test-appendfile", fi.ModTime(), fi.ModTime())
|
2014-12-29 13:15:08 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2014-12-17 11:31:03 +00:00
|
|
|
// Prepare the expected state of folders after the sync
|
2014-12-30 14:13:58 +00:00
|
|
|
e1, err = directoryContents("s1")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
e2, err = directoryContents("s12-1")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
e3, err = directoryContents("s23-2")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-12-17 11:31:03 +00:00
|
|
|
expected = [][]fileInfo{e1, e2, e3}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func scStartProcesses() ([]syncthingProcess, error) {
|
|
|
|
p := make([]syncthingProcess, 3)
|
|
|
|
|
|
|
|
p[0] = syncthingProcess{ // id1
|
2015-01-11 08:26:20 +00:00
|
|
|
instance: "1",
|
|
|
|
argv: []string{"-home", "h1"},
|
|
|
|
port: 8081,
|
|
|
|
apiKey: apiKey,
|
2014-12-17 11:31:03 +00:00
|
|
|
}
|
|
|
|
err := p[0].start()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
p[1] = syncthingProcess{ // id2
|
2015-01-11 08:26:20 +00:00
|
|
|
instance: "2",
|
|
|
|
argv: []string{"-home", "h2"},
|
|
|
|
port: 8082,
|
|
|
|
apiKey: apiKey,
|
2014-12-17 11:31:03 +00:00
|
|
|
}
|
|
|
|
err = p[1].start()
|
|
|
|
if err != nil {
|
2015-04-30 06:43:57 +00:00
|
|
|
p[0].stop()
|
2014-12-17 11:31:03 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
p[2] = syncthingProcess{ // id3
|
2015-01-11 08:26:20 +00:00
|
|
|
instance: "3",
|
|
|
|
argv: []string{"-home", "h3"},
|
|
|
|
port: 8083,
|
|
|
|
apiKey: apiKey,
|
2014-12-17 11:31:03 +00:00
|
|
|
}
|
|
|
|
err = p[2].start()
|
|
|
|
if err != nil {
|
2015-04-30 06:43:57 +00:00
|
|
|
p[0].stop()
|
|
|
|
p[1].stop()
|
2014-12-17 11:31:03 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return p, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func scSyncAndCompare(p []syncthingProcess, expected [][]fileInfo) error {
|
|
|
|
log.Println("Syncing...")
|
|
|
|
|
2015-05-23 20:56:43 +00:00
|
|
|
// Special handling because we know which devices share which folders...
|
|
|
|
if err := awaitCompletion("default", p...); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := awaitCompletion("s12", p[0], p[1]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := awaitCompletion("s23", p[1], p[2]); err != nil {
|
|
|
|
return err
|
2014-12-17 11:31:03 +00:00
|
|
|
}
|
|
|
|
|
2015-04-01 08:43:19 +00:00
|
|
|
// This is necessary, or all files won't be in place even when everything
|
|
|
|
// is already reported in sync. Why?!
|
|
|
|
time.Sleep(5 * time.Second)
|
|
|
|
|
2014-12-17 11:31:03 +00:00
|
|
|
log.Println("Checking...")
|
|
|
|
|
|
|
|
for _, dir := range []string{"s1", "s2", "s3"} {
|
2014-12-30 14:13:58 +00:00
|
|
|
actual, err := directoryContents(dir)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-12-17 11:31:03 +00:00
|
|
|
if err := compareDirectoryContents(actual, expected[0]); err != nil {
|
|
|
|
return fmt.Errorf("%s: %v", dir, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, dir := range []string{"s12-1", "s12-2"} {
|
2014-12-30 14:13:58 +00:00
|
|
|
actual, err := directoryContents(dir)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-12-17 11:31:03 +00:00
|
|
|
if err := compareDirectoryContents(actual, expected[1]); err != nil {
|
|
|
|
return fmt.Errorf("%s: %v", dir, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, dir := range []string{"s23-2", "s23-3"} {
|
2014-12-30 14:13:58 +00:00
|
|
|
actual, err := directoryContents(dir)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-12-17 11:31:03 +00:00
|
|
|
if err := compareDirectoryContents(actual, expected[2]); err != nil {
|
|
|
|
return fmt.Errorf("%s: %v", dir, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|