def test_exists_already(): """ Test with the same value does nothing """ with patch("os.path.exists") as exists_mock: expected = { "changes": {}, "comment": "All values existed correctly.", "name": "/path/to/file", "result": True, } exists_mock.return_value = True list_mock = MagicMock(return_value={"key": "value"}) write_mock = MagicMock() with patch.dict(xattr.__salt__, { "xattr.list": list_mock, "xattr.write": write_mock }): out = xattr.exists("/path/to/file", ["key=value"]) list_mock.assert_called_once_with("/path/to/file") assert not write_mock.called assert out == expected
def test_exists_already(self): ''' Test that having the same value does nothing ''' with patch('os.path.exists') as exists_mock: expected = { 'changes': {}, 'comment': 'All values existed correctly.', 'name': '/path/to/file', 'result': True } exists_mock.return_value = True list_mock = MagicMock(return_value={'key': 'value'}) write_mock = MagicMock() with patch.dict(xattr.__salt__, { 'xattr.list': list_mock, 'xattr.write': write_mock }): out = xattr.exists('/path/to/file', ['key=value']) list_mock.assert_called_once_with('/path/to/file') assert not write_mock.called self.assertEqual(out, expected)
def test_exists_change(): """ Test changing an attribute value """ with patch("os.path.exists") as exists_mock: expected = { "changes": { "key": "other_value" }, "comment": "", "name": "/path/to/file", "result": True, } exists_mock.return_value = True list_mock = MagicMock(return_value={"key": "value"}) write_mock = MagicMock() with patch.dict(xattr.__salt__, { "xattr.list": list_mock, "xattr.write": write_mock }): out = xattr.exists("/path/to/file", ["key=other_value"]) list_mock.assert_called_once_with("/path/to/file") write_mock.assert_called_once_with("/path/to/file", "key", "other_value", False) assert out == expected
def test_exists_change(self): ''' Test changing and attribute value ''' with patch('os.path.exists') as exists_mock: expected = { 'changes': { 'key': 'other_value' }, 'comment': '', 'name': '/path/to/file', 'result': True } exists_mock.return_value = True list_mock = MagicMock(return_value={'key': 'value'}) write_mock = MagicMock() with patch.dict(xattr.__salt__, { 'xattr.list': list_mock, 'xattr.write': write_mock }): out = xattr.exists('/path/to/file', ['key=other_value']) list_mock.assert_called_once_with('/path/to/file') write_mock.assert_called_once_with('/path/to/file', 'key', 'other_value', False) self.assertEqual(out, expected)
def test_exists_not(self, exists_mock): ''' Test adding an attribute when it doesn't exist ''' expected = { 'changes': { 'key': 'value' }, 'comment': '', 'name': '/path/to/file', 'result': True } exists_mock.return_value = True list_mock = MagicMock(return_value={'other.id': 'value2'}) write_mock = MagicMock() with patch.dict(xattr.__salt__, { 'xattr.list': list_mock, 'xattr.write': write_mock }): out = xattr.exists('/path/to/file', ['key=value']) list_mock.assert_called_once_with('/path/to/file') write_mock.assert_called_once_with('/path/to/file', 'key', 'value', False) self.assertEqual(out, expected)
def test_exists_not(self): """ Test adding an attribute when it doesn't exist """ with patch("os.path.exists") as exists_mock: expected = { "changes": { "key": "value" }, "comment": "", "name": "/path/to/file", "result": True, } exists_mock.return_value = True list_mock = MagicMock(return_value={"other.id": "value2"}) write_mock = MagicMock() with patch.dict(xattr.__salt__, { "xattr.list": list_mock, "xattr.write": write_mock }): out = xattr.exists("/path/to/file", ["key=value"]) list_mock.assert_called_once_with("/path/to/file") write_mock.assert_called_once_with("/path/to/file", "key", "value", False) self.assertEqual(out, expected)
def test_exists_already(self, exists_mock): ''' Test that having the same value does nothing ''' expected = { 'changes': {}, 'comment': 'All values existed correctly.', 'name': '/path/to/file', 'result': True } exists_mock.return_value = True list_mock = MagicMock(return_value={'key': 'value'}) write_mock = MagicMock() with patch.dict(xattr.__salt__, {'xattr.list': list_mock, 'xattr.write': write_mock}): out = xattr.exists('/path/to/file', ['key=value']) list_mock.assert_called_once_with('/path/to/file') assert not write_mock.called self.assertEqual(out, expected)
def test_exists_change(self, exists_mock): ''' Test changing and attribute value ''' expected = { 'changes': {'key': 'other_value'}, 'comment': '', 'name': '/path/to/file', 'result': True } exists_mock.return_value = True list_mock = MagicMock(return_value={'key': 'value'}) write_mock = MagicMock() with patch.dict(xattr.__salt__, {'xattr.list': list_mock, 'xattr.write': write_mock}): out = xattr.exists('/path/to/file', ['key=other_value']) list_mock.assert_called_once_with('/path/to/file') write_mock.assert_called_once_with('/path/to/file', 'key', 'other_value', False) self.assertEqual(out, expected)