Skip to content

Commit 1f4fdd5

Browse files
committed
format and lint
1 parent 2346c14 commit 1f4fdd5

7 files changed

Lines changed: 114 additions & 83 deletions

File tree

Makefile

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,6 @@ endif
1010

1111
VENV_RUN = . $(VENV_ACTIVATE)
1212

13-
$(VENV_ACTIVATE): setup.py setup.cfg
14-
test -d $(VENV_DIR) || $(VENV_BIN) $(VENV_DIR)
15-
$(VENV_RUN); $(PIP_CMD) install --upgrade pip setuptools wheel plux
16-
touch $(VENV_ACTIVATE)
17-
1813
venv: $(VENV_ACTIVATE)
1914

2015
format: venv ## Run ruff and black to format the whole codebase

localstack_utils/container.py

Lines changed: 56 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,54 +3,80 @@
33
import docker
44
from time import sleep
55

6-
LOCALSTACK_NAME = "localstack/localstack";
7-
LOCALSTACK_TAG = "latest";
8-
LOCALSTACK_PORT_EDGE = "4566";
9-
LOCALSTACK_PORT_ELASTICSEARCH = "4571";
10-
11-
MAX_PORT_CONNECTION_ATTEMPTS = 10;
12-
MAX_LOG_COLLECTION_ATTEMPTS = 120;
13-
POLL_INTERVAL = 1;
14-
NUM_LOG_LINES = 10;
15-
16-
ENV_DEBUG = "DEBUG";
17-
ENV_USE_SSL = "USE_SSL";
18-
ENV_DEBUG_DEFAULT = "1";
6+
LOCALSTACK_NAME = "localstack/localstack"
7+
LOCALSTACK_TAG = "latest"
8+
LOCALSTACK_PORT_EDGE = "4566"
9+
LOCALSTACK_PORT_ELASTICSEARCH = "4571"
10+
11+
MAX_PORT_CONNECTION_ATTEMPTS = 10
12+
MAX_LOG_COLLECTION_ATTEMPTS = 120
13+
POLL_INTERVAL = 1
14+
NUM_LOG_LINES = 10
15+
16+
ENV_DEBUG = "DEBUG"
17+
ENV_USE_SSL = "USE_SSL"
18+
ENV_DEBUG_DEFAULT = "1"
1919
LOCALSTACK_EXTERNAL_HOSTNAME = "HOSTNAME_EXTERNAL"
2020
DEFAULT_CONTAINER_ID = "localstack_main"
2121

2222
DOCKER_CLIENT = docker.from_env()
2323

24-
class Container:
2524

25+
class Container:
2626
@staticmethod
27-
def create_localstack_container(external_hostname, pull_new_image, randomize_ports, image_name, image_tag, port_edge, port_elasticsearch, environment_variables, port_mappings, bind_mounts, platform):
27+
def create_localstack_container(
28+
external_hostname,
29+
pull_new_image,
30+
randomize_ports,
31+
image_name,
32+
image_tag,
33+
port_edge,
34+
port_elasticsearch,
35+
environment_variables,
36+
port_mappings,
37+
bind_mounts,
38+
platform,
39+
):
40+
environment_variables = (
41+
{} if environment_variables is None else environment_variables
42+
)
43+
bind_mounts = {} if bind_mounts is None else bind_mounts
44+
port_mappings = {} if port_mappings is None else port_mappings
45+
image_name_or_default = LOCALSTACK_NAME if image_name is None else image_name
46+
image_exists = (
47+
True
48+
if len(DOCKER_CLIENT.images.list(name=image_name_or_default))
49+
else False
50+
)
2851

29-
environment_variables = {} if environment_variables == None else environment_variables
30-
bind_mounts = {} if bind_mounts == None else bind_mounts
31-
port_mappings = {} if port_mappings == None else port_mappings
32-
image_name_or_default = LOCALSTACK_NAME if image_name == None else image_name
33-
image_exists = True if len(DOCKER_CLIENT.images.list(name=image_name_or_default)) else False
34-
35-
fullPortEdge = {(LOCALSTACK_PORT_EDGE if port_edge == None else port_edge) : (LOCALSTACK_PORT_EDGE)}
52+
fullPortEdge = {
53+
(LOCALSTACK_PORT_EDGE if port_edge is None else port_edge): (
54+
LOCALSTACK_PORT_EDGE
55+
)
56+
}
3657

3758
if pull_new_image or not image_exists:
3859
logging.info("Pulling latest image")
3960
DOCKER_CLIENT.images.pull(image_name_or_default, image_tag)
40-
41-
return DOCKER_CLIENT.containers.run(image_name_or_default, ports=fullPortEdge, environment=environment_variables, detach=True)
42-
61+
62+
return DOCKER_CLIENT.containers.run(
63+
image_name_or_default,
64+
ports=fullPortEdge,
65+
environment=environment_variables,
66+
detach=True,
67+
)
68+
4369
@staticmethod
4470
def waitForReady(container, pattern):
4571
attemps = 0
46-
72+
4773
while True:
48-
logs = container.logs(tail=NUM_LOG_LINES).decode('utf-8')
74+
logs = container.logs(tail=NUM_LOG_LINES).decode("utf-8")
4975
if re.search(pattern, logs):
50-
return;
51-
76+
return
77+
5278
sleep(POLL_INTERVAL)
5379
attemps += 1
5480

5581
if attemps >= MAX_LOG_COLLECTION_ATTEMPTS:
56-
raise "Could not find token: " +pattern.toString()+ "in logs"
82+
raise "Could not find token: " + pattern.toString() + "in logs"

localstack_utils/localstack_docker_configuration.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ class LocalstackDockerConfiguration:
55
image_tag = None
66
platform = None
77

