[apycotlib] fix archive upload of narvalbot
authorPaul Tonelli <paul.tonelli@logilab.fr>
Fri, 25 Apr 2014 11:07:13 +0200
changeset 1486 882e316ee83b
parent 1485 b772de6bc982
child 1487 c9c9ccc9fd07
[apycotlib] fix archive upload of narvalbot - use texec eid in archive name (previously, the URL containing unauthorized characters was used) - put relative path in archive instead of absolute path - don't go through useless StringIO when we can just pass the file object to requests
_apycotlib/writer.py
test/unittest_apycot.py
--- a/_apycotlib/writer.py	Mon Apr 07 18:59:37 2014 +0200
+++ b/_apycotlib/writer.py	Fri Apr 25 11:07:13 2014 +0200
@@ -16,7 +16,6 @@
 from datetime import datetime
 from StringIO import StringIO
 from threading import RLock
-
 from logilab.mtconverter import xml_escape
 
 REVERSE_SEVERITIES = {
@@ -30,6 +29,7 @@
 ARCHIVE_EXT = '.tar.bz2'
 ARCHIVE_MODE = 'w:bz2'
 ARCHIVE_NAME = "apycot-archive-%(instance-id)s-%(exec-id)s"+ARCHIVE_EXT
+ARCHIVE_MIME_TYPE = u'application/x-tar'
 
 def make_archive_name(cwinstid, execution_id):
     # replace ':' as tar use them to fetch archive over network
@@ -206,23 +206,26 @@
         self.set_exec_status(u'set up')
 
     def end(self, status, archivedir=None):
-        """mark the current test as closed (with status <status>) and archive if requested."""
+        """mark the current test as closed (with status <status>) and upload
+        archive if requested."""
+
         self.refresh_log()
         self.cnxh.http_post(self._url, vid='set_attributes',
                             status = self._unicode(status))
-        if False and archivedir: # XXX this should be refactored! (mostly) useless as is
-            archive = make_archive_name(self.cnxh.cwinstid, self._url)
+        if archivedir:
+            archive = make_archive_name(self.cnxh.instance_id, self.target_eid())
             archivefpath = os.path.join(tempfile.gettempdir(), archive)
             tarball = tarfile.open(archivefpath, ARCHIVE_MODE)
             try:
-                tarball.add(archivedir)
+                tarball.add(archivedir, arcname=os.path.basename(archivedir))
                 tarball.close()
-                files = {'data': (archive, StringIO(open(archivefpath,
-                                 'rb').read()))}
-                self.cnxh.http_post(self.instance_url() + 'file-upload',
-                                     files=files,
-                                     rname='execution_archive',
-                                     eid=self.target_eid())
+                files = {'data': ('dummy', open(archivefpath, 'rb'))}
+                data = self.cnxh.http_post(self._url, vid='create_subentity',
+                                    __cwetype__='File',
+                                    __cwrel__='reverse_execution_archive',
+                                    data_name=archive,
+                                    data_format=ARCHIVE_MIME_TYPE)[0]
+                self.cnxh.http_post(url=self.instance_url() + '/narval-file-append', files=files, eid=data['eid'])
             except:
                 self.error('while archiving execution directory', tb=True)
             finally:
--- a/test/unittest_apycot.py	Mon Apr 07 18:59:37 2014 +0200
+++ b/test/unittest_apycot.py	Fri Apr 25 11:07:13 2014 +0200
@@ -2,6 +2,7 @@
 
 from cubes.apycot.testutils import ApycotBaseTC
 
+from apycotlib import FAILURE, SUCCESS 
 from apycotlib.writer import TestDataWriter
 from narvalbot import HTTPConnectionHandler
 
@@ -38,14 +39,14 @@
             cwriter.debug('hip', path='/tmp/something', line=12)
             cwriter.info('hop', path='/tmp/something')
             cwriter.warning('''momo\n\n<br/>''')
-            cwriter.end(u'success')
+            cwriter.end(SUCCESS)
             cwriter = writer.make_check_writer()
             cwriter.start(MockChecker(u'lintian', {'option': 'value'}))
             cwriter.raw('lintian_version', '1.0')
             cwriter.error('bouh')
             cwriter.fatal('di&d')
-            cwriter.end(u'failure')
-            writer.end(u'failure')
+            cwriter.end(FAILURE)
+            writer.end(FAILURE)
         self.checks = self.execute('Any X, N ORDERBY N WHERE X is CheckResult, X name N')
 
     def test_writer_log_content(self):