def sub_test_sls(self): ''' Sub function of test_sls ''' mock = MagicMock(return_value=True) with patch.object(os.path, 'join', mock): with patch.object(os, 'umask', mock): mock = MagicMock(return_value=False) with patch.object(salt.utils, 'is_windows', mock): mock = MagicMock(return_value=True) with patch.object(os, 'umask', mock): with patch.object(state, '_set_retcode', mock): with patch.dict(state.__opts__, {"test": True}): with patch('salt.utils.fopen', mock_open()): self.assertTrue( state.sls("core,edit" ".vim dev", None, None, None, True))
def sub_test_sls(self): ''' Sub function of test_sls ''' mock = MagicMock(return_value=True) with patch.object(os.path, 'join', mock): with patch.object(os, 'umask', mock): mock = MagicMock(return_value=False) with patch.object(salt.utils, 'is_windows', mock): mock = MagicMock(return_value=True) with patch.object(os, 'umask', mock): with patch.object(state, '_set_retcode', mock): with patch.dict(state.__opts__, {"test": True}): with patch('salt.utils.fopen', mock_open()): self.assertTrue(state.sls("core,edit" ".vim dev", None, None, None, True))
def test_sls(self): ''' Test to execute a set list of state files from an environment ''' arg = "core,edit.vim dev" ret = ['Pillar failed to render with the following messages:', 'E', '1'] mock = MagicMock(return_value=True) with patch.object(state, 'running', mock): with patch.dict(state.__context__, {"retcode": 1}): self.assertEqual(state.sls("core,edit.vim dev"), True) mock = MagicMock(side_effect=[True, True, True, True, True, True]) with patch.object(state, '_wait', mock): mock = MagicMock(side_effect=[["A"], [], [], [], [], []]) with patch.object(state, '_disabled', mock): with patch.dict(state.__context__, {"retcode": 1}): self.assertEqual( state.sls("core,edit.vim dev", None, None, None, True), ["A"]) mock = MagicMock(side_effect=[False, True, True, True, True]) with patch.object(state, '_check_pillar', mock): with patch.dict(state.__context__, {"retcode": 5}): with patch.dict(state.__pillar__, {"_errors": "E1"}): self.assertListEqual(state.sls("core,edit.vim dev", None, None, None, True), ret) with patch.dict(state.__opts__, {"test": None}): mock = MagicMock(return_value={"test": ""}) with patch.object(state, '_get_opts', mock): mock = MagicMock(return_value=True) with patch.object(salt.utils, 'test_mode', mock): self.assertRaises( SaltInvocationError, state.sls, "core,edit.vim dev", None, None, None, True, pillar="A") mock = MagicMock(return_value="/D/cache.cache.p") with patch.object(os.path, 'join', mock): mock = MagicMock(return_value=True) with patch.object(os.path, 'isfile', mock): with patch( 'salt.utils.fopen', mock_open()): self.assertTrue( state.sls(arg, None, None, None, True, cache =True ) ) MockState.HighState.flag = True self.assertTrue(state.sls("core,edit" ".vim dev", None, None, None, True) ) MockState.HighState.flag = False mock = MagicMock(return_value=True) with patch.dict(state.__salt__, {'config.option': mock}): mock = MagicMock(return_value= True) with patch.object( state, '_filter_' 'running', mock): self.sub_test_sls()
def test_sls(): """ Test to execute a set list of state files from an environment """ arg = "core,edit.vim dev" ret = ["Pillar failed to render with the following messages:", "E", "1"] with patch.object(state, "running", return_value=True): with patch.dict(state.__context__, {"retcode": 1}): assert state.sls("core,edit.vim dev") is True with patch.object( state, "_wait", side_effect=[True, True, True, True, True, True] ), patch.object(state, "_disabled", side_effect=[["A"], [], [], [], [], []]): with patch.dict(state.__context__, {"retcode": 1}): assert state.sls("core,edit.vim dev", None, None, True) == ["A"] with patch.object( state, "_get_pillar_errors", side_effect=[["E", "1"], None, None, None, None], ): with patch.dict(state.__context__, {"retcode": 5}), patch.dict( state.__pillar__, {"_errors": ["E", "1"]} ): assert state.sls("core,edit.vim dev", None, None, True) == ret with patch.dict(state.__opts__, {"test": None}), patch.object( salt.utils.state, "get_sls_opts", return_value={"test": "", "saltenv": None}, ), patch.object(salt.utils.args, "test_mode", return_value=True): pytest.raises( SaltInvocationError, state.sls, "core,edit.vim dev", None, None, True, pillar="A", ) with patch.object(os.path, "join", return_value="/D/cache.cache.p"): with patch.object(os.path, "isfile", return_value=True), patch( "salt.utils.files.fopen", mock_open(b"") ): assert state.sls(arg, None, None, True, cache=True) MockState.HighState.flag = True assert state.sls("core,edit" ".vim dev", None, None, True) MockState.HighState.flag = False with patch.object( state, "_filter_" "running", return_value=True ), patch.object(os.path, "join", return_value=True), patch.object( os, "umask", return_value=True ), patch.object( salt.utils.platform, "is_windows", return_value=False ), patch.object( state, "_set_retcode", return_value=True ), patch.dict( state.__opts__, {"test": True} ), patch( "salt.utils.files.fopen", mock_open() ): assert state.sls("core,edit" ".vim dev", None, None, True)
def test_lock_saltenv(): """ Tests lock_saltenv in each function which accepts saltenv on the CLI """ lock_msg = "lock_saltenv is enabled, saltenv cannot be changed" empty_list_mock = MagicMock(return_value=[]) with patch.dict(state.__opts__, {"lock_saltenv": True}), patch.dict( state.__salt__, {"grains.get": empty_list_mock} ), patch.object(state, "running", empty_list_mock): # Test high with pytest.raises(CommandExecutionError, match=lock_msg): state.high([{"vim": {"pkg": ["installed"]}}], saltenv="base") # Test template with pytest.raises(CommandExecutionError, match=lock_msg): state.template("foo", saltenv="base") # Test template_str with pytest.raises(CommandExecutionError, match=lock_msg): state.template_str("foo", saltenv="base") # Test apply_ with SLS with pytest.raises(CommandExecutionError, match=lock_msg): state.apply_("foo", saltenv="base") # Test apply_ with Highstate with pytest.raises(CommandExecutionError, match=lock_msg): state.apply_(saltenv="base") # Test "test" with SLS with pytest.raises(CommandExecutionError, match=lock_msg): state.test("foo", saltenv="base") # Test "test" with Highstate with pytest.raises(CommandExecutionError, match=lock_msg): state.test(saltenv="base") # Test highstate with pytest.raises(CommandExecutionError, match=lock_msg): state.highstate(saltenv="base") # Test sls with pytest.raises(CommandExecutionError, match=lock_msg): state.sls("foo", saltenv="base") # Test top with pytest.raises(CommandExecutionError, match=lock_msg): state.top("foo.sls", saltenv="base") # Test show_highstate with pytest.raises(CommandExecutionError, match=lock_msg): state.show_highstate(saltenv="base") # Test show_lowstate with pytest.raises(CommandExecutionError, match=lock_msg): state.show_lowstate(saltenv="base") # Test sls_id with pytest.raises(CommandExecutionError, match=lock_msg): state.sls_id("foo", "bar", saltenv="base") # Test show_low_sls with pytest.raises(CommandExecutionError, match=lock_msg): state.show_low_sls("foo", saltenv="base") # Test show_sls with pytest.raises(CommandExecutionError, match=lock_msg): state.show_sls("foo", saltenv="base") # Test show_top with pytest.raises(CommandExecutionError, match=lock_msg): state.show_top(saltenv="base") # Test single with pytest.raises(CommandExecutionError, match=lock_msg): state.single("foo.bar", name="baz", saltenv="base") # Test pkg with pytest.raises(CommandExecutionError, match=lock_msg): state.pkg( "/tmp/salt_state.tgz", "760a9353810e36f6d81416366fc426dc", "md5", saltenv="base", )
def test_sls_sync(subtests): """ Test test.sls with the sync argument We're only mocking the sync functions we expect to sync. If any other sync functions are run then they will raise a KeyError, which we want as it will tell us that we are syncing things we shouldn't. """ expected_err_msg = "{} called {} time(s) (expected: {})" mock_empty_list = MagicMock(return_value=[]) with patch.object(state, "running", mock_empty_list), patch.object( state, "_disabled", mock_empty_list ), patch.object(state, "_get_pillar_errors", mock_empty_list): with subtests.test("sync_mods=modules,states"): sync_mocks = { "saltutil.sync_modules": Mock(), "saltutil.sync_states": Mock(), } if salt.utils.platform.is_windows(): sync_mocks["cmd.run"] = Mock() with patch.dict(state.__salt__, sync_mocks): state.sls("foo", sync_mods="modules,states") for key in sync_mocks: call_count = sync_mocks[key].call_count expected = 1 assert call_count == expected, expected_err_msg.format( key, call_count, expected ) with subtests.test("sync_mods=all"): # Test syncing all sync_mocks = {"saltutil.sync_all": Mock()} if salt.utils.platform.is_windows(): sync_mocks["cmd.run"] = Mock() with patch.dict(state.__salt__, sync_mocks): state.sls("foo", sync_mods="all") for key in sync_mocks: call_count = sync_mocks[key].call_count expected = 1 assert call_count == expected, expected_err_msg.format( key, call_count, expected ) with subtests.test("sync_mods=True"): # sync_mods=True should be interpreted as sync_mods=all sync_mocks = {"saltutil.sync_all": Mock()} if salt.utils.platform.is_windows(): sync_mocks["cmd.run"] = Mock() with patch.dict(state.__salt__, sync_mocks): state.sls("foo", sync_mods=True) for key in sync_mocks: call_count = sync_mocks[key].call_count expected = 1 assert call_count == expected, expected_err_msg.format( key, call_count, expected ) with subtests.test("sync_mods=modules,all"): # Test syncing all when "all" is passed along with module types. # This tests that we *only* run a sync_all and avoid unnecessary # extra syncing. sync_mocks = {"saltutil.sync_all": Mock()} if salt.utils.platform.is_windows(): sync_mocks["cmd.run"] = Mock() with patch.dict(state.__salt__, sync_mocks): state.sls("foo", sync_mods="modules,all") for key in sync_mocks: call_count = sync_mocks[key].call_count expected = 1 assert call_count == expected, expected_err_msg.format( key, call_count, expected )
def test_sls(self): ''' Test to execute a set list of state files from an environment ''' arg = "core,edit.vim dev" ret = ['Pillar failed to render with the following messages:', 'E', '1'] mock = MagicMock(return_value=True) with patch.object(state, 'running', mock): with patch.dict(state.__context__, {"retcode": 1}): self.assertEqual(state.sls("core,edit.vim dev"), True) mock = MagicMock(side_effect=[True, True, True, True, True, True]) with patch.object(state, '_wait', mock): mock = MagicMock(side_effect=[["A"], [], [], [], [], []]) with patch.object(state, '_disabled', mock): with patch.dict(state.__context__, {"retcode": 1}): self.assertEqual( state.sls("core,edit.vim dev", None, None, None, True), ["A"]) mock = MagicMock(side_effect=[False, True, True, True, True]) with patch.object(state, '_check_pillar', mock): with patch.dict(state.__context__, {"retcode": 5}): with patch.dict(state.__pillar__, {"_errors": "E1"}): self.assertListEqual(state.sls("core,edit.vim dev", None, None, None, True), ret) with patch.dict(state.__opts__, {"test": None}): mock = MagicMock(return_value={"test": ""}) with patch.object(state, '_get_opts', mock): mock = MagicMock(return_value=True) with patch.object(salt.utils, 'test_mode', mock): self.assertRaises( SaltInvocationError, state.sls, "core,edit.vim dev", None, None, None, True, pillar="A") with patch.dict( state.__opts__, {"cachedir": "/D/"}): mock = MagicMock(return_value= "/D/cache.cache.p") with patch.object(os.path, 'join', mock): mock = MagicMock(return_value=True) with patch.object(os.path, 'isfile', mock): with patch( 'salt.utils.fopen', mock_open()): self.assertTrue( state.sls(arg, None, None, None, True, cache =True ) ) MockState.HighState.flag = True self.assertTrue(state.sls("core,edit" ".vim dev", None, None, None, True) ) MockState.HighState.flag = False mock = MagicMock(return_value=True) with patch.dict(state.__salt__, {'config.option': mock}): mock = MagicMock(return_value= True) with patch.object( state, '_filter_' 'running', mock): with patch.dict( state. __opts__, {"cachedir": "/D/"}): self.sub_test_sls()
def test_lock_saltenv(self): ''' Tests lock_saltenv in each function which accepts saltenv on the CLI ''' lock_msg = 'lock_saltenv is enabled, saltenv cannot be changed' empty_list_mock = MagicMock(return_value=[]) with patch.dict(state.__opts__, {'lock_saltenv': True}), \ patch.dict(state.__salt__, {'grains.get': empty_list_mock}), \ patch.object(state, 'running', empty_list_mock): # Test high with self.assertRaisesRegex(CommandExecutionError, lock_msg): state.high( [{"vim": {"pkg": ["installed"]}}], saltenv='base') # Test template with self.assertRaisesRegex(CommandExecutionError, lock_msg): state.template('foo', saltenv='base') # Test template_str with self.assertRaisesRegex(CommandExecutionError, lock_msg): state.template_str('foo', saltenv='base') # Test apply_ with SLS with self.assertRaisesRegex(CommandExecutionError, lock_msg): state.apply_('foo', saltenv='base') # Test apply_ with Highstate with self.assertRaisesRegex(CommandExecutionError, lock_msg): state.apply_(saltenv='base') # Test highstate with self.assertRaisesRegex(CommandExecutionError, lock_msg): state.highstate(saltenv='base') # Test sls with self.assertRaisesRegex(CommandExecutionError, lock_msg): state.sls('foo', saltenv='base') # Test top with self.assertRaisesRegex(CommandExecutionError, lock_msg): state.top('foo.sls', saltenv='base') # Test show_highstate with self.assertRaisesRegex(CommandExecutionError, lock_msg): state.show_highstate(saltenv='base') # Test show_lowstate with self.assertRaisesRegex(CommandExecutionError, lock_msg): state.show_lowstate(saltenv='base') # Test sls_id with self.assertRaisesRegex(CommandExecutionError, lock_msg): state.sls_id('foo', 'bar', saltenv='base') # Test show_low_sls with self.assertRaisesRegex(CommandExecutionError, lock_msg): state.show_low_sls('foo', saltenv='base') # Test show_sls with self.assertRaisesRegex(CommandExecutionError, lock_msg): state.show_sls('foo', saltenv='base') # Test show_top with self.assertRaisesRegex(CommandExecutionError, lock_msg): state.show_top(saltenv='base') # Test single with self.assertRaisesRegex(CommandExecutionError, lock_msg): state.single('foo.bar', name='baz', saltenv='base') # Test pkg with self.assertRaisesRegex(CommandExecutionError, lock_msg): state.pkg( '/tmp/salt_state.tgz', '760a9353810e36f6d81416366fc426dc', 'md5', saltenv='base')
def test_sls_sync(self): ''' Test test.sls with the sync argument We're only mocking the sync functions we expect to sync. If any other sync functions are run then they will raise a KeyError, which we want as it will tell us that we are syncing things we shouldn't. ''' mock_empty_list = MagicMock(return_value=[]) with patch.object(state, 'running', mock_empty_list), \ patch.object(state, '_disabled', mock_empty_list), \ patch.object(state, '_get_pillar_errors', mock_empty_list): sync_mocks = { 'saltutil.sync_modules': Mock(), 'saltutil.sync_states': Mock(), } with patch.dict(state.__salt__, sync_mocks): state.sls('foo', sync_mods='modules,states') for key in sync_mocks: call_count = sync_mocks[key].call_count expected = 1 assert call_count == expected, \ '{0} called {1} time(s) (expected: {2})'.format( key, call_count, expected ) # Test syncing all sync_mocks = {'saltutil.sync_all': Mock()} with patch.dict(state.__salt__, sync_mocks): state.sls('foo', sync_mods='all') for key in sync_mocks: call_count = sync_mocks[key].call_count expected = 1 assert call_count == expected, \ '{0} called {1} time(s) (expected: {2})'.format( key, call_count, expected ) # sync_mods=True should be interpreted as sync_mods=all sync_mocks = {'saltutil.sync_all': Mock()} with patch.dict(state.__salt__, sync_mocks): state.sls('foo', sync_mods=True) for key in sync_mocks: call_count = sync_mocks[key].call_count expected = 1 assert call_count == expected, \ '{0} called {1} time(s) (expected: {2})'.format( key, call_count, expected ) # Test syncing all when "all" is passed along with module types. # This tests that we *only* run a sync_all and avoid unnecessary # extra syncing. sync_mocks = {'saltutil.sync_all': Mock()} with patch.dict(state.__salt__, sync_mocks): state.sls('foo', sync_mods='modules,all') for key in sync_mocks: call_count = sync_mocks[key].call_count expected = 1 assert call_count == expected, \ '{0} called {1} time(s) (expected: {2})'.format( key, call_count, expected )