diff --git a/salt/grains/core.py b/salt/grains/core.py index 04c1ae91b5f5..34547b46b4c7 100644 --- a/salt/grains/core.py +++ b/salt/grains/core.py @@ -2403,6 +2403,13 @@ def get_machine_id(): return {'machine_id': machineid.read().strip()} +def cwd(): + ''' + Current working directory + ''' + return {'cwd': os.getcwd()} + + def path(): ''' Return the path diff --git a/tests/unit/grains/test_core.py b/tests/unit/grains/test_core.py index 8c45ee194abd..2d3fac62d6a0 100644 --- a/tests/unit/grains/test_core.py +++ b/tests/unit/grains/test_core.py @@ -1241,3 +1241,25 @@ def test_locale_info_no_tz_tzname(self): is_proxy.assert_called_once_with() is_windows.assert_not_called() self.assertEqual(ret['locale_info']['timezone'], 'unknown') + + def test_cwd_exists(self): + cwd_grain = core.cwd() + + self.assertIsInstance(cwd_grain, dict) + self.assertTrue('cwd' in cwd_grain) + self.assertEqual(cwd_grain['cwd'], os.getcwd()) + + def test_cwd_is_cwd(self): + cwd = os.getcwd() + + try: + # change directory + new_dir = os.path.split(cwd)[0] + os.chdir(new_dir) + + cwd_grain = core.cwd() + + self.assertEqual(cwd_grain['cwd'], new_dir) + finally: + # change back to original directory + os.chdir(cwd)