123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442 |
- // 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,
- // You can obtain one at https://mozilla.org/MPL/2.0/.
- //go:build integration
- // +build integration
- package integration
- import (
- "io"
- "log"
- "os"
- "strings"
- "testing"
- "time"
- "github.com/syncthing/syncthing/lib/config"
- "github.com/syncthing/syncthing/lib/events"
- "github.com/syncthing/syncthing/lib/protocol"
- "github.com/syncthing/syncthing/lib/rc"
- )
- func TestOverride(t *testing.T) {
- // Enable "send-only" on s1/default
- id, _ := protocol.DeviceIDFromString(id1)
- cfg, _, _ := config.Load("h1/config.xml", id, events.NoopLogger)
- fld := cfg.Folders()["default"]
- fld.Type = config.FolderTypeSendOnly
- cfg.SetFolder(fld)
- os.Rename("h1/config.xml", "h1/config.xml.orig")
- defer os.Rename("h1/config.xml.orig", "h1/config.xml")
- 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)
- }
- sendOnly := startInstance(t, 1)
- defer checkedStop(t, sendOnly)
- sendRecv := startInstance(t, 2)
- defer checkedStop(t, sendRecv)
- sendOnly.ResumeAll()
- sendRecv.ResumeAll()
- log.Println("Syncing...")
- rc.AwaitSync("default", sendOnly, sendRecv)
- 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 sendRecv 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)
- }
- if err := sendRecv.Rescan("default"); err != nil {
- t.Fatal(err)
- }
- log.Println("Waiting for index to send...")
- time.Sleep(10 * time.Second)
- log.Println("Hitting Override on sendOnly...")
- if _, err := sendOnly.Post("/rest/db/override?folder=default", nil); err != nil {
- t.Fatal(err)
- }
- log.Println("Syncing...")
- rc.AwaitSync("default", sendOnly, sendRecv)
- // Verify that the override worked
- fd, err = os.Open("s1/testfile.txt")
- if err != nil {
- t.Fatal(err)
- }
- bs, err := io.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 sendOnly")
- }
- fd, err = os.Open("s2/testfile.txt")
- if err != nil {
- t.Fatal(err)
- }
- bs, err = io.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 sendRecv")
- }
- }
- /* This doesn't currently work with detection completion, as we don't actually
- get to completion when in sendOnly/sendRecv mode. Needs fixing.
- func TestOverrideIgnores(t *testing.T) {
- // Enable "sendOnly" on s1/default
- id, _ := protocol.DeviceIDFromString(id1)
- cfg, _, _ := config.Load("h1/config.xml", id, events.NoopLogger)
- fld := cfg.Folders()["default"]
- fld.ReadOnly = true
- cfg.SetFolder(fld)
- os.Rename("h1/config.xml", "h1/config.xml.orig")
- defer os.Rename("h1/config.xml.orig", "h1/config.xml")
- 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 sendOnly...")
- sendOnly := syncthingProcess{ // id1
- instance: "1",
- argv: []string{"--home", "h1"},
- port: 8081,
- apiKey: apiKey,
- }
- err = sendOnly.start()
- if err != nil {
- t.Fatal(err)
- }
- defer sendOnly.stop()
- log.Println("Starting sendRecv...")
- sendRecv := syncthingProcess{ // id2
- instance: "2",
- argv: []string{"--home", "h2"},
- port: 8082,
- apiKey: apiKey,
- }
- err = sendRecv.start()
- if err != nil {
- sendOnly.stop()
- t.Fatal(err)
- }
- defer sendRecv.stop()
- log.Println("Syncing...")
- err = awaitCompletion("default", sendOnly, sendRecv)
- if err != nil {
- 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 sendOnly...")
- 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 sendOnly...")
- fd, err = os.Create("s1/testfile.txt")
- if err != nil {
- t.Fatal(err)
- }
- _, err = fd.WriteString("updated on sendOnly 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 = sendOnly.rescan("default")
- log.Println("Waiting for sync...")
- time.Sleep(10 * time.Second)
- // Verify that sync worked
- fd, err = os.Open("s2/testfile.txt")
- if err != nil {
- t.Fatal(err)
- }
- bs, err := io.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 sendRecv")
- }
- fd, err = os.Open("s2/testfile2.txt")
- if err != nil {
- t.Fatal(err)
- }
- bs, err = io.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 sendRecv")
- }
- log.Println("Removing file on sendRecv side...")
- os.Remove("s2/testfile.txt")
- err = sendRecv.rescan("default")
- if err != nil {
- t.Fatal(err)
- }
- log.Println("Waiting for sync...")
- time.Sleep(10 * time.Second)
- // Verify that nothing changed
- fd, err = os.Open("s1/testfile.txt")
- if err != nil {
- t.Fatal(err)
- }
- bs, err = io.ReadAll(fd)
- if err != nil {
- t.Fatal(err)
- }
- fd.Close()
- if !strings.Contains(string(bs), "updated on sendOnly but ignored") {
- t.Error("Changes should not have been synced to sendOnly")
- }
- fd, err = os.Open("s2/testfile.txt")
- if err == nil {
- t.Error("File should not exist on the sendRecv")
- }
- log.Println("Creating file on sendRecv...")
- fd, err = os.Create("s2/testfile3.txt")
- if err != nil {
- t.Fatal(err)
- }
- _, err = fd.WriteString("created on sendRecv, 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 sendOnly...")
- resp, err := sendOnly.post("/rest/db/override?folder=default", nil)
- if err != nil {
- t.Fatal(err)
- }
- if resp.StatusCode != 200 {
- t.Fatal(resp.Status)
- }
- log.Println("Waiting for sync...")
- time.Sleep(10 * time.Second)
- fd, err = os.Open("s2/testfile.txt")
- if err == nil {
- t.Error("File should not exist on the sendRecv")
- }
- fd, err = os.Open("s2/testfile2.txt")
- if err != nil {
- t.Fatal(err)
- }
- bs, err = io.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 sendRecv")
- }
- fd, err = os.Open("s2/testfile3.txt")
- if err != nil {
- t.Error("File should still exist on the sendRecv")
- }
- fd.Close()
- log.Println("Hitting Override on sendOnly (again)...")
- resp, err = sendOnly.post("/rest/db/override?folder=default", nil)
- if err != nil {
- t.Fatal(err)
- }
- if resp.StatusCode != 200 {
- t.Fatal(resp.Status)
- }
- log.Println("Waiting for sync...")
- time.Sleep(10 * time.Second)
- fd, err = os.Open("s2/testfile.txt")
- if err == nil {
- t.Error("File should not exist on the sendRecv")
- }
- fd, err = os.Open("s2/testfile2.txt")
- if err != nil {
- t.Fatal(err)
- }
- bs, err = io.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 sendRecv")
- }
- fd, err = os.Open("s2/testfile3.txt")
- if err != nil {
- t.Error("File should still exist on the sendRecv")
- }
- fd.Close()
- }
- */
|