Merge branch '21356-clean-imports'
[arvados.git] / sdk / python / tests / test_arv_copy.py
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: Apache-2.0
4
5 import os
6 import sys
7 import tempfile
8 import unittest
9 import shutil
10 import arvados.api
11 from arvados.collection import Collection, CollectionReader
12
13 import arvados.commands.arv_copy as arv_copy
14 from . import arvados_testutil as tutil
15 from . import run_test_server
16
17 class ArvCopyVersionTestCase(run_test_server.TestCaseWithServers, tutil.VersionChecker):
18     MAIN_SERVER = {}
19     KEEP_SERVER = {}
20
21     def run_copy(self, args):
22         sys.argv = ['arv-copy'] + args
23         return arv_copy.main()
24
25     def test_unsupported_arg(self):
26         with self.assertRaises(SystemExit):
27             self.run_copy(['-x=unknown'])
28
29     def test_version_argument(self):
30         with tutil.redirected_streams(
31                 stdout=tutil.StringIO, stderr=tutil.StringIO) as (out, err):
32             with self.assertRaises(SystemExit):
33                 self.run_copy(['--version'])
34         self.assertVersionOutput(out, err)
35
36     def test_copy_project(self):
37         api = arvados.api()
38         src_proj = api.groups().create(body={"group": {"name": "arv-copy project", "group_class": "project"}}).execute()["uuid"]
39
40         c = Collection()
41         with c.open('foo', 'wt') as f:
42             f.write('foo')
43         c.save_new("arv-copy foo collection", owner_uuid=src_proj)
44         coll_record = api.collections().get(uuid=c.manifest_locator()).execute()
45         assert coll_record['storage_classes_desired'] == ['default']
46
47         dest_proj = api.groups().create(body={"group": {"name": "arv-copy dest project", "group_class": "project"}}).execute()["uuid"]
48
49         tmphome = tempfile.mkdtemp()
50         home_was = os.environ['HOME']
51         os.environ['HOME'] = tmphome
52         try:
53             cfgdir = os.path.join(tmphome, ".config", "arvados")
54             os.makedirs(cfgdir)
55             with open(os.path.join(cfgdir, "zzzzz.conf"), "wt") as f:
56                 f.write("ARVADOS_API_HOST=%s\n" % os.environ["ARVADOS_API_HOST"])
57                 f.write("ARVADOS_API_TOKEN=%s\n" % os.environ["ARVADOS_API_TOKEN"])
58                 f.write("ARVADOS_API_HOST_INSECURE=1\n")
59
60             contents = api.groups().list(filters=[["owner_uuid", "=", dest_proj]]).execute()
61             assert len(contents["items"]) == 0
62
63             with tutil.redirected_streams(
64                     stdout=tutil.StringIO, stderr=tutil.StringIO) as (out, err):
65                 try:
66                     self.run_copy(["--project-uuid", dest_proj, "--storage-classes", "foo", src_proj])
67                 except SystemExit as e:
68                     assert e.code == 0
69                 copy_uuid_from_stdout = out.getvalue().strip()
70
71             contents = api.groups().list(filters=[["owner_uuid", "=", dest_proj]]).execute()
72             assert len(contents["items"]) == 1
73
74             assert contents["items"][0]["name"] == "arv-copy project"
75             copied_project = contents["items"][0]["uuid"]
76
77             assert copied_project == copy_uuid_from_stdout
78
79             contents = api.collections().list(filters=[["owner_uuid", "=", copied_project]]).execute()
80             assert len(contents["items"]) == 1
81
82             assert contents["items"][0]["uuid"] != c.manifest_locator()
83             assert contents["items"][0]["name"] == "arv-copy foo collection"
84             assert contents["items"][0]["portable_data_hash"] == c.portable_data_hash()
85             assert contents["items"][0]["storage_classes_desired"] == ["foo"]
86
87         finally:
88             os.environ['HOME'] = home_was
89             shutil.rmtree(tmphome)