|
@@ -16,12 +16,40 @@ See the License for the specific language governing permissions and
|
|
|
limitations under the License.
|
|
|
'''
|
|
|
|
|
|
-import os
|
|
|
+import os, sys
|
|
|
import tempfile
|
|
|
from unittest import TestCase
|
|
|
from mock.mock import patch, MagicMock
|
|
|
|
|
|
from resource_management import *
|
|
|
+from resource_management.libraries.providers import repository
|
|
|
+
|
|
|
+
|
|
|
+class DummyTemplate(object):
|
|
|
+
|
|
|
+ def __init__(self, name, extra_imports=[], **kwargs):
|
|
|
+ self._template = InlineTemplate(DummyTemplate._inline_text, extra_imports, **kwargs)
|
|
|
+ self.context = self._template.context
|
|
|
+ self.name = name
|
|
|
+
|
|
|
+ def get_content(self):
|
|
|
+ self.content = self._template.get_content()
|
|
|
+ return self.content
|
|
|
+
|
|
|
+ @classmethod
|
|
|
+ def create(cls, text):
|
|
|
+ cls._inline_text = text
|
|
|
+ return cls
|
|
|
+
|
|
|
+DEBIAN_DEFAUTL_TEMPLATE = "{{package_type}} {{base_url}} {{components}}\n"
|
|
|
+RHEL_SUSE_DEFAULT_TEMPLATE ="""[{{repo_id}}]
|
|
|
+name={{repo_file_name}}
|
|
|
+{% if mirror_list %}mirrorlist={{mirror_list}}{% else %}baseurl={{base_url}}{% endif %}
|
|
|
+
|
|
|
+path=/
|
|
|
+enabled=1
|
|
|
+gpgcheck=0
|
|
|
+"""
|
|
|
|
|
|
|
|
|
class TestRepositoryResource(TestCase):
|
|
@@ -29,14 +57,20 @@ class TestRepositoryResource(TestCase):
|
|
|
@patch("resource_management.libraries.providers.repository.File")
|
|
|
def test_create_repo_redhat(self, file_mock):
|
|
|
with Environment('/') as env:
|
|
|
+ with patch.object(repository,"Template", new=DummyTemplate.create(RHEL_SUSE_DEFAULT_TEMPLATE)):
|
|
|
Repository('hadoop',
|
|
|
base_url='http://download.base_url.org/rpm/',
|
|
|
mirror_list='https://mirrors.base_url.org/?repo=Repository&arch=$basearch',
|
|
|
- repo_file_name='Repository')
|
|
|
+ repo_file_name='Repository',
|
|
|
+ repo_template='dummy.j2')
|
|
|
|
|
|
self.assertTrue('hadoop' in env.resources['Repository'])
|
|
|
defined_arguments = env.resources['Repository']['hadoop'].arguments
|
|
|
- expected_arguments = {'base_url': 'http://download.base_url.org/rpm/',
|
|
|
+ expected_arguments = {'repo_template': 'dummy.j2',
|
|
|
+ 'base_url': 'http://download.base_url.org/rpm/',
|
|
|
+ 'mirror_list': 'https://mirrors.base_url.org/?repo=Repository&arch=$basearch',
|
|
|
+ 'repo_file_name': 'Repository'}
|
|
|
+ expected_template_arguments = {'base_url': 'http://download.base_url.org/rpm/',
|
|
|
'mirror_list': 'https://mirrors.base_url.org/?repo=Repository&arch=$basearch',
|
|
|
'repo_file_name': 'Repository'}
|
|
|
|
|
@@ -45,30 +79,31 @@ class TestRepositoryResource(TestCase):
|
|
|
|
|
|
template_item = file_mock.call_args[1]['content']
|
|
|
template = str(template_item.name)
|
|
|
- expected_arguments.update({'repo_id': 'hadoop'})
|
|
|
+ expected_template_arguments.update({'repo_id': 'hadoop'})
|
|
|
|
|
|
- self.assertEqual(expected_arguments, template_item.context._dict)
|
|
|
- self.assertEqual("""[{{repo_id}}]
|
|
|
-name={{repo_file_name}}
|
|
|
-{% if mirror_list %}mirrorlist={{mirror_list}}{% else %}baseurl={{base_url}}{% endif %}
|
|
|
-path=/
|
|
|
-enabled=1
|
|
|
-gpgcheck=0""", template)
|
|
|
+ self.assertEqual(expected_template_arguments, template_item.context._dict)
|
|
|
+ self.assertEqual('dummy.j2', template)
|
|
|
|
|
|
|
|
|
@patch.object(System, "os_family", new='suse')
|
|
|
@patch("resource_management.libraries.providers.repository.File")
|
|
|
def test_create_repo_suse(self, file_mock):
|
|
|
with Environment('/') as env:
|
|
|
+ with patch.object(repository,"Template", new=DummyTemplate.create(RHEL_SUSE_DEFAULT_TEMPLATE)):
|
|
|
Repository('hadoop',
|
|
|
base_url='http://download.base_url.org/rpm/',
|
|
|
mirror_list='https://mirrors.base_url.org/?repo=Repository&arch=$basearch',
|
|
|
+ repo_template = "dummy.j2",
|
|
|
repo_file_name='Repository')
|
|
|
|
|
|
self.assertTrue('hadoop' in env.resources['Repository'])
|
|
|
defined_arguments = env.resources['Repository']['hadoop'].arguments
|
|
|
- expected_arguments = {'base_url': 'http://download.base_url.org/rpm/',
|
|
|
+ expected_arguments = {'repo_template': 'dummy.j2',
|
|
|
'mirror_list': 'https://mirrors.base_url.org/?repo=Repository&arch=$basearch',
|
|
|
+ 'base_url': 'http://download.base_url.org/rpm/',
|
|
|
+ 'repo_file_name': 'Repository'}
|
|
|
+ expected_template_arguments = {'mirror_list': 'https://mirrors.base_url.org/?repo=Repository&arch=$basearch',
|
|
|
+ 'base_url': 'http://download.base_url.org/rpm/',
|
|
|
'repo_file_name': 'Repository'}
|
|
|
|
|
|
self.assertEqual(defined_arguments, expected_arguments)
|
|
@@ -76,15 +111,10 @@ gpgcheck=0""", template)
|
|
|
|
|
|
template_item = file_mock.call_args[1]['content']
|
|
|
template = str(template_item.name)
|
|
|
- expected_arguments.update({'repo_id': 'hadoop'})
|
|
|
+ expected_template_arguments.update({'repo_id': 'hadoop'})
|
|
|
|
|
|
- self.assertEqual(expected_arguments, template_item.context._dict)
|
|
|
- self.assertEqual("""[{{repo_id}}]
|
|
|
-name={{repo_file_name}}
|
|
|
-{% if mirror_list %}mirrorlist={{mirror_list}}{% else %}baseurl={{base_url}}{% endif %}
|
|
|
-path=/
|
|
|
-enabled=1
|
|
|
-gpgcheck=0""", template)
|
|
|
+ self.assertEqual(expected_template_arguments, template_item.context._dict)
|
|
|
+ self.assertEqual('dummy.j2', template)
|
|
|
|
|
|
|
|
|
@patch.object(tempfile, "NamedTemporaryFile")
|
|
@@ -99,9 +129,11 @@ gpgcheck=0""", template)
|
|
|
tempfile_mock.return_value.__enter__.return_value.name = "/tmp/1.txt"
|
|
|
|
|
|
with Environment('/') as env:
|
|
|
+ with patch.object(repository,"Template", new=DummyTemplate.create(DEBIAN_DEFAUTL_TEMPLATE)):
|
|
|
Repository('HDP',
|
|
|
base_url='http://download.base_url.org/rpm/',
|
|
|
repo_file_name='HDP',
|
|
|
+ repo_template = "dummy.j2",
|
|
|
components = ['a','b','c']
|
|
|
)
|
|
|
|
|
@@ -130,9 +162,11 @@ gpgcheck=0""", template)
|
|
|
tempfile_mock.return_value.__enter__.return_value.name = "/tmp/1.txt"
|
|
|
|
|
|
with Environment('/') as env:
|
|
|
+ with patch.object(repository,"Template", new=DummyTemplate.create(DEBIAN_DEFAUTL_TEMPLATE)):
|
|
|
Repository('HDP',
|
|
|
base_url='http://download.base_url.org/rpm/',
|
|
|
repo_file_name='HDP',
|
|
|
+ repo_template = "dummy.j2",
|
|
|
components = ['a','b','c']
|
|
|
)
|
|
|
|