1# (c) 2017 Red Hat Inc. 2# 3# This file is part of Ansible 4# 5# Ansible is free software: you can redistribute it and/or modify 6# it under the terms of the GNU General Public License as published by 7# the Free Software Foundation, either version 3 of the License, or 8# (at your option) any later version. 9# 10# Ansible is distributed in the hope that it will be useful, 11# but WITHOUT ANY WARRANTY; without even the implied warranty of 12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13# GNU General Public License for more details. 14# 15# You should have received a copy of the GNU General Public License 16# along with Ansible. If not, see <http://www.gnu.org/licenses/>. 17 18# Make coding more python3-ish 19from __future__ import (absolute_import, division, print_function) 20__metaclass__ = type 21 22import os 23import json 24 25from units.modules.utils import AnsibleExitJson, AnsibleFailJson, ModuleTestCase 26 27 28fixture_path = os.path.join(os.path.dirname(__file__), 'fixtures') 29fixture_data = {} 30 31 32def load_fixture(name): 33 path = os.path.join(fixture_path, name) 34 35 if path in fixture_data: 36 return fixture_data[path] 37 38 with open(path) as f: 39 data = f.read() 40 41 try: 42 data = json.loads(data) 43 except Exception: 44 pass 45 46 fixture_data[path] = data 47 return data 48 49 50class TestOpenVSwitchModule(ModuleTestCase): 51 52 def execute_module(self, failed=False, changed=False, 53 commands=None, test_name=None): 54 55 self.load_fixtures(test_name) 56 57 if failed: 58 result = self.failed() 59 self.assertTrue(result['failed'], result) 60 else: 61 result = self.changed(changed) 62 self.assertEqual(result['changed'], changed, result) 63 64 if commands is not None: 65 self.assertEqual(commands, result['commands'], result['commands']) 66 67 return result 68 69 def failed(self): 70 with self.assertRaises(AnsibleFailJson) as exc: 71 self.module.main() 72 73 result = exc.exception.args[0] 74 self.assertTrue(result['failed'], result) 75 return result 76 77 def changed(self, changed=False): 78 with self.assertRaises(AnsibleExitJson) as exc: 79 self.module.main() 80 81 result = exc.exception.args[0] 82 self.assertEqual(result['changed'], changed, result) 83 return result 84 85 def load_fixtures(self, test_name): 86 pass 87