def test_write(self): with patch('etcd.Client', autospec=True) as mock: client = etcd_util.EtcdClient({}) etcd_client = mock.return_value etcd_client.write.return_value = MagicMock(value='salt') self.assertEqual(client.write('/some-key', 'salt'), 'salt') etcd_client.write.assert_called_with('/some-key', 'salt', ttl=None, dir=False) self.assertEqual(client.write('/some-key', 'salt', ttl=5), 'salt') etcd_client.write.assert_called_with('/some-key', 'salt', ttl=5, dir=False) etcd_client.write.return_value = MagicMock(dir=True) self.assertEqual(client.write('/some-dir', 'salt', ttl=0, directory=True), True) etcd_client.write.assert_called_with('/some-dir', None, ttl=0, dir=True) etcd_client.write.side_effect = etcd.EtcdRootReadOnly() self.assertEqual(client.write('/', 'some-val'), None) etcd_client.write.side_effect = etcd.EtcdNotFile() self.assertEqual(client.write('/some-key', 'some-val'), None) etcd_client.write.side_effect = etcd.EtcdNotDir() self.assertEqual(client.write('/some-dir', 'some-val'), None) etcd_client.write.side_effect = MaxRetryError(None, None) self.assertEqual(client.write('/some-key', 'some-val'), None) etcd_client.write.side_effect = ValueError self.assertEqual(client.write('/some-key', 'some-val'), None) etcd_client.write.side_effect = Exception self.assertRaises(Exception, client.set, 'some-key', 'some-val')
def test_write(self): with patch("etcd.Client", autospec=True) as mock: client = etcd_util.EtcdClient({}) etcd_client = mock.return_value etcd_client.write.return_value = MagicMock(value="salt") self.assertEqual(client.write("/some-key", "salt"), "salt") etcd_client.write.assert_called_with( "/some-key", "salt", ttl=None, dir=False ) self.assertEqual(client.write("/some-key", "salt", ttl=5), "salt") etcd_client.write.assert_called_with("/some-key", "salt", ttl=5, dir=False) etcd_client.write.return_value = MagicMock(dir=True) self.assertEqual( client.write("/some-dir", "salt", ttl=0, directory=True), True ) etcd_client.write.assert_called_with("/some-dir", None, ttl=0, dir=True) # Check when a file is attempted to be written to a read-only root etcd_client.write.side_effect = etcd.EtcdRootReadOnly() self.assertEqual(client.write("/", "some-val", directory=False), None) # Check when a directory is attempted to be written to a read-only root etcd_client.write.side_effect = etcd.EtcdRootReadOnly() self.assertEqual(client.write("/", None, directory=True), None) # Check when a file is attempted to be written when unable to connect to the service etcd_client.write.side_effect = MaxRetryError(None, None) self.assertEqual( client.write("/some-key", "some-val", directory=False), None ) # Check when a directory is attempted to be written when unable to connect to the service etcd_client.write.side_effect = MaxRetryError(None, None) self.assertEqual(client.write("/some-dir", None, directory=True), None) # Check when a file is attempted to be written to a directory that already exists (name-collision) etcd_client.write.side_effect = etcd.EtcdNotFile() self.assertEqual( client.write("/some-dir", "some-val", directory=False), None ) # Check when a directory is attempted to be written to a file that already exists (name-collision) etcd_client.write.side_effect = etcd.EtcdNotDir() self.assertEqual(client.write("/some-key", None, directory=True), None) # Check when a directory is attempted to be written to a directory that already exists (update-ttl) etcd_client.write.side_effect = etcd.EtcdNotFile() self.assertEqual(client.write("/some-dir", None, directory=True), True) etcd_client.write.side_effect = ValueError self.assertEqual(client.write("/some-key", "some-val"), None) etcd_client.write.side_effect = Exception self.assertRaises(Exception, client.set, "some-key", "some-val")
def test_write(use_v2, client_name): with patch(client_name, autospec=True) as mock: etcd_client = mock.return_value client = etcd_util.get_conn( {"etcd.require_v2": use_v2, "etcd.encode_values": False} ) if use_v2: etcd_client.write.return_value = MagicMock(value="salt") assert client.write("/some-key", "salt") == "salt" etcd_client.write.assert_called_with( "/some-key", "salt", ttl=None, dir=False ) assert client.write("/some-key", "salt", ttl=5) == "salt" etcd_client.write.assert_called_with("/some-key", "salt", ttl=5, dir=False) etcd_client.write.return_value = MagicMock(dir=True) assert client.write("/some-dir", "salt", ttl=0, directory=True) etcd_client.write.assert_called_with("/some-dir", None, ttl=0, dir=True) # Check when a file is attempted to be written to a read-only root etcd_client.write.side_effect = etcd.EtcdRootReadOnly() assert client.write("/", "some-val", directory=False) is None # Check when a directory is attempted to be written to a read-only root etcd_client.write.side_effect = etcd.EtcdRootReadOnly() assert client.write("/", None, directory=True) is None # Check when a file is attempted to be written when unable to connect to the service etcd_client.write.side_effect = MaxRetryError(None, None) assert client.write("/some-key", "some-val", directory=False) is None # Check when a directory is attempted to be written when unable to connect to the service etcd_client.write.side_effect = MaxRetryError(None, None) assert client.write("/some-dir", None, directory=True) is None # Check when a file is attempted to be written to a directory that already exists (name-collision) etcd_client.write.side_effect = etcd.EtcdNotFile() assert client.write("/some-dir", "some-val", directory=False) is None # Check when a directory is attempted to be written to a file that already exists (name-collision) etcd_client.write.side_effect = etcd.EtcdNotDir() assert client.write("/some-key", None, directory=True) is None # Check when a directory is attempted to be written to a directory that already exists (update-ttl) etcd_client.write.side_effect = etcd.EtcdNotFile() assert client.write("/some-dir", None, directory=True) etcd_client.write.side_effect = ValueError assert client.write("/some-key", "some-val") is None etcd_client.write.side_effect = Exception with pytest.raises(Exception): client.set("some-key", "some-val") else: with pytest.raises(etcd_util.Etcd3DirectoryException): client.write("key", None, directory=True) with patch.object(client, "get", autospec=True) as get_mock: get_mock.return_value = "stack" assert client.write("salt", "stack") == "stack" etcd_client.put.assert_called_with("salt", "stack") lease_mock = MagicMock(ID=1) with patch.object(etcd_client, "Lease", return_value=lease_mock): assert client.write("salt", "stack", ttl=5) == "stack" etcd_client.put.assert_called_with("salt", "stack", lease=1)