Skip to content

Commit

Permalink
tests: replaced pickle with json
Browse files Browse the repository at this point in the history
- pickle encodes the object's absolute module location while encoding, and
  use it while decoding.
- pytest will change the top-level module depending on the arguments it
  is provided, breaking pickle decoding.

This changes to use json, since that does not depends on the above.
  • Loading branch information
Augusto F. Hack committed Mar 12, 2021
1 parent dac4ef3 commit 3fb0e2b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
20 changes: 14 additions & 6 deletions tests/integration/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
Copyright (c) 2019 Aiven Ltd
See LICENSE for details
"""
from dataclasses import dataclass
from dataclasses import asdict, dataclass
from filelock import FileLock
from kafka import KafkaAdminClient, KafkaProducer
from karapace.config import set_config_defaults, write_config
Expand All @@ -15,8 +15,8 @@
from tests.utils import Client, client_for, get_broker_ip, KafkaConfig, mock_factory, new_random_name, REGISTRY_URI, REST_URI
from typing import AsyncIterator, Dict, Iterator, List, Optional, Tuple

import json
import os
import pickle
import pytest
import random
import signal
Expand Down Expand Up @@ -84,6 +84,14 @@ class ZKConfig:
admin_port: int
path: str

@staticmethod
def from_dict(data: dict) -> "ZKConfig":
return ZKConfig(
data['client_port'],
data['admin_port'],
data['path'],
)


def port_is_listening(hostname: str, port: int, ipv6: bool) -> bool:
if ipv6:
Expand Down Expand Up @@ -173,10 +181,10 @@ def fixture_zkserver(session_tmppath: Path) -> Iterator[Optional[ZKConfig]]:
# transfer_file (primarily the server's port number)
with FileLock(str(lock_path_for(zk_dir))):
if transfer_file.exists():
config = pickle.loads(transfer_file.read_bytes())
config = ZKConfig.from_dict(json.loads(transfer_file.read_text()))
else:
config, proc = configure_and_start_zk(zk_dir)
transfer_file.write_bytes(pickle.dumps(config))
transfer_file.write_text(json.dumps(asdict(config)))
try:
wait_for_port(config.client_port)
yield config
Expand All @@ -202,10 +210,10 @@ def fixture_kafka_server(session_tmppath: Path, zkserver: ZKConfig) -> Iterator[
# transfer_file (primarily the server's port number)
with FileLock(str(lock_path_for(kafka_dir))):
if transfer_file.exists():
config = pickle.loads(transfer_file.read_bytes())
config = KafkaConfig.from_dict(json.loads(transfer_file.read_text()))
else:
config, proc = configure_and_start_kafka(kafka_dir, zkserver)
transfer_file.write_bytes(pickle.dumps(config))
transfer_file.write_text(json.dumps(asdict(config)))

try:
wait_for_kafka(config.kafka_port, wait_time=60)
Expand Down
9 changes: 9 additions & 0 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,15 @@ class KafkaConfig:
kafka_port: int
zookeeper_port: int

@staticmethod
def from_dict(data: dict) -> "KafkaConfig":
return KafkaConfig(
data["datadir"],
data["kafka_keystore_password"],
data["kafka_port"],
data["zookeeper_port"],
)


def get_broker_ip():
if REST_URI in os.environ and REGISTRY_URI in os.environ:
Expand Down

0 comments on commit 3fb0e2b

Please sign in to comment.