3 # The software in this package is distributed under the GNU General
4 # Public License version 2 (with a special exception described below).
6 # A copy of GNU General Public License (GPL) is included in this distribution,
7 # in the file COPYING.GPL.
9 # As a special exception, if other files instantiate templates or use macros
10 # or inline functions from this file, or you compile this file and link it
11 # with other works to produce a work based on this file, this file
12 # does not by itself cause the resulting work to be covered
13 # by the GNU General Public License.
15 # However the source code for this file must still be made available
16 # in accordance with section (3) of the GNU General Public License.
18 # This exception does not invalidate any other reasons why a work based
19 # on this file might be covered by the GNU General Public License.
21 # Copyright (c) 2016-2018 Intra2net AG <info@intra2net.com>
24 import unittest.mock as mock
27 from src import arnied_wrapper
30 class DummyCmdOutputMapping(object):
34 {"cmd": "pgrep -l -x arnied", "stdout": b"", "returncode": 0},
36 {"cmd": 'echo "LICENSE_ACCEPTED,0: \\"1\\"" | set_cnf', "stdout": b"", "returncode": 0},
37 {"cmd": '/usr/intranator/bin/arnied_helper --wait-for-program-end GENERATE', "stdout": b"", "returncode": 0},
39 {"cmd": 'get_cnf PROVIDER 1', "stdout": b"PRODIVER 1, 'sample-provider'", "returncode": 0},
40 {"cmd": 'tell-connd --online P1', "stdout": b"", "returncode": 0},
42 {"cmd": "pgrep -l -x arnied", "stdout": b"", "returncode": 0},
43 {"cmd": 'get_cnf VIRSCAN_UPDATE_CRON | set_cnf -x', "stdout": b"", "returncode": 0},
44 {"cmd": '/usr/intranator/bin/arnied_helper --is-scheduled-or-running GENERATE', "stdout": b"", "returncode": 1},
45 {"cmd": '/usr/intranator/bin/arnied_helper --wait-for-program-end GENERATE --wait-for-program-timeout 300', "stdout": b"", "returncode": 1},
46 {"cmd": '/usr/intranator/bin/arnied_helper --is-scheduled-or-running GENERATE_OFFLINE', "stdout": b"", "returncode": 1},
47 {"cmd": '/usr/intranator/bin/arnied_helper --wait-for-program-end GENERATE_OFFLINE --wait-for-program-timeout 300', "stdout": b"", "returncode": 1},
48 {"cmd": 'echo \'VIRSCAN_UPDATE_DNS_PUSH,0:"0"\' |set_cnf', "stdout": b"", "returncode": 0},
49 {"cmd": 'rm -f /var/intranator/schedule/UPDATE_VIRSCAN_NODIAL*', "stdout": b"", "returncode": 0},
51 {"cmd": '/usr/intranator/bin/arnied_helper --transfer-mail', "stdout": b"", "returncode": 0},
55 def __init__(self, cmd="", ignore_errors=False, vm=None, timeout=60):
63 return "status %i, stdout %s" % (self.returncode, self.stdout)
65 def _get_result(self, cmd):
66 for dummy_cmd in self.asserted_cmds:
67 if dummy_cmd['cmd'] == cmd:
68 self.returncode = dummy_cmd['returncode']
69 self.stdout = dummy_cmd['stdout']
71 raise ValueError("Could not locate the command '%s' among the known answers "
72 "for the universe" % cmd)
75 @mock.patch('src.arnied_wrapper.run_cmd', DummyCmdOutputMapping)
76 class ArniedWrapperTest(unittest.TestCase):
79 DummyCmdOutputMapping.fail_switch = False
80 DummyCmdOutputMapping.asserted_cmds = []
81 self.cmd_db = DummyCmdOutputMapping.cmds
83 def test_verify_running(self):
84 DummyCmdOutputMapping.asserted_cmds = self.cmd_db[0:1]
85 arnied_wrapper.verify_running(timeout=1)
86 DummyCmdOutputMapping.fail_switch = True
87 with self.assertRaises(RuntimeError):
88 arnied_wrapper.verify_running(timeout=1)
90 def test_accept_license(self):
91 DummyCmdOutputMapping.asserted_cmds = self.cmd_db[1:3]
92 arnied_wrapper.accept_licence()
93 # make sure an error is ignored since license might
95 DummyCmdOutputMapping.fail_switch = True
96 arnied_wrapper.accept_licence()
98 def test_go_online(self):
99 DummyCmdOutputMapping.asserted_cmds = self.cmd_db[3:5]
100 arnied_wrapper.go_online(1)
101 # TODO: for some reason failing to go online doesn't raise
102 # an error which could be very misleading during debugging
103 # DummyCmdOutputMapping.fail_switch = True
104 # with self.assertRaises(Exception):
105 # arnied_wrapper.go_online(1)
107 def test_disable_virscan(self):
108 DummyCmdOutputMapping.asserted_cmds = self.cmd_db[5:13]
109 arnied_wrapper.disable_virscan()
111 def test_email_transfer(self):
112 DummyCmdOutputMapping.asserted_cmds = self.cmd_db[13:14]
113 arnied_wrapper.email_transfer()