Skip to content

Commit

Permalink
fix: run ruff format
Browse files Browse the repository at this point in the history
  • Loading branch information
18alantom committed Sep 18, 2024
1 parent f90e8b6 commit 7b1ef1e
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 78 deletions.
8 changes: 2 additions & 6 deletions agent/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,12 @@ def reset(self, abbreviation="HEAD"):

def fetch(self):
# Automatically unshallow repository while fetching
shallow = self.execute("git rev-parse --is-shallow-repository")[
"output"
]
shallow = self.execute("git rev-parse --is-shallow-repository")["output"]
unshallow = "--unshallow" if shallow == "true" else ""
return self.execute(f"git fetch {self.remote} {unshallow}")

def fetch_ref(self, ref):
return self.execute(
f"git fetch --progress --depth 1 {self.remote} {ref}"
)
return self.execute(f"git fetch --progress --depth 1 {self.remote} {ref}")

def checkout(self, ref):
return self.execute(f"git checkout {ref}")
Expand Down
13 changes: 3 additions & 10 deletions agent/proxysql.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ def __init__(self, directory=None):
self.config_file = os.path.join(self.directory, "config.json")
self.name = self.config["name"]

self.proxysql_admin_password = self.config.get(
"proxysql_admin_password"
)
self.proxysql_admin_password = self.config.get("proxysql_admin_password")
self.job = None
self.step = None

Expand All @@ -39,15 +37,10 @@ def add_backend_job(self, backend):
def add_backend(self, backend):
backend_id = backend["id"]
backend_ip = backend["ip"]
if self.proxysql_execute(
f"SELECT 1 from mysql_servers where hostgroup_id = {backend_id}"
)["output"]:
if self.proxysql_execute(f"SELECT 1 from mysql_servers where hostgroup_id = {backend_id}")["output"]:
return
commands = [
(
"INSERT INTO mysql_servers (hostgroup_id, hostname) "
f'VALUES ({backend_id}, "{backend_ip}")'
),
("INSERT INTO mysql_servers (hostgroup_id, hostname) " f'VALUES ({backend_id}, "{backend_ip}")'),
"LOAD MYSQL SERVERS TO RUNTIME",
"SAVE MYSQL SERVERS TO DISK",
]
Expand Down
4 changes: 1 addition & 3 deletions agent/ssh.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,7 @@ def add_certificate(self, name, certificate):
@step("Add Principal to User")
def add_principal(self, name, principal, ssh):
cd_command = "cd frappe-bench; exec bash --login"
force_command = (
f"ssh frappe@{ssh['ip']} -p {ssh['port']} -t '{cd_command}'"
)
force_command = f"ssh frappe@{ssh['ip']} -p {ssh['port']} -t '{cd_command}'"
principal_line = f'restrict,pty,command="{force_command}" {principal}'
source = tempfile.mkstemp()[1]
with open(source, "w") as f:
Expand Down
78 changes: 19 additions & 59 deletions agent/tests/test_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,8 @@ class TestProxy(unittest.TestCase):

def _create_needed_files(self):
"""Create host dirs for 2 domains test json files."""
os.makedirs(
os.path.join(self.hosts_directory, self.domain_1), exist_ok=True
)
os.makedirs(
os.path.join(self.hosts_directory, self.domain_2), exist_ok=True
)
os.makedirs(os.path.join(self.hosts_directory, self.domain_1), exist_ok=True)
os.makedirs(os.path.join(self.hosts_directory, self.domain_2), exist_ok=True)
os.makedirs(self.upstreams_directory, exist_ok=True)

map_1 = os.path.join(self.hosts_directory, self.domain_1, "map.json")
Expand All @@ -46,9 +42,7 @@ def setUp(self):
self.tld = "frappe.cloud"

self.hosts_directory = os.path.join(self.test_dir, "nginx/hosts")
self.upstreams_directory = os.path.join(
self.test_dir, "nginx/upstreams"
)
self.upstreams_directory = os.path.join(self.test_dir, "nginx/upstreams")
self._create_needed_files()

