|
16 | 16 | # You should have received a copy of the GNU General Public License |
17 | 17 | # along with Checkbox. If not, see <http://www.gnu.org/licenses/>. |
18 | 18 |
|
19 | | -import os |
20 | 19 | import contextlib |
| 20 | +import os |
21 | 21 | from pathlib import Path |
22 | 22 | from unittest import TestCase, mock |
23 | 23 |
|
|
28 | 28 | dangerous_nsenter, |
29 | 29 | get_execution_command_subshell, |
30 | 30 | get_execution_command_systemd_unit, |
| 31 | + get_execution_environment, |
31 | 32 | ) |
32 | 33 | from plainbox.impl.unit.job import InvalidJob |
33 | 34 |
|
@@ -459,3 +460,63 @@ def test_present(self): |
459 | 460 | self.assertIn("og_path2", new_path) |
460 | 461 | self.assertIn("some", new_path) |
461 | 462 | self.assertIn("path", new_path) |
| 463 | + |
| 464 | + |
| 465 | +class TestGetExecutionEnvironment(TestCase): |
| 466 | + def setUp(self): |
| 467 | + self.job = mock.Mock() |
| 468 | + self.job.get_flag_set.return_value = set() |
| 469 | + self.job.provider.gettext_domain = None |
| 470 | + self.job.provider.locale_dir = None |
| 471 | + self.job.provider.extra_PYTHONPATH = [] |
| 472 | + self.job.provider.extra_PATH = [] |
| 473 | + self.job.provider.extra_LD_LIBRARY_PATH = [] |
| 474 | + self.job.provider.extra_snap_environment = {} |
| 475 | + self.job.provider.data_dir = None |
| 476 | + self.job.provider.units_dir = None |
| 477 | + self.job.provider.CHECKBOX_SHARE = None |
| 478 | + |
| 479 | + @mock.patch.dict(os.environ, {"ORIGINAL_ENV": "value"}, clear=True) |
| 480 | + @mock.patch("plainbox.impl.execution.WellKnownDirsHelper") |
| 481 | + def test_basic_environment(self, mock_well_known): |
| 482 | + mock_well_known.session_share.return_value = "/session/share" |
| 483 | + |
| 484 | + env = get_execution_environment( |
| 485 | + self.job, None, "test_session", "/nest" |
| 486 | + ) |
| 487 | + |
| 488 | + self.assertIn("ORIGINAL_ENV", env) |
| 489 | + self.assertEqual(env["ORIGINAL_ENV"], "value") |
| 490 | + self.assertEqual(env["PLAINBOX_SESSION_SHARE"], "/session/share") |
| 491 | + self.assertIn("/nest", env["PATH"]) |
| 492 | + mock_well_known.session_share.assert_called_once_with("test_session") |
| 493 | + |
| 494 | + @mock.patch.dict(os.environ, {"SNAP": "/snap/checkbox24"}, clear=True) |
| 495 | + @mock.patch("plainbox.impl.execution.get_checkbox_runtime_path") |
| 496 | + @mock.patch("plainbox.impl.execution.WellKnownDirsHelper") |
| 497 | + def test_checkbox_runtime_in_snap( |
| 498 | + self, mock_well_known, mock_runtime_path |
| 499 | + ): |
| 500 | + mock_well_known.session_share.return_value = "/session/share" |
| 501 | + mock_runtime_path.return_value = Path("/snap/checkbox24/current") |
| 502 | + |
| 503 | + env = get_execution_environment( |
| 504 | + self.job, None, "test_session", "/nest" |
| 505 | + ) |
| 506 | + |
| 507 | + self.assertEqual(env["CHECKBOX_RUNTIME"], "/snap/checkbox24/current") |
| 508 | + |
| 509 | + @mock.patch.dict( |
| 510 | + os.environ, {"EXISTING_VAR": "original_value"}, clear=True |
| 511 | + ) |
| 512 | + @mock.patch("plainbox.impl.execution.WellKnownDirsHelper") |
| 513 | + def test_environ_dict_does_not_override_existing(self, mock_well_known): |
| 514 | + mock_well_known.session_share.return_value = "/session/share" |
| 515 | + |
| 516 | + environ = {"EXISTING_VAR": "new_value"} |
| 517 | + |
| 518 | + env = get_execution_environment( |
| 519 | + self.job, environ, "test_session", "/nest" |
| 520 | + ) |
| 521 | + |
| 522 | + self.assertEqual(env["EXISTING_VAR"], "original_value") |
0 commit comments