def test_enable_does_not_install_livepatch_snap_when_present(
         self, m_can_enable, m_which, m_subp, tmpdir):
     """Do not attempt to install livepatch snap when it is present."""
     cfg = config.UAConfig(cfg={'data_dir': tmpdir.strpath})
     cfg.write_cache('machine-token', dict(LIVEPATCH_MACHINE_TOKEN))
     cfg.write_cache('machine-access-livepatch',
                     dict(LIVEPATCH_RESOURCE_ENTITLED))
     entitlement = LivepatchEntitlement(cfg)
     with mock.patch('sys.stdout', new_callable=StringIO) as m_stdout:
         assert entitlement.enable()
     assert self.mocks_config == m_subp.call_args_list
     assert 'Canonical livepatch enabled.\n' == m_stdout.getvalue()
 def test_enable_false_when_can_enable_false(
         self, m_can_enable, caplog_text, tmpdir):
     """When can_enable returns False enable returns False."""
     cfg = config.UAConfig(cfg={'data_dir': tmpdir.strpath})
     cfg.write_cache('machine-token', dict(LIVEPATCH_MACHINE_TOKEN))
     cfg.write_cache('machine-access-livepatch',
                     dict(LIVEPATCH_RESOURCE_ENTITLED))
     entitlement = LivepatchEntitlement(cfg)
     with mock.patch('sys.stdout', new_callable=StringIO) as m_stdout:
         assert not entitlement.enable()
     info_level_logs = [  # see uaclient/conftest.py
         line for line in caplog_text().splitlines()
         if 'DEBUG' not in line]
     assert [] == info_level_logs
     assert '' == m_stdout.getvalue()  # No additional prints
     assert [mock.call()] == m_can_enable.call_args_list
 def test_enable_installs_livepatch_snap_when_absent(
         self, m_can_enable, m_which, m_subp, tmpdir):
     """Install canonical-livepatch snap when not present on the system."""
     cfg = config.UAConfig(cfg={'data_dir': tmpdir.strpath})
     cfg.write_cache('machine-token', dict(LIVEPATCH_MACHINE_TOKEN))
     cfg.write_cache('machine-access-livepatch',
                     dict(LIVEPATCH_RESOURCE_ENTITLED))
     entitlement = LivepatchEntitlement(cfg)
     with mock.patch('sys.stdout', new_callable=StringIO) as m_stdout:
         assert entitlement.enable()
     assert self.mocks_install + self.mocks_config in m_subp.call_args_list
     msg = ('Installing snapd...\n'
            'Installing canonical-livepatch snap...\n'
            'Canonical livepatch enabled.\n')
     assert msg == m_stdout.getvalue()
     expected_calls = [mock.call('/snap/bin/canonical-livepatch'),
                       mock.call('snap')]
     assert expected_calls == m_which.call_args_list