def test_not_subscribed(self, exec_with_redirect):
        """Test that nothing is done if Insights is requested but system is not subscribed."""

        with tempfile.TemporaryDirectory() as sysroot:
            task = ConnectToInsightsTask(sysroot=sysroot,
                                         subscription_attached=False,
                                         connect_to_insights=True)
            task.run()
            # check that no attempt to call the Insights client has been attempted
            exec_with_redirect.assert_not_called()
    def test_utility_not_available(self, exec_with_redirect):
        """Test that the client-missing exception is raised if Insights client is missing."""

        with tempfile.TemporaryDirectory() as sysroot:
            task = ConnectToInsightsTask(sysroot=sysroot,
                                         subscription_attached=True,
                                         connect_to_insights=True)
            with self.assertRaises(InsightsClientMissingError):
                task.run()
            # check that no attempt to call the Insights client has been attempted
            exec_with_redirect.assert_not_called()
 def test_connect_error(self, exec_with_redirect):
     """Test that the expected exception is raised if the Insights client fails when called."""
     with tempfile.TemporaryDirectory() as sysroot:
         # create a fake insights client tool file
         utility_path = ConnectToInsightsTask.INSIGHTS_TOOL_PATH
         directory = os.path.split(utility_path)[0]
         os.makedirs(util.join_paths(sysroot, directory))
         os.mknod(util.join_paths(sysroot, utility_path))
         task = ConnectToInsightsTask(sysroot=sysroot,
                                      subscription_attached=True,
                                      connect_to_insights=True)
         # make sure execWithRedirect has a non zero return code
         exec_with_redirect.return_value = 1
         with self.assertRaises(InsightsConnectError):
             task.run()
         # check that call to the insights client has been done with the expected parameters
         exec_with_redirect.assert_called_once_with(
             '/usr/bin/insights-client', ['--register'], root=sysroot)
 def test_connect(self, exec_with_redirect):
     """Test that it is possible to connect to Insights."""
     with tempfile.TemporaryDirectory() as sysroot:
         # create a fake insights client tool file
         utility_path = ConnectToInsightsTask.INSIGHTS_TOOL_PATH
         directory = os.path.split(utility_path)[0]
         # we use + here instead of os.path.join() as both paths are absolute and
         # os.path.join() does not handle that very well
         os.makedirs(sysroot + directory)
         os.mknod(sysroot + utility_path)
         task = ConnectToInsightsTask(sysroot=sysroot,
                                      subscription_attached=True,
                                      connect_to_insights=True)
         # make sure execWithRedirect has a zero return code
         exec_with_redirect.return_value = 0
         task.run()
         # check that call to the insights client has been done with the expected parameters
         exec_with_redirect.assert_called_once_with(
             '/usr/bin/insights-client', ['--register'], root=sysroot)