1 ##############################################################################
2 # Copyright (c) 2016 Dan Radez (Red Hat)
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 ##############################################################################
15 from apex.common import utils
16 from apex.settings.network_settings import NetworkSettings
17 from apex.tests.constants import (
21 from nose.tools import (
24 assert_not_is_instance,
27 NET_SETS = os.path.join(TEST_CONFIG_DIR, 'network', 'network_settings.yaml')
30 class TestCommonUtils:
33 """This method is run once for each class before any tests are run"""
36 def teardown_class(cls):
37 """This method is run once for each class _after_ all tests are run"""
40 """This method is run once before _each_ test method is executed"""
43 """This method is run once after _each_ test method is executed"""
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)
51 def test_parse_yaml(self):
52 assert_is_instance(utils.parse_yaml(NET_SETS), dict)
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)
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),
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)
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',
76 assert os.path.isfile('/tmp/fetch_test/INFO')
77 shutil.rmtree('/tmp/fetch_test')
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',
87 assert os.path.isfile("/tmp/fetch_test/{}".format(test_file))
88 shutil.rmtree('/tmp/fetch_test')
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',
95 shutil.rmtree('/tmp/fetch_test')
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')