Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 2 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ fcn_exclude_functions =
benedict,
logger,
warn,
pytest,
json,

enable-extensions =
FCN,
Empty file added tests/scripts/__init__.py
Empty file.
1 change: 1 addition & 0 deletions tests/scripts/resources_definitions.json

Large diffs are not rendered by default.

51 changes: 51 additions & 0 deletions tests/scripts/update_resources_definitions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import shlex
import subprocess
import json
from json import JSONDecodeError


def resources_dict_from_api_resources():
"""
Build dict with resources and matched values.

Output example for resource:
{
'api_version': 'networking.k8s.io/v1',
'api_group': {'config.openshift.io': 'false', 'networking.k8s.io': 'true'}
}
"""
resources_dict = {}
api_resources = subprocess.check_output(shlex.split("oc api-resources --no-headers"))
api_resources = api_resources.decode("utf-8")
for line in api_resources.splitlines():
line_list = line.split()
try:
_, _, api_version, namespaced, kind = line_list
except ValueError:
_, api_version, namespaced, kind = line_list

split_api_version = api_version.split("/")
api_group = split_api_version[0] if len(split_api_version) > 1 else None
resources_dict.setdefault(kind, {}).setdefault("api_group", {})
resources_dict[kind]["api_group"].update({api_group: {}})
resources_dict[kind]["api_group"][api_group]["namespaced"] = namespaced
resources_dict[kind]["api_group"][api_group]["api_version"] = split_api_version[-1]

return resources_dict


if __name__ == "__main__":
data_file = "tests/scripts/resources_definitions.json"
with open(data_file, "r") as fd_read:
try:
data = json.loads(fd_read.read())
except JSONDecodeError:
data = {}

new_data = resources_dict_from_api_resources()
if new_data:
for key in new_data:
data[key] = new_data[key]

with open(data_file, "w") as fd_write:
fd_write.write(json.dumps(data))
8 changes: 4 additions & 4 deletions tests/test_validate_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import os

import pytest
import requests

from ocp_resources.resource import Resource # noqa

Expand Down Expand Up @@ -103,9 +102,8 @@ def _resource_file():

@pytest.fixture()
def resources_definitions():
file_ = "https://raw.githubusercontent.com/RedHatQE/openshift-resources-definitions/main/resources_definitions.json"
content = requests.get(file_).content
return json.loads(content)
with open("tests/scripts/resources_definitions.json") as fd:
yield json.load(fd)


@pytest.fixture()
Expand All @@ -118,6 +116,8 @@ def resources_definitions_errors(resources_definitions):
for cls in classes:
resource_dict = resources_definitions.get(cls.name)
if not resource_dict:
# TODO: Fail and let the user know that 'tests/scripts/resources_definitions.json'
# need to be updated using 'update_resources_definitions' script
continue

bodies = [body_ for body_ in getattr(cls, "body") if isinstance(body_, ast.Assign)]
Expand Down