def tearDown(self):
Expand All @@ -68,9 +62,7 @@ def test_hosts_redirects_default_domain(self):
"""
proxy = self._get_fake_proxy()
os.makedirs(os.path.join(self.hosts_directory, self.default_domain))
redirect_file = os.path.join(
self.hosts_directory, self.default_domain, "redirect.json"
)
redirect_file = os.path.join(self.hosts_directory, self.default_domain, "redirect.json")
with open(redirect_file, "w") as r:
json.dump({self.default_domain: self.domain_1}, r)

Expand All @@ -90,11 +82,7 @@ def _test_add_host(self, proxy, host):
# get undecorated method with __wrapped__
proxy.add_host(host, "www.test.com", {})

self.assertTrue(
os.path.exists(
os.path.join(proxy.hosts_directory, host, "map.json")
)
)
self.assertTrue(os.path.exists(os.path.join(proxy.hosts_directory, host, "map.json")))

def test_add_hosts_works_without_hosts_dir(self):
"""Ensure add_host works when hosts directory doesn't exist."""
Expand All @@ -117,9 +105,7 @@ def test_add_hosts_works_with_host_dir(self):

def _test_add_upstream(self, proxy, upstream):
upstream_dir = os.path.join(proxy.upstreams_directory, upstream)
with patch.object(
Proxy, "add_upstream", new=Proxy.add_upstream.__wrapped__
):
with patch.object(Proxy, "add_upstream", new=Proxy.add_upstream.__wrapped__):
# get undecorated method with __wrapped__
proxy.add_upstream(upstream)
self.assertTrue(os.path.exists(upstream_dir))
Expand Down Expand Up @@ -147,9 +133,7 @@ def test_remove_redirect_for_default_domain_deletes_host_dir(self):
with open(redir_file, "w") as r:
json.dump({self.default_domain: self.domain_1}, r)

with patch.object(
Proxy, "remove_redirect", new=Proxy.remove_redirect.__wrapped__
):
with patch.object(Proxy, "remove_redirect", new=Proxy.remove_redirect.__wrapped__):
proxy.remove_redirect(self.default_domain)
self.assertFalse(os.path.exists(redir_file))
self.assertFalse(os.path.exists(host_dir))
Expand All @@ -160,9 +144,7 @@ def test_setup_redirect_creates_redirect_json_for_given_hosts(self):
proxy.domain = self.tld
host = self.domain_2
target = self.domain_1
with patch.object(
Proxy, "setup_redirect", new=Proxy.setup_redirect.__wrapped__
):
with patch.object(Proxy, "setup_redirect", new=Proxy.setup_redirect.__wrapped__):
proxy.setup_redirect(host, target)
host_dir = os.path.join(proxy.hosts_directory, host)
redir_file = os.path.join(host_dir, "redirect.json")
Expand All @@ -174,14 +156,10 @@ def test_remove_redirect_deletes_redirect_json_for_given_hosts(self):
proxy.domain = self.tld
host = self.domain_2
target = self.domain_1
with patch.object(
Proxy, "setup_redirect", new=Proxy.setup_redirect.__wrapped__
):
with patch.object(Proxy, "setup_redirect", new=Proxy.setup_redirect.__wrapped__):
proxy.setup_redirect(host, target)
# assume that setup redirects works properly based on previous test
with patch.object(
Proxy, "remove_redirect", new=Proxy.remove_redirect.__wrapped__
):
with patch.object(Proxy, "remove_redirect", new=Proxy.remove_redirect.__wrapped__):
proxy.remove_redirect(host)
host_dir = os.path.join(proxy.hosts_directory, host)
redir_file = os.path.join(host_dir, "redirect.json")
Expand Down Expand Up @@ -221,15 +199,11 @@ def test_rename_on_site_host_renames_redirect_json(self):
"rename_site_in_host_dir",
new=Proxy.rename_site_in_host_dir.__wrapped__,
):
proxy.rename_site_in_host_dir(
"yyy.frappe.cloud", self.default_domain, "yyy.frappe.cloud"
)
proxy.rename_site_in_host_dir("yyy.frappe.cloud", self.default_domain, "yyy.frappe.cloud")
new_host_dir = os.path.join(proxy.hosts_directory, "yyy.frappe.cloud")
redirect_file = os.path.join(new_host_dir, "redirect.json")
with open(redirect_file) as r:
self.assertDictEqual(
json.load(r), {"yyy.frappe.cloud": self.domain_1}
)
self.assertDictEqual(json.load(r), {"yyy.frappe.cloud": self.domain_1})

