def test_notifications(self): """Test the desktop notification functionality.""" timer = Timer() program = RsyncSystemBackup(destination='/backups/system') # The happy path. with MockedProgram('notify-send', returncode=0): program.notify_starting() program.notify_finished(timer) program.notify_failed(timer) # The sad path (should not raise exceptions). with MockedProgram('notify-send', returncode=1): program.notify_starting() program.notify_finished(timer) program.notify_failed(timer)
def test_cron_graceful_additions(self): """Test :func:`proc.cron.run_additions()`.""" # If no program with the name cron-graceful-additions is available # on the $PATH we expect this to be handled ... gracefully :-). with CustomSearchPath(isolated=True): run_additions() # If a program with the name cron-graceful-additions is available # on the $PATH we expect it to be executed successfully. with MockedProgram(ADDITIONS_SCRIPT_NAME): run_additions() # If the cron-graceful-additions program fails we also expect this # to be handled gracefully (i.e. a message is logged but the error # isn't propagated). with MockedProgram(ADDITIONS_SCRIPT_NAME, returncode=1): run_additions()
def test_mocked_program(self): """Test :class:`humanfriendly.testing.MockedProgram`.""" name = random_string() with MockedProgram(name=name, returncode=42) as directory: assert os.path.isdir(directory) assert os.path.isfile(os.path.join(directory, name)) assert subprocess.call(name) == 42
def test_unsupported_platform_with_force(self): """Test that UnsupportedPlatformError is raised as expected.""" with MockedProgram('uname'): program = RsyncSystemBackup(destination='/some/random/directory', force=True) # Avoid making an actual backup. program.execute_helper = MagicMock() program.execute() assert program.execute_helper.called
def mock_ip(self): """Mocked ``ip route show`` program.""" return MockedProgram(name='ip', script=dedent(""" cat << EOF default via 192.168.1.1 dev wlp3s0 proto dhcp metric 600 169.254.0.0/16 dev virbr0 scope link metric 1000 linkdown 192.168.0.0/16 dev wlp3s0 proto kernel scope link src 192.168.2.214 metric 600 192.168.122.0/24 dev virbr0 proto kernel scope link src 192.168.122.1 linkdown EOF """))
def test_notify_desktop(self): """Test that :func:`proc.notify.notify_desktop()` works.""" env = dict((name, 'value') for name in REQUIRED_VARIABLES) with ExternalCommand('sleep 60', environment=env): with MockedProgram('notify-send'): notify_desktop( summary="Headless notifications", body= "They actually work! (this message brought to you by the 'proc' test suite :-)", urgency='low', )
def test_edit_entry(self): """Test editing of an entry on the command line.""" # Create a fake password store that we can test against. with TemporaryDirectory() as directory: touch(os.path.join(directory, "Personal", "Zabbix.gpg")) touch(os.path.join(directory, "Work", "Zabbix.gpg")) # Make sure we're not running the real `pass' program because its # intended purpose is user interaction, which has no place in an # automated test suite :-). with MockedProgram("pass"): returncode, output = run_cli(main, "--password-store=%s" % directory, "--edit", "p/z", merged=True) assert returncode == 0 assert "Matched one entry: Personal/Zabbix" in output
def mock_arp(self): """Mocked ``arp`` program.""" return MockedProgram(name='arp', script=dedent(""" cat << EOF Address HWtype HWaddress Flags Mask Iface 192.168.1.4 ether 4b:21:f5:49:88:85 C wlp3s0 192.168.3.28 ether 3d:a6:19:62:9a:83 C wlp3s0 192.168.3.5 ether c5:4c:8d:56:25:0c C wlp3s0 192.168.1.1 ether 80:34:58:ad:6c:f5 C wlp3s0 192.168.3.2 ether 20:22:a0:22:0c:db C wlp3s0 192.168.1.12 ether ad:12:75:46:e9:70 C wlp3s0 192.168.3.6 ether 08:33:c7:ef:f7:27 C wlp3s0 192.168.1.11 ether c9:0e:95:24:68:31 C wlp3s0 192.168.3.4 ether e7:e6:2c:3b:bc:8a C wlp3s0 192.168.3.3 ether 72:d7:d3:2c:54:93 C wlp3s0 192.168.1.6 ether 95:ef:85:cf:d3:36 C wlp3s0 192.168.3.7 ether 65:c0:be:40:cd:31 C wlp3s0 EOF """))
def test_unsupported_platform_error(self): """Test that UnsupportedPlatformError is raised as expected.""" with MockedProgram('uname'): program = RsyncSystemBackup(destination='/some/random/directory') self.assertRaises(UnsupportedPlatformError, program.execute)
def test_have_internet_connection(self): """Test :func:`linux_utils.network.have_internet_connection().`""" with MockedProgram(name='ping', returncode=0): assert have_internet_connection() is True with MockedProgram(name='ping', returncode=1): assert have_internet_connection() is False