From d012f3ac3ec4aa2730532be095956867d797aefb Mon Sep 17 00:00:00 2001 From: Cédric Ollivier Date: Sat, 2 Nov 2019 12:18:22 +0100 Subject: Publish artifacts to S3 repository MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit It simplifies Jenkins or Gitlab jobs by automatically publishing all artifacts via the framework. It leverages on Amazon Web Services (AWS) SDK [1] which supports the current cases (OPNFV, Xtesting Ansible role [2], etc.). [1] https://boto3.amazonaws.com/v1/documentation/api/latest/index.html?id=docs_gateway [2] https://github.com/collivier/ansible-role-xtesting Change-Id: I66e380c4da29fb0f973472a2c59ae0ea3c44fcfd Signed-off-by: Cédric Ollivier --- xtesting/tests/unit/core/test_testcase.py | 52 +++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) (limited to 'xtesting/tests') diff --git a/xtesting/tests/unit/core/test_testcase.py b/xtesting/tests/unit/core/test_testcase.py index fc612973..eff64d5a 100644 --- a/xtesting/tests/unit/core/test_testcase.py +++ b/xtesting/tests/unit/core/test_testcase.py @@ -17,6 +17,7 @@ import logging import os import unittest +import botocore import mock import requests @@ -61,6 +62,9 @@ class TestCaseTesting(unittest.TestCase): os.environ['DEPLOY_SCENARIO'] = "scenario" os.environ['NODE_NAME'] = "node_name" os.environ['BUILD_TAG'] = "foo-daily-master-bar" + os.environ['S3_ENDPOINT_URL'] = "http://127.0.0.1:9000" + os.environ['S3_DST_URL'] = "s3://xtesting/prefix" + os.environ['HTTP_DST_URL'] = "http://127.0.0.1/prefix" def test_run_fake(self): self.assertEqual(self.test.run(), testcase.TestCase.EX_OK) @@ -311,6 +315,54 @@ class TestCaseTesting(unittest.TestCase): def test_clean(self): self.assertEqual(self.test.clean(), None) + def _test_publish_artifacts_nokw(self, key): + del os.environ[key] + self.assertEqual(self.test.publish_artifacts(), + testcase.TestCase.EX_PUBLISH_ARTIFACTS_ERROR) + + def test_publish_artifacts_exc1(self): + for key in ["S3_ENDPOINT_URL", "S3_DST_URL", "HTTP_DST_URL"]: + self._test_publish_artifacts_nokw(key) + + @mock.patch('boto3.resource', + side_effect=botocore.exceptions.NoCredentialsError) + def test_publish_artifacts_exc2(self, *args): + self.assertEqual(self.test.publish_artifacts(), + testcase.TestCase.EX_PUBLISH_ARTIFACTS_ERROR) + args[0].assert_called_once_with( + 's3', endpoint_url=os.environ['S3_ENDPOINT_URL']) + + @mock.patch('boto3.resource', side_effect=Exception) + def test_publish_artifacts_exc3(self, *args): + self.assertEqual(self.test.publish_artifacts(), + testcase.TestCase.EX_PUBLISH_ARTIFACTS_ERROR) + args[0].assert_called_once_with( + 's3', endpoint_url=os.environ['S3_ENDPOINT_URL']) + + @mock.patch('boto3.resource') + @mock.patch('os.walk', return_value=[]) + def test_publish_artifacts1(self, *args): + self.assertEqual(self.test.publish_artifacts(), + testcase.TestCase.EX_OK) + args[0].assert_called_once_with(self.test.dir_results) + args[1].assert_called_once_with( + 's3', endpoint_url=os.environ['S3_ENDPOINT_URL']) + + @mock.patch('boto3.resource') + @mock.patch('os.walk', + return_value=[ + (testcase.TestCase.dir_results, ('',), ('bar',))]) + def test_publish_artifacts2(self, *args): + self.assertEqual(self.test.publish_artifacts(), + testcase.TestCase.EX_OK) + args[0].assert_called_once_with(self.test.dir_results) + expected = [ + mock.call('s3', endpoint_url=os.environ['S3_ENDPOINT_URL']), + mock.call().Bucket('xtesting'), + mock.call().Bucket().upload_file( + '/var/lib/xtesting/results/bar', 'prefix/bar')] + self.assertEqual(args[1].mock_calls, expected) + if __name__ == "__main__": logging.disable(logging.CRITICAL) -- cgit 1.2.3-korg