def test_pkg(self): ''' Test to execute a packaged state run ''' mock = MagicMock(side_effect=[False, True, True, True, True, True]) with patch.object(os.path, 'isfile', mock), \ patch('salt.modules.state.tarfile', MockTarFile), \ patch('salt.modules.state.json', MockJson()): self.assertEqual(state.pkg("/tmp/state_pkg.tgz", "", "md5"), {}) mock = MagicMock(side_effect=[False, 0, 0, 0, 0]) with patch.object(salt.utils, 'get_hash', mock): self.assertDictEqual(state.pkg("/tmp/state_pkg.tgz", "", "md5"), {}) self.assertDictEqual(state.pkg("/tmp/state_pkg.tgz", 0, "md5"), {}) MockTarFile.path = "" MockJson.flag = True with patch('salt.utils.fopen', mock_open()): self.assertListEqual(state.pkg("/tmp/state_pkg.tgz", 0, "md5"), [True]) MockTarFile.path = "" MockJson.flag = False with patch('salt.utils.fopen', mock_open()): self.assertTrue(state.pkg("/tmp/state_pkg.tgz", 0, "md5"))
def test_pkg(): """ Test to execute a packaged state run """ tar_file = os.sep + os.path.join("tmp", "state_pkg.tgz") mock = MagicMock( side_effect=[False, True, True, True, True, True, True, True, True, True, True] ) mock_json_loads_true = MagicMock(return_value=[True]) mock_json_loads_dictlist = MagicMock(return_value=[{"test": ""}]) with patch.object(os.path, "isfile", mock), patch( "salt.modules.state.tarfile", MockTarFile ), patch.object(salt.utils, "json", mock_json_loads_dictlist): assert state.pkg(tar_file, "", "md5") == {} mock = MagicMock(side_effect=[False, 0, 0, 0, 0]) with patch.object(salt.utils.hashutils, "get_hash", mock): # Verify hash assert state.pkg(tar_file, "", "md5") == {} # Verify file outside intended root assert state.pkg(tar_file, 0, "md5") == {} MockTarFile.path = "" with patch("salt.utils.files.fopen", mock_open()), patch.object( salt.utils.json, "loads", mock_json_loads_true ), patch.object(state, "_format_cached_grains", MagicMock()): assert state.pkg(tar_file, 0, "md5") is True state._format_cached_grains.assert_called_once() MockTarFile.path = "" with patch("salt.utils.files.fopen", mock_open()): assert state.pkg(tar_file, 0, "md5")
def test_pkg(self): ''' Test to execute a packaged state run ''' mock = MagicMock(side_effect=[False, True, True, True, True, True]) with patch.object(os.path, 'isfile', mock): self.assertEqual(state.pkg("/tmp/state_pkg.tgz", "", "md5"), {}) mock = MagicMock(side_effect=[False, 0, 0, 0, 0]) with patch.object(salt.utils, 'get_hash', mock): self.assertDictEqual(state.pkg("/tmp/state_pkg.tgz", "", "md5"), {}) self.assertDictEqual(state.pkg("/tmp/state_pkg.tgz", 0, "md5"), {}) MockTarFile.path = "" MockJson.flag = True with patch('salt.utils.fopen', mock_open()): self.assertListEqual(state.pkg("/tmp/state_pkg.tgz", 0, "md5"), [True]) MockTarFile.path = "" MockJson.flag = False with patch('salt.utils.fopen', mock_open()): self.assertTrue(state.pkg("/tmp/state_pkg.tgz", 0, "md5"))
def test_pkg(self): ''' Test to execute a packaged state run ''' tar_file = os.sep + os.path.join('tmp', 'state_pkg.tgz') mock = MagicMock(side_effect=[False, True, True, True, True, True]) with patch.object(os.path, 'isfile', mock), \ patch('salt.modules.state.tarfile', MockTarFile), \ patch('salt.modules.state.json', MockJson()): self.assertEqual(state.pkg(tar_file, "", "md5"), {}) mock = MagicMock(side_effect=[False, 0, 0, 0, 0]) with patch.object(salt.utils, 'get_hash', mock): # Verify hash self.assertDictEqual(state.pkg(tar_file, "", "md5"), {}) # Verify file outside intended root self.assertDictEqual(state.pkg(tar_file, 0, "md5"), {}) MockTarFile.path = "" MockJson.flag = True with patch('salt.utils.fopen', mock_open()): self.assertListEqual(state.pkg(tar_file, 0, "md5"), [True]) MockTarFile.path = "" MockJson.flag = False with patch('salt.utils.fopen', mock_open()): self.assertTrue(state.pkg(tar_file, 0, "md5"))
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_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')