MCPcopy
hub / github.com/kopia/kopia / TestImportPolicy

Function TestImportPolicy

cli/command_policy_import_test.go:19–180  ·  view source on GitHub ↗

note: dependent on policy export working.

(t *testing.T)

Source from the content-addressed store, hash-verified

17
18// note: dependent on policy export working.
19func TestImportPolicy(t *testing.T) {
20 e := testenv.NewCLITest(t, testenv.RepoFormatNotImportant, testenv.NewInProcRunner(t))
21 defer e.RunAndExpectSuccess(t, "repo", "disconnect")
22
23 e.RunAndExpectSuccess(t, "repo", "create", "filesystem", "--path", e.RepoDir, "--override-username=user", "--override-hostname=host")
24
25 td := testutil.TempDirectory(t)
26 policyFilePath := path.Join(td, "policy.json")
27
28 // poor man's deep copy
29 defaultPolicyJSON, err := json.Marshal(policy.DefaultPolicy)
30 if err != nil {
31 t.Fatalf("unable to marshal policy: %v", err)
32 }
33
34 var defaultPolicy *policy.Policy
35
36 testutil.MustParseJSONLines(t, []string{string(defaultPolicyJSON)}, &defaultPolicy)
37
38 specifiedPolicies := map[string]*policy.Policy{
39 "(global)": defaultPolicy,
40 }
41 makePolicyFile := func() {
42 data, err := json.Marshal(specifiedPolicies)
43 if err != nil {
44 t.Fatalf("unable to marshal policy: %v", err)
45 }
46
47 err = os.WriteFile(policyFilePath, data, 0o600)
48 if err != nil {
49 t.Fatalf("unable to write policy file: %v", err)
50 }
51 }
52
53 // sanity check that we have the default global policy
54 assertPoliciesEqual(t, e, specifiedPolicies)
55
56 // change the global policy
57 specifiedPolicies["(global)"].SplitterPolicy.Algorithm = "FIXED-4M"
58
59 makePolicyFile()
60 e.RunAndExpectSuccess(t, "policy", "import", "--from-file", policyFilePath)
61 assertPoliciesEqual(t, e, specifiedPolicies)
62
63 // create a new policy
64 id := snapshot.SourceInfo{
65 Host: "host",
66 UserName: "user",
67 Path: filepath.ToSlash(td),
68 }.String()
69
70 specifiedPolicies[id] = &policy.Policy{
71 SplitterPolicy: policy.SplitterPolicy{
72 Algorithm: "FIXED-8M",
73 },
74 }
75
76 makePolicyFile()

Callers

nothing calls this directly

Calls 10

RunAndExpectSuccessMethod · 0.95
RunAndExpectFailureMethod · 0.95
NewCLITestFunction · 0.92
NewInProcRunnerFunction · 0.92
TempDirectoryFunction · 0.92
MustParseJSONLinesFunction · 0.92
assertPoliciesEqualFunction · 0.85
FatalfMethod · 0.80
WriteFileMethod · 0.65
StringMethod · 0.45

Tested by

no test coverage detected