def test_help(self): """ verify how `plainbox-trusted-launcher-1 --help` looks like """ # Run the program with io intercept with TestIO(combined=True) as io: with self.assertRaises(SystemExit) as call: main(['--help']) self.assertEqual(call.exception.args, (0, )) self.maxDiff = None expected = """ usage: plainbox-trusted-launcher-1 [-h] (-w | -t CHECKSUM) [-T NAME=VALUE [NAME=VALUE ...]] [-g CHECKSUM] [-G NAME=VALUE [NAME=VALUE ...]] optional arguments: -h, --help show this help message and exit -w, --warmup return immediately, only useful when used with pkexec(1) -t CHECKSUM, --target CHECKSUM run a job with this checksum target job specification: -T NAME=VALUE [NAME=VALUE ...], --target-environment NAME=VALUE [NAME=VALUE ...] environment passed to the target job generator job specification: -g CHECKSUM, --generator CHECKSUM also run a job with this checksum (assuming it is a local job) -G NAME=VALUE [NAME=VALUE ...], --generator-environment NAME=VALUE [NAME=VALUE ...] environment passed to the generator job """ self.assertEqual(io.combined, cleandoc(expected) + "\n")
def test_help(self): """ verify how `plainbox-trusted-launcher-1 --help` looks like """ # Run the program with io intercept with TestIO(combined=True) as io: with self.assertRaises(SystemExit) as call: main(['--help']) self.assertEqual(call.exception.args, (0,)) self.maxDiff = None expected = """ usage: plainbox-trusted-launcher-1 [-h] (-w | -t CHECKSUM) [-T NAME=VALUE [NAME=VALUE ...]] [-g CHECKSUM] [-G NAME=VALUE [NAME=VALUE ...]] optional arguments: -h, --help show this help message and exit -w, --warmup return immediately, only useful when used with pkexec(1) -t CHECKSUM, --target CHECKSUM run a job with this checksum target job specification: -T NAME=VALUE [NAME=VALUE ...], --target-environment NAME=VALUE [NAME=VALUE ...] environment passed to the target job generator job specification: -g CHECKSUM, --generator CHECKSUM also run a job with this checksum (assuming it is a local job) -G NAME=VALUE [NAME=VALUE ...], --generator-environment NAME=VALUE [NAME=VALUE ...] environment passed to the generator job """ self.assertEqual(io.combined, cleandoc(expected) + "\n")
def test_run_invalid_target_checksum(self): """ verify what happens when `plainbox-trusted-launcher-1` is called with a target job checksum that cannot be found in any of the providers. """ # Ensure this there are no jobs that the launcher knows about self.provider.get_builtin_jobs.return_value = [] # Run the program with io intercept with TestIO(combined=True) as io: with self.assertRaises(SystemExit) as call: main(['--target=1234']) # Ensure that the error message contains the checksum of the target job self.assertEqual(call.exception.args, ( 'Cannot find job with checksum 1234',)) self.assertEqual(io.combined, '')
def test_run_invalid_target_checksum(self): """ verify what happens when `plainbox-trusted-launcher-1` is called with a target job checksum that cannot be found in any of the providers. """ # Ensure this there are no jobs that the launcher knows about self.provider.get_builtin_jobs.return_value = [] # Run the program with io intercept with TestIO(combined=True) as io: with self.assertRaises(SystemExit) as call: main(['--target=1234']) # Ensure that the error message contains the checksum of the target job self.assertEqual(call.exception.args, ('Cannot find job with checksum 1234', )) self.assertEqual(io.combined, '')
def test_run_valid_hash_and_via(self, mock_launcher): """ verify what happens when `plainbox-trusted-launcher-1` is called with both --hash and --via that both are okay and designate existing jobs. """ # Create a mock (local) job, give it a predictable checksum local_job = mock.Mock( name='local_job', spec=JobDefinition, checksum='5678') # Create a mock (target) job, give it a predictable checksum target_job = mock.Mock( name='target_job', spec=JobDefinition, checksum='1234') # Ensure this local job is enumerated by the provider self.provider.get_builtin_jobs.return_value = [local_job] # Ensure that the target job is generated by the local job mock_launcher.run_local_job.return_value = [target_job] # Run the program with io intercept with TestIO(combined=True) as io: retval = main(['--target=1234', '--generator=5678']) # Ensure that the local job command was invoked mock_launcher().run_local_job.assert_called_with(local_job.checksum, None) # Ensure that the target job command was invoked mock_launcher().run_shell_from_job.assert_called_with( target_job.checksum, None) # Ensure that the return code is propagated self.assertEqual(retval, mock_launcher().run_shell_from_job()) # Ensure that we didn't print anything (we normally do but this is not # tested here since we mock that part away) self.assertEqual(io.combined, '')
def test_run_valid_hash_and_via(self, mock_launcher): """ verify what happens when `plainbox-trusted-launcher-1` is called with both --hash and --via that both are okay and designate existing jobs. """ # Create a mock (local) job, give it a predictable checksum local_job = mock.Mock(name='local_job', spec=JobDefinition, checksum='5678') # Create a mock (target) job, give it a predictable checksum target_job = mock.Mock(name='target_job', spec=JobDefinition, checksum='1234') # Ensure this local job is enumerated by the provider self.provider.get_builtin_jobs.return_value = [local_job] # Ensure that the target job is generated by the local job mock_launcher.run_local_job.return_value = [target_job] # Run the program with io intercept with TestIO(combined=True) as io: retval = main(['--target=1234', '--generator=5678']) # Ensure that the local job command was invoked mock_launcher().run_local_job.assert_called_with( local_job.checksum, None) # Ensure that the target job command was invoked mock_launcher().run_shell_from_job.assert_called_with( target_job.checksum, None) # Ensure that the return code is propagated self.assertEqual(retval, mock_launcher().run_shell_from_job()) # Ensure that we didn't print anything (we normally do but this is not # tested here since we mock that part away) self.assertEqual(io.combined, '')
def test_run_valid_hash(self, mock_launcher): """ verify what happens when `plainbox-trusted-launcher-1` is called with --hash that designates an existing job. """ # Create a mock job, give it a predictable checksum job = mock.Mock(name='job', spec=JobDefinition, checksum='1234') # Ensure this job is enumerated by the provider self.provider.get_builtin_jobs.return_value = [job] # Run the program with io intercept with TestIO(combined=True) as io: retval = main( ['--target=1234', '-T', 'key=value', '-T', 'other=value']) # Ensure that the job command was invoked # and that environment was properly parsed and provided mock_launcher().run_shell_from_job.assert_called_with( job.checksum, { 'key': 'value', 'other': 'value' }) # Ensure that the return code is propagated self.assertEqual(retval, mock_launcher().run_shell_from_job()) # Ensure that we didn't print anything (we normally do but this is not # tested here since we mock that part away) self.assertEqual(io.combined, '')
def test_run_invalid_generator_checksum(self): """ verify what happens when `plainbox-trusted-launcher-1` is called with a generator job checksum that cannot be found in any of the providers. """ # Ensure this there are no jobs that the launcher knows about self.provider.get_builtin_jobs.return_value = [] # Run the program with io intercept with TestIO(combined=True) as io: with self.assertRaises(SystemExit) as call: main(['--target=1234', '--generator=4567']) # Ensure that the error message contains the checksum of the via job self.assertEqual(call.exception.args, ( 'Cannot find job with checksum 4567',)) # Ensure that we didn't print anything (we normally do but this is not # tested here since we mock that part away) self.assertEqual(io.combined, '')
def test_run_without_args(self): """ verify what `plainbox-trusted-launcher-1` does """ # Run the program with io intercept with TestIO(combined=True) as io: with self.assertRaises(SystemExit) as call: main([]) self.assertEqual(call.exception.args, (2,)) expected = """ usage: plainbox-trusted-launcher-1 [-h] (-w | -t CHECKSUM) [-T NAME=VALUE [NAME=VALUE ...]] [-g CHECKSUM] [-G NAME=VALUE [NAME=VALUE ...]] plainbox-trusted-launcher-1: error: one of the arguments -w/--warmup -t/--target is required """ self.assertEqual(io.combined, cleandoc(expected) + "\n")
def test_run_invalid_generator_checksum(self): """ verify what happens when `plainbox-trusted-launcher-1` is called with a generator job checksum that cannot be found in any of the providers. """ # Ensure this there are no jobs that the launcher knows about self.provider.get_builtin_jobs.return_value = [] # Run the program with io intercept with TestIO(combined=True) as io: with self.assertRaises(SystemExit) as call: main(['--target=1234', '--generator=4567']) # Ensure that the error message contains the checksum of the via job self.assertEqual(call.exception.args, ('Cannot find job with checksum 4567', )) # Ensure that we didn't print anything (we normally do but this is not # tested here since we mock that part away) self.assertEqual(io.combined, '')
def test_run_without_args(self): """ verify what `plainbox-trusted-launcher-1` does """ # Run the program with io intercept with TestIO(combined=True) as io: with self.assertRaises(SystemExit) as call: main([]) self.assertEqual(call.exception.args, (2, )) expected = """ usage: plainbox-trusted-launcher-1 [-h] (-w | -t CHECKSUM) [-T NAME=VALUE [NAME=VALUE ...]] [-g CHECKSUM] [-G NAME=VALUE [NAME=VALUE ...]] plainbox-trusted-launcher-1: error: one of the arguments -w/--warmup -t/--target is required """ self.assertEqual(io.combined, cleandoc(expected) + "\n")
def test_run_invalid_env(self): """ verify what happens when `plainbox-trusted-launcher-1` is called with a checksum that cannot be found in any of the providers. """ # Run the program with io intercept with TestIO(combined=True) as io: with self.assertRaises(SystemExit) as call: main(['--target=1234', '-T', 'blarg']) # Ensure that we exit with an error code self.assertEqual(call.exception.args, (2,)) # Ensure that we print a meaningful error message expected = """ usage: plainbox-trusted-launcher-1 [-h] (-w | -t CHECKSUM) [-T NAME=VALUE [NAME=VALUE ...]] [-g CHECKSUM] [-G NAME=VALUE [NAME=VALUE ...]] plainbox-trusted-launcher-1: error: argument -T/--target-environment: expected NAME=VALUE """ self.assertEqual(io.combined, cleandoc(expected) + "\n")
def test_warmup(self): """ verify what `plainbox-trusted-launcher-1 --warmup` does """ # Run the program with io intercept with TestIO(combined=True) as io: retval = main(['--warmup']) # Ensure that it just returns 0 self.assertEqual(retval, 0) # Without printing anything self.assertEqual(io.combined, '')
def test_run_invalid_env(self): """ verify what happens when `plainbox-trusted-launcher-1` is called with a checksum that cannot be found in any of the providers. """ # Run the program with io intercept with TestIO(combined=True) as io: with self.assertRaises(SystemExit) as call: main(['--target=1234', '-T', 'blarg']) # Ensure that we exit with an error code self.assertEqual(call.exception.args, (2, )) # Ensure that we print a meaningful error message expected = """ usage: plainbox-trusted-launcher-1 [-h] (-w | -t CHECKSUM) [-T NAME=VALUE [NAME=VALUE ...]] [-g CHECKSUM] [-G NAME=VALUE [NAME=VALUE ...]] plainbox-trusted-launcher-1: error: argument -T/--target-environment: expected NAME=VALUE """ self.assertEqual(io.combined, cleandoc(expected) + "\n")
def test_run_valid_hash(self, mock_launcher): """ verify what happens when `plainbox-trusted-launcher-1` is called with --hash that designates an existing job. """ # Create a mock job, give it a predictable checksum job = mock.Mock(name='job', spec=JobDefinition, checksum='1234') # Ensure this job is enumerated by the provider self.provider.get_builtin_jobs.return_value = [job] # Run the program with io intercept with TestIO(combined=True) as io: retval = main([ '--target=1234', '-T', 'key=value', '-T', 'other=value']) # Ensure that the job command was invoked # and that environment was properly parsed and provided mock_launcher().run_shell_from_job.assert_called_with( job.checksum, {'key': 'value', 'other': 'value'}) # Ensure that the return code is propagated self.assertEqual(retval, mock_launcher().run_shell_from_job()) # Ensure that we didn't print anything (we normally do but this is not # tested here since we mock that part away) self.assertEqual(io.combined, '')