Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
06a6f36
Allow user to provide arbitrary config
hardbyte Feb 11, 2020
fac1c23
Allow user to provide arbitrary deployment annotations
hardbyte Feb 11, 2020
b1441a9
Try use default tracing connection
hardbyte Feb 11, 2020
5a7583a
Use default tracing connection for api too
hardbyte Feb 11, 2020
292e46a
Extract load_yaml_config from setup_logging and add tests
hardbyte Feb 11, 2020
4fb2578
K8s: Add a config file for tracing. Always mount all config files in …
hardbyte Feb 11, 2020
cea3bb7
Remove redundant helper function from test_serialization
hardbyte Feb 11, 2020
f87a2ca
Remove individual tracing setting in favor of passing in a file path.
hardbyte Feb 11, 2020
f5f8aad
Use tracing config file or default
hardbyte Feb 11, 2020
e6a2f5a
Strings to yaml not objects
hardbyte Feb 12, 2020
39df146
Add config volume to db init job
hardbyte Feb 12, 2020
5b4316d
Try to use jaeger during CI testing for api
hardbyte Feb 12, 2020
4548c59
Use default tracing connection for workers
hardbyte Feb 12, 2020
f5a6677
Try enable tracing
hardbyte Feb 12, 2020
bedc79a
Use default tracing connection for api too
hardbyte Feb 12, 2020
083affb
Ok enough of azure, just enable the tracing annotation by default
hardbyte Feb 12, 2020
2f2b7e2
Use default logging config in k8s
hardbyte Feb 12, 2020
729032f
fix filename typo
hardbyte Feb 12, 2020
5439e49
cleanup after review
hardbyte Feb 12, 2020
278cccd
Only log celery container from worker pod
hardbyte Feb 12, 2020
b563cb4
Remove a stray print statement
hardbyte Feb 13, 2020
95ff98c
Minor tweaks to improve logging
hardbyte Feb 13, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Extract load_yaml_config from setup_logging and add tests
  • Loading branch information
hardbyte committed Feb 12, 2020
commit 292e46ad707712d290d15bce42691060a14ce375
14 changes: 4 additions & 10 deletions backend/entityservice/logger_setup.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import logging.config
import os
from pathlib import Path

import structlog
import yaml

from entityservice.errors import InvalidConfiguration
from entityservice.utils import load_yaml_config


def setup_logging(
Expand All @@ -15,14 +15,8 @@ def setup_logging(
Setup logging configuration
"""
path = os.getenv(env_key, Path(__file__).parent / default_path)
try:
with open(path, 'rt') as f:
config = yaml.safe_load(f)
logging.config.dictConfig(config)
except yaml.YAMLError as e:
raise InvalidConfiguration("Parsing YAML logging config failed") from e
except FileNotFoundError as e:
raise InvalidConfiguration(f"Logging config YAML file '{path}' doesn't exist.") from e
config = load_yaml_config(path)
logging.config.dictConfig(config)

# Configure Structlog wrapper for client use
setup_structlog()
Expand Down
64 changes: 64 additions & 0 deletions backend/entityservice/tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import textwrap

import pytest

from entityservice.errors import InvalidConfiguration
from entityservice.utils import load_yaml_config
from entityservice.tests.util import generate_bytes, temp_file_containing


class TestYamlLoader:

def test_empty(self):
with temp_file_containing(b'') as fp:
filename = fp.name
assert None == load_yaml_config(filename)

def test_list(self):
with temp_file_containing(b'[1,2,3]') as fp:
filename = fp.name
assert [1,2,3] == load_yaml_config(filename)

def test_missing_file(self):
filename = 'unlikely a valid file'
with pytest.raises(InvalidConfiguration):
load_yaml_config(filename)

def test_random_bytes(self):
with temp_file_containing(generate_bytes(128)) as fp:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is a non-zero chance that those random bytes resemble valid yaml. Why not just feeding a known invalid config to the test?

filename = fp.name
with pytest.raises(InvalidConfiguration):
load_yaml_config(filename)

def test_valid_yaml(self):
yamldata = textwrap.dedent("""
api:
number: 42
ingress:
enabled: true
host: example.com
""")
self._check_valid_yaml(yamldata)

def _check_valid_yaml(self, yamldata:str):
with temp_file_containing(yamldata.encode()) as fp:
filename = fp.name
loaded = load_yaml_config(filename)
assert 'api' in loaded
assert 'number' in loaded['api']
assert loaded['api']['number'] == 42
assert loaded['api']['ingress']['enabled']
return loaded

def test_valid_yaml_with_comments(self):
yamldata = textwrap.dedent("""
## Api is a thing
api:
number: 42
ingress:
enabled: true
# host: example.com
""")
loaded = self._check_valid_yaml(yamldata)
assert 'host' not in loaded['api']['ingress']

10 changes: 10 additions & 0 deletions backend/entityservice/tests/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import os
import random
import time
import tempfile
from contextlib import contextmanager
from enum import IntEnum

Expand All @@ -14,6 +15,15 @@
from entityservice.tests.config import url


@contextmanager
def temp_file_containing(data):
# Code to acquire resource, e.g.:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that is not a very useful comment

with tempfile.NamedTemporaryFile('wb') as fp:
fp.write(data)
fp.seek(0)
yield fp


def serialize_bytes(hash_bytes):
""" Serialize bloomfilter bytes

Expand Down
23 changes: 22 additions & 1 deletion backend/entityservice/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,36 @@

import binascii
import bitmath
from flask import request
from connexion import ProblemException
from flask import request
from structlog import get_logger
import yaml

from entityservice.errors import InvalidConfiguration
from entityservice.database import DBConn, get_number_parties_uploaded, get_number_parties_ready, get_project_column

logger = get_logger()


def load_yaml_config(filename):
"""
Load a yaml file as a Python object.
:param filename: a Path or String object
:raises InvalidConfiguration if the file isn't found or the yaml isn't valid.
:return: Python representation of yaml file's contents (usually Dict), or None if empty.
"""
try:
with open(filename, 'rt') as f:
return yaml.safe_load(f)
except UnicodeDecodeError as e:
raise InvalidConfiguration("YAML file appears corrupt") from e
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it would be nice to include the filename in the error message.

except yaml.YAMLError as e:
raise InvalidConfiguration("Parsing YAML config failed") from e
except FileNotFoundError as e:
raise InvalidConfiguration(f"Logging config YAML file '{filename}' doesn't exist.") from e


def fmt_bytes(num_bytes):
"""
Displays an integer number of bytes in a human friendly form.
Expand Down