Skip to content

Commit

Permalink
Merge pull request avocado-framework#3896 from bgartzi/env_process_re…
Browse files Browse the repository at this point in the history
…factoring-firewalld

env_process: Refactor firewalld setup/cleanup into a Setuper
  • Loading branch information
YongxueHong authored May 9, 2024
2 parents 17b30d8 + 3ceca49 commit 0c3a0a0
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 29 deletions.
36 changes: 7 additions & 29 deletions virttest/env_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from avocado.core import exceptions
from avocado.utils import archive
from avocado.utils import cpu as cpu_utils
from avocado.utils import crypto, distro, path
from avocado.utils import crypto, path
from avocado.utils import process as a_process
from six.moves import xrange

Expand All @@ -32,7 +32,6 @@
qemu_storage,
storage,
test_setup,
utils_iptables,
utils_kernel_module,
utils_libguestfs,
utils_logfile,
Expand All @@ -46,10 +45,13 @@

# lazy imports for dependencies that are not needed in all modes of use
from virttest._wrappers import lazy_import
from virttest.staging import service
from virttest.test_setup.core import SetupManager
from virttest.test_setup.libvirt_setup import LibvirtdDebugLogConfig
from virttest.test_setup.networking import BridgeConfig, NetworkProxies
from virttest.test_setup.networking import (
BridgeConfig,
FirewalldService,
NetworkProxies,
)
from virttest.test_setup.os_posix import UlimitConfig
from virttest.test_setup.storage import StorageConfig
from virttest.utils_conn import SSHConnection
Expand Down Expand Up @@ -1098,37 +1100,13 @@ def preprocess(test, params, env):
_setup_manager.register(LibvirtdDebugLogConfig)
_setup_manager.register(BridgeConfig)
_setup_manager.register(StorageConfig)
_setup_manager.register(FirewalldService)
_setup_manager.do_setup()

vm_type = params.get("vm_type")

base_dir = data_dir.get_data_dir()

firewalld_service = params.get("firewalld_service")
if firewalld_service == "disable":
firewalld = service.Service("firewalld")
if firewalld.status():
firewalld.stop()
if firewalld.status():
test.log.warning("Failed to stop firewalld")
else:
if firewalld_service == "enable":
firewalld = service.Service("firewalld")
if not firewalld.status():
firewalld.start()
if not firewalld.status():
test.log.warning("Failed to start firewalld")

if distro.detect().name == "Ubuntu":
params["firewalld_dhcp_workaround"] = "no"

# Workaround know issue where firewall blocks dhcp from guest
# through virbr0
if params.get("firewalld_dhcp_workaround", "no") == "yes":
firewall_cmd = utils_iptables.Firewall_cmd()
if not firewall_cmd.add_service("dhcp", permanent=True):
test.log.warning("Failed to add dhcp service to be permitted")

# Start ip sniffing if it isn't already running
# The fact it has to be started here is so that the test params
# have to be honored.
Expand Down
35 changes: 35 additions & 0 deletions virttest/test_setup/networking.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
import re
import urllib.request

from avocado.utils import distro

from virttest import utils_iptables
from virttest.staging import service
from virttest.test_setup import PrivateBridgeConfig, PrivateOvsBridgeConfig
from virttest.test_setup.core import Setuper

Expand Down Expand Up @@ -70,3 +74,34 @@ def cleanup(self):
else:
brcfg = PrivateBridgeConfig(params_pb)
brcfg.cleanup()


class FirewalldService(Setuper):
def setup(self):
firewalld_service = self.params.get("firewalld_service")
if firewalld_service == "disable":
firewalld = service.Service("firewalld")
if firewalld.status():
firewalld.stop()
if firewalld.status():
self.test.log.warning("Failed to stop firewalld")
else:
if firewalld_service == "enable":
firewalld = service.Service("firewalld")
if not firewalld.status():
firewalld.start()
if not firewalld.status():
self.test.log.warning("Failed to start firewalld")

if distro.detect().name == "Ubuntu":
self.params["firewalld_dhcp_workaround"] = "no"

# Workaround know issue where firewall blocks dhcp from guest
# through virbr0
if self.params.get("firewalld_dhcp_workaround", "no") == "yes":
firewall_cmd = utils_iptables.Firewall_cmd()
if not firewall_cmd.add_service("dhcp", permanent=True):
self.test.log.warning("Failed to add dhcp service to be permitted")

def cleanup(self):
pass

0 comments on commit 0c3a0a0

Please sign in to comment.