Example #1
0
    def test_execute_empty_return_string(self, m_Popen):
        m_proc = mock.MagicMock()
        m_proc.returncode = None

        stdout = ""
        stderr = "Logs logs logs\nBlah blah blah"

        def m_communicate_side_effect():
            m_proc.returncode = 0
            return stdout, stderr

        m_proc.communicate.side_effect = m_communicate_side_effect
        m_Popen.return_value = m_proc
        ps = PsCommand("Get-At-Me")

        ps.execute()

        m_proc.communicate.assert_called_once()
        m_Popen.assert_called_once_with([
            "pwsh", "-command",
            "& { . %s; Get-At-Me }" % COMMAND_RUNNER_PATH
        ],
                                        stderr=subprocess.PIPE,
                                        stdout=subprocess.PIPE,
                                        cwd=r"%s" % PS_SCRIPT_PATH)
        self.assertEqual("failed", ps.status)
        self.assertEqual(None, ps.response)
        self.assertEqual("Curie failed to parse PowerShell JSON response: ''",
                         ps.error)
Example #2
0
    def test_execute_basic(self, m_Popen):
        m_proc = mock.MagicMock()
        m_proc.returncode = None

        stdout = "{\"Response\": {\"fruit\": \"apple\"}, \"StatusCode\": 200}"
        stderr = "Logs logs logs\nBlah blah blah"

        def m_communicate_side_effect():
            m_proc.returncode = 0
            return stdout, stderr

        m_proc.communicate.side_effect = m_communicate_side_effect
        m_Popen.return_value = m_proc
        ps = PsCommand("Get-At-Me")

        ps.execute()

        m_proc.communicate.assert_called_once()
        m_Popen.assert_called_once_with([
            "pwsh", "-command",
            "& { . %s; Get-At-Me }" % COMMAND_RUNNER_PATH
        ],
                                        stderr=subprocess.PIPE,
                                        stdout=subprocess.PIPE,
                                        cwd=r"%s" % PS_SCRIPT_PATH)
        self.assertEqual("completed", ps.status)
        self.assertEqual({"fruit": "apple"}, ps.response)
Example #3
0
    def test_execute_bad_proc_return_code(self, m_Popen):
        m_proc = mock.MagicMock()
        m_proc.returncode = None

        stdout = "** Error in 'pwsh': corrupted double-linked list: 0xdeadbeef **"
        stderr = "Logs logs logs\nBlah blah blah"

        def m_communicate_side_effect():
            m_proc.returncode = 1
            return stdout, stderr

        m_proc.communicate.side_effect = m_communicate_side_effect
        m_Popen.return_value = m_proc
        ps = PsCommand("Get-At-Me")

        ps.execute()

        m_proc.communicate.assert_called_once()
        m_Popen.assert_called_once_with([
            "pwsh", "-command",
            "& { . %s; Get-At-Me }" % COMMAND_RUNNER_PATH
        ],
                                        stderr=subprocess.PIPE,
                                        stdout=subprocess.PIPE,
                                        cwd=r"%s" % PS_SCRIPT_PATH)
        self.assertEqual("failed", ps.status)
        self.assertEqual(None,
                         ps.response)  # Non-zero code implies useless data.
        self.assertEqual("Curie received non-zero PowerShell return code 1",
                         ps.error)
Example #4
0
 def test_as_ps_task_init(self):
     ps = PsCommand("Get-At-Me")
     task = ps.as_ps_task()
     self.assertEqual("ps", task["task_type"])
     self.assertEqual(False, task["completed"])
     self.assertEqual(0, task["progress"])
     self.assertEqual(None, task["state"])
     self.assertEqual("", task["status"])
     self.assertEqual(None, task["error"])
Example #5
0
 def test_as_ps_task_completed(self):
     ps = PsCommand("Get-At-Me")
     ps.status = "completed"
     task = ps.as_ps_task()
     self.assertEqual("ps", task["task_type"])
     self.assertEqual(True, task["completed"])
     self.assertEqual(100, task["progress"])
     self.assertEqual("completed", task["state"])
     self.assertEqual("", task["status"])
     self.assertEqual(None, task["error"])
Example #6
0
 def test_as_ps_task_failed(self):
     ps = PsCommand("Get-At-Me")
     ps.status = "failed"
     ps.error = "Oh noes"
     task = ps.as_ps_task()
     self.assertEqual("ps", task["task_type"])
     self.assertEqual(False, task["completed"])
     self.assertEqual(0, task["progress"])
     self.assertEqual("failed", task["state"])
     self.assertEqual("", task["status"])
     self.assertEqual("Oh noes", task["error"])
Example #7
0
    def test_execute_bad_status_code(self, m_Popen):
        m_proc = mock.MagicMock()
        m_proc.returncode = None

        error_list = [
            {
                "message": "Fake message from PS",
                "details": "The fake details from PS"
            },
            {
                "message": "Another fake message from PS",
                "details": "More fake details from PS"
            },
        ]
        stdout = json.dumps({"Response": error_list, "StatusCode": 500})
        stderr = "Logs logs logs\nBlah blah blah"

        def m_communicate_side_effect():
            m_proc.returncode = 0
            return stdout, stderr

        m_proc.communicate.side_effect = m_communicate_side_effect
        m_Popen.return_value = m_proc
        ps = PsCommand("Get-At-Me")

        ps.execute()

        m_proc.communicate.assert_called_once()
        m_Popen.assert_called_once_with([
            "pwsh", "-command",
            "& { . %s; Get-At-Me }" % COMMAND_RUNNER_PATH
        ],
                                        stderr=subprocess.PIPE,
                                        stdout=subprocess.PIPE,
                                        cwd=r"%s" % PS_SCRIPT_PATH)
        self.assertEqual("failed", ps.status)
        self.assertEqual(error_list, ps.response)
        self.assertEqual(
            "PowerShell command error:\n"
            "Fake message from PS: The fake details from PS\n"
            "Another fake message from PS: More fake details from PS",
            ps.error)