Best Python code snippet using tempest_python
main_helpers_test.py
Source:main_helpers_test.py  
1# Copyright 2019 The Chromium Authors. All rights reserved.2# Use of this source code is governed by a BSD-style license that can be3# found in the LICENSE file.4import argparse5import mock6import random7import string8import unittest9from infra.services.swarm_docker import main_helpers10MAIN_HELPERS = 'infra.services.swarm_docker.main_helpers.'11class TestMainHelpers(unittest.TestCase):12  def setUp(self):13    self.args = argparse.Namespace(14        reboot_schedule=None, canary=False, image_name='swarm_docker:latest',15        registry_project='mock-registry', max_container_uptime=240,16        reboot_grace_period=240)17  def testGetUptime(self):18    uptime = '1440.75 103734.55'19    with mock.patch("__builtin__.open", mock.mock_open(read_data=uptime)) as _:20      self.assertEqual(main_helpers.get_host_uptime(), 24.0125)21  @mock.patch(MAIN_HELPERS + 'fuzz_max_uptime', return_value=60)22  @mock.patch(MAIN_HELPERS + 'get_host_uptime', return_value=70)23  @mock.patch(MAIN_HELPERS + 'reboot_host')24  def testRebootOnMaxHostUptime(self, reboot_host, _, __):25    self.args.max_host_uptime = 6026    self.assertTrue(main_helpers.reboot_gracefully(self.args, []))27    reboot_host.assert_called()28  @mock.patch(MAIN_HELPERS + 'fuzz_max_uptime', return_value=60)29  @mock.patch(MAIN_HELPERS + 'get_host_uptime', return_value=70)30  @mock.patch(MAIN_HELPERS + 'reboot_host')31  def testNoRebootWithContainers(self, reboot_host, _, __):32    self.args.max_host_uptime = 6033    self.assertTrue(main_helpers.reboot_gracefully(self.args, [mock.Mock()]))34    reboot_host.assert_not_called()35  @mock.patch(MAIN_HELPERS + 'fuzz_max_uptime', return_value=60)36  @mock.patch(MAIN_HELPERS + 'get_host_uptime', return_value=310)37  @mock.patch(MAIN_HELPERS + 'reboot_host')38  def testForceRebootAfterGracePeriod(self, reboot_host, _, __):39    self.args.max_host_uptime = 6040    self.assertTrue(main_helpers.reboot_gracefully(self.args, [mock.Mock()]))41    reboot_host.assert_called()42  @mock.patch(MAIN_HELPERS + 'fuzz_max_uptime', return_value=60)43  @mock.patch(MAIN_HELPERS + 'get_host_uptime', return_value=50)44  @mock.patch(MAIN_HELPERS + 'reboot_host')45  def testNoRebootBeforeMaxUptime(self, reboot_host, _, __):46    self.args.max_host_uptime = 6047    self.assertFalse(main_helpers.reboot_gracefully(self.args, [mock.Mock()]))48    reboot_host.assert_not_called()49  @mock.patch('socket.getfqdn')50  def test_deterministic_fuzz(self, mock_gethostname):51    hostname = 'some_hostname'52    fuzz_amount = 113  # md5sum'ed the hostname module 240 (20% of 1200)53    mock_gethostname.return_value = hostname54    fuzzed_max_uptime = main_helpers.fuzz_max_uptime(1200)55    self.assertEqual(fuzzed_max_uptime - 1200, fuzz_amount)56  @mock.patch('socket.getfqdn')57  def test_fuzz_range(self, mock_gethostname):58    # Test a bunch of random hostnames.59    for n in xrange(1, 101):60      hostname = ''.join([random.choice(string.lowercase) for _ in xrange(n)])61      mock_gethostname.return_value = hostname62      fuzzed_amount = main_helpers.fuzz_max_uptime(1200) - 120063      self.assertGreaterEqual(fuzzed_amount, 0)...reboot_host.py
Source:reboot_host.py  
...50'''51from ansible.module_utils.basic import AnsibleModule52from ..module_utils.exceptions import EmptySetException, SSLCertVerificationError53from ..module_utils.helper_functions import _configure_connection, get_host_id54def _reboot_host(module, api_instance):55    """56    Reboots the host57    """58    try:59        api_instance.reboot_host(60            get_host_id(61                module.params.get('name'),62                api_instance63            )64        )65        module.exit_json(changed=True)66    except SSLCertVerificationError:67        module.fail_json(msg="Failed to verify SSL certificate")68    except EmptySetException as err:69        module.fail_json(msg=f"Exception when calling UyuniAPI->reboot_host: {err}")70def main():71    argument_spec = dict(72        uyuni_host=dict(required=True),73        uyuni_user=dict(required=True),74        uyuni_password=dict(required=True, no_log=True),75        uyuni_port=dict(default=443, type='int'),76        uyuni_verify_ssl=dict(default=True, type='bool'),77        name=dict(required=True)78    )79    module = AnsibleModule(argument_spec=argument_spec)80    connection_params = dict(81        host=module.params.get('uyuni_host'),82        username=module.params.get('uyuni_user'),83        password=module.params.get('uyuni_password'),84        port=module.params.get('uyuni_port'),85        verify_ssl=module.params.get('uyuni_verify_ssl')86    )87    api_instance = _configure_connection(connection_params)88    _reboot_host(module, api_instance)89if __name__ == '__main__':...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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
