Arvados-DCO-1.1-Signed-off-by: Radhika Chippada <radhika@curoverse.com>
[arvados.git] / sdk / python / tests / test_arv_get.py
index 8be528fe6ebbb8f36579c2d6d6866895c55a4195..2af649c65899e8a5ae7c7eb7eaa0f5a07ff285af 100644 (file)
@@ -1,19 +1,24 @@
-#!/usr/bin/env python
-# -*- coding: utf-8 -*-
+# Copyright (C) The Arvados Authors. All rights reserved.
+#
+# SPDX-License-Identifier: Apache-2.0
 
+from __future__ import absolute_import
+from future.utils import listitems
 import io
 import os
+import re
 import shutil
 import tempfile
 
 import arvados
 import arvados.collection as collection
 import arvados.commands.get as arv_get
-import run_test_server
+from . import run_test_server
 
-from arvados_testutil import redirected_streams
+from . import arvados_testutil as tutil
 
-class ArvadosGetTestCase(run_test_server.TestCaseWithServers):
+class ArvadosGetTestCase(run_test_server.TestCaseWithServers,
+                         tutil.VersionChecker):
     MAIN_SERVER = {}
     KEEP_SERVER = {}
 
@@ -34,37 +39,36 @@ class ArvadosGetTestCase(run_test_server.TestCaseWithServers):
                                   'subdir/baz.txt' : 'baz',
                               }):
         c = collection.Collection()
-        for path, data in contents.items():
-            with c.open(path, 'w') as f:
+        for path, data in listitems(contents):
+            with c.open(path, 'wb') as f:
                 f.write(data)
         c.save_new()
+
         return (c.manifest_locator(),
                 c.portable_data_hash(),
                 c.manifest_text(strip=strip_manifest))
-    
+
     def run_get(self, args):
-        self.stdout = io.BytesIO()
-        self.stderr = io.BytesIO()
+        self.stdout = tutil.BytesIO()
+        self.stderr = tutil.StringIO()
         return arv_get.main(args, self.stdout, self.stderr)
 
     def test_version_argument(self):
-        err = io.BytesIO()
-        out = io.BytesIO()
-        with redirected_streams(stdout=out, stderr=err):
+        with tutil.redirected_streams(
+                stdout=tutil.StringIO, stderr=tutil.StringIO) as (out, err):
             with self.assertRaises(SystemExit):
                 self.run_get(['--version'])
-        self.assertEqual(out.getvalue(), '')
-        self.assertRegexpMatches(err.getvalue(), "[0-9]+\.[0-9]+\.[0-9]+")
+        self.assertVersionOutput(out, err)
 
     def test_get_single_file(self):
         # Get the file using the collection's locator
         r = self.run_get(["{}/subdir/baz.txt".format(self.col_loc), '-'])
         self.assertEqual(0, r)
-        self.assertEqual('baz', self.stdout.getvalue())
+        self.assertEqual(b'baz', self.stdout.getvalue())
         # Then, try by PDH
         r = self.run_get(["{}/subdir/baz.txt".format(self.col_pdh), '-'])
         self.assertEqual(0, r)
-        self.assertEqual('baz', self.stdout.getvalue())        
+        self.assertEqual(b'baz', self.stdout.getvalue())
 
     def test_get_multiple_files(self):
         # Download the entire collection to the temp directory
@@ -78,20 +82,21 @@ class ArvadosGetTestCase(run_test_server.TestCaseWithServers):
             self.assertEqual("baz", f.read())
 
     def test_get_collection_unstripped_manifest(self):
+        dummy_token = "+Axxxxxxx"
         # Get the collection manifest by UUID
         r = self.run_get([self.col_loc, self.tempdir])
         self.assertEqual(0, r)
-        m_from_collection = collection.Collection(self.col_manifest).manifest_text(strip=True)
+        m_from_collection = re.sub(r"\+A[0-9a-f@]+", dummy_token, self.col_manifest)
         with open(os.path.join(self.tempdir, self.col_loc), "r") as f:
-            # Strip manifest before comparison to avoid races
-            m_from_file = collection.Collection(f.read()).manifest_text(strip=True)
+            # Replace manifest tokens before comparison to avoid races
+            m_from_file = re.sub(r"\+A[0-9a-f@]+", dummy_token, f.read())
             self.assertEqual(m_from_collection, m_from_file)
         # Get the collection manifest by PDH
         r = self.run_get([self.col_pdh, self.tempdir])
         self.assertEqual(0, r)
         with open(os.path.join(self.tempdir, self.col_pdh), "r") as f:
-            # Strip manifest before comparison to avoid races
-            m_from_file = collection.Collection(f.read()).manifest_text(strip=True)
+            # Replace manifest tokens before comparison to avoid races
+            m_from_file = re.sub(r"\+A[0-9a-f@]+", dummy_token, f.read())
             self.assertEqual(m_from_collection, m_from_file)
 
     def test_get_collection_stripped_manifest(self):