Merge pull request #2050 from peterhorsley/exec-provider-exception

Fix exception in ExecProvider when no console is attached.
This commit is contained in:
Kubernetes Prow Robot 2023-05-24 12:24:50 -07:00 committed by GitHub
commit 5509bb8afa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 2 deletions

View File

@ -53,11 +53,11 @@ class ExecProvider(object):
value = item['value']
additional_vars[name] = value
self.env.update(additional_vars)
self.cwd = cwd or None
def run(self, previous_response=None):
is_interactive = sys.stdout.isatty()
is_interactive = hasattr(sys.stdout, 'isatty') and sys.stdout.isatty()
kubernetes_exec_info = {
'apiVersion': self.api_version,
'kind': 'ExecCredential',

View File

@ -149,6 +149,19 @@ class ExecProviderTest(unittest.TestCase):
ep.run()
self.assertEqual(mock.call_args[1]['cwd'], '/some/directory')
@mock.patch('subprocess.Popen')
def test_ok_no_console_attached(self, mock):
instance = mock.return_value
instance.wait.return_value = 0
instance.communicate.return_value = (self.output_ok, '')
mock_stdout = unittest.mock.patch(
'sys.stdout', new=None) # Simulate detached console
with mock_stdout:
ep = ExecProvider(self.input_ok, None)
result = ep.run()
self.assertTrue(isinstance(result, dict))
self.assertTrue('token' in result)
if __name__ == '__main__':
unittest.main()