1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: Apache-2.0
11 from arvados.collection import Collection, CollectionReader
13 import arvados.commands.arv_copy as arv_copy
14 from . import arvados_testutil as tutil
15 from . import run_test_server
17 class ArvCopyVersionTestCase(run_test_server.TestCaseWithServers, tutil.VersionChecker):
21 def run_copy(self, args):
22 sys.argv = ['arv-copy'] + args
23 return arv_copy.main()
25 def test_unsupported_arg(self):
26 with self.assertRaises(SystemExit):
27 self.run_copy(['-x=unknown'])
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)
36 def test_copy_project(self):
38 src_proj = api.groups().create(body={"group": {"name": "arv-copy project", "group_class": "project"}}).execute()["uuid"]
41 with c.open('foo', 'wt') as f:
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']
47 dest_proj = api.groups().create(body={"group": {"name": "arv-copy dest project", "group_class": "project"}}).execute()["uuid"]
49 tmphome = tempfile.mkdtemp()
50 home_was = os.environ['HOME']
51 os.environ['HOME'] = tmphome
53 cfgdir = os.path.join(tmphome, ".config", "arvados")
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")
60 contents = api.groups().list(filters=[["owner_uuid", "=", dest_proj]]).execute()
61 assert len(contents["items"]) == 0
63 with tutil.redirected_streams(
64 stdout=tutil.StringIO, stderr=tutil.StringIO) as (out, err):
66 self.run_copy(["--project-uuid", dest_proj, "--storage-classes", "foo", src_proj])
67 except SystemExit as e:
69 copy_uuid_from_stdout = out.getvalue().strip()
71 contents = api.groups().list(filters=[["owner_uuid", "=", dest_proj]]).execute()
72 assert len(contents["items"]) == 1
74 assert contents["items"][0]["name"] == "arv-copy project"
75 copied_project = contents["items"][0]["uuid"]
77 assert copied_project == copy_uuid_from_stdout
79 contents = api.collections().list(filters=[["owner_uuid", "=", copied_project]]).execute()
80 assert len(contents["items"]) == 1
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"]
88 os.environ['HOME'] = home_was
89 shutil.rmtree(tmphome)