Skip to content

Commit acdcfd5

Browse files
authored
Move resources-definitions update script to this repository (RedHatQE#1603)
* Move `resources-definitions` update script to this repository * update_resources_definitions: do not overwite data
1 parent 2ec6297 commit acdcfd5

File tree

5 files changed

+57
-4
lines changed

5 files changed

+57
-4
lines changed

.flake8

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ fcn_exclude_functions =
4646
benedict,
4747
logger,
4848
pytest,
49+
json,
4950

5051
enable-extensions =
5152
FCN,

tests/scripts/__init__.py

Whitespace-only changes.

tests/scripts/resources_definitions.json

+1
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import shlex
2+
import subprocess
3+
import json
4+
from json import JSONDecodeError
5+
6+
7+
def resources_dict_from_api_resources():
8+
"""
9+
Build dict with resources and matched values.
10+
11+
Output example for resource:
12+
{
13+
'api_version': 'networking.k8s.io/v1',
14+
'api_group': {'config.openshift.io': 'false', 'networking.k8s.io': 'true'}
15+
}
16+
"""
17+
resources_dict = {}
18+
api_resources = subprocess.check_output(shlex.split("oc api-resources --no-headers"))
19+
api_resources = api_resources.decode("utf-8")
20+
for line in api_resources.splitlines():
21+
line_list = line.split()
22+
try:
23+
_, _, api_version, namespaced, kind = line_list
24+
except ValueError:
25+
_, api_version, namespaced, kind = line_list
26+
27+
split_api_version = api_version.split("/")
28+
api_group = split_api_version[0] if len(split_api_version) > 1 else None
29+
resources_dict.setdefault(kind, {}).setdefault("api_group", {})
30+
resources_dict[kind]["api_group"].update({api_group: {}})
31+
resources_dict[kind]["api_group"][api_group]["namespaced"] = namespaced
32+
resources_dict[kind]["api_group"][api_group]["api_version"] = split_api_version[-1]
33+
34+
return resources_dict
35+
36+
37+
if __name__ == "__main__":
38+
data_file = "tests/scripts/resources_definitions.json"
39+
with open(data_file, "r") as fd_read:
40+
try:
41+
data = json.loads(fd_read.read())
42+
except JSONDecodeError:
43+
data = {}
44+
45+
new_data = resources_dict_from_api_resources()
46+
if new_data:
47+
for key in new_data:
48+
data[key] = new_data[key]
49+
50+
with open(data_file, "w") as fd_write:
51+
fd_write.write(json.dumps(data))

tests/test_validate_resources.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import os
77

88
import pytest
9-
import requests
109

1110
from ocp_resources.resource import Resource # noqa
1211

@@ -103,9 +102,8 @@ def _resource_file():
103102

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

110108

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

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

0 commit comments

Comments
 (0)