def test_configure_auth(self, mock_popen):
     p = Mock(Popen)
     mock_popen.return_value = p
     p.returncode = 0
     p.communicate.return_value = self.to_bytes("out: foo"), self.to_bytes("error: bar")
     om_manager.config_opsman_auth(self.settings)
     mock_popen.assert_called_with(
         "om -k --target https://cf.example.com configure-authentication --username 'admin' --password 'monkey-123' --decryption-passphrase 'monkey-123'",
         stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True
     )
    def test_retries(self, mock_print, mock_sleep, mock_popen):
        errors = ["out: i/o timeout", "connection refused", "yo, no opsman for you"]
        p = Mock(Popen)
        mock_popen.return_value = p
        p.returncode = 1
        for recoverable_error in errors:
            mock_popen.reset_mock()
            p.communicate.return_value = self.to_bytes(recoverable_error), self.to_bytes("error: bar")
            om_manager.config_opsman_auth(self.settings)

            self.assertEqual(mock_popen.call_count, 6)
    def test_exponential_backoff(self, mock_print, mock_sleep, mock_popen):
        p = Mock(Popen)
        mock_popen.return_value = p
        p.returncode = 1
        mock_popen.reset_mock()
        p.communicate.return_value = self.to_bytes("out: i/o timeout"), self.to_bytes("error: bar")
        om_manager.config_opsman_auth(self.settings)

        self.assertEqual(mock_sleep.call_count, 5)
        self.assertEqual(mock_sleep.call_args_list[0][0][0], 0)
        self.assertEqual(mock_sleep.call_args_list[1][0][0], 1)
        self.assertEqual(mock_sleep.call_args_list[2][0][0], 8)
        self.assertEqual(mock_sleep.call_args_list[3][0][0], 27)
        self.assertEqual(mock_sleep.call_args_list[4][0][0], 64)
    def test_exponential_backoff_result(self, mock_print, mock_sleep, mock_run_command):
        mock_run_command.return_value = "foo", "bar", 42
        # mock_popen.reset_mock()
        out, err, status_code = om_manager.config_opsman_auth(self.settings)

        self.assertEqual(status_code, 42)
        self.assertEqual(out, "foo")
        self.assertEqual(err, "bar")
 def test_configure_auth(self, mock_popen):
     p = Mock(Popen)
     mock_popen.return_value = p
     p.returncode = 0
     p.communicate.return_value = self.to_bytes(
         "out: foo"), self.to_bytes("error: bar")
     om_manager.config_opsman_auth(self.settings)
     mock_popen.assert_called_with(
         ["om",
          "-k",
          "--target", "https://cf.example.com",
          "configure-authentication",
          "--username", "admin",
          "--password", "monkey-123",
          "--decryption-passphrase", "monkey-123"],
         stdin=None, stdout=subprocess.PIPE, stderr=subprocess.PIPE
     )
    def test_prints_error(self, mock_print, mock_sleep, mock_run_command):
        mock_run_command.return_value = "foo", "bar", 1

        om_manager.config_opsman_auth(self.settings)

        mock_print.assert_called_with("error: bar")