Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify Manifester class to work with MockStub #4

Merged
merged 26 commits into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
14c039a
Modify Manifester class to work with MockStub
synkd Jun 1, 2022
0996b26
Rebase PR and address merge conflicts
synkd Nov 8, 2023
36ba344
Get initial tests passing
synkd Nov 14, 2023
ac11f42
Add test for get_manifest(), add docstrings
synkd Nov 15, 2023
c4880fa
Add test for delete_subscription_allocation
synkd Nov 16, 2023
d7c4861
Add negative test for simple retry's timeout
synkd Nov 17, 2023
6f34d36
Add test, modify mock method returns
synkd Nov 21, 2023
809bd13
Remove extraneous comments
synkd Nov 21, 2023
3202681
Revert max_timeout change
synkd Nov 21, 2023
1149cb7
Correct logic error in sat version evaluation
synkd Nov 21, 2023
f992b47
Use more realistic data in version check
synkd Nov 21, 2023
6a43c1c
Fix failing test for simple retry timeout
synkd Nov 21, 2023
f2a897e
Add test for reading manifest data from dictionary
synkd Nov 22, 2023
fb44056
Add test of subscription pool retrieval with offset
synkd Nov 28, 2023
7e45b04
Use realistic data to simplify mock handling
synkd Nov 28, 2023
4fe55e0
Add test for correct subs in allocation
synkd Nov 28, 2023
341cada
Tweak test_correct_subs_added_to_allocation
synkd Nov 28, 2023
83156c2
Add test for invalid sat_version
synkd Nov 29, 2023
f53bfaa
Add pre-commit hooks and run pre-commit
synkd Nov 29, 2023
55f3cda
Switch to ruff for linting/formatting
synkd Dec 4, 2023
6e1a3d2
Modify workflow to workaround CodeQL failure
synkd Dec 4, 2023
79a4b4e
Add pytest dep and correct typo in test manifest_data
synkd Dec 4, 2023
2af7760
Use manifest_data dict for all tests
synkd Dec 4, 2023
2bc2572
Modify checkout action to pull latest commit from PR
synkd Dec 5, 2023
39483fa
Address reviewer feedback; delete status code in one additional spot
synkd Dec 12, 2023
96e2640
Address settings issue and test flakiness
synkd Dec 12, 2023
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
43 changes: 19 additions & 24 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,27 +1,22 @@
# configuration for pre-commit git hooks

repos:
- repo: https://github.com/asottile/reorder_python_imports
rev: v3.0.1
hooks:
- id: reorder-python-imports
- repo: https://github.com/asottile/pyupgrade
rev: v2.32.0
hooks:
- id: pyupgrade
args: [--py36-plus]
- repo: https://github.com/psf/black
rev: 22.3.0
hooks:
- id: black
- repo: https://gitlab.com/pycqa/flake8
rev: 3.9.2
hooks:
- id: flake8
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.2.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml
- id: debug-statements
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
hooks:
- id: trailing-whitespace
- id: check-yaml
- id: debug-statements
- repo: https://github.com/psf/black
rev: 22.10.0
hooks:
- id: black
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.0.277
hooks:
- id: ruff
args: [--fix, --exit-non-zero-on-fix]
- repo: https://github.com/gitleaks/gitleaks
rev: v8.18.0
hooks:
- id: gitleaks
4 changes: 1 addition & 3 deletions manifester/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ def cli():
type=str,
help="Category of manifest (golden_ticket or robottelo_automation by default)",
)
@click.option(
"--allocation_name", type=str, help="Name of upstream subscription allocation"
)
@click.option("--allocation_name", type=str, help="Name of upstream subscription allocation")
def get_manifest(manifest_category, allocation_name):
manifester = Manifester(manifest_category, allocation_name)
manifester.create_subscription_allocation()
Expand Down
60 changes: 56 additions & 4 deletions manifester/helpers.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
from collections import UserDict
import random
import time

from logzero import logger


def simple_retry(cmd, cmd_args=None, cmd_kwargs=None, max_timeout=240, _cur_timeout=1):
"""Re(Try) a function given its args and kwargs up until a max timeout"""

cmd_args = cmd_args if cmd_args else []
cmd_kwargs = cmd_kwargs if cmd_kwargs else {}
# If additional debug information is needed, the following log entry can be modified to
Expand All @@ -16,15 +19,16 @@ def simple_retry(cmd, cmd_args=None, cmd_kwargs=None, max_timeout=240, _cur_time
if response.status_code in [429, 500, 504]:
new_wait = _cur_timeout * 2
if new_wait > max_timeout:
raise Exception("Timeout exceeded")
raise Exception("Retry timeout exceeded")
logger.debug(f"Trying again in {_cur_timeout} seconds")
time.sleep(_cur_timeout)
response = simple_retry(cmd, cmd_args, cmd_kwargs, max_timeout, new_wait)
return response


def process_sat_version(sat_version, valid_sat_versions):
"""Ensure that the sat_version parameter is properly formatted for the RHSM API when creating
a subscription allocation with the 'POST allocations' endpoint"""
a subscription allocation with the 'POST allocations' endpoint"""
if sat_version not in valid_sat_versions:
# The valid values for the sat_version parameter when creating a subscription allocation
# are all 8 characters or less (e.g. 'sat-6.11'). Some data sources may include a Z-stream
Expand All @@ -37,5 +41,53 @@ def process_sat_version(sat_version, valid_sat_versions):
sat_version = ".".join(sat_version)
# If sat_version is still not valid, default to the latest valid version.
if sat_version not in valid_sat_versions:
valid_sat_versions.sort(key = lambda i: int(i.split('-')[-1].split('.')[-1]), reverse = True)
return valid_sat_versions[0]
valid_sat_versions.sort(
key=lambda i: int(i.split('-')[-1].split('.')[-1]), reverse=True
)
return valid_sat_versions[0]
return sat_version


def fake_http_response_code(good_codes=None, bad_codes=None, fail_rate=20):
"""Return an HTTP response code randomly selected from sets of good and bad codes"""

if random.random() > (fail_rate / 100):
return random.choice(good_codes)
else:
return random.choice(bad_codes)


class MockStub(UserDict):
"""Test helper class. Allows for both arbitrary mocking and stubbing"""

def __init__(self, in_dict=None):
"""Initialize the class and all nested dictionaries"""
if in_dict is None:
in_dict = {}
for key, value in in_dict.items():
if isinstance(value, dict):
setattr(self, key, MockStub(value))
elif type(value) in (list, tuple):
setattr(
self,
key,
[MockStub(x) if isinstance(x, dict) else x for x in value],
)
else:
setattr(self, key, value)
super().__init__(in_dict)

def __getattr__(self, name):
return self

def __getitem__(self, key):
if isinstance(key, str):
item = getattr(self, key, self)
try:
item = super().__getitem__(key)
except KeyError:
item = self
return item

def __call__(self, *args, **kwargs):
return self
Loading