summaryrefslogtreecommitdiffstats
path: root/lib/versioner/staggered_test.go
blob: fbf78d4dfe061e269ed0270314149c453167e1f7 (plain)
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
// Copyright (C) 2014 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 versioner

import (
	"io/ioutil"
	"path/filepath"
	"sort"
	"strconv"
	"testing"
	"time"

	"github.com/d4l3k/messagediff"
	"github.com/syncthing/syncthing/lib/config"
	"github.com/syncthing/syncthing/lib/fs"
)

func TestStaggeredVersioningVersionCount(t *testing.T) {
	/* Default settings:

	{30, 3600},       // first hour -> 30 sec between versions
	{3600, 86400},    // next day -> 1 h between versions
	{86400, 592000},  // next 30 days -> 1 day between versions
	{604800, maxAge}, // next year -> 1 week between versions
	*/

	now := parseTime("20160415-140000")
	versionsWithMtime := []string{
		// 14:00:00 is "now"
		"test~20160415-140000", // 0 seconds ago

		"test~20160415-135959", // 1 second ago
		"test~20160415-135931", // 29 seconds ago
		"test~20160415-135930", // 30 seconds ago

		"test~20160415-130059", // 59 minutes 01 seconds ago
		"test~20160415-130030", // 59 minutes 30 seconds ago

		"test~20160415-130000", // 1 hour ago
		"test~20160415-120001", // 1 hour 59:59 ago

		"test~20160414-155959", // 22 hours 1 second ago
		"test~20160414-150001", // 22 hours 59 seconds ago
		"test~20160414-150000", // 23 hours ago

		"test~20160414-140000", // 1 day ago
		"test~20160414-130001", // 1 days 59:59 second ago

		"test~20160409-135959", // 6 days 1 second ago
		"test~20160408-140001", // 6 days 23:59:59 second ago
		"test~20160408-140000", // 7 days ago

		"test~20160408-135959", // 7 days 1 second ago
		"test~20160407-140001", // 7 days 23:59:59 ago
		"test~20160407-140000", // 8 days ago

		"test~20160317-140000", // 29 days ago
		"test~20160317-135959", // 29 days 1 second ago
		"test~20160316-140000", // 30 days ago

		"test~20160308-135959", // 37 days 1 second ago
		"test~20160301-140000", // 44 days ago

		"test~20160223-140000", // 51 days ago

		"test~20150423-140000", // 358 days ago (!!! 2016 was a leap year !!!)

		"test~20150417-140000", // 364 days ago
		"test~20150416-140000", // 365 days ago

		// exceeds maxAge
		"test~20150416-135959", // 365 days 1 second ago
		"test~20150416-135958", // 365 days 2 seconds ago
		"test~20150414-140000", // 367 days ago
	}

	delete := []string{
		"test~20160415-135959", // 1 second ago
		"test~20160415-135931", // 29 seconds ago
		"test~20160415-130059", // 59 minutes 01 seconds ago
		"test~20160415-130000", // 1 hour ago
		"test~20160414-155959", // 22 hours 1 second ago
		"test~20160414-150001", // 22 hours 59 seconds ago
		"test~20160414-140000", // 1 day ago
		"test~20160409-135959", // 6 days 1 second ago
		"test~20160408-140001", // 6 days 23:59:59 second ago
		"test~20160408-135959", // 7 days 1 second ago
		"test~20160407-140001", // 7 days 23:59:59 ago
		"test~20160317-135959", // 29 days 1 second ago
		"test~20160308-135959", // 37 days 1 second ago
		"test~20150417-140000", // 364 days ago
		"test~20150416-135959", // 365 days 1 second ago
		"test~20150416-135958", // 365 days 2 seconds ago
		"test~20150414-140000", // 367 days ago
	}
	sort.Strings(delete)

	cfg := config.FolderConfiguration{
		FilesystemType: fs.FilesystemTypeBasic,
		Path:           "testdata",
		Versioning: config.VersioningConfiguration{
			Params: map[string]string{
				"maxAge": strconv.Itoa(365 * 86400),
			},
		},
	}

	v := newStaggered(cfg).(*staggered)
	rem := v.toRemove(versionsWithMtime, now)
	sort.Strings(rem)

	if diff, equal := messagediff.PrettyDiff(delete, rem); !equal {
		t.Errorf("Incorrect deleted files; got %v, expected %v\n%v", rem, delete, diff)
	}
}

func parseTime(in string) time.Time {
	t, err := time.ParseInLocation(TimeFormat, in, time.Local)
	if err != nil {
		panic(err.Error())
	}
	return t
}

func TestCreateVersionPath(t *testing.T) {
	const (
		versionsDir = "some/nested/dir"
		archiveFile = "testfile"
	)

	// Create a test dir and file
	tmpDir, err := ioutil.TempDir("", "")
	if err != nil {
		t.Fatal(err)
	}
	if err := ioutil.WriteFile(filepath.Join(tmpDir, archiveFile), []byte("sup"), 0644); err != nil {
		t.Fatal(err)
	}

	folderCfg := config.FolderConfiguration{
		ID:   "default",
		Path: tmpDir,
		Versioning: config.VersioningConfiguration{
			Type:   "staggered",
			FSPath: versionsDir,
		},
	}

	// Archive the file
	versioner := newStaggered(folderCfg)
	if err := versioner.Archive(archiveFile); err != nil {
		t.Fatal(err)
	}

	// Look for files named like the test file, in the archive dir.
	files, err := filepath.Glob(filepath.Join(tmpDir, versionsDir, archiveFile) + "*")
	if err != nil {
		t.Fatal(err)
	}
	if len(files) == 0 {
		t.Error("expected file to have been archived")
	}
}