dc760f78ad77cd1b4856c6c01ccb1bbd0ae7b188
[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, 0)
43         //c.Check(stdout.String(), check.Equals, "")
44         c.Check(stdout.String(), check.Matches, "(?ms).*Collections:[[:space:]]+1.*")
45         c.Check(stdout.String(), check.Matches, "(?ms).*Nominal size of stored data:[[:space:]]+3 bytes \\(3 B\\).*")
46         c.Check(stdout.String(), check.Matches, "(?ms).*Actual size of stored data:[[:space:]]+3 bytes \\(3 B\\).*")
47         c.Check(stdout.String(), check.Matches, "(?ms).*Saved by Keep deduplication:[[:space:]]+0 bytes \\(0 B\\).*")
48         c.Log(stderr.String())
49 }
50
51 func (*Suite) TestTwoUUIDsInvalidPDH(c *check.C) {
52         var stdout, stderr bytes.Buffer
53         // Run dedupreport with pdh,uuid where pdh does not match
54         exitcode := Command.RunCommand("deduplicationreport.test", []string{arvadostest.FooAndBarFilesInDirPDH + "," + arvadostest.FooCollection, arvadostest.FooCollection}, &bytes.Buffer{}, &stdout, &stderr)
55         c.Check(exitcode, check.Equals, 1)
56         c.Check(stdout.String(), check.Equals, "")
57         c.Log(stderr.String())
58         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.*`)
59 }
60
61 func (*Suite) TestNonExistentCollection(c *check.C) {
62         var stdout, stderr bytes.Buffer
63         // Run dedupreport with many UUIDs
64         exitcode := Command.RunCommand("deduplicationreport.test", []string{arvadostest.FooCollection, arvadostest.NonexistentCollection}, &bytes.Buffer{}, &stdout, &stderr)
65         c.Check(exitcode, check.Equals, 1)
66         c.Check(stdout.String(), check.Equals, "Collection zzzzz-4zz18-fy296fx3hot09f7: pdh 1f4b0bc7583c2a7f9102c395f4ffc5e3+45; nominal size 3 (3 B)\n")
67         c.Log(stderr.String())
68         c.Check(stderr.String(), check.Matches, `(?ms).*Error: unable to retrieve collection:.*404 Not Found.*`)
69 }
70
71 func (*Suite) TestManyUUIDsNoOverlap(c *check.C) {
72         var stdout, stderr bytes.Buffer
73         // Run dedupreport with 5 UUIDs
74         exitcode := Command.RunCommand("deduplicationreport.test", []string{arvadostest.FooCollection, arvadostest.HelloWorldCollection, arvadostest.FooBarDirCollection, arvadostest.WazVersion1Collection, arvadostest.UserAgreementCollection}, &bytes.Buffer{}, &stdout, &stderr)
75         c.Check(exitcode, check.Equals, 0)
76         c.Check(stdout.String(), check.Matches, "(?ms).*Collections:[[:space:]]+5.*")
77         c.Check(stdout.String(), check.Matches, "(?ms).*Nominal size of stored data:[[:space:]]+249049 bytes \\(243 KiB\\).*")
78         c.Check(stdout.String(), check.Matches, "(?ms).*Actual size of stored data:[[:space:]]+249049 bytes \\(243 KiB\\).*")
79         c.Check(stdout.String(), check.Matches, "(?ms).*Saved by Keep deduplication:[[:space:]]+0 bytes \\(0 B\\).*")
80         c.Log(stderr.String())
81         c.Check(stderr.String(), check.Equals, "")
82 }
83
84 func (*Suite) TestTwoOverlappingCollections(c *check.C) {
85         var stdout, stderr bytes.Buffer
86         // Create two collections
87         arv := arvados.NewClientFromEnv()
88
89         var c1 arvados.Collection
90         err := arv.RequestAndDecode(&c1, "POST", "arvados/v1/collections", nil, map[string]interface{}{"collection": map[string]interface{}{"manifest_text": ". d3b07384d113edec49eaa6238ad5ff00+4 0:4:foo\n"}})
91         c.Assert(err, check.Equals, nil)
92
93         var c2 arvados.Collection
94         err = arv.RequestAndDecode(&c2, "POST", "arvados/v1/collections", nil, map[string]interface{}{"collection": map[string]interface{}{"manifest_text": ". c157a79031e1c40f85931829bc5fc552+4 d3b07384d113edec49eaa6238ad5ff00+4 0:4:bar 4:4:foo\n"}})
95         c.Assert(err, check.Equals, nil)
96
97         for _, trial := range []struct {
98                 field1 string
99                 field2 string
100         }{
101                 {
102                         // Run dedupreport with 2 arguments: uuid uuid
103                         field1: c1.UUID,
104                         field2: c2.UUID,
105                 },
106                 {
107                         // Run dedupreport with 2 arguments: pdh,uuid uuid
108                         field1: c1.PortableDataHash + "," + c1.UUID,
109                         field2: c2.UUID,
110                 },
111         } {
112                 exitcode := Command.RunCommand("deduplicationreport.test", []string{trial.field1, trial.field2}, &bytes.Buffer{}, &stdout, &stderr)
113                 c.Check(exitcode, check.Equals, 0)
114                 c.Check(stdout.String(), check.Matches, "(?ms).*Nominal size of stored data:[[:space:]]+12 bytes \\(12 B\\).*")
115                 c.Check(stdout.String(), check.Matches, "(?ms).*Actual size of stored data:[[:space:]]+8 bytes \\(8 B\\).*")
116                 c.Check(stdout.String(), check.Matches, "(?ms).*Saved by Keep deduplication:[[:space:]]+4 bytes \\(4 B\\).*")
117                 c.Log(stderr.String())
118                 c.Check(stderr.String(), check.Equals, "")
119         }
120 }