8-
port_edge = '4566'
9-
port_elastic_search = '4571'
10-
external_hostname = 'localhost'
8+
port_edge = "4566"
9+
port_elastic_search = "4571"
10+
external_hostname = "localhost"
1111
environment_variables = {}
1212
port_mappings = {}
1313
bind_mounts = {}
Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
11
import threading
22
import logging
33

4-
class LocalstackLogger():
4+
5+
class LocalstackLogger:
56
stream = None
67
log_thread = None
8+
79
def __init__(self, localsack_container) -> None:
8-
self.stream = localsack_container.logs(stream=True, follow=True)
9-
10+
self.stream = localsack_container.logs(stream=True, follow=True)
11+
1012
def start(self):
11-
self.log_thread = threading.Thread(target=log_stream,args=[self.stream]).start()
12-
13+
self.log_thread = threading.Thread(
14+
target=log_stream, args=[self.stream]
15+
).start()
16+
17+
1318
def log_stream(stream):
1419
try:
1520
while True:
16-
line = next(stream).decode("utf-8").replace("\n",'')
21+
line = next(stream).decode("utf-8").replace("\n", "")
1722
logging.info(line)
1823
except StopIteration:
1924
pass

localstack_utils/localstack_utils.py

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
import docker
44
import logging
55
from localstack_utils.container import Container
6-
from localstack_utils.localstack_docker_configuration import LocalstackDockerConfiguration
6+
from localstack_utils.localstack_docker_configuration import (
7+
LocalstackDockerConfiguration,
8+
)
79
from localstack_utils.localstack_logger import LocalstackLogger
810

911
ENV_CONFIG_USE_SSL = "USE_SSL"
@@ -12,14 +14,16 @@
1214
TMP_PATH = "/tmp/localstack"
1315
READY_TOKEN = re.compile("Ready\\.")
1416
DEFAULT_EDGE_PORT = 4566
15-
PORT_CONFIG_FILENAME = "/opt/code/localstack/.venv/lib/python3.8/site-packages/localstack_client/config.py"
17+
PORT_CONFIG_FILENAME = (
18+
"/opt/code/localstack/.venv/lib/python3.8/site-packages/localstack_client/config.py"
19+
)
1620
# DEFAULT_PORT_PATTERN = re.compile("'(\\w+)'\\Q: '{proto}://{host}:\\E(\\d+)'")
1721

1822
localstack_instance = None
19-
logging.basicConfig(level=logging.INFO, format='%(message)s')
23+
logging.basicConfig(level=logging.INFO, format="%(message)s")
24+
2025

2126
class Localstack:
22-
2327
localstack_container = None
2428
service_to_map = {}
2529
locked = False
@@ -28,9 +32,9 @@ class Localstack:
2832
def INSTANCE():
2933
return Localstack()
3034

31-
external_hostName = ''
35+
external_hostName = ""
3236

33-
def startup(self, docker_configuration) :
37+
def startup(self, docker_configuration):
3438
if self.locked:
3539
raise Exception("A docker instance is starting or already started.")
3640
self.locked = True
@@ -48,42 +52,42 @@ def startup(self, docker_configuration) :
4852
docker_configuration.environment_variables,
4953
docker_configuration.port_mappings,
5054
docker_configuration.bind_mounts,
51-
docker_configuration.platform
55+
docker_configuration.platform,
5256
)
5357

5458
self.setup_logger()
5559

5660
Container.waitForReady(self.localstack_container, READY_TOKEN)
5761

58-
except docker.errors.APIError as error:
62+
except docker.errors.APIError:
5963
if not docker_configuration.ignore_docker_runerrors:
6064
raise "Unable to start docker"
6165

62-
except:
66+
except Exception:
6367
raise sys.exc_info()
6468

65-
6669
def stop(self):
6770
self.localstack_container.stop()
6871

69-
7072
def setup_logger(self):
7173
localstack_logger = LocalstackLogger(self.localstack_container)
7274
localstack_logger.start()
7375

74-
def startup_localstack(port=4566, services=[], ignore_docker_errors = False):
76+
77+
def startup_localstack(port=4566, services=[], ignore_docker_errors=False):
7578
global localstack_instance
7679
localstack_instance = Localstack.INSTANCE()
7780
config = LocalstackDockerConfiguration()
7881

7982
if len(services):
80-
config.envirement_variables.update({'SERVICES': ','.join(services)})
83+
config.envirement_variables.update({"SERVICES": ",".join(services)})
8184
if port != 4566:
8285
config.port_edge = str(port)
8386
config.ignore_docker_runerrors = ignore_docker_errors
8487

8588
localstack_instance.startup(config)
8689

90+
8791
def stop_localstack():
8892
if localstack_instance:
8993
localstack_instance.stop()

tests/kinesis_test.py

Lines changed: 0 additions & 26 deletions
This file was deleted.

tests/test_kinesis.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import time
2+
import boto3
3+
import unittest
4+
from localstack_utils import startup_localstack, stop_localstack
5+
6+
7+
class TestKinesis(unittest.TestCase):
8+
def setUp(self):
9+
startup_localstack()
10+
11+
def tearDown(self):
12+
stop_localstack()
13+
return super().tearDown()
14+
15+
def test_create_stream(self):
16+
kinesis = boto3.client(
17+
service_name="kinesis",
18+
aws_access_key_id="test",
19+
aws_secret_access_key="test",
20+
endpoint_url="http://localhost:4566",
21+
)
22+
23+
kinesis.create_stream(StreamName="test", ShardCount=1)
24+
time.sleep(5)
25+
26+
response = kinesis.list_streams()
27+
self.assertGreater(len(response.get("StreamNames", [])), 0)

0 commit comments

Comments
 (0)