16573: Merge branch 'master' into 16573-keep-deduplication-reporting-tool
[arvados.git] / lib / deduplicationreport / report_test.go
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 package deduplicationreport
6
7 import (
8         "bytes"
9         "testing"
10
11         "git.arvados.org/arvados.git/sdk/go/arvados"
12         "git.arvados.org/arvados.git/sdk/go/arvadostest"
13         "gopkg.in/check.v1"
14 )
15
16 func Test(t *testing.T) {
17         check.TestingT(t)
18 }
19
20 var _ = check.Suite(&Suite{})
21
22 type Suite struct{}
23
24 func (s *Suite) TearDownSuite(c *check.C) {
25         // Undo any changes/additions to the database so they don't affect subsequent tests.
26         arvadostest.ResetEnv()
27 }
28
29 func (*Suite) TestUsage(c *check.C) {
30         var stdout, stderr bytes.Buffer
31         exitcode := Command.RunCommand("deduplicationreport.test", []string{"-log-level=debug"}, &bytes.Buffer{}, &stdout, &stderr)
32         c.Check(exitcode, check.Equals, 2)
33         c.Check(stdout.String(), check.Equals, "")
34         c.Log(stderr.String())
35         c.Check(stderr.String(), check.Matches, `(?ms).*Usage:.*`)
36 }
37
38 func (*Suite) TestTwoIdenticalUUIDs(c *check.C) {
39         var stdout, stderr bytes.Buffer
40         // Run dedupreport with 2 identical uuids
41         exitcode := Command.RunCommand("deduplicationreport.test", []string{arvadostest.FooCollection, arvadostest.FooCollection}, &bytes.Buffer{}, &stdout, &stderr)
42         c.Check(exitcode, check.Equals, 2)
43         c.Check(stdout.String(), check.Equals, "")
44         c.Log(stderr.String())
45         c.Check(stderr.String(), check.Matches, `(?ms).*Error: at least 2 different collections UUIDs required.*`)
46 }
47
48 func (*Suite) TestTwoUUIDsInvalidPDH(c *check.C) {
49         var stdout, stderr bytes.Buffer
50         // Run dedupreport with pdh,uuid where pdh does not match
51         exitcode := Command.RunCommand("deduplicationreport.test", []string{arvadostest.FooAndBarFilesInDirPDH + "," + arvadostest.FooCollection, arvadostest.FooCollection}, &bytes.Buffer{}, &stdout, &stderr)
52         c.Check(exitcode, check.Equals, 1)
53         c.Check(stdout.String(), check.Equals, "")
54         c.Log(stderr.String())
55         c.Check(stderr.String(), check.Matches, `(?ms).*Error: the collection with UUID zzzzz-4zz18-fy296fx3hot09f7 has PDH 1f4b0bc7583c2a7f9102c395f4ffc5e3\+45, but a different PDH was provided in the arguments: 870369fc72738603c2fad16664e50e2d\+58.*`)
56 }
57
58 func (*Suite) TestNonExistentCollection(c *check.C) {
59         var stdout, stderr bytes.Buffer
60         // Run dedupreport with many UUIDs
61         exitcode := Command.RunCommand("deduplicationreport.test", []string{arvadostest.FooCollection, arvadostest.NonexistentCollection}, &bytes.Buffer{}, &stdout, &stderr)
62         c.Check(exitcode, check.Equals, 1)
63         c.Check(stdout.String(), check.Equals, "Collection zzzzz-4zz18-fy296fx3hot09f7: pdh 1f4b0bc7583c2a7f9102c395f4ffc5e3+45; nominal size 3 (3 B)\n")
64         c.Log(stderr.String())
65         c.Check(stderr.String(), check.Matches, `(?ms).*Error: unable to retrieve collection:.*404 Not Found.*`)
66 }
67
68 func (*Suite) TestManyUUIDsNoOverlap(c *check.C) {
69         var stdout, stderr bytes.Buffer
70         // Run dedupreport with 5 UUIDs
71         exitcode := Command.RunCommand("deduplicationreport.test", []string{arvadostest.FooCollection, arvadostest.HelloWorldCollection, arvadostest.FooBarDirCollection, arvadostest.WazVersion1Collection, arvadostest.UserAgreementCollection}, &bytes.Buffer{}, &stdout, &stderr)
72         c.Check(exitcode, check.Equals, 0)
73         c.Check(stdout.String(), check.Matches, "(?ms).*Nominal size of stored data:[[:space:]]+249049 bytes \\(243 KiB\\).*")
74         c.Check(stdout.String(), check.Matches, "(?ms).*Actual size of stored data:[[:space:]]+249049 bytes \\(243 KiB\\).*")
75         c.Check(stdout.String(), check.Matches, "(?ms).*Saved by Keep deduplication:[[:space:]]+0 bytes \\(0 B\\).*")
76         c.Log(stderr.String())
77         c.Check(stderr.String(), check.Equals, "")
78 }
79
80 func (*Suite) TestTwoOverlappingCollections(c *check.C) {
81         var stdout, stderr bytes.Buffer
82         // Create two collections
83         arv := arvados.NewClientFromEnv()
84
85         var c1 arvados.Collection
86         err := arv.RequestAndDecode(&c1, "POST", "arvados/v1/collections", nil, map[string]interface{}{"collection": map[string]interface{}{"manifest_text": ". d3b07384d113edec49eaa6238ad5ff00+4+A2705511e0c47c92cc73e9ddc95b9822ef774c406@5f0de808 0:4:foo\n"}})
87         c.Assert(err, check.Equals, nil)
88
89         var c2 arvados.Collection
90         err = arv.RequestAndDecode(&c2, "POST", "arvados/v1/collections", nil, map[string]interface{}{"collection": map[string]interface{}{"manifest_text": ". c157a79031e1c40f85931829bc5fc552+4+A1544eb0cee937934dc565d2b11836c804384c139@5f0e0bf9 d3b07384d113edec49eaa6238ad5ff00+4+A60746cad7ecc16fe26a0c17c55af90db675369c2@5f0e0bf9 0:4:bar 4:4:foo\n"}})
91         c.Assert(err, check.Equals, nil)
92
93         // Run dedupreport with 2 arguments: uuid uuid
94         exitcode := Command.RunCommand("deduplicationreport.test", []string{c1.UUID, c2.UUID}, &bytes.Buffer{}, &stdout, &stderr)
95         c.Check(exitcode, check.Equals, 0)
96         c.Check(stdout.String(), check.Matches, "(?ms).*Nominal size of stored data:[[:space:]]+12 bytes \\(12 B\\).*")
97         c.Check(stdout.String(), check.Matches, "(?ms).*Actual size of stored data:[[:space:]]+8 bytes \\(8 B\\).*")
98         c.Check(stdout.String(), check.Matches, "(?ms).*Saved by Keep deduplication:[[:space:]]+4 bytes \\(4 B\\).*")
99         c.Log(stderr.String())
100         c.Check(stderr.String(), check.Equals, "")
101 }
102
103 func (*Suite) TestTwoOverlappingCollectionsWithPDH(c *check.C) {
104         var stdout, stderr bytes.Buffer
105         // Create two collections
106         arv := arvados.NewClientFromEnv()
107
108         var c1 arvados.Collection
109         err := arv.RequestAndDecode(&c1, "POST", "arvados/v1/collections", nil, map[string]interface{}{"collection": map[string]interface{}{"manifest_text": ". d3b07384d113edec49eaa6238ad5ff00+4+A2705511e0c47c92cc73e9ddc95b9822ef774c406@5f0de808 0:4:foo\n"}})
110         c.Assert(err, check.Equals, nil)
111
112         var c2 arvados.Collection
113         err = arv.RequestAndDecode(&c2, "POST", "arvados/v1/collections", nil, map[string]interface{}{"collection": map[string]interface{}{"manifest_text": ". c157a79031e1c40f85931829bc5fc552+4+A1544eb0cee937934dc565d2b11836c804384c139@5f0e0bf9 d3b07384d113edec49eaa6238ad5ff00+4+A60746cad7ecc16fe26a0c17c55af90db675369c2@5f0e0bf9 0:4:bar 4:4:foo\n"}})
114         c.Assert(err, check.Equals, nil)
115
116         // Run dedupreport with 2 arguments: pdh,uuid uuid
117         exitcode := Command.RunCommand("deduplicationreport.test", []string{c1.PortableDataHash + "," + c1.UUID, c2.UUID}, &bytes.Buffer{}, &stdout, &stderr)
118         c.Check(exitcode, check.Equals, 0)
119         c.Check(stdout.String(), check.Matches, "(?ms).*Nominal size of stored data:[[:space:]]+12 bytes \\(12 B\\).*")
120         c.Check(stdout.String(), check.Matches, "(?ms).*Actual size of stored data:[[:space:]]+8 bytes \\(8 B\\).*")
121         c.Check(stdout.String(), check.Matches, "(?ms).*Saved by Keep deduplication:[[:space:]]+4 bytes \\(4 B\\).*")
122         c.Log(stderr.String())
123         c.Check(stderr.String(), check.Equals, "")
124 }