How to use systemd_command_generator method in avocado

Best Python code snippet using avocado_python

service.py

Source:service.py Github

copy

Full Screen

...231 return set_target_command232 def method(service_name):233 return [command_name, service_name, command]234 return method235def systemd_command_generator(command):236 """237 Generate list of command line argument strings for systemctl.238 One argument per string for compatibility Popen239 WARNING: If systemctl detects that it is running on a tty it will use color,240 pipe to $PAGER, change column sizes and not truncate unit names.241 Use --no-pager to suppress pager output, or set PAGER=cat in the environment.242 You may need to take other steps to suppress color output.243 See https://bugzilla.redhat.com/show_bug.cgi?id=713567244 :param command: start,stop,restart, etc.245 :type command: str246 :return: list of command and arguments to pass to utils.run or similar functions247 :rtype: list248 """249 command_name = "systemctl"...

Full Screen

Full Screen

service_lib.py

Source:service_lib.py Github

copy

Full Screen

...18from tempfile import mktemp19USAGE = """20import service_lib21# to generate raw list of str commands, e.g. ["systemctl", "start", "networkd"]22systemd_cmd_gen = service_lib.systemd_command_generator()23cmd = " ".join(systemd_cmd_gen.start("networkd"))24# to directly call cli_send_command but still accepts kwargs for cli_send_command25networkd = service_lib.SpecificServiceManager("networkd", self.cli_send_command)26networkd.stop(expected_rcs={0, 3})27networkd.start(expected_rcs={1})28# to directly call cli_send_command but still accepts kwargs for cli_send_command29systemd = service_lib.systemd_manager_factory(self.cli_send_command)30systemd.stop("networkd", expected_rcs={0, 3})31systemd.list(expected_rcs={0, 3})32systemd.start(service="networkd", expected_rcs={0, 3})33systemd.change_default_runlevel("rescue.target")34"""35COMMAND_TABLE_DOC = """36service frobozz start37systemctl start frobozz.service38 Used to start a service (not reboot persistent)39service frobozz stop40systemctl stop frobozz.service41 Used to stop a service (not reboot persistent)42service frobozz restart43systemctl restart frobozz.service44 Used to stop and then start a service45service frobozz reload46systemctl reload frobozz.service47 When supported, reloads the config file without interrupting pending operations.48service frobozz condrestart49systemctl condrestart frobozz.service50 Restarts if the service is already running.51service frobozz status52systemctl status frobozz.service53 Tells whether a service is currently running.54ls /etc/rc.d/init.d/55systemctl list-unit-files --type=service (preferred)56 Used to list the services that can be started or stopped57ls /lib/systemd/system/*.service /etc/systemd/system/*.service58 Used to list all the services and other units59chkconfig frobozz on60systemctl enable frobozz.service61 Turn the service on, for start at next boot, or other trigger.62chkconfig frobozz off63systemctl disable frobozz.service64 Turn the service off for the next reboot, or any other trigger.65chkconfig frobozz66systemctl is-enabled frobozz.service67 Used to check whether a service is configured to start or not in the current environment.68chkconfig --list69systemctl list-unit-files --type=service(preferred)70ls /etc/systemd/system/*.wants/71 Print a table of services that lists which runlevels each is configured on or off72chkconfig frobozz --list73ls /etc/systemd/system/*.wants/frobozz.service74 Used to list what levels this service is configured on or off75chkconfig frobozz --add76systemctl daemon-reload77 Used when you create a new service file or modify any configuration78"""79class ReturnCodes(object):80 SUCCESS = 081 RUNNING = 082 STOPPED = 383 UNKNOWN = None84class SystemdReturnCodes(ReturnCodes):85 pass86REPLACE_COMMAND_LIST = {87 'is_enabled',88 'is_active',89 'daemon_reload',90}91COMMANDS = {92 "start",93 "stop",94 "reload",95 "restart",96 "condrestart",97 "status",98 "enable",99 "disable",100 "is_enabled",101 "is_active",102 "list",103 "daemon_reload",104 "cat"105}106def systemd_command_generator(command):107 command_name = "systemctl"108 if command in REPLACE_COMMAND_LIST:109 command = command.replace('_', '-')110 if command == "list":111 # noinspection PyUnusedLocal112 def list_command(_):113 return [command_name, "list-unit-files", "--type=service"]114 return list_command115 elif command == "daemon-reload":116 def daemon_reload_command(*_):117 return [command_name, command, '']118 return daemon_reload_command119 def method(service_name):120 return [command_name, command, "{}.service".format(service_name)]...

