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
21__metaclass__ = type
22
23import os
24import json
25
26from ansible_collections.openvswitch.openvswitch.tests.unit.modules.utils import (
27    AnsibleExitJson,
28    AnsibleFailJson,
29    ModuleTestCase,
30)
31
32
33fixture_path = os.path.join(os.path.dirname(__file__), "fixtures")
34fixture_data = {}
35
36
37def load_fixture(name):
38    path = os.path.join(fixture_path, name)
39
40    if path in fixture_data:
41        return fixture_data[path]
42
43    with open(path) as f:
44        data = f.read()
45
46    try:
47        data = json.loads(data)
48    except Exception:
49        pass
50
51    fixture_data[path] = data
52    return data
53
54
55class TestOpenVSwitchModule(ModuleTestCase):
56    def execute_module(
57        self, failed=False, changed=False, commands=None, test_name=None
58    ):
59
60        self.load_fixtures(test_name)
61
62        if failed:
63            result = self.failed()
64            self.assertTrue(result["failed"], result)
65        else:
66            result = self.changed(changed)
67            self.assertEqual(result["changed"], changed, result)
68
69        if commands is not None:
70            self.assertEqual(commands, result["commands"], result["commands"])
71
72        return result
73
74    def failed(self):
75        with self.assertRaises(AnsibleFailJson) as exc:
76            self.module.main()
77
78        result = exc.exception.args[0]
79        self.assertTrue(result["failed"], result)
80        return result
81
82    def changed(self, changed=False):
83        with self.assertRaises(AnsibleExitJson) as exc:
84            self.module.main()
85
86        result = exc.exception.args[0]
87        self.assertEqual(result["changed"], changed, result)
88        return result
89
90    def load_fixtures(self, test_name):
91        pass
92