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