def test_install_os_image(mock_os, mock_con, mock_time, mock_wait_device, mock_onie_install, mock_wait_for_onie, device, cli_args): image_name = 'test_image' results = 'test result message' device.api.execute.return_value = (True, results) local_cb = cumulus_bootstrap.CumulusBootstrap(args['server'], cli_args) local_cb.dev = device local_cb.image_name = image_name sem_ver = semver.parse_version_info(device.facts['os_version']) if sem_ver >= (3, 0, 0): # Cumulus 3.x install command cmd = 'sudo onie-select -rf' method_calls = [ mock.call.execute([cmd]), mock.call.execute(['sudo reboot']) ] else: # Cumulus 2.x install command cmd = 'sudo /usr/cumulus/bin/cl-img-install -sf http://{server}/images/{os_name}/{image_name}'.format( server=local_cb.cli_args.server, os_name=_OS_NAME, image_name=image_name) method_calls = [mock.call.execute([cmd])] local_cb.install_os() assert device.api.method_calls == method_calls
def test_ensure_os_version(mock_wait_for_device, mock_get_os, mock_install_os, mock_time, device, cli_args): results = 'test result message' device.api.execute.return_value = (True, results) ver_required = '3.1.2' device_semver = semver.parse_version_info(device.facts['os_version']) image_name = 'image_file_name' def mock_get_os_function(dev): diff = semver.compare(dev.facts['os_version'], ver_required) # Check if upgrade is required if diff < 0: # upgrade required return {'image': image_name} else: # upgrade not required return {'image': None} mock_get_os.side_effect = mock_get_os_function local_cb = cumulus_bootstrap.CumulusBootstrap(args['server'], cli_args) local_cb.ensure_os_version(device) # If upgrade was required, check that the correct calls were made if mock_get_os_function(device)['image']: assert mock_install_os.called_with(mock.call(device), image_name=image_name) if device_semver < (3, 0, 0): device.api.execute.assert_called_with(['sudo reboot']) mock_wait_for_device.assert_called_with(countdown=local_cb.cli_args.reload_delay, poll_delay=10) else: # Ensure device was not rebooted if v3 or greater, and wait_for_device was called assert not device.api.execute.called else: assert not device.api.execute.called assert not mock_install_os.called
def test_install_os_image_not_all_good(mock_exit_results, mock_os, mock_con, device, cli_args): image_name = 'test_image' errmsg = 'error running command' device.api.execute.return_value = (False, errmsg) local_cb = cumulus_bootstrap.CumulusBootstrap(args['server'], cli_args) local_cb.dev = device local_cb.image_name = image_name sem_ver = semver.parse_version_info(device.facts['os_version']) if sem_ver >= (3, 0, 0): # Cumulus 3.x install command cmd = 'sudo onie-select -rf' else: # Cumulus 2.x install command cmd = 'sudo /usr/cumulus/bin/cl-img-install -sf http://{server}/images/{os_name}/{image_name}'.format( server=local_cb.cli_args.server, os_name=_OS_NAME, image_name=image_name) with pytest.raises(SystemExit): local_cb.install_os() mock_exit_results.assert_called_with( results={ 'ok': False, 'error_type': 'install', 'message': 'Unable to run command: {cmd}. ' 'Error message: {errmsg}'.format(cmd=cmd, errmsg=errmsg) })
def test_wait_for_onie_rescue_exception(mock_pxssh, mock_post_dev, mock_exit_results): error = 'Super weird error youve never seen before' mock_pxssh.return_value.login.side_effect = ExceptionPxssh(error) countdown = 1 poll_delay = 1 user = '******' target = cli_args().target mock_post_dev_calls = [ mock.call( message= 'Cumulus installation in progress. Waiting for boot to ONIE rescue mode. ' 'Timeout remaining: 1 seconds', state='AWAIT-ONLINE') ] local_cb = cumulus_bootstrap.CumulusBootstrap(args['server'], cli_args()) with pytest.raises(SystemExit): local_cb.wait_for_onie_rescue(countdown, poll_delay, user=user) mock_post_dev.assert_has_calls(mock_post_dev_calls) mock_exit_results.assert_called_with( results={ 'message': 'Error accessing {target} in ONIE rescue' ' mode: {error}.'.format(target=target, error=error), 'error_type': 'login', 'ok': False })
def test_wait_for_onie_rescue_pxsshexception(mock_pxssh, mock_post_dev, mock_exit_results, mock_time): mock_pxssh.return_value.login.side_effect = ExceptionPxssh( 'Could not establish connection to host') countdown = 1 poll_delay = 1 user = '******' mock_post_dev_calls = [ mock.call( message= 'Cumulus installation in progress. Waiting for boot to ONIE rescue mode. ' 'Timeout remaining: 1 seconds', state='AWAIT-ONLINE'), mock.call( message= 'Cumulus installation in progress. Waiting for boot to ONIE rescue mode. ' 'Timeout remaining: 0 seconds', state='AWAIT-ONLINE') ] local_cb = cumulus_bootstrap.CumulusBootstrap(args['server'], cli_args()) with pytest.raises(SystemExit): local_cb.wait_for_onie_rescue(countdown, poll_delay, user=user) mock_post_dev.assert_has_calls(mock_post_dev_calls) mock_exit_results.assert_called_with( results={ 'message': 'Device 1.1.1.1 not reachable in ONIE rescue mode within reload countdown.', 'error_type': 'login', 'ok': False })
def test_get_required_os_exception(mock_exit_results, mock_subprocess, device): mock_subprocess.Popen.return_value.communicate.return_value = ('stdout', 'stderr') local_cb = cumulus_bootstrap.CumulusBootstrap(args['server'], cli_args()) with pytest.raises(SystemExit): local_cb.get_required_os(device) mock_exit_results.assert_called_with(dev=device, results={'message': 'Unable to load os-select output as JSON: stdout', 'error_type': 'install', 'ok': False})
def test_wait_for_device_missing_passwd(mock_exit, cli_args, device): new_args = deepcopy(cli_args) new_args.env_passwd = None with pytest.raises(SystemExit): cumulus_bootstrap.CumulusBootstrap(args['server'], new_args) mock_exit.assert_called_with( results={'ok': False, 'error_type': 'login', 'message': 'login user-password missing'} )
def test_get_required_os(mock_subprocess, device): expected_os_sel_output = '{"output": "os-select test output"}' mock_subprocess.return_value.communicate.return_value = (expected_os_sel_output, 'stderr') local_cb = cumulus_bootstrap.CumulusBootstrap(args['server'], cli_args()) conf_fpath = '{topdir}/etc/profiles/default/cumulus/os-selector.cfg'.format(topdir=cli_args().topdir) cmd = "{topdir}/bin/aztp_os_selector.py -j '{dev_json}' -c {config}".format(topdir=cli_args().topdir, dev_json=json.dumps(device.facts), config=conf_fpath) os_sel_output = local_cb.get_required_os(device) assert os_sel_output == json.loads(expected_os_sel_output) mock_subprocess.assert_called_with(cmd, shell=True, stdout=-1)
def test_wait_for_device_missing_username(mock_exit, cli_args, device): new_args = deepcopy(cli_args) new_args.user = None new_args.env_user = None local_cb = cumulus_bootstrap.CumulusBootstrap(args['server'], new_args) with pytest.raises(SystemExit): local_cb.wait_for_device(1, 2) mock_exit.assert_called_with( target=device.target, results={'ok': False, 'error_type': 'login', 'message': 'login user-name missing'} )
def cb_obj(cli_args): os.environ['ENV_PASS'] = '******' cb = cumulus_bootstrap.CumulusBootstrap(args['server'], cli_args) return cb