-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
reloader: Do not apply config if target dir is unmounted
If the target dir is unmounted but is a very specific mount, the config should not be applied, because either the target directory contains incorrect files or it is empty.
- Loading branch information
Showing
4 changed files
with
180 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import json | ||
import subprocess | ||
|
||
|
||
def directory_is_unmounted(path): | ||
output = subprocess.check_output( | ||
["systemctl", "list-units", "-t", "mount", "--all", "-o", "json"], | ||
encoding="utf-8", | ||
) | ||
units = json.loads(output) | ||
for unit in units: | ||
if unit["description"] == path: | ||
return unit["active"] != "active" or unit["sub"] != "mounted" | ||
return False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import json | ||
|
||
from nginx_config_reloader import directory_is_unmounted | ||
from tests.testcase import TestCase | ||
|
||
|
||
class TestDirectoryIsUnmounted(TestCase): | ||
def setUp(self): | ||
self.check_output = self.set_up_patch( | ||
"nginx_config_reloader.utils.subprocess.check_output", | ||
return_value=json.dumps( | ||
[ | ||
{ | ||
"unit": "-.mount", | ||
"load": "loaded", | ||
"active": "active", | ||
"sub": "mounted", | ||
"description": "Root Mount", | ||
}, | ||
{ | ||
"unit": "data-web-nginx.mount", | ||
"load": "loaded", | ||
"active": "active", | ||
"sub": "mounted", | ||
"description": "/data/web/nginx", | ||
}, | ||
] | ||
), | ||
) | ||
|
||
def test_it_calls_systemctl_list_units(self): | ||
directory_is_unmounted("/data/web/nginx") | ||
|
||
self.check_output.assert_called_once_with( | ||
["systemctl", "list-units", "-t", "mount", "--all", "-o", "json"], | ||
encoding="utf-8", | ||
) | ||
|
||
def test_it_returns_false_if_no_mount_found(self): | ||
self.check_output.return_value = json.dumps( | ||
[ | ||
{ | ||
"unit": "-.mount", | ||
"load": "loaded", | ||
"active": "active", | ||
"sub": "mounted", | ||
"description": "Root Mount", | ||
}, | ||
] | ||
) | ||
|
||
self.assertFalse(directory_is_unmounted("/data/web/nginx")) | ||
|
||
def test_it_returns_false_if_mount_exists_active_mounted(self): | ||
self.assertFalse(directory_is_unmounted("/data/web/nginx")) | ||
|
||
def test_it_returns_true_if_mount_exists_not_active(self): | ||
self.check_output.return_value = json.dumps( | ||
[ | ||
{ | ||
"unit": "-.mount", | ||
"load": "loaded", | ||
"active": "active", | ||
"sub": "mounted", | ||
"description": "Root Mount", | ||
}, | ||
{ | ||
"unit": "data-web-nginx.mount", | ||
"load": "loaded", | ||
"active": "inactive", | ||
"sub": "dead", | ||
"description": "/data/web/nginx", | ||
}, | ||
] | ||
) | ||
|
||
self.assertTrue(directory_is_unmounted("/data/web/nginx")) | ||
|
||
def test_it_returns_true_if_mount_exists_active_not_mounted(self): | ||
self.check_output.return_value = json.dumps( | ||
[ | ||
{ | ||
"unit": "-.mount", | ||
"load": "loaded", | ||
"active": "active", | ||
"sub": "mounted", | ||
"description": "Root Mount", | ||
}, | ||
{ | ||
"unit": "data-web-nginx.mount", | ||
"load": "loaded", | ||
"active": "active", | ||
"sub": "dead", | ||
"description": "/data/web/nginx", | ||
}, | ||
] | ||
) | ||
|
||
self.assertTrue(directory_is_unmounted("/data/web/nginx")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters