1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437
|
// Copyright (C) 2016 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/.
package model
import (
"context"
"os"
"testing"
"time"
"github.com/syncthing/syncthing/lib/config"
"github.com/syncthing/syncthing/lib/db"
"github.com/syncthing/syncthing/lib/db/backend"
"github.com/syncthing/syncthing/lib/events"
"github.com/syncthing/syncthing/lib/fs"
"github.com/syncthing/syncthing/lib/ignore"
"github.com/syncthing/syncthing/lib/protocol"
"github.com/syncthing/syncthing/lib/protocol/mocks"
"github.com/syncthing/syncthing/lib/rand"
)
var (
myID, device1, device2 protocol.DeviceID
defaultCfgWrapper config.Wrapper
defaultCfgWrapperCancel context.CancelFunc
defaultFolderConfig config.FolderConfiguration
defaultCfg config.Configuration
defaultAutoAcceptCfg config.Configuration
device1Conn = &mocks.Connection{}
device2Conn = &mocks.Connection{}
)
func init() {
myID, _ = protocol.DeviceIDFromString("ZNWFSWE-RWRV2BD-45BLMCV-LTDE2UR-4LJDW6J-R5BPWEB-TXD27XJ-IZF5RA4")
device1, _ = protocol.DeviceIDFromString("AIR6LPZ-7K4PTTV-UXQSMUU-CPQ5YWH-OEDFIIQ-JUG777G-2YQXXR5-YD6AWQR")
device2, _ = protocol.DeviceIDFromString("GYRZZQB-IRNPV4Z-T7TC52W-EQYJ3TT-FDQW6MW-DFLMU42-SSSU6EM-FBK2VAY")
device1Conn.DeviceIDReturns(device1)
device1Conn.ConnectionIDReturns(rand.String(16))
device2Conn.DeviceIDReturns(device2)
device2Conn.ConnectionIDReturns(rand.String(16))
cfg := config.New(myID)
cfg.Options.MinHomeDiskFree.Value = 0 // avoids unnecessary free space checks
defaultCfgWrapper, defaultCfgWrapperCancel = newConfigWrapper(cfg)
defaultFolderConfig = newFolderConfig()
waiter, _ := defaultCfgWrapper.Modify(func(cfg *config.Configuration) {
cfg.SetDevice(newDeviceConfiguration(cfg.Defaults.Device, device1, "device1"))
cfg.SetFolder(defaultFolderConfig)
cfg.Options.KeepTemporariesH = 1
})
waiter.Wait()
defaultCfg = defaultCfgWrapper.RawCopy()
defaultAutoAcceptCfg = config.Configuration{
Version: config.CurrentVersion,
Devices: []config.DeviceConfiguration{
{
DeviceID: myID, // self
},
{
DeviceID: device1,
AutoAcceptFolders: true,
},
{
DeviceID: device2,
AutoAcceptFolders: true,
},
},
Defaults: config.Defaults{
Folder: config.FolderConfiguration{
FilesystemType: config.FilesystemTypeFake,
Path: rand.String(32),
},
},
Options: config.OptionsConfiguration{
MinHomeDiskFree: config.Size{}, // avoids unnecessary free space checks
},
}
}
func newConfigWrapper(cfg config.Configuration) (config.Wrapper, context.CancelFunc) {
wrapper := config.Wrap("", cfg, myID, events.NoopLogger)
ctx, cancel := context.WithCancel(context.Background())
go wrapper.Serve(ctx)
return wrapper, cancel
}
func newDefaultCfgWrapper() (config.Wrapper, config.FolderConfiguration, context.CancelFunc) {
w, cancel := newConfigWrapper(defaultCfgWrapper.RawCopy())
fcfg := newFolderConfig()
_, _ = w.Modify(func(cfg *config.Configuration) {
cfg.SetFolder(fcfg)
})
return w, fcfg, cancel
}
func newFolderConfig() config.FolderConfiguration {
cfg := newFolderConfiguration(defaultCfgWrapper, "default", "default", config.FilesystemTypeFake, rand.String(32)+"?content=true")
cfg.FSWatcherEnabled = false
cfg.Devices = append(cfg.Devices, config.FolderDeviceConfiguration{DeviceID: device1})
return cfg
}
func setupModelWithConnection(t testing.TB) (*testModel, *fakeConnection, config.FolderConfiguration, context.CancelFunc) {
t.Helper()
w, fcfg, cancel := newDefaultCfgWrapper()
m, fc := setupModelWithConnectionFromWrapper(t, w)
return m, fc, fcfg, cancel
}
func setupModelWithConnectionFromWrapper(t testing.TB, w config.Wrapper) (*testModel, *fakeConnection) {
t.Helper()
m := setupModel(t, w)
fc := addFakeConn(m, device1, "default")
fc.folder = "default"
_ = m.ScanFolder("default")
return m, fc
}
func setupModel(t testing.TB, w config.Wrapper) *testModel {
t.Helper()
m := newModel(t, w, myID, nil)
m.ServeBackground()
<-m.started
m.ScanFolders()
return m
}
type testModel struct {
*model
t testing.TB
cancel context.CancelFunc
evCancel context.CancelFunc
stopped chan struct{}
}
func newModel(t testing.TB, cfg config.Wrapper, id protocol.DeviceID, protectedFiles []string) *testModel {
t.Helper()
evLogger := events.NewLogger()
ldb, err := db.NewLowlevel(backend.OpenMemory(), evLogger)
if err != nil {
t.Fatal(err)
}
m := NewModel(cfg, id, ldb, protectedFiles, evLogger, protocol.NewKeyGenerator()).(*model)
ctx, cancel := context.WithCancel(context.Background())
go evLogger.Serve(ctx)
return &testModel{
model: m,
evCancel: cancel,
stopped: make(chan struct{}),
t: t,
}
}
func (m *testModel) ServeBackground() {
ctx, cancel := context.WithCancel(context.Background())
m.cancel = cancel
go func() {
m.model.Serve(ctx)
close(m.stopped)
}()
<-m.started
}
func (m *testModel) testAvailability(folder string, file protocol.FileInfo, block protocol.BlockInfo) []Availability {
av, err := m.model.Availability(folder, file, block)
must(m.t, err)
return av
}
func (m *testModel) testCurrentFolderFile(folder string, file string) (protocol.FileInfo, bool) {
f, ok, err := m.model.CurrentFolderFile(folder, file)
must(m.t, err)
return f, ok
}
func (m *testModel) testCompletion(device protocol.DeviceID, folder string) FolderCompletion {
comp, err := m.Completion(device, folder)
must(m.t, err)
return comp
}
func cleanupModel(m *testModel) {
if m.cancel != nil {
m.cancel()
<-m.stopped
}
m.evCancel()
m.db.Close()
os.Remove(m.cfg.ConfigPath())
}
func cleanupModelAndRemoveDir(m *testModel, dir string) {
cleanupModel(m)
os.RemoveAll(dir)
}
type alwaysChangedKey struct {
fs fs.Filesystem
name string
}
// alwaysChanges is an ignore.ChangeDetector that always returns true on Changed()
type alwaysChanged struct {
seen map[alwaysChangedKey]struct{}
}
func newAlwaysChanged() *alwaysChanged {
return &alwaysChanged{
seen: make(map[alwaysChangedKey]struct{}),
}
}
func (c *alwaysChanged) Remember(fs fs.Filesystem, name string, _ time.Time) {
c.seen[alwaysChangedKey{fs, name}] = struct{}{}
}
func (c *alwaysChanged) Reset() {
c.seen = make(map[alwaysChangedKey]struct{})
}
func (c *alwaysChanged) Seen(fs fs.Filesystem, name string) bool {
_, ok := c.seen[alwaysChangedKey{fs, name}]
return ok
}
func (*alwaysChanged) Changed() bool {
return true
}
func localSize(t *testing.T, m Model, folder string) db.Counts {
t.Helper()
snap := dbSnapshot(t, m, folder)
defer snap.Release()
return snap.LocalSize()
}
func globalSize(t *testing.T, m Model, folder string) db.Counts {
t.Helper()
snap := dbSnapshot(t, m, folder)
defer snap.Release()
return snap.GlobalSize()
}
func receiveOnlyChangedSize(t *testing.T, m Model, folder string) db.Counts {
t.Helper()
snap := dbSnapshot(t, m, folder)
defer snap.Release()
return snap.ReceiveOnlyChangedSize()
}
func needSizeLocal(t *testing.T, m Model, folder string) db.Counts {
t.Helper()
snap := dbSnapshot(t, m, folder)
defer snap.Release()
return snap.NeedSize(protocol.LocalDeviceID)
}
func dbSnapshot(t *testing.T, m Model, folder string) *db.Snapshot {
t.Helper()
snap, err := m.DBSnapshot(folder)
if err != nil {
t.Fatal(err)
}
return snap
}
func fsetSnapshot(t *testing.T, fset *db.FileSet) *db.Snapshot {
t.Helper()
snap, err := fset.Snapshot()
if err != nil {
t.Fatal(err)
}
return snap
}
// Reach in and update the ignore matcher to one that always does
// reloads when asked to, instead of checking file mtimes. This is
// because we will be changing the files on disk often enough that the
// mtimes will be unreliable to determine change status.
func folderIgnoresAlwaysReload(t testing.TB, m *testModel, fcfg config.FolderConfiguration) {
t.Helper()
m.removeFolder(fcfg)
fset := newFileSet(t, fcfg.ID, m.db)
ignores := ignore.New(fcfg.Filesystem(nil), ignore.WithCache(true), ignore.WithChangeDetector(newAlwaysChanged()))
m.mut.Lock()
m.addAndStartFolderLockedWithIgnores(fcfg, fset, ignores)
m.mut.Unlock()
}
func basicClusterConfig(local, remote protocol.DeviceID, folders ...string) *protocol.ClusterConfig {
var cc protocol.ClusterConfig
for _, folder := range folders {
cc.Folders = append(cc.Folders, protocol.Folder{
ID: folder,
Devices: []protocol.Device{
{
ID: local,
},
{
ID: remote,
},
},
})
}
return &cc
}
func localIndexUpdate(m *testModel, folder string, fs []protocol.FileInfo) {
m.mut.RLock()
fset := m.folderFiles[folder]
m.mut.RUnlock()
fset.Update(protocol.LocalDeviceID, fs)
seq := fset.Sequence(protocol.LocalDeviceID)
filenames := make([]string, len(fs))
for i, file := range fs {
filenames[i] = file.Name
}
m.evLogger.Log(events.LocalIndexUpdated, map[string]interface{}{
"folder": folder,
"items": len(fs),
"filenames": filenames,
"sequence": seq,
"version": seq, // legacy for sequence
})
}
func newDeviceConfiguration(defaultCfg config.DeviceConfiguration, id protocol.DeviceID, name string) config.DeviceConfiguration {
cfg := defaultCfg.Copy()
cfg.DeviceID = id
cfg.Name = name
return cfg
}
func newFileSet(t testing.TB, folder string, ldb *db.Lowlevel) *db.FileSet {
t.Helper()
fset, err := db.NewFileSet(folder, ldb)
if err != nil {
t.Fatal(err)
}
return fset
}
func replace(t testing.TB, w config.Wrapper, to config.Configuration) {
t.Helper()
waiter, err := w.Modify(func(cfg *config.Configuration) {
*cfg = to
})
if err != nil {
t.Fatal(err)
}
waiter.Wait()
}
func pauseFolder(t testing.TB, w config.Wrapper, id string, paused bool) {
t.Helper()
waiter, err := w.Modify(func(cfg *config.Configuration) {
_, i, _ := cfg.Folder(id)
cfg.Folders[i].Paused = paused
})
if err != nil {
t.Fatal(err)
}
waiter.Wait()
}
func setFolder(t testing.TB, w config.Wrapper, fcfg config.FolderConfiguration) {
t.Helper()
waiter, err := w.Modify(func(cfg *config.Configuration) {
cfg.SetFolder(fcfg)
})
if err != nil {
t.Fatal(err)
}
waiter.Wait()
}
func pauseDevice(t testing.TB, w config.Wrapper, id protocol.DeviceID, paused bool) {
t.Helper()
waiter, err := w.Modify(func(cfg *config.Configuration) {
_, i, _ := cfg.Device(id)
cfg.Devices[i].Paused = paused
})
if err != nil {
t.Fatal(err)
}
waiter.Wait()
}
func setDevice(t testing.TB, w config.Wrapper, device config.DeviceConfiguration) {
t.Helper()
waiter, err := w.Modify(func(cfg *config.Configuration) {
cfg.SetDevice(device)
})
if err != nil {
t.Fatal(err)
}
waiter.Wait()
}
func addDevice2(t testing.TB, w config.Wrapper, fcfg config.FolderConfiguration) {
waiter, err := w.Modify(func(cfg *config.Configuration) {
cfg.SetDevice(newDeviceConfiguration(cfg.Defaults.Device, device2, "device2"))
fcfg.Devices = append(fcfg.Devices, config.FolderDeviceConfiguration{DeviceID: device2})
cfg.SetFolder(fcfg)
})
must(t, err)
waiter.Wait()
}
func writeFile(t testing.TB, filesystem fs.Filesystem, name string, data []byte) {
t.Helper()
fd, err := filesystem.Create(name)
must(t, err)
defer fd.Close()
_, err = fd.Write(data)
must(t, err)
}
func writeFilePerm(t testing.TB, filesystem fs.Filesystem, name string, data []byte, perm fs.FileMode) {
t.Helper()
writeFile(t, filesystem, name, data)
must(t, filesystem.Chmod(name, perm))
}
|