Enables containerized overcloud deployments
[apex.git] / apex / tests / test_apex_common_utils.py
1 ##############################################################################
2 # Copyright (c) 2016 Dan Radez (Red Hat)
3 #
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9
10 import ipaddress
11 import os
12 import shutil
13 import urllib.error
14
15 from apex.common import exceptions
16 from apex.common import utils
17 from apex.settings.network_settings import NetworkSettings
18 from apex.tests.constants import (
19     TEST_CONFIG_DIR,
20     TEST_PLAYBOOK_DIR)
21
22 from mock import patch, mock_open
23 from nose.tools import (
24     assert_equal,
25     assert_is_instance,
26     assert_not_is_instance,
27     assert_raises)
28
29 NET_SETS = os.path.join(TEST_CONFIG_DIR, 'network', 'network_settings.yaml')
30 a_mock_open = mock_open(read_data=None)
31
32
33 class TestCommonUtils:
34     @classmethod
35     def setup_class(cls):
36         """This method is run once for each class before any tests are run"""
37
38     @classmethod
39     def teardown_class(cls):
40         """This method is run once for each class _after_ all tests are run"""
41
42     def setup(self):
43         """This method is run once before _each_ test method is executed"""
44
45     def teardown(self):
46         """This method is run once after _each_ test method is executed"""
47
48     def test_str2bool(self):
49         assert_equal(utils.str2bool(True), True)
50         assert_equal(utils.str2bool(False), False)
51         assert_equal(utils.str2bool("True"), True)
52         assert_equal(utils.str2bool("YES"), True)
53
54     def test_parse_yaml(self):
55         assert_is_instance(utils.parse_yaml(NET_SETS), dict)
56
57     def test_dict_to_string(self):
58         net_settings = NetworkSettings(NET_SETS)
59         output = utils.dict_objects_to_str(net_settings)
60         assert_is_instance(output, dict)
61         for k, v in output.items():
62             assert_is_instance(k, str)
63             assert_not_is_instance(v, ipaddress.IPv4Address)
64
65     def test_run_ansible(self):
66         playbook = 'apex/tests/playbooks/test_playbook.yaml'
67         assert_equal(utils.run_ansible(None, os.path.join(playbook),
68                                        dry_run=True), None)
69
70     def test_failed_run_ansible(self):
71         playbook = 'apex/tests/playbooks/test_failed_playbook.yaml'
72         assert_raises(Exception, utils.run_ansible, None,
73                       os.path.join(playbook), dry_run=True)
74
75     def test_fetch_upstream_and_unpack(self):
76         url = 'https://github.com/opnfv/apex/blob/master/'
77         utils.fetch_upstream_and_unpack('/tmp/fetch_test',
78                                         url, ['INFO'])
79         assert os.path.isfile('/tmp/fetch_test/INFO')
80         shutil.rmtree('/tmp/fetch_test')
81
82     def test_fetch_upstream_previous_file(self):
83         test_file = 'overcloud-full.tar.md5'
84         url = 'https://images.rdoproject.org/master/delorean/' \
85               'current-tripleo/stable/'
86         os.makedirs('/tmp/fetch_test', exist_ok=True)
87         open("/tmp/fetch_test/{}".format(test_file), 'w').close()
88         utils.fetch_upstream_and_unpack('/tmp/fetch_test',
89                                         url, [test_file])
90         assert os.path.isfile("/tmp/fetch_test/{}".format(test_file))
91         shutil.rmtree('/tmp/fetch_test')
92
93     def test_fetch_upstream_invalid_url(self):
94         url = 'http://notavalidsite.com/'
95         assert_raises(urllib.error.URLError,
96                       utils.fetch_upstream_and_unpack, '/tmp/fetch_test',
97                       url, ['INFO'])
98         shutil.rmtree('/tmp/fetch_test')
99
100     def test_fetch_upstream_and_unpack_tarball(self):
101         url = 'http://artifacts.opnfv.org/apex/tests/'
102         utils.fetch_upstream_and_unpack('/tmp/fetch_test',
103                                         url, ['dummy_test.tar'])
104         assert os.path.isfile('/tmp/fetch_test/test.txt')
105         shutil.rmtree('/tmp/fetch_test')
106
107     def test_nofetch_upstream_and_unpack(self):
108         test_file = 'overcloud-full.tar.md5'
109         url = 'https://images.rdoproject.org/master/delorean/' \
110               'current-tripleo/stable/'
111         os.makedirs('/tmp/fetch_test', exist_ok=True)
112         target = "/tmp/fetch_test/{}".format(test_file)
113         open(target, 'w').close()
114         target_mtime = os.path.getmtime(target)
115         utils.fetch_upstream_and_unpack('/tmp/fetch_test',
116                                         url, [test_file], fetch=False)
117         post_target_mtime = os.path.getmtime(target)
118         shutil.rmtree('/tmp/fetch_test')
119         assert_equal(target_mtime, post_target_mtime)
120
121     def test_nofetch_upstream_and_unpack_no_target(self):
122         test_file = 'overcloud-full.tar.md5'
123         url = 'https://images.rdoproject.org/master/delorean/' \
124               'current-tripleo/stable/'
125         utils.fetch_upstream_and_unpack('/tmp/fetch_test',
126                                         url, [test_file])
127         assert os.path.isfile("/tmp/fetch_test/{}".format(test_file))
128         shutil.rmtree('/tmp/fetch_test')
129
130     def test_open_webpage(self):
131         output = utils.open_webpage('http://opnfv.org')
132         assert output is not None
133
134     def test_open_invalid_webpage(self):
135         assert_raises(urllib.request.URLError, utils.open_webpage,
136                       'http://inv4lIdweb-page.com')
137
138     @patch('builtins.open', a_mock_open)
139     @patch('yaml.safe_dump')
140     @patch('yaml.safe_load')
141     def test_edit_tht_env(self, mock_yaml_load, mock_yaml_dump):
142         settings = {'SomeParameter': 'some_value'}
143         mock_yaml_load.return_value = {
144             'parameter_defaults': {'SomeParameter': 'dummy'}
145         }
146         utils.edit_tht_env('/dummy-environment.yaml', 'parameter_defaults',
147                            settings)
148         new_data = {'parameter_defaults': settings}
149         mock_yaml_dump.assert_called_once_with(new_data, a_mock_open(),
150                                                default_flow_style=False)