Get rid of old hashes of file that 'are expected to be removed' and are indeed 'already removed'

This commit is contained in:
Alexandre Aubin 2020-05-28 21:37:15 +02:00
parent 5e877449ed
commit 28a922de51
2 changed files with 56 additions and 5 deletions

View file

@ -503,6 +503,12 @@ def _update_conf_hashes(category, hashes):
if category_conf is None: if category_conf is None:
category_conf = {} category_conf = {}
# If a file shall be removed and is indeed removed, forget entirely about
# that path.
# It avoid keeping weird old entries like
# /etc/nginx/conf.d/some.domain.that.got.removed.conf
hashes = {path: hash_ for path, hash_ in hashes.items() if hash_ is not None or os.path.exists(path)}
category_conf['conffiles'] = hashes category_conf['conffiles'] = hashes
categories[category] = category_conf categories[category] = category_conf
_save_regenconf_infos(categories) _save_regenconf_infos(categories)

View file

@ -1,8 +1,4 @@
import glob
import os import os
import pytest
import shutil
import requests
from conftest import message, raiseYunohostError from conftest import message, raiseYunohostError
@ -14,7 +10,8 @@ from yunohost.utils.error import YunohostError
from yunohost.regenconf import manually_modified_files, regen_conf, _get_conf_hashes, _force_clear_hashes from yunohost.regenconf import manually_modified_files, regen_conf, _get_conf_hashes, _force_clear_hashes
TEST_DOMAIN = "secondarydomain.test" TEST_DOMAIN = "secondarydomain.test"
TEST_DOMAIN_NGINX_CONFIG = "/etc/nginx/conf.d/secondarydomain.test.conf" TEST_DOMAIN_NGINX_CONFIG = "/etc/nginx/conf.d/%s.conf" % TEST_DOMAIN
TEST_DOMAIN_DNSMASQ_CONFIG = "/etc/dnsmasq.d/%s" % TEST_DOMAIN
SSHD_CONFIG = "/etc/ssh/sshd_config" SSHD_CONFIG = "/etc/ssh/sshd_config"
def setup_function(function): def setup_function(function):
@ -22,11 +19,13 @@ def setup_function(function):
_force_clear_hashes([TEST_DOMAIN_NGINX_CONFIG]) _force_clear_hashes([TEST_DOMAIN_NGINX_CONFIG])
clean() clean()
def teardown_function(function): def teardown_function(function):
clean() clean()
_force_clear_hashes([TEST_DOMAIN_NGINX_CONFIG]) _force_clear_hashes([TEST_DOMAIN_NGINX_CONFIG])
def clean(): def clean():
assert os.system("pgrep slapd >/dev/null") == 0 assert os.system("pgrep slapd >/dev/null") == 0
@ -116,3 +115,49 @@ def test_ssh_conf_unmanaged_and_manually_modified(mocker):
assert SSHD_CONFIG in _get_conf_hashes("ssh") assert SSHD_CONFIG in _get_conf_hashes("ssh")
assert SSHD_CONFIG not in manually_modified_files() assert SSHD_CONFIG not in manually_modified_files()
def test_stale_hashes_get_removed_if_empty():
"""
This is intended to test that if a file gets removed and is indeed removed,
we don't keep a useless empty hash corresponding to an old file.
In this case, we test this using the dnsmasq conf file (we don't do this
using the nginx conf file because it's already force-removed during
domain_remove())
"""
domain_add(TEST_DOMAIN)
assert os.path.exists(TEST_DOMAIN_DNSMASQ_CONFIG)
assert TEST_DOMAIN_DNSMASQ_CONFIG in _get_conf_hashes("dnsmasq")
domain_remove(TEST_DOMAIN)
assert not os.path.exists(TEST_DOMAIN_DNSMASQ_CONFIG)
assert TEST_DOMAIN_DNSMASQ_CONFIG not in _get_conf_hashes("dnsmasq")
def test_stale_hashes_if_file_manually_deleted():
"""
Same as other test, but manually delete the file in between and check
behavior
"""
domain_add(TEST_DOMAIN)
assert os.path.exists(TEST_DOMAIN_DNSMASQ_CONFIG)
assert TEST_DOMAIN_DNSMASQ_CONFIG in _get_conf_hashes("dnsmasq")
os.remove(TEST_DOMAIN_DNSMASQ_CONFIG)
assert not os.path.exists(TEST_DOMAIN_DNSMASQ_CONFIG)
regen_conf(names=["dnsmasq"])
assert not os.path.exists(TEST_DOMAIN_DNSMASQ_CONFIG)
assert TEST_DOMAIN_DNSMASQ_CONFIG in _get_conf_hashes("dnsmasq")
domain_remove(TEST_DOMAIN)
assert not os.path.exists(TEST_DOMAIN_DNSMASQ_CONFIG)
assert TEST_DOMAIN_DNSMASQ_CONFIG not in _get_conf_hashes("dnsmasq")