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 = ubuntu_bootstrap.UbuntuBootstrap(args['server'], cli_args) 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=local_cb.os_name, image_name=image_name) with pytest.raises(SystemExit): local_cb.install_os(device, image_name) mock_exit_results.assert_called_with(dev=device, results={ 'ok': False, 'error_type': 'install', 'message': 'Unable to run command: {cmd}. ' 'Error message: {errmsg}'.format( cmd=cmd, errmsg=errmsg) })
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' 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 = ubuntu_bootstrap.UbuntuBootstrap(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) 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: assert not device.api.execute.called assert not mock_install_os.called
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): ubuntu_bootstrap.UbuntuBootstrap(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_exception(mock_exit_results, mock_subprocess, device): mock_subprocess.Popen.return_value.communicate.return_value = ('stdout', 'stderr') local_cb = ubuntu_bootstrap.UbuntuBootstrap(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_username(mock_exit, cli_args, device): new_args = deepcopy(cli_args) new_args.user = None new_args.env_user = None local_cb = ubuntu_bootstrap.UbuntuBootstrap(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 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 = ubuntu_bootstrap.UbuntuBootstrap(args['server'], cli_args()) conf_fpath = '{topdir}/etc/profiles/default/ubuntu/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_install_os_image(mock_os, mock_con, mock_time, mock_wait_device, device, cli_args): image_name = 'test_image' results = 'test result message' device.api.execute.return_value = (True, results) local_cb = ubuntu_bootstrap.UbuntuBootstrap(args['server'], cli_args) 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=local_cb.os_name, image_name=image_name) method_calls = [mock.call.execute([cmd])] local_cb.install_os(device, image_name) assert device.api.method_calls == method_calls
def ub_obj(cli_args): os.environ['ENV_PASS'] = '******' cb = ubuntu_bootstrap.UbuntuBootstrap(args['server'], cli_args) return cb