class TrustedLauncherTests(TestCase): """ Unit tests for the TrustedLauncher class that implements much of plainbox-trusted-launcher-1 """ def setUp(self): self.launcher = TrustedLauncher() def test_init(self): self.assertEqual(self.launcher._job_list, []) def test_add_job_list(self): job = mock.Mock(spec=JobDefinition, name='job') self.launcher.add_job_list([job]) # Ensure that the job was added correctly self.assertEqual(self.launcher._job_list, [job]) def test_find_job_when_it_doesnt_work(self): job = mock.Mock(spec=JobDefinition, name='job') self.launcher.add_job_list([job]) with self.assertRaises(LookupError) as boom: self.launcher.find_job('foo') # Ensure that LookupError is raised if a job cannot be found self.assertIsInstance(boom.exception, LookupError) self.assertEqual(boom.exception.args, ('Cannot find job with checksum foo', )) def test_find_job_when_it_works(self): job = mock.Mock(spec=JobDefinition, name='job') self.launcher.add_job_list([job]) # Ensure that the job was found correctly self.assertIs(self.launcher.find_job(job.checksum), job) @mock.patch.dict('os.environ', clear=True) @mock.patch('subprocess.call') def test_run_shell_from_job(self, mock_call): # Create a mock job and add it to the launcher job = mock.Mock(spec=JobDefinition, name='job') self.launcher.add_job_list([job]) # Create a environment we'll pass (empty) env = {'key': 'value'} # Run the tested method retval = self.launcher.run_shell_from_job(job.checksum, env) # Ensure that we run the job command via bash mock_call.assert_called_once_with(['bash', '-c', job.command], env=env) # Ensure that the return value of subprocess.call() is returned self.assertEqual(retval, mock_call()) @mock.patch.dict('os.environ', clear=True, DISPLAY='foo') @mock.patch('subprocess.call') def test_run_shell_from_job_with_env_preserved(self, mock_call): # Create a mock job and add it to the launcher job = mock.Mock(spec=JobDefinition, name='job') self.launcher.add_job_list([job]) # Create a environment we'll pass (empty) env = {'key': 'value'} # Run the tested method retval = self.launcher.run_shell_from_job(job.checksum, env) # Ensure that we run the job command via bash with a preserved env expected_env = dict(os.environ) expected_env.update(env) mock_call.assert_called_once_with(['bash', '-c', job.command], env=expected_env) # Ensure that the return value of subprocess.call() is returned self.assertEqual(retval, mock_call()) @mock.patch.dict('os.environ', clear=True) @mock.patch('plainbox.impl.job.JobDefinition.from_rfc822_record') @mock.patch('plainbox.impl.secure.launcher1.load_rfc822_records') @mock.patch('subprocess.check_output') def test_run_local_job(self, mock_check_output, mock_load_rfc822_records, mock_from_rfc822_record): # Create a mock job and add it to the launcher job = mock.Mock(spec=JobDefinition, name='job') self.launcher.add_job_list([job]) # Create two mock rfc822 records record1 = mock.Mock(spec=RFC822Record, name='record') record2 = mock.Mock(spec=RFC822Record, name='record') # Ensure that load_rfc822_records() returns some mocked records mock_load_rfc822_records.return_value = [record1, record2] # Run the tested method job_list = self.launcher.run_local_job(job.checksum, None) # Ensure that we run the job command via bash mock_check_output.assert_called_with(['bash', '-c', job.command], env={}, universal_newlines=True) # Ensure that we parse all of the output mock_load_rfc822_records.assert_called_with( mock_check_output(), source=JobOutputTextSource(job)) # Ensure that we return the jobs back self.assertEqual(len(job_list), 2) self.assertEqual(job_list[0], mock_from_rfc822_record(record1)) self.assertEqual(job_list[1], mock_from_rfc822_record(record2))
class TrustedLauncherTests(TestCase): """ Unit tests for the TrustedLauncher class that implements much of plainbox-trusted-launcher-1 """ def setUp(self): self.launcher = TrustedLauncher() def test_init(self): self.assertEqual(self.launcher._job_list, []) def test_add_job_list(self): job = mock.Mock(spec=JobDefinition, name='job') self.launcher.add_job_list([job]) # Ensure that the job was added correctly self.assertEqual(self.launcher._job_list, [job]) def test_find_job_when_it_doesnt_work(self): job = mock.Mock(spec=JobDefinition, name='job') self.launcher.add_job_list([job]) with self.assertRaises(LookupError) as boom: self.launcher.find_job('foo') # Ensure that LookupError is raised if a job cannot be found self.assertIsInstance(boom.exception, LookupError) self.assertEqual(boom.exception.args, ( 'Cannot find job with checksum foo',)) def test_find_job_when_it_works(self): job = mock.Mock(spec=JobDefinition, name='job') self.launcher.add_job_list([job]) # Ensure that the job was found correctly self.assertIs(self.launcher.find_job(job.checksum), job) @mock.patch.dict('os.environ', clear=True) @mock.patch('subprocess.call') def test_run_shell_from_job(self, mock_call): # Create a mock job and add it to the launcher job = mock.Mock(spec=JobDefinition, name='job') self.launcher.add_job_list([job]) # Create a environment we'll pass (empty) env = {'key': 'value'} # Run the tested method retval = self.launcher.run_shell_from_job(job.checksum, env) # Ensure that we run the job command via bash mock_call.assert_called_once_with(['bash', '-c', job.command], env=env) # Ensure that the return value of subprocess.call() is returned self.assertEqual(retval, mock_call()) @mock.patch.dict('os.environ', clear=True, DISPLAY='foo') @mock.patch('subprocess.call') def test_run_shell_from_job_with_env_preserved(self, mock_call): # Create a mock job and add it to the launcher job = mock.Mock(spec=JobDefinition, name='job') self.launcher.add_job_list([job]) # Create a environment we'll pass (empty) env = {'key': 'value'} # Run the tested method retval = self.launcher.run_shell_from_job(job.checksum, env) # Ensure that we run the job command via bash with a preserved env expected_env = dict(os.environ) expected_env.update(env) mock_call.assert_called_once_with(['bash', '-c', job.command], env=expected_env) # Ensure that the return value of subprocess.call() is returned self.assertEqual(retval, mock_call()) @mock.patch.dict('os.environ', clear=True) @mock.patch('plainbox.impl.job.JobDefinition.from_rfc822_record') @mock.patch('plainbox.impl.secure.launcher1.load_rfc822_records') @mock.patch('subprocess.check_output') def test_run_local_job(self, mock_check_output, mock_load_rfc822_records, mock_from_rfc822_record): # Create a mock job and add it to the launcher job = mock.Mock(spec=JobDefinition, name='job') self.launcher.add_job_list([job]) # Create two mock rfc822 records record1 = mock.Mock(spec=RFC822Record, name='record') record2 = mock.Mock(spec=RFC822Record, name='record') # Ensure that load_rfc822_records() returns some mocked records mock_load_rfc822_records.return_value = [record1, record2] # Run the tested method job_list = self.launcher.run_local_job(job.checksum, None) # Ensure that we run the job command via bash mock_check_output.assert_called_with( ['bash', '-c', job.command], env={}, universal_newlines=True) # Ensure that we parse all of the output mock_load_rfc822_records.assert_called_with( mock_check_output(), source=JobOutputTextSource(job)) # Ensure that we return the jobs back self.assertEqual(len(job_list), 2) self.assertEqual(job_list[0], mock_from_rfc822_record(record1)) self.assertEqual(job_list[1], mock_from_rfc822_record(record2))
class TrustedLauncherTests(TestCase): """ Unit tests for the TrustedLauncher class that implements much of plainbox-trusted-launcher-1 """ def setUp(self): self.launcher = TrustedLauncher() def test_init(self): self.assertEqual(self.launcher._job_list, []) def test_add_job_list(self): job = mock.Mock(spec=JobDefinition, name='job') self.launcher.add_job_list([job]) # Ensure that the job was added correctly self.assertEqual(self.launcher._job_list, [job]) def test_find_job_when_it_doesnt_work(self): job = mock.Mock(spec=JobDefinition, name='job') self.launcher.add_job_list([job]) with self.assertRaises(LookupError) as boom: self.launcher.find_job('foo') # Ensure that LookupError is raised if a job cannot be found self.assertIsInstance(boom.exception, LookupError) self.assertEqual(boom.exception.args, ('Cannot find job with checksum foo', )) def test_find_job_when_it_works(self): job = mock.Mock(spec=JobDefinition, name='job') self.launcher.add_job_list([job]) # Ensure that the job was found correctly self.assertIs(self.launcher.find_job(job.checksum), job) @mock.patch.dict('os.environ', clear=True) @mock.patch('subprocess.call') def test_run_shell_from_job(self, mock_call): # Create a mock job and add it to the launcher job = mock.Mock(spec=JobDefinition, name='job') self.launcher.add_job_list([job]) # Create a environment we'll pass (empty) env = {'key': 'value'} # Run the tested method retval = self.launcher.run_shell_from_job(job.checksum, env) # Ensure that we run the job command via job.shell mock_call.assert_called_once_with([job.shell, '-c', job.command], env=env) # Ensure that the return value of subprocess.call() is returned self.assertEqual(retval, mock_call()) @mock.patch.dict('os.environ', clear=True, DISPLAY='foo') @mock.patch('subprocess.call') def test_run_shell_from_job_with_env_preserved(self, mock_call): # Create a mock job and add it to the launcher job = mock.Mock(spec=JobDefinition, name='job') self.launcher.add_job_list([job]) # Create a environment we'll pass (empty) env = {'key': 'value'} # Run the tested method retval = self.launcher.run_shell_from_job(job.checksum, env) # Ensure that we run the job command via job.shell with a preserved env expected_env = dict(os.environ) expected_env.update(env) mock_call.assert_called_once_with([job.shell, '-c', job.command], env=expected_env) # Ensure that the return value of subprocess.call() is returned self.assertEqual(retval, mock_call())