Skip to content

Commit 090d12d

Browse files
authored
Add isort and Black (#2929)
1 parent d352e76 commit 090d12d

File tree

98 files changed

+6877
-5222
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

98 files changed

+6877
-5222
lines changed

.github/workflows/lint.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,10 @@ jobs:
3838
uses: golangci/golangci-lint-action@v3
3939
with:
4040
only-new-issues: true
41+
42+
lint-python:
43+
runs-on: ubuntu-20.04
44+
steps:
45+
- uses: actions/checkout@v2
46+
- uses: isort/isort-action@v1.0.0
47+
- uses: psf/black@stable

Makefile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ lint: ## Run linter
3131
@git fetch
3232
docker run --pull always --rm -v $(shell pwd):/kubernetes-ingress -w /kubernetes-ingress -v $(shell go env GOCACHE):/cache/go -e GOCACHE=/cache/go -e GOLANGCI_LINT_CACHE=/cache/go -v $(shell go env GOPATH)/pkg:/go/pkg golangci/golangci-lint:latest git diff -p origin/main > /tmp/diff.patch && golangci-lint --color always run -v --new-from-patch=/tmp/diff.patch
3333

34+
.PHONY: lint-python
35+
lint-python: ## Run linter for python tests
36+
@isort -V || (code=$$?; printf "\033[0;31mError\033[0m: there was a problem with isort, use 'brew install isort' to install it\n"; exit $$code)
37+
@black --version || (code=$$?; printf "\033[0;31mError\033[0m: there was a problem with black, use 'brew install black' to install it\n"; exit $$code)
38+
@isort .
39+
@black .
40+
3441
.PHONY: test
3542
test: ## Run tests
3643
go test -tags=aws -shuffle=on -race ./...

perf-tests/conftest.py

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
11
"""Describe overall framework configuration."""
22

3-
from suite.resources_utils import get_first_pod_name
4-
from settings import (
5-
DEFAULT_IMAGE,
6-
DEFAULT_PULL_POLICY,
7-
DEFAULT_IC_TYPE,
8-
DEFAULT_SERVICE,
9-
DEFAULT_DEPLOYMENT_TYPE,
10-
)
11-
from kubernetes.config.kube_config import KUBE_CONFIG_DEFAULT_LOCATION
123
import os
13-
import pytest
144
import sys
155

6+
import pytest
7+
from kubernetes.config.kube_config import KUBE_CONFIG_DEFAULT_LOCATION
8+
from settings import DEFAULT_DEPLOYMENT_TYPE, DEFAULT_IC_TYPE, DEFAULT_IMAGE, DEFAULT_PULL_POLICY, DEFAULT_SERVICE
9+
from suite.resources_utils import get_first_pod_name
10+
1611
sys.path.insert(0, "../tests")
1712

1813

@@ -23,10 +18,16 @@ def pytest_addoption(parser) -> None:
2318
:return:
2419
"""
2520
parser.addoption(
26-
"--context", action="store", default="", help="The context to use in the kubeconfig file.",
21+
"--context",
22+
action="store",
23+
default="",
24+
help="The context to use in the kubeconfig file.",
2725
)
2826
parser.addoption(
29-
"--image", action="store", default=DEFAULT_IMAGE, help="The Ingress Controller image.",
27+
"--image",
28+
action="store",
29+
default=DEFAULT_IMAGE,
30+
help="The Ingress Controller image.",
3031
)
3132
parser.addoption(
3233
"--image-pull-policy",
@@ -70,10 +71,16 @@ def pytest_addoption(parser) -> None:
7071
help="Show IC logs in stdout on test failure",
7172
)
7273
parser.addoption(
73-
"--users", action="store", default="10", help="No. of users for response perf tests",
74+
"--users",
75+
action="store",
76+
default="10",
77+
help="No. of users for response perf tests",
7478
)
7579
parser.addoption(
76-
"--hatch-rate", action="store", default="5", help="No. of users hatched per second",
80+
"--hatch-rate",
81+
action="store",
82+
default="5",
83+
help="No. of users hatched per second",
7784
)
7885
parser.addoption(
7986
"--time",
@@ -97,20 +104,17 @@ def pytest_collection_modifyitems(config, items) -> None:
97104
:return:
98105
"""
99106
if config.getoption("--ic-type") == "nginx-ingress":
100-
skip_for_nginx_oss = pytest.mark.skip(
101-
reason="Skip a test for Nginx OSS")
107+
skip_for_nginx_oss = pytest.mark.skip(reason="Skip a test for Nginx OSS")
102108
for item in items:
103109
if "skip_for_nginx_oss" in item.keywords:
104110
item.add_marker(skip_for_nginx_oss)
105111
if config.getoption("--ic-type") == "nginx-plus-ingress":
106-
skip_for_nginx_plus = pytest.mark.skip(
107-
reason="Skip a test for Nginx Plus")
112+
skip_for_nginx_plus = pytest.mark.skip(reason="Skip a test for Nginx Plus")
108113
for item in items:
109114
if "skip_for_nginx_plus" in item.keywords:
110115
item.add_marker(skip_for_nginx_plus)
111116
if "-ap" not in config.getoption("--image"):
112-
appprotect = pytest.mark.skip(
113-
reason="Skip AppProtect test in non-AP image")
117+
appprotect = pytest.mark.skip(reason="Skip AppProtect test in non-AP image")
114118
for item in items:
115119
if "appprotect" in item.keywords:
116120
item.add_marker(appprotect)
@@ -134,9 +138,7 @@ def pytest_runtest_makereport(item) -> None:
134138
# we only look at actual failing test calls, not setup/teardown
135139
if rep.when == "call" and rep.failed and item.config.getoption("--show-ic-logs") == "yes":
136140
pod_namespace = item.funcargs["ingress_controller_prerequisites"].namespace
137-
pod_name = get_first_pod_name(
138-
item.funcargs["kube_apis"].v1, pod_namespace)
141+
pod_name = get_first_pod_name(item.funcargs["kube_apis"].v1, pod_namespace)
139142
print("\n===================== IC Logs Start =====================")
140-
print(item.funcargs["kube_apis"].v1.read_namespaced_pod_log(
141-
pod_name, pod_namespace))
143+
print(item.funcargs["kube_apis"].v1.read_namespaced_pod_log(pod_name, pod_namespace))
142144
print("\n===================== IC Logs End =====================")

perf-tests/suite/ap_request_perf.py

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,33 @@
1-
import yaml, os
2-
from locust import TaskSet, HttpUser, task
1+
import os
2+
3+
import yaml
4+
from locust import HttpUser, TaskSet, task
35

46
host = ""
7+
8+
59
class TestAPResponse(HttpUser):
6-
# locust class to be invoked
10+
# locust class to be invoked
711
def on_start(self):
8-
# get host from appprotect-ingress yaml before each test
12+
# get host from appprotect-ingress yaml before each test
913
ap_yaml = os.path.join(os.path.dirname(__file__), "../data/appprotect-ingress.yaml")
1014
with open(ap_yaml) as f:
1115
docs = yaml.safe_load_all(f)
1216
for dep in docs:
13-
self.host = dep['spec']['rules'][0]['host']
17+
self.host = dep["spec"]["rules"][0]["host"]
1418
print("Setup finished")
1519

1620
@task
1721
def send_block_request(self):
18-
# Send invalid request while dataguard alarm policy is active
19-
response = self.client.get(
20-
url="/<script>",
21-
headers={"host": self.host},
22-
verify=False)
22+
# Send invalid request while dataguard alarm policy is active
23+
response = self.client.get(url="/<script>", headers={"host": self.host}, verify=False)
2324
print(response.text)
24-
25+
2526
@task
2627
def send_allow_request(self):
27-
# Send valid request while dataguard alarm policy is active
28-
response = self.client.get(
29-
url="",
30-
headers={"host": self.host},
31-
verify=False)
28+
# Send valid request while dataguard alarm policy is active
29+
response = self.client.get(url="", headers={"host": self.host}, verify=False)
3230
print(response.text)
33-
31+
3432
min_wait = 400
3533
max_wait = 1400
36-
37-

perf-tests/suite/test_ap_reload_perf.py

Lines changed: 27 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,40 @@
1-
import requests, logging
2-
import pytest, json
3-
import os, re, yaml, subprocess
1+
import json
2+
import logging
3+
import os
4+
import re
5+
import subprocess
46
from datetime import datetime
5-
from settings import TEST_DATA, DEPLOYMENTS
7+
8+
import pytest
9+
import requests
10+
import yaml
11+
from kubernetes.client import V1ContainerPort
12+
from settings import DEPLOYMENTS, TEST_DATA
613
from suite.custom_resources_utils import (
714
create_ap_logconf_from_yaml,
815
create_ap_policy_from_yaml,
9-
delete_ap_policy,
1016
delete_ap_logconf,
17+
delete_ap_policy,
18+
read_ap_crd,
1119
)
12-
from kubernetes.client import V1ContainerPort
1320
from suite.resources_utils import (
14-
wait_before_test,
1521
create_example_app,
16-
wait_until_all_pods_are_ready,
22+
create_ingress,
23+
create_ingress_with_ap_annotations,
1724
create_items_from_yaml,
18-
delete_items_from_yaml,
1925
delete_common_app,
26+
delete_items_from_yaml,
2027
ensure_connection_to_public_endpoint,
21-
create_ingress,
22-
create_ingress_with_ap_annotations,
23-
replace_ingress_with_ap_annotations,
2428
ensure_response_from_backend,
25-
wait_before_test,
2629
get_events,
27-
get_ingress_nginx_template_conf,
30+
get_file_contents,
2831
get_first_pod_name,
32+
get_ingress_nginx_template_conf,
33+
replace_ingress_with_ap_annotations,
34+
wait_before_test,
2935
wait_for_event_increment,
30-
get_file_contents,
36+
wait_until_all_pods_are_ready,
3137
)
32-
from suite.custom_resources_utils import read_ap_crd
3338
from suite.yaml_utils import get_first_ingress_host_from_yaml
3439

3540
ap_policy = "dataguard-alarm"
@@ -68,9 +73,7 @@ def enable_prometheus_port(
6873

6974

7075
@pytest.fixture(scope="class")
71-
def appprotect_setup(
72-
request, kube_apis, ingress_controller_endpoint, test_namespace
73-
) -> AppProtectSetup:
76+
def appprotect_setup(request, kube_apis, ingress_controller_endpoint, test_namespace) -> AppProtectSetup:
7477
"""
7578
Deploy simple application and all the AppProtect(dataguard-alarm) resources under test in one namespace.
7679
@@ -169,7 +172,7 @@ def collect_prom_reload_metrics(self, metric_list, scenario, ip, port) -> None:
169172
reload_metric = ""
170173
for line in resp_decoded.splitlines():
171174
if "last_reload_milliseconds{class" in line:
172-
reload_metric = re.findall("\d+", line)[0]
175+
reload_metric = re.findall(r"\d+", line)[0]
173176
metric_list.append(
174177
{
175178
f"Reload time ({scenario}) ": f"{reload_metric}ms",
@@ -200,9 +203,7 @@ def test_ap_perf_create_ingress(
200203
print("--------- Run test while AppProtect module is enabled with correct policy ---------")
201204
ensure_response_from_backend(appprotect_setup.req_url, ingress_host)
202205
wait_before_test(40)
203-
response = requests.get(
204-
appprotect_setup.req_url + "/<script>", headers={"host": ingress_host}, verify=False
205-
)
206+
response = requests.get(appprotect_setup.req_url + "/<script>", headers={"host": ingress_host}, verify=False)
206207
print(response.text)
207208
self.collect_prom_reload_metrics(
208209
reload_ap,
@@ -252,9 +253,7 @@ def test_ap_perf_ingress_path_change(
252253
ensure_response_from_backend(appprotect_setup.req_url, ingress_host)
253254
wait_before_test(30)
254255
response = ""
255-
response = requests.get(
256-
appprotect_setup.req_url + "/v1/<script>", headers={"host": ingress_host}, verify=False
257-
)
256+
response = requests.get(appprotect_setup.req_url + "/v1/<script>", headers={"host": ingress_host}, verify=False)
258257
print(response.text)
259258
self.collect_prom_reload_metrics(
260259
reload_ap_path,
@@ -298,9 +297,7 @@ def test_ap_perf_multiple_ingress(
298297
print("--------- Run test while AppProtect module is enabled with correct policy ---------")
299298
ensure_response_from_backend(appprotect_setup.req_url, ingress_host)
300299
wait_before_test(30)
301-
response = requests.get(
302-
appprotect_setup.req_url + "/<script>", headers={"host": ingress_host}, verify=False
303-
)
300+
response = requests.get(appprotect_setup.req_url + "/<script>", headers={"host": ingress_host}, verify=False)
304301
print(response.text)
305302
self.collect_prom_reload_metrics(
306303
reload_ap_with_ingress,
@@ -343,9 +340,7 @@ def test_ap_perf_response(
343340
ensure_response_from_backend(appprotect_setup.req_url, ingress_host)
344341
wait_before_test(30)
345342
response = ""
346-
response = requests.get(
347-
appprotect_setup.req_url + "/<script>", headers={"host": ingress_host}, verify=False
348-
)
343+
response = requests.get(appprotect_setup.req_url + "/<script>", headers={"host": ingress_host}, verify=False)
349344
print(appprotect_setup.req_url + "/<script>")
350345
print(ingress_host)
351346
print(response.text)

pyproject.toml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[tool.black]
2+
line-length = 120
3+
target-version = ['py310']
4+
extend-exclude = '.*pb2.*'
5+
6+
[tool.isort]
7+
atomic = true
8+
profile = "black"
9+
line_length = 120
10+
skip_gitignore = true
11+
balanced_wrapping = true
12+
filter_files = true
13+
skip_glob = ['*pb2*']

tests/conftest.py

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,16 @@
44

55
import pytest
66
from kubernetes.config.kube_config import KUBE_CONFIG_DEFAULT_LOCATION
7-
from settings import (BATCH_RESOURCES, BATCH_START, DEFAULT_DEPLOYMENT_TYPE,
8-
DEFAULT_IC_TYPE, DEFAULT_IMAGE, DEFAULT_PULL_POLICY,
9-
DEFAULT_SERVICE, NUM_REPLICAS)
7+
from settings import (
8+
BATCH_RESOURCES,
9+
BATCH_START,
10+
DEFAULT_DEPLOYMENT_TYPE,
11+
DEFAULT_IC_TYPE,
12+
DEFAULT_IMAGE,
13+
DEFAULT_PULL_POLICY,
14+
DEFAULT_SERVICE,
15+
NUM_REPLICAS,
16+
)
1017
from suite.resources_utils import get_first_pod_name
1118

1219

@@ -127,7 +134,7 @@ def pytest_collection_modifyitems(config, items) -> None:
127134
for item in items:
128135
if "dos" in item.keywords:
129136
item.add_marker(dos)
130-
if str(config.getoption("--batch-start")) != "True":
137+
if str(config.getoption("--batch-start")) != "True":
131138
batch_start = pytest.mark.skip(reason="Skipping pod restart test with multiple resources")
132139
for item in items:
133140
if "batch_start" in item.keywords:
@@ -150,17 +157,9 @@ def pytest_runtest_makereport(item) -> None:
150157
rep = outcome.get_result()
151158

152159
# we only look at actual failing test calls, not setup/teardown
153-
if (
154-
rep.when == "call"
155-
and rep.failed
156-
and item.config.getoption("--show-ic-logs") == "yes"
157-
):
160+
if rep.when == "call" and rep.failed and item.config.getoption("--show-ic-logs") == "yes":
158161
pod_namespace = item.funcargs["ingress_controller_prerequisites"].namespace
159162
pod_name = get_first_pod_name(item.funcargs["kube_apis"].v1, pod_namespace)
160163
print("\n===================== IC Logs Start =====================")
161-
print(
162-
item.funcargs["kube_apis"].v1.read_namespaced_pod_log(
163-
pod_name, pod_namespace
164-
)
165-
)
164+
print(item.funcargs["kube_apis"].v1.read_namespaced_pod_log(pod_name, pod_namespace))
166165
print("\n===================== IC Logs End =====================")

0 commit comments

Comments
 (0)