def setUp(self):
     super().setUp()
     self.setup_livepatch(installed=True, enabled=True)
     self.livepatch_token = '0123456789abcdef1234567890abcdef'
     self.ua_status_cache.write_text(
         STATUS_CACHE_LIVEPATCH_ENABLED.format(check_state="checked",
                                               patch_state="applied"))
 def test_check_failed(self):
     """checkState is check-failed."""
     self.ua_status_cache.write_text(
         STATUS_CACHE_LIVEPATCH_ENABLED.format(check_state='check-failed',
                                               patch_state='irrelevant'))
     process = self.script()
     self.assertEqual(0, process.returncode)
     for line in LIVEPATCH_STATE_MESSAGES['check-failed']:
         self.assertIn(line, process.stdout)
 def test_checked_applying(self):
     """checkState is checked, patchState is applying."""
     self.ua_status_cache.write_text(
         STATUS_CACHE_LIVEPATCH_ENABLED.format(check_state='checked',
                                               patch_state='applying'))
     process = self.script()
     self.assertEqual(0, process.returncode)
     for line in LIVEPATCH_STATE_MESSAGES['checked']['applying']:
         self.assertIn(line, process.stdout)
 def test_patch_unknown_state(self):
     """patchState is something unexpected."""
     random_state = 'patch-state-{}'.format(randrange(99999))
     self.ua_status_cache.write_text(
         STATUS_CACHE_LIVEPATCH_ENABLED.format(check_state='checked',
                                               patch_state=random_state))
     process = self.script()
     self.assertEqual(0, process.returncode)
     for line in LIVEPATCH_STATE_MESSAGES['patch-state-unknown']:
         self.assertIn(line.format(random_state), process.stdout)
 def test_script_uses_cached_status(self):
     """The script uses the cached status when it exists."""
     random_state = 'patch-state-{}'.format(randrange(99999))
     ua_status_cache = STATUS_CACHE_LIVEPATCH_ENABLED.format(
         check_state='checked', patch_state=random_state)
     self.ua_status_cache.write_text(ua_status_cache)
     # setup the livepatch command to show a status different from
     # the cache
     self.setup_livepatch(installed=True,
                          enabled=True,
                          livepatch_command=LIVEPATCH_CHECKED_UNAPPLIED)
     process = self.script()
     self.assertEqual(0, process.returncode)
     # this confirms the cached data was used
     for line in LIVEPATCH_STATE_MESSAGES['patch-state-unknown']:
         self.assertIn(line.format(random_state), process.stdout)
     # this confirms that LIVEPATCH_CHECKED_UNAPPLIED was not used
     for line in LIVEPATCH_STATE_MESSAGES['checked']['unapplied'][1:]:
         self.assertNotIn(line, process.stdout)
     # this confirms the cached file did not change after the script ran
     self.assertEqual(ua_status_cache.format(random_state),
                      self.ua_status_cache.read_text())