def test_rename_updates_map_json_of_custom(self):
"""Ensure custom domains have map.json updated on site rename."""
Expand All @@ -239,15 +213,11 @@ def test_rename_updates_map_json_of_custom(self):
"rename_site_in_host_dir",
new=Proxy.rename_site_in_host_dir.__wrapped__,
):
proxy.rename_site_in_host_dir(
self.domain_1, self.default_domain, "yyy.frappe.cloud"
)
proxy.rename_site_in_host_dir(self.domain_1, self.default_domain, "yyy.frappe.cloud")
host_directory = os.path.join(proxy.hosts_directory, self.domain_1)
map_file = os.path.join(host_directory, "map.json")
with open(map_file) as m:
self.assertDictEqual(
json.load(m), {self.domain_1: "yyy.frappe.cloud"}
)
self.assertDictEqual(json.load(m), {self.domain_1: "yyy.frappe.cloud"})

def test_rename_updates_redirect_json_of_custom(self):
"""Ensure redirect.json updated for domains redirected to default."""
Expand All @@ -261,14 +231,10 @@ def test_rename_updates_redirect_json_of_custom(self):
"rename_site_in_host_dir",
new=Proxy.rename_site_in_host_dir.__wrapped__,
):
proxy.rename_site_in_host_dir(
self.domain_1, self.default_domain, "yyy.frappe.cloud"
)
proxy.rename_site_in_host_dir(self.domain_1, self.default_domain, "yyy.frappe.cloud")
redirect_file = os.path.join(host_directory, "redirect.json")
with open(redirect_file) as r:
self.assertDictEqual(
json.load(r), {self.domain_1: "yyy.frappe.cloud"}
)
self.assertDictEqual(json.load(r), {self.domain_1: "yyy.frappe.cloud"})

def test_rename_does_not_update_redirect_json_of_custom(self):
"""Test redirects not updated for domains not redirected to default."""
Expand All @@ -283,9 +249,7 @@ def test_rename_does_not_update_redirect_json_of_custom(self):
"rename_site_in_host_dir",
new=Proxy.rename_site_in_host_dir.__wrapped__,
):
proxy.rename_site_in_host_dir(
self.domain_1, self.default_domain, "yyy.frappe.cloud"
)
proxy.rename_site_in_host_dir(self.domain_1, self.default_domain, "yyy.frappe.cloud")
with open(redirect_file) as r:
self.assertDictEqual(json.load(r), original_dict)

Expand All @@ -300,12 +264,8 @@ def test_rename_does_not_update_partial_strings(self):
"rename_site_in_host_dir",
new=Proxy.rename_site_in_host_dir.__wrapped__,
):
proxy.rename_site_in_host_dir(
self.domain_1, self.default_domain, "yyy.frappe.cloud"
)
proxy.rename_site_in_host_dir(self.domain_1, self.default_domain, "yyy.frappe.cloud")
host_dir = os.path.join(self.hosts_directory, self.domain_1)
map_file = os.path.join(host_dir, "map.json")
with open(map_file) as m:
self.assertDictEqual(
json.load(m), {self.domain_1: "yyy.frappe.cloud"}
)
self.assertDictEqual(json.load(m), {self.domain_1: "yyy.frappe.cloud"})

0 comments on commit 7b1ef1e

Please sign in to comment.