3 # Copyright (c) 2021 Orange and others.
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # pylint: disable=missing-docstring
18 from xtesting.core import ansible
21 class RunTesting(unittest.TestCase):
24 self.test = ansible.Ansible()
26 @mock.patch("shutil.which", return_value=None)
27 def test_check1(self, which):
28 self.test.check_requirements()
29 self.assertEqual(self.test.is_skipped, True)
30 which.assert_called_once_with("ansible-playbook")
32 @mock.patch("shutil.which", return_value='/usr/bin/ansible-playbook')
33 def test_check2(self, which):
34 self.test.check_requirements()
35 self.assertEqual(self.test.is_skipped, False)
36 which.assert_called_once_with("ansible-playbook")
38 @mock.patch("os.path.isdir", return_value=False)
39 def test_fail1(self, isdir):
40 self.assertEqual(self.test.run(), self.test.EX_RUN_ERROR)
41 isdir.assert_not_called()
43 @mock.patch("os.path.isdir", return_value=False)
44 def test_fail2(self, isdir):
45 private_data_dir = "titi"
46 self.assertEqual(self.test.run(
47 private_data_dir=private_data_dir), self.test.EX_RUN_ERROR)
48 isdir.assert_called_once_with(private_data_dir)
50 @mock.patch("ansible_runner.run", side_effect=Exception)
51 @mock.patch("os.makedirs")
52 @mock.patch("os.path.exists", return_value=True)
53 @mock.patch("os.path.isdir", return_value=True)
54 def test_fail3(self, *args):
55 private_data_dir = "titi"
56 self.assertEqual(self.test.run(
57 private_data_dir=private_data_dir), self.test.EX_RUN_ERROR)
58 args[0].assert_called_once_with(private_data_dir)
59 args[1].assert_called_once_with(self.test.res_dir)
60 args[2].assert_not_called()
61 args[3].assert_called_with(
62 private_data_dir=private_data_dir, quiet=True,
63 artifact_dir=self.test.res_dir)
65 @mock.patch("ansible_runner.run", side_effect=Exception)
66 @mock.patch("os.makedirs")
67 @mock.patch("os.path.exists", return_value=False)
68 @mock.patch("os.path.isdir", return_value=True)
69 def test_fail4(self, *args):
70 private_data_dir = "titi"
71 self.assertEqual(self.test.run(
72 private_data_dir=private_data_dir), self.test.EX_RUN_ERROR)
73 args[0].assert_called_once_with(private_data_dir)
74 args[1].assert_called_once_with(self.test.res_dir)
75 args[2].assert_called_once_with(self.test.res_dir)
76 args[3].assert_called_with(
77 private_data_dir=private_data_dir, quiet=True,
78 artifact_dir=self.test.res_dir)
80 @mock.patch("ansible_runner.run")
81 @mock.patch("os.makedirs", side_effect=Exception)
82 @mock.patch("os.path.exists", return_value=False)
83 @mock.patch("os.path.isdir", return_value=True)
84 def test_fail5(self, *args):
85 private_data_dir = "titi"
86 self.assertEqual(self.test.run(
87 private_data_dir=private_data_dir), self.test.EX_RUN_ERROR)
88 args[0].assert_called_once_with(private_data_dir)
89 args[1].assert_called_once_with(self.test.res_dir)
90 args[2].assert_called_once_with(self.test.res_dir)
91 args[3].assert_not_called()
93 @mock.patch("ansible_runner.run", return_value={})
94 @mock.patch("os.makedirs")
95 @mock.patch("os.path.exists", return_value=False)
96 @mock.patch("os.path.isdir", return_value=True)
97 def test_fail6(self, *args):
98 private_data_dir = "titi"
99 self.assertEqual(self.test.run(
100 private_data_dir=private_data_dir, quiet=False,
101 artifact_dir="overridden"), self.test.EX_RUN_ERROR)
102 args[0].assert_called_once_with(private_data_dir)
103 args[1].assert_called_once_with(self.test.res_dir)
104 args[2].assert_called_once_with(self.test.res_dir)
105 args[3].assert_called_with(
106 private_data_dir=private_data_dir, quiet=True,
107 artifact_dir=self.test.res_dir)
109 @mock.patch("ansible_runner.run",
110 return_value=munch.Munch(rc=0, stats={"foo": "bar"}))
111 @mock.patch("os.makedirs")
112 @mock.patch("os.path.exists", return_value=False)
113 @mock.patch("os.path.isdir", return_value=True)
114 def test_res_ok(self, *args):
115 private_data_dir = "titi"
116 self.assertEqual(self.test.run(
117 private_data_dir=private_data_dir, quiet=False,
118 artifact_dir="overridden"), self.test.EX_OK)
119 args[0].assert_called_once_with(private_data_dir)
120 args[1].assert_called_once_with(self.test.res_dir)
121 args[2].assert_called_once_with(self.test.res_dir)
122 args[3].assert_called_with(
123 private_data_dir=private_data_dir, quiet=True,
124 artifact_dir=self.test.res_dir)
125 self.assertEqual(self.test.is_successful(), self.test.EX_OK)
126 self.assertEqual(self.test.details, {"foo": "bar"})
128 @mock.patch("ansible_runner.run",
129 return_value=munch.Munch(rc=1, stats={"foo": "bar"}))
130 @mock.patch("os.makedirs")
131 @mock.patch("os.path.exists", return_value=False)
132 @mock.patch("os.path.isdir", return_value=True)
133 def test_res_ko(self, *args):
134 private_data_dir = "titi"
135 self.assertEqual(self.test.run(
136 private_data_dir=private_data_dir, quiet=False,
137 artifact_dir="overridden"), self.test.EX_OK)
138 args[0].assert_called_once_with(private_data_dir)
139 args[1].assert_called_once_with(self.test.res_dir)
140 args[2].assert_called_once_with(self.test.res_dir)
141 args[3].assert_called_with(
142 private_data_dir=private_data_dir, quiet=True,
143 artifact_dir=self.test.res_dir)
144 self.assertEqual(self.test.is_successful(),
145 self.test.EX_TESTCASE_FAILED)
146 self.assertEqual(self.test.details, {"foo": "bar"})
149 if __name__ == "__main__":
150 logging.disable(logging.CRITICAL)
151 unittest.main(verbosity=2)