def test_node_not_found_exception(payload): response = mock.Mock() response.content = payload response.raise_for_status.side_effect = HTTPError('404 error', response=response) with pytest.raises(EtcdKeyNotFound): EtcdResult(response)
def test_get_active_read_locks(mock_read, payload, active_read_locks, cursor): response = mock.MagicMock() response.content = payload etcd_result = EtcdResult(response) mock_read.return_value = etcd_result assert cursor._get_active_read_locks('foo', 'bar') == active_read_locks
def test_etcd_index(headers): response = mock.Mock() response.content = '{"action":"get","node":{"key":"/foo","value":"bar","modifiedIndex":7,"createdIndex":7}}' response.headers = headers # noinspection PyTypeChecker res = EtcdResult(response) assert res.x_etcd_index == 2007
def test_version(): payload = '{"etcdserver":"2.3.7","etcdcluster":"2.3.0"}' response = mock.Mock() response.content = payload res = EtcdResult(response) assert res.version_etcdcluster == "2.3.0" assert res.version_etcdserver == "2.3.7"
def test_show_tables(content, rows): response = mock.Mock() response.content = content etcd_response = EtcdResult(response) etcd_client = mock.Mock() etcd_client.read.return_value = etcd_response cols = ColumnSet() cols.add(Column('Tables_in_foo')) # print(cols) rs = ResultSet(cols, rows) tree = SQLTree() tree.db = 'foo' tree.options['full'] = False # noinspection PyTypeChecker result = show_tables(etcd_client, tree, tree.db) print("Expected: \n%s" % rs) print("Actual: \n%s" % result) # noinspection PyTypeChecker assert result == rs
def test_get_pks_returns_pks(mock_get_pk, mock_read, db, tbl, pk, payload, result, cursor): response = mock.MagicMock() response.content = payload etcd_result = EtcdResult(response) mock_read.return_value = etcd_result mock_get_pk.return_value = pk assert cursor._get_pks(db, tbl) == result
def test_show_tables(mock_client, db, payload, result, cursor): response = mock.MagicMock() response.content = payload etcd_result = EtcdResult(response) tree = SQLTree() tree.db = db tree.options['full'] = False mock_client.return_value = etcd_result assert cursor._execute_show_tables(tree) == result mock_client.assert_called_once_with('/%s' % db)
def test_select_returns_records(mock_release_read_lock, mock_get_read_lock, mock_read, payload, result, cursor): cursor._get_pks = mock.Mock() cursor._get_pks.return_value = ['10'] response = mock.MagicMock() response.content = payload etcd_result = EtcdResult(response) mock_read.return_value = etcd_result cursor.execute("SELECT id from bar") assert cursor.fetchone() == result
def test_show_full_tables(mock_client, payload, result, etcdb_connection): cursor = etcdb_connection.cursor() assert cursor._db == 'foo' response = mock.MagicMock() response.content = payload etcd_result = EtcdResult(response) tree = SQLTree() tree.db = cursor._db tree.options['full'] = True mock_client.return_value = etcd_result assert cursor._execute_show_tables(tree) == result mock_client.assert_called_once_with('/foo')
def test_count_star(mock_read, mock_get_pks, mock_release_read_lock, mock_get_read_lock, payload, rowcount, cursor): response = mock.MagicMock() response.content = payload etcd_result = EtcdResult(response) mock_read.return_value = etcd_result mock_get_pks.return_value = [] query = "SELECT COUNT(*) AS `__count` FROM `foo_config`" assert cursor.execute(query) == 1 assert cursor.fetchone()[0] == rowcount mock_read.assert_called_once_with('/foo/foo_config')
def test_select_from_django_migrations(mock_get_read_lock, mock_release_read_lock, mock_read, payload, result, cursor): cursor._get_pks = mock.Mock() cursor._get_pks.return_value = ['10'] response = mock.MagicMock() response.content = payload etcd_result = EtcdResult(response) mock_read.return_value = etcd_result query = "SELECT `django_migrations`.`app`, `django_migrations`.`name` FROM `django_migrations`" cursor.execute(query) assert cursor._rows == result
def test_selfstats(payload_self): response = mock.Mock() response.content = payload_self res = EtcdResult(response) assert res.id == "ce2a822cea30bfca" assert res.leaderInfo == { "leader": "ce2a822cea30bfca", "startTime": "2016-09-19T06:08:51.937661067Z", "uptime": "17h5m58.934381551s" } assert res.name == "default" assert res.recvAppendRequestCnt == 0 assert res.sendAppendRequestCnt == 0 assert res.startTime == "2016-09-19T06:08:51.527241706Z" assert res.state == "StateLeader"
def _request_call(self, uri, method='get', wait=False, **kwargs): if self._allow_reconnect: urls = self._urls else: urls = [self._urls[0]] error_messages = [] for u in urls: try: url = u + uri return EtcdResult(getattr(requests, method)(url, **kwargs)) except RequestException as err: error_messages.append("%s: %s" % (u, err)) raise EtcdException('No more hosts to connect.\nErrors: %s' % '\n'.join(error_messages))
def test_show_tables(mock_client, query, db, payload, result, cursor): response = mock.Mock() response.content = payload etcd_result = EtcdResult(response) mock_client.return_value = etcd_result cursor.execute("USE %s" % db) cursor.execute(query) rows = () while True: row = cursor.fetchone() if row: assert isinstance(row, tuple) rows += (row, ) else: break assert result == rows
def test_get_meta_lock(mock_cas, mock_update_ttl, cursor): payload = '{"action":"set","node":{"key":"/foo/bar/_lock_meta","value":"","modifiedIndex":625,"createdIndex":625}}' response = mock.MagicMock() response.content = payload etcd_result = EtcdResult(response) mock_cas.return_value = etcd_result mock_update_ttl.side_effect = EtcdKeyNotFound response = cursor._get_meta_lock('foo', 'bar') mock_cas.assert_called_once_with('/foo/bar/_lock_meta', '', ttl=1, prev_exist=False) assert response.node['key'] == '/foo/bar/_lock_meta' assert response.node['value'] == ''
def test_get_pks_with_limit(mock_get_pk, mock_read, payload, limit, result, cursor): response = mock.MagicMock() response.content = payload etcd_result = EtcdResult(response) mock_read.return_value = etcd_result tree = SQLTree() tree.limit = limit mock_get_pk.return_value = { 'id': { 'type': 'INT', 'options': { 'nullable': False, 'primary': True } } } assert cursor._get_pks('foo', 'bar', tree=tree) == result
def test_show_databases(content, rows): response = mock.Mock() response.content = content etcd_response = EtcdResult(response) etcd_client = mock.Mock() etcd_client.read.return_value = etcd_response cols = ColumnSet() cols.add(Column('Database')) # print(cols) rs = ResultSet(cols, rows) # noinspection PyTypeChecker result = show_databases(etcd_client) print("Expected: \n%s" % rs) print("Actual: \n%s" % result) # noinspection PyTypeChecker assert result == rs
def test_get_pk_field(): mock_response = mock.Mock() content = { "action": "get", "node": { "key": "/foo/bar/_fields", "value": '{"id": {"type": "INT", ' '"options": {"primary": true, "nullable": false}}, ' '"name": {"type": "VARCHAR", ' '"options": {"nullable": true}}}', "modifiedIndex": 2218, "createdIndex": 2218 } } mock_response.content = json.dumps(content) etcd_response = EtcdResult(mock_response) etcd_client = mock.Mock() etcd_client.read.return_value = etcd_response assert get_pk_field(etcd_client, 'foo', 'bar') == Column('id')
def test_etcd_result_response(payload_self): response = mock.Mock() response.content = payload_self r = EtcdResult(response) assert r.__repr__() == payload_self
def test_action(payload, expected): response = mock.Mock() response.content = payload # noinspection PyTypeChecker res = EtcdResult(response) assert res.action == expected
def test_exception_not_json_content(): payload = mock.Mock() payload.content = 'foo' with pytest.raises(EtcdInvalidResponse): # noinspection PyTypeChecker EtcdResult(payload)
def test_exception_legit_empty_content(code): payload = mock.Mock() payload.content = '' payload.status_code = code # noinspection PyTypeChecker assert EtcdResult(payload).action is None
def test_exception_empty_content(content): payload = mock.Mock() payload.content = content with pytest.raises(EtcdEmptyResponse): # noinspection PyTypeChecker EtcdResult(payload)
def test_exception_invalid_payload(payload): with pytest.raises(EtcdInvalidResponse): EtcdResult(payload)
def test_prev_node(payload, expected): response = mock.Mock() response.content = payload # noinspection PyTypeChecker res = EtcdResult(response) assert res.prevNode == expected
def test_action(payload, expected): response = mock.Mock() response.content = payload res = EtcdResult(response) assert res.action == expected
def test_etcd_result_response(payload_self): response = mock.Mock() response.content = payload_self # noinspection PyTypeChecker r = EtcdResult(response) assert r.__repr__() == payload_self
def test_etcd_noindex(): response = mock.Mock() response.content = '{"action":"get","node":{"key":"/foo","value":"bar","modifiedIndex":7,"createdIndex":7}}' res = EtcdResult(response) assert res.x_etcd_index is None
def test_leader(): payload = """ { "followers": { "6e3bd23ae5f1eae0": { "counts": { "fail": 0, "success": 745 }, "latency": { "average": 0.017039507382550306, "current": 0.000138, "maximum": 1.007649, "minimum": 0, "standardDeviation": 0.05289178277920594 } }, "a8266ecf031671f3": { "counts": { "fail": 0, "success": 735 }, "latency": { "average": 0.012124141496598642, "current": 0.000559, "maximum": 0.791547, "minimum": 0, "standardDeviation": 0.04187900156583733 } } }, "leader": "924e2e83e93f2560" } """ response = mock.Mock() response.content = payload # noinspection PyTypeChecker res = EtcdResult(response) assert res.leader == "924e2e83e93f2560" assert res.followers == { "6e3bd23ae5f1eae0": { "counts": { "fail": 0, "success": 745 }, "latency": { "average": 0.017039507382550306, "current": 0.000138, "maximum": 1.007649, "minimum": 0, "standardDeviation": 0.05289178277920594 } }, "a8266ecf031671f3": { "counts": { "fail": 0, "success": 735 }, "latency": { "average": 0.012124141496598642, "current": 0.000559, "maximum": 0.791547, "minimum": 0, "standardDeviation": 0.04187900156583733 } } }
def test_show_databases(mock_client, payload, result, cursor): response = mock.MagicMock() response.content = payload etcd_result = EtcdResult(response) mock_client.return_value = etcd_result assert cursor._execute_show_databases() == result