Add ContentType when publishing artifacts 06/68906/1
authorCédric Ollivier <cedric.ollivier@orange.com>
Sat, 16 Nov 2019 15:21:44 +0000 (16:21 +0100)
committerCédric Ollivier <cedric.ollivier@orange.com>
Sat, 16 Nov 2019 15:31:15 +0000 (16:31 +0100)
The default value 'application/octet-stream' is mostly
incorrect and forces downloading all links.

https://build.opnfv.org/ci/job/functest-opnfv-functest-healthcheck-hunter-connection_check-run/436/console

Change-Id: Ia680654f5e9f67154504264bc380f0a8d6a74d85
Signed-off-by: Cédric Ollivier <cedric.ollivier@orange.com>
(cherry picked from commit e2e7dbe185a552b21315063dbbed8ac4f40f309d)

docker/Dockerfile
xtesting/core/testcase.py
xtesting/tests/unit/core/test_testcase.py

index a1c635d..0b13909 100644 (file)
@@ -3,7 +3,7 @@ FROM alpine:3.10
 ARG BRANCH=stable/jerma
 ARG OPENSTACK_TAG=stable/train
 
-RUN apk --no-cache add --update python3 bash git && \
+RUN apk --no-cache add --update python3 bash git mailcap && \
     apk --no-cache add --virtual .build-deps --update \
         python3-dev build-base && \
     git init /src/functest-xtesting && \
index 54baaae..c3b8f61 100644 (file)
@@ -13,6 +13,7 @@ import abc
 from datetime import datetime
 import json
 import logging
+import mimetypes
 import os
 import re
 import sys
@@ -301,21 +302,33 @@ class TestCase():
             self.details["links"] = []
             for log_file in [self.output_log_name, self.output_debug_log_name]:
                 if os.path.exists(os.path.join(self.dir_results, log_file)):
+                    abs_file = os.path.join(self.dir_results, log_file)
+                    mime_type = mimetypes.guess_type(abs_file)
+                    self.__logger.debug(
+                        "Publishing %s %s", abs_file, mime_type)
                     # pylint: disable=no-member
                     b3resource.Bucket(bucket_name).upload_file(
-                        os.path.join(self.dir_results, log_file),
-                        os.path.join(path, log_file))
+                        abs_file,
+                        os.path.join(path, log_file),
+                        ExtraArgs={'ContentType': mime_type[
+                            0] or 'application/octet-stream'})
                     link = os.path.join(dst_http_url, log_file)
                     output_str += "\n{}".format(link)
                     self.details["links"].append(link)
             for root, _, files in os.walk(self.res_dir):
                 for pub_file in files:
+                    abs_file = os.path.join(root, pub_file)
+                    mime_type = mimetypes.guess_type(abs_file)
+                    self.__logger.debug(
+                        "Publishing %s %s", abs_file, mime_type)
                     # pylint: disable=no-member
                     b3resource.Bucket(bucket_name).upload_file(
-                        os.path.join(root, pub_file),
+                        abs_file,
                         os.path.join(path, os.path.relpath(
                             os.path.join(root, pub_file),
-                            start=self.dir_results)))
+                            start=self.dir_results)),
+                        ExtraArgs={'ContentType': mime_type[
+                            0] or 'application/octet-stream'})
                     link = os.path.join(dst_http_url, os.path.relpath(
                         os.path.join(root, pub_file),
                         start=self.dir_results))
index 6d18a96..5d34c34 100644 (file)
@@ -359,6 +359,7 @@ class TestCaseTesting(unittest.TestCase):
         args[0].assert_called_once_with(
             's3', endpoint_url=os.environ['S3_ENDPOINT_URL'])
 
+    @mock.patch('mimetypes.guess_type', return_value=(None, None))
     @mock.patch('boto3.resource')
     @mock.patch('os.walk', return_value=[])
     def test_publish_artifacts1(self, *args):
@@ -368,6 +369,7 @@ class TestCaseTesting(unittest.TestCase):
         args[1].assert_called_once_with(
             's3', endpoint_url=os.environ['S3_ENDPOINT_URL'])
 
+    @mock.patch('mimetypes.guess_type', return_value=(None, None))
     @mock.patch('boto3.resource')
     @mock.patch('os.walk', return_value=[])
     def test_publish_artifacts2(self, *args):
@@ -381,6 +383,7 @@ class TestCaseTesting(unittest.TestCase):
         args[1].assert_called_once_with(
             's3', endpoint_url=os.environ['S3_ENDPOINT_URL'])
 
+    @mock.patch('mimetypes.guess_type', return_value=(None, None))
     @mock.patch('os.path.exists', return_value=True)
     @mock.patch('boto3.resource')
     @mock.patch('os.walk',
@@ -396,16 +399,47 @@ class TestCaseTesting(unittest.TestCase):
             mock.call().Bucket('xtesting'),
             mock.call().Bucket().upload_file(
                 '/var/lib/xtesting/results/xtesting.log',
-                'prefix/xtesting.log'),
+                'prefix/xtesting.log',
+                ExtraArgs={'ContentType': 'application/octet-stream'}),
             mock.call().Bucket('xtesting'),
             mock.call().Bucket().upload_file(
                 '/var/lib/xtesting/results/xtesting.debug.log',
-                'prefix/xtesting.debug.log'),
+                'prefix/xtesting.debug.log',
+                ExtraArgs={'ContentType': 'application/octet-stream'}),
             mock.call().Bucket('xtesting'),
             mock.call().Bucket().upload_file(
-                '/var/lib/xtesting/results/bar', 'prefix/bar')]
+                '/var/lib/xtesting/results/bar', 'prefix/bar',
+                ExtraArgs={'ContentType': 'application/octet-stream'})]
         self.assertEqual(args[1].mock_calls, expected)
 
+    @mock.patch('mimetypes.guess_type', return_value=('text/plain', None))
+    @mock.patch('os.path.exists', return_value=True)
+    @mock.patch('boto3.resource')
+    @mock.patch('os.walk',
+                return_value=[
+                    (testcase.TestCase.dir_results, ('',), ('bar',))])
+    def test_publish_artifacts4(self, *args):
+        self.assertEqual(self.test.publish_artifacts(),
+                         testcase.TestCase.EX_OK)
+        args[0].assert_called_once_with(self.test.res_dir)
+        expected = [
+            mock.call('s3', endpoint_url=os.environ['S3_ENDPOINT_URL']),
+            mock.call().meta.client.head_bucket(Bucket='xtesting'),
+            mock.call().Bucket('xtesting'),
+            mock.call().Bucket().upload_file(
+                '/var/lib/xtesting/results/xtesting.log',
+                'prefix/xtesting.log',
+                ExtraArgs={'ContentType': 'text/plain'}),
+            mock.call().Bucket('xtesting'),
+            mock.call().Bucket().upload_file(
+                '/var/lib/xtesting/results/xtesting.debug.log',
+                'prefix/xtesting.debug.log',
+                ExtraArgs={'ContentType': 'text/plain'}),
+            mock.call().Bucket('xtesting'),
+            mock.call().Bucket().upload_file(
+                '/var/lib/xtesting/results/bar', 'prefix/bar',
+                ExtraArgs={'ContentType': 'text/plain'})]
+        self.assertEqual(args[1].mock_calls, expected)
 
 if __name__ == "__main__":
     logging.disable(logging.CRITICAL)