Full Screen

Full Screen

test_service_lib.py

Source:test_service_lib.py Github

copy

Full Screen

1# Copyright (c) 2013 - 2017, Intel Corporation.2#3# Licensed under the Apache License, Version 2.0 (the "License");4# you may not use this file except in compliance with the License.5# You may obtain a copy of the License at6#7# http://www.apache.org/licenses/LICENSE-2.08#9# Unless required by applicable law or agreed to in writing, software10# distributed under the License is distributed on an "AS IS" BASIS,11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.12# See the License for the specific language governing permissions and13# limitations under the License.14"""``test_service_lib.py``15`SystemD service library unittests`16"""17import unittest18from testlib.linux import service_lib19from unittest.mock import (patch, MagicMock)20__author__ = 'rbbratta'21class TestSystemd(unittest.TestCase):22 def setUp(self):23 self.service_name = "fake_service"24 return_codes = service_lib.SystemdReturnCodes25 self.service_command_generator = service_lib.ServiceCommandGenerator(26 service_lib.systemd_command_generator,27 return_codes,28 )29 def test_all_commands(self):30 for cmd in (c for c in self.service_command_generator.commands31 if c not in ["list", "daemon_reload"]):32 ret = getattr(33 self.service_command_generator, cmd)(self.service_name)34 assert ret == ["systemctl", cmd.replace('_', '-'), "%s.service" % self.service_name]35class TestSpecificServiceManager(unittest.TestCase):36 def setUp(self):37 self.run_mock = MagicMock()38 self.service_manager = service_lib.SpecificServiceManager("lldpad", self.run_mock)39 def test_start(self):40 service = "lldpad"41 # should really use --generated-members, but start() is too generic42 self.service_manager.start() # pylint: disable=no-member43 assert self.run_mock.call_args[0][0] == "systemctl start %s.service" % service44 def test_stop_with_args(self):45 service = "lldpad"46 self.service_manager.stop(ignore_status=True) # pylint: disable=no-member47 assert self.run_mock.call_args[0][0] == "systemctl stop %s.service" % service48 assert self.run_mock.call_args[1] == {'ignore_status': True}49 def test_list_is_not_present_in_SpecificServiceManager(self):50 assert not hasattr(self.service_manager, "list")51class TestSystemdServiceManager(unittest.TestCase):52 def setUp(self):53 self.run_mock = MagicMock()54 self.service_manager = service_lib.SystemdServiceManager(self.run_mock)55 def test_start(self):56 service = "lldpad"57 self.service_manager.start(service) # pylint: disable=no-member58 assert self.run_mock.call_args[0][59 0] == "systemctl start %s.service" % service60 def test_list(self):61 self.service_manager.list() # pylint: disable=no-member62 assert self.run_mock.call_args[0][63 0] == "systemctl list-unit-files --type=service"64 def test_set_default_runlevel(self):65 runlevel = "multi-user.target"66 mktemp_mock = MagicMock(return_value="temp_filename")67 symlink_mock = MagicMock()68 rename_mock = MagicMock()69 @patch.object(service_lib, "mktemp", mktemp_mock)70 @patch("os.symlink", symlink_mock)71 @patch("os.rename", rename_mock)72 def _():73 self.service_manager.change_default_runlevel(runlevel)74 assert mktemp_mock.called75 assert symlink_mock.call_args[0][0] == "/usr/lib/systemd/system/multi-user.target"76 assert rename_mock.call_args[0][1] == "/etc/systemd/system/default.target"77 _()78_examples = """79 try:80 service.start("lldpad")81 pgrep("lldpad")82 except CmdError, c:83 c.err == "something"84 try:85 service.stop("fcoe", "force", ignoreStatus=True)86 if not pgrep("fcoemon"):87 pass88 except:89 pass90 try:91 service.stop("fcoe", "-HUP", ignoreStatus=True)92 except:93 pass94 try:95 service.start("boot.lldpad")96 except:97 pass98 try:99 try:100 service.start("ntp")101 except:102 pass...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run avocado automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful