예제 #1
0
class RebootTestCase(BaseFeatureCase):

    @mock.patch('pyhpecw7.comware.HPCOM7')
    def setUp(self, mock_device):
        self.device = mock_device
        self.reboot = Reboot(self.device)

    def test_build(self):
        self.reboot.build(reboot=True)
        self.device.cli_display.assert_called_with(['reboot force'])

        self.reboot.build(stage=True, reboot=True)
        self.device.stage_config.assert_called_with(['reboot force'], 'cli_display')

    def test_build_delay(self):
        self.reboot.build(reboot=True, delay='20')
        self.device.cli_display.assert_called_with(['scheduler reboot delay 20'])

        self.reboot.build(stage=True, delay='20', reboot=True)
        self.device.stage_config.assert_called_with(['scheduler reboot delay 20'], 'cli_display')

    def test_build_at_time(self):
        self.reboot.build(reboot=True, time='10')
        self.device.cli_display.assert_called_with(['scheduler reboot at 10'])

        self.reboot.build(stage=True, time='10', reboot=True)
        self.device.stage_config.assert_called_with(['scheduler reboot at 10'], 'cli_display')

    def test_build_at_time_and_date(self):
        self.reboot.build(reboot=True, time='10', date='15')
        self.device.cli_display.assert_called_with(['scheduler reboot at 10 15'])

        self.reboot.build(stage=True, time='10', date='15', reboot=True)
        self.device.stage_config.assert_called_with(['scheduler reboot at 10 15'], 'cli_display')

    def test_param_check_time(self):
        result = self.reboot.param_check(time='03:02')
        self.assertEqual(result, None)

        with self.assertRaises(RebootTimeError):
            self.reboot.param_check(time='20')

        with self.assertRaises(RebootTimeError):
            self.reboot.param_check(time='0::03')

        with self.assertRaises(RebootTimeError):
            self.reboot.param_check(time='0:203')

    def test_param_check_date(self):
        result = self.reboot.param_check(date='03/02/2939')
        self.assertEqual(result, None)

        with self.assertRaises(RebootDateError):
            self.reboot.param_check(date='03.02.2939')

        with self.assertRaises(RebootDateError):
            self.reboot.param_check(date='03/02/2939/3929')

        with self.assertRaises(RebootDateError):
            self.reboot.param_check(date='003/02/2939')

        with self.assertRaises(RebootDateError):
            self.reboot.param_check(date='03/002/2939')

        with self.assertRaises(RebootDateError):
            self.reboot.param_check(date='03/02/29393')
예제 #2
0
class RebootTestCase(BaseFeatureCase):
    @mock.patch('pyhpecw7.comware.HPCOM7')
    def setUp(self, mock_device):
        self.device = mock_device
        self.reboot = Reboot(self.device)

    def test_build(self):
        self.reboot.build(reboot=True)
        self.device.cli_display.assert_called_with(['reboot force'])

        self.reboot.build(stage=True, reboot=True)
        self.device.stage_config.assert_called_with(['reboot force'],
                                                    'cli_display')

    def test_build_delay(self):
        self.reboot.build(reboot=True, delay='20')
        self.device.cli_display.assert_called_with(
            ['scheduler reboot delay 20'])

        self.reboot.build(stage=True, delay='20', reboot=True)
        self.device.stage_config.assert_called_with(
            ['scheduler reboot delay 20'], 'cli_display')

    def test_build_at_time(self):
        self.reboot.build(reboot=True, time='10')
        self.device.cli_display.assert_called_with(['scheduler reboot at 10'])

        self.reboot.build(stage=True, time='10', reboot=True)
        self.device.stage_config.assert_called_with(['scheduler reboot at 10'],
                                                    'cli_display')

    def test_build_at_time_and_date(self):
        self.reboot.build(reboot=True, time='10', date='15')
        self.device.cli_display.assert_called_with(
            ['scheduler reboot at 10 15'])

        self.reboot.build(stage=True, time='10', date='15', reboot=True)
        self.device.stage_config.assert_called_with(
            ['scheduler reboot at 10 15'], 'cli_display')

    def test_param_check_time(self):
        result = self.reboot.param_check(time='03:02')
        self.assertEqual(result, None)

        with self.assertRaises(RebootTimeError):
            self.reboot.param_check(time='20')

        with self.assertRaises(RebootTimeError):
            self.reboot.param_check(time='0::03')

        with self.assertRaises(RebootTimeError):
            self.reboot.param_check(time='0:203')

    def test_param_check_date(self):
        result = self.reboot.param_check(date='03/02/2939')
        self.assertEqual(result, None)

        with self.assertRaises(RebootDateError):
            self.reboot.param_check(date='03.02.2939')

        with self.assertRaises(RebootDateError):
            self.reboot.param_check(date='03/02/2939/3929')

        with self.assertRaises(RebootDateError):
            self.reboot.param_check(date='003/02/2939')

        with self.assertRaises(RebootDateError):
            self.reboot.param_check(date='03/002/2939')

        with self.assertRaises(RebootDateError):
            self.reboot.param_check(date='03/02/29393')
예제 #3
0
def main():
    module = AnsibleModule(argument_spec=dict(
        reboot=dict(required=True, choices=BOOLEANS, type='bool'),
        delay=dict(required=False, type='str'),
        date=dict(required=False, type='str'),
        time=dict(required=False, type='str'),
        port=dict(default=830, type='int'),
        hostname=dict(required=True),
        username=dict(required=True),
        password=dict(required=True),
    ),
                           supports_check_mode=True)

    if not HAS_PYHP:
        module.fail_json(msg='There was a problem loading from the pyhpecw7 ' +
                         'module.',
                         error=str(ie))

    username = module.params['username']
    password = module.params['password']
    port = module.params['port']
    hostname = socket.gethostbyname(module.params['hostname'])

    device_args = dict(host=hostname,
                       username=username,
                       password=password,
                       port=port)

    device = HPCOM7(**device_args)

    reboot = module.params['reboot']
    delay = module.params['delay']
    date = module.params['date']
    time = module.params['time']

    if date:
        if not time:
            module.fail_json(msg='time is also required when specifying date')

    proposed = dict(reboot=reboot, delay=delay, time=time, date=date)

    changed = False

    try:
        device.open()
    except ConnectionError as e:
        safe_fail(module,
                  device,
                  msg=str(e),
                  descr='error opening connection to device')

    try:
        reboot_me = Reboot(device)
        reboot_me.param_check(**proposed)
    except RebootDateError as rde:
        safe_fail(module, device, msg=str(rde))
    except RebootTimeError as rte:
        safe_fail(module, device, msg=str(rte))
    except PYHPError as e:
        safe_fail(module,
                  device,
                  msg=str(e),
                  descr='error using Reboot object')

    reboot_me.build(stage=True, **proposed)

    commands = None
    response = None
    changed = False

    results = {}

    if device.staged:
        commands = device.staged_to_string()
        if module.check_mode:
            safe_exit(module, device, changed=True, commands=commands)
        else:
            try:
                response = device.execute_staged()
                changed = True
            except PYHPError as e:
                if isinstance(e, NCTimeoutError):
                    results['changed'] = True
                    results['rebooted'] = True
                    results['commands'] = commands
                    module.exit_json(**results)
                else:
                    safe_fail(module,
                              device,
                              msg=str(e),
                              descr='error during execution')

    results['proposed'] = proposed
    results['commands'] = commands
    results['changed'] = changed
    results['end_state'] = 'N/A for this module'
    results['response'] = response

    safe_exit(module, device, **results)
def main():
    module = AnsibleModule(
        argument_spec=dict(
            reboot=dict(required=True, choices=BOOLEANS, type='bool'),
            delay=dict(required=False, type='str'),
            date=dict(required=False, type='str'),
            time=dict(required=False, type='str'),
            port=dict(default=830, type='int'),
            hostname=dict(required=True),
            username=dict(required=True),
            password=dict(required=True),
        ),
        supports_check_mode=True
    )

    if not HAS_PYHP:
        module.fail_json(msg='There was a problem loading from the pyhpecw7 '
                         + 'module.', error=str(ie))

    username = module.params['username']
    password = module.params['password']
    port = module.params['port']
    hostname = socket.gethostbyname(module.params['hostname'])

    device_args = dict(host=hostname, username=username,
                       password=password, port=port)

    device = HPCOM7(**device_args)

    reboot = module.params['reboot']
    delay = module.params['delay']
    date = module.params['date']
    time = module.params['time']

    if date:
        if not time:
            module.fail_json(msg='time is also required when specifying date')

    proposed = dict(reboot=reboot, delay=delay,
                    time=time, date=date)

    changed = False

    try:
        device.open()
    except ConnectionError as e:
        safe_fail(module, device, msg=str(e),
                  descr='error opening connection to device')

    try:
        reboot_me = Reboot(device)
        reboot_me.param_check(**proposed)
    except RebootDateError as rde:
        safe_fail(module, device, msg=str(rde))
    except RebootTimeError as rte:
        safe_fail(module, device, msg=str(rte))
    except PYHPError as e:
        safe_fail(module, device, msg=str(e),
                  descr='error using Reboot object')

    reboot_me.build(stage=True, **proposed)

    commands = None
    response = None
    changed = False

    results = {}

    if device.staged:
        commands = device.staged_to_string()
        if module.check_mode:
            safe_exit(module, device, changed=True,
                      commands=commands)
        else:
            try:
                response = device.execute_staged()
                changed = True
            except PYHPError as e:
                if isinstance(e, NCTimeoutError):
                    results['changed'] = True
                    results['rebooted'] = True
                    results['commands'] = commands
                    module.exit_json(**results)
                else:
                    safe_fail(module, device, msg=str(e),
                              descr='error during execution')

    results['proposed'] = proposed
    results['commands'] = commands
    results['changed'] = changed
    results['end_state'] = 'N/A for this module'
    results['response'] = response

    safe_exit(module, device, **results)