forked from kopia/kopia
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand_policy_import_test.go
More file actions
190 lines (145 loc) · 6.04 KB
/
Copy pathcommand_policy_import_test.go
File metadata and controls
190 lines (145 loc) · 6.04 KB
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
package cli_test
import (
"encoding/json"
"os"
"path"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"github.com/kopia/kopia/internal/testutil"
"github.com/kopia/kopia/snapshot"
"github.com/kopia/kopia/snapshot/policy"
"github.com/kopia/kopia/tests/testenv"
)
// note: dependent on policy export working.
func TestImportPolicy(t *testing.T) {
e := testenv.NewCLITest(t, testenv.RepoFormatNotImportant, testenv.NewInProcRunner(t))
defer e.RunAndExpectSuccess(t, "repo", "disconnect")
e.RunAndExpectSuccess(t, "repo", "create", "filesystem", "--path", e.RepoDir, "--override-username=user", "--override-hostname=host")
td := testutil.TempDirectory(t)
policyFilePath := path.Join(td, "policy.json")
// poor man's deep copy
defaultPolicyJSON, err := json.Marshal(policy.DefaultPolicy)
if err != nil {
t.Fatalf("unable to marshal policy: %v", err)
}
var defaultPolicy *policy.Policy
testutil.MustParseJSONLines(t, []string{string(defaultPolicyJSON)}, &defaultPolicy)
specifiedPolicies := map[string]*policy.Policy{
"(global)": defaultPolicy,
}
makePolicyFile := func() {
data, err := json.Marshal(specifiedPolicies)
if err != nil {
t.Fatalf("unable to marshal policy: %v", err)
}
err = os.WriteFile(policyFilePath, data, 0o600)
if err != nil {
t.Fatalf("unable to write policy file: %v", err)
}
}
// sanity check that we have the default global policy
assertPoliciesEqual(t, e, specifiedPolicies)
// change the global policy
specifiedPolicies["(global)"].SplitterPolicy.Algorithm = "FIXED-4M"
makePolicyFile()
e.RunAndExpectSuccess(t, "policy", "import", "--from-file", policyFilePath)
assertPoliciesEqual(t, e, specifiedPolicies)
// create a new policy
id := snapshot.SourceInfo{
Host: "host",
UserName: "user",
Path: filepath.ToSlash(td),
}.String()
specifiedPolicies[id] = &policy.Policy{
SplitterPolicy: policy.SplitterPolicy{
Algorithm: "FIXED-8M",
},
}
makePolicyFile()
e.RunAndExpectSuccess(t, "policy", "import", "--from-file", policyFilePath)
assertPoliciesEqual(t, e, specifiedPolicies)
// import from a file specifying changes in both policies but limiting import to only one
specifiedPolicies["(global)"].CompressionPolicy.CompressorName = "zstd"
specifiedPolicies[id].CompressionPolicy.CompressorName = "gzip"
makePolicyFile()
e.RunAndExpectSuccess(t, "policy", "import", "--from-file", policyFilePath, "(global)")
// local policy should not have changed
specifiedPolicies[id].CompressionPolicy.CompressorName = ""
assertPoliciesEqual(t, e, specifiedPolicies)
specifiedPolicies[id].CompressionPolicy.CompressorName = "gzip"
e.RunAndExpectSuccess(t, "policy", "import", "--from-file", policyFilePath, id)
assertPoliciesEqual(t, e, specifiedPolicies)
// deleting values should work
specifiedPolicies[id].CompressionPolicy.CompressorName = ""
makePolicyFile()
e.RunAndExpectSuccess(t, "policy", "import", "--from-file", policyFilePath, id)
assertPoliciesEqual(t, e, specifiedPolicies)
// create a new policy
td2 := testutil.TempDirectory(t)
id2 := snapshot.SourceInfo{
Host: "host",
UserName: "user",
Path: filepath.ToSlash(td2),
}.String()
policy2 := &policy.Policy{
MetadataCompressionPolicy: policy.MetadataCompressionPolicy{
CompressorName: "zstd",
},
}
specifiedPolicies[id2] = policy2
makePolicyFile()
e.RunAndExpectSuccess(t, "policy", "import", "--from-file", policyFilePath, id2)
assertPoliciesEqual(t, e, specifiedPolicies)
// unknown fields should be disallowed by default
err = os.WriteFile(policyFilePath, []byte(`{ "`+id2+`": { "not-a-real-field": 50, "metadataCompression": { "compressorName": "zstd" } } }`), 0o600)
if err != nil {
t.Fatalf("unable to write policy file: %v", err)
}
e.RunAndExpectFailure(t, "policy", "import", "--from-file", policyFilePath, id2)
// unless explicitly allowed
e.RunAndExpectSuccess(t, "policy", "import", "--from-file", policyFilePath, "--allow-unknown-fields", id2)
assertPoliciesEqual(t, e, specifiedPolicies) // no change
// deleteOtherPolicies should work
delete(specifiedPolicies, id2)
makePolicyFile()
e.RunAndExpectSuccess(t, "policy", "import", "--from-file", policyFilePath, "--delete-other-policies")
assertPoliciesEqual(t, e, specifiedPolicies)
// add it back in
specifiedPolicies[id2] = policy2
makePolicyFile()
e.RunAndExpectSuccess(t, "policy", "import", "--from-file", policyFilePath)
assertPoliciesEqual(t, e, specifiedPolicies)
// deleteOtherPolicies should work with specified targets as well
// don't change policy file
e.RunAndExpectSuccess(t, "policy", "import", "--from-file", policyFilePath, "--delete-other-policies", "(global)", id)
delete(specifiedPolicies, id2)
assertPoliciesEqual(t, e, specifiedPolicies)
// --global should be equivalent to (global)
specifiedPolicies[id2] = policy2
makePolicyFile()
e.RunAndExpectSuccess(t, "policy", "import", "--from-file", policyFilePath, "--global")
delete(specifiedPolicies, id2) // should NOT have been imported
assertPoliciesEqual(t, e, specifiedPolicies)
// sanity check against (global)
e.RunAndExpectSuccess(t, "policy", "import", "--from-file", policyFilePath, "(global)")
assertPoliciesEqual(t, e, specifiedPolicies)
// another sanity check
e.RunAndExpectSuccess(t, "policy", "import", "--from-file", policyFilePath)
specifiedPolicies[id2] = policy2
assertPoliciesEqual(t, e, specifiedPolicies)
// reading an invalid file should fail
e.RunAndExpectFailure(t, "policy", "import", "--from-file", "/not/a/real/file")
// invalid targets should fail
err = os.WriteFile(policyFilePath, []byte(`{ "userwithouthost@": { "metadataCompression": { "compressorName": "zstd" } } }`), 0o600)
if err != nil {
t.Fatalf("unable to write policy file: %v", err)
}
e.RunAndExpectFailure(t, "policy", "import", "--from-file", policyFilePath)
}
func assertPoliciesEqual(t *testing.T, e *testenv.CLITest, expected map[string]*policy.Policy) {
t.Helper()
var policies map[string]*policy.Policy
testutil.MustParseJSONLines(t, e.RunAndExpectSuccess(t, "policy", "export"), &policies)
assert.Equal(t, expected, policies, "unexpected policies")
}