Adds ability to deploy from upstream openstack
[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 utils
16 from apex.settings.network_settings import NetworkSettings
17 from apex.tests.constants import (
18     TEST_CONFIG_DIR,
19     TEST_PLAYBOOK_DIR)
20
21 from nose.tools import (
22     assert_equal,
23     assert_is_instance,
24     assert_not_is_instance,
25     assert_raises)
26
27 NET_SETS = os.path.join(TEST_CONFIG_DIR, 'network', 'network_settings.yaml')
28
29
30 class TestCommonUtils:
31     @classmethod
32     def setup_class(cls):
33         """This method is run once for each class before any tests are run"""
34
35     @classmethod
36     def teardown_class(cls):
37         """This method is run once for each class _after_ all tests are run"""
38
39     def setup(self):
40         """This method is run once before _each_ test method is executed"""
41
42     def teardown(self):
43         """This method is run once after _each_ test method is executed"""
44
45     def test_str2bool(self):
46         assert_equal(utils.str2bool(True), True)
47         assert_equal(utils.str2bool(False), False)
48         assert_equal(utils.str2bool("True"), True)
49         assert_equal(utils.str2bool("YES"), True)
50
51     def test_parse_yaml(self):
52         assert_is_instance(utils.parse_yaml(NET_SETS), dict)
53
54     def test_dict_to_string(self):
55         net_settings = NetworkSettings(NET_SETS)
56         output = utils.dict_objects_to_str(net_settings)
57         assert_is_instance(output, dict)
58         for k, v in output.items():
59             assert_is_instance(k, str)
60             assert_not_is_instance(v, ipaddress.IPv4Address)
61
62     def test_run_ansible(self):
63         playbook = 'apex/tests/playbooks/test_playbook.yaml'
64         assert_equal(utils.run_ansible(None, os.path.join(playbook),
65                                        dry_run=True), None)
66
67     def test_failed_run_ansible(self):
68         playbook = 'apex/tests/playbooks/test_failed_playbook.yaml'
69         assert_raises(Exception, utils.run_ansible, None,
70                       os.path.join(playbook), dry_run=True)
71
72     def test_fetch_upstream_and_unpack(self):
73         url = 'https://github.com/opnfv/apex/blob/master/'
74         utils.fetch_upstream_and_unpack('/tmp/fetch_test',
75                                         url, ['INFO'])
76         assert os.path.isfile('/tmp/fetch_test/INFO')
77         shutil.rmtree('/tmp/fetch_test')
78
79     def test_fetch_upstream_previous_file(self):
80         test_file = 'overcloud-full.tar.md5'
81         url = 'https://images.rdoproject.org/master/delorean/' \
82               'current-tripleo/stable/'
83         os.makedirs('/tmp/fetch_test', exist_ok=True)
84         open("/tmp/fetch_test/{}".format(test_file), 'w').close()
85         utils.fetch_upstream_and_unpack('/tmp/fetch_test',
86                                         url, [test_file])
87         assert os.path.isfile("/tmp/fetch_test/{}".format(test_file))
88         shutil.rmtree('/tmp/fetch_test')
89
90     def test_fetch_upstream_invalid_url(self):
91         url = 'http://notavalidsite.com/'
92         assert_raises(urllib.error.URLError,
93                       utils.fetch_upstream_and_unpack, '/tmp/fetch_test',
94                       url, ['INFO'])
95         shutil.rmtree('/tmp/fetch_test')
96
97     def test_fetch_upstream_and_unpack_tarball(self):
98         url = 'http://artifacts.opnfv.org/apex/tests/'
99         utils.fetch_upstream_and_unpack('/tmp/fetch_test',
100                                         url, ['dummy_test.tar'])
101         assert os.path.isfile('/tmp/fetch_test/test.txt')
102         shutil.rmtree('/tmp/fetch_test')