Adds parser tests and cleanup
[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
13 from apex.common import utils
14 from apex.settings.network_settings import NetworkSettings
15 from apex.tests.constants import (
16     TEST_CONFIG_DIR,
17     TEST_PLAYBOOK_DIR)
18
19 from nose.tools import (
20     assert_equal,
21     assert_is_instance,
22     assert_not_is_instance)
23
24 NET_SETS = os.path.join(TEST_CONFIG_DIR, 'network', 'network_settings.yaml')
25
26
27 class TestCommonUtils:
28     @classmethod
29     def setup_class(cls):
30         """This method is run once for each class before any tests are run"""
31
32     @classmethod
33     def teardown_class(cls):
34         """This method is run once for each class _after_ all tests are run"""
35
36     def setup(self):
37         """This method is run once before _each_ test method is executed"""
38
39     def teardown(self):
40         """This method is run once after _each_ test method is executed"""
41
42     def test_str2bool(self):
43         assert_equal(utils.str2bool(True), True)
44         assert_equal(utils.str2bool(False), False)
45         assert_equal(utils.str2bool("True"), True)
46         assert_equal(utils.str2bool("YES"), True)
47
48     def test_parse_yaml(self):
49         assert_is_instance(utils.parse_yaml(NET_SETS), dict)
50
51     def test_dict_to_string(self):
52         net_settings = NetworkSettings(NET_SETS)
53         output = utils.dict_objects_to_str(net_settings)
54         assert_is_instance(output, dict)
55         for k, v in output.items():
56             assert_is_instance(k, str)
57             assert_not_is_instance(v, ipaddress.IPv4Address)
58
59     def test_run_ansible(self):
60         playbook = 'apex/tests/playbooks/test_playbook.yaml'
61         assert_equal(utils.run_ansible(None, os.path.join(playbook),
62                                        dry_run=True), None)