Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
agoscinski committed May 24, 2024
1 parent 33e229d commit 5169197
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 53 deletions.
7 changes: 4 additions & 3 deletions src/aiida/cmdline/commands/cmd_computer.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from copy import deepcopy
from functools import partial
from math import isclose
import pathlib

import click

Expand Down Expand Up @@ -747,7 +748,7 @@ def computer_export_setup(computer, output_file):
"""Export computer setup to a yaml file."""
import yaml

computer_setup_data = {
computer_setup = {
'label': computer.label,
'description': computer.description,
'hostname': computer.hostname,
Expand All @@ -764,7 +765,7 @@ def computer_export_setup(computer, output_file):
}
try:
with open(output_file, 'w', encoding='utf-8') as yfhandle:
yaml.dump(computer_configuration, yfhandle, sort_keys=True)
yaml.dump(computer_setup, yfhandle, sort_keys=True)
echo.echo_success(f"Computer<{computer.pk}> {computer.label} setup exported to file '{output_file}'.")
except Exception as e:
error_traceback = traceback.format_exc()
Expand All @@ -790,7 +791,7 @@ def computer_export_config(computer, user, output_file):
if not computer.is_configured:
echo.echo_error(
f'Computer<{computer.pk}> {computer.label} configuration cannot be exported,'
'because computer has not been configured yet.'
' because computer has not been configured yet.'
)
else:
try:
Expand Down
90 changes: 40 additions & 50 deletions tests/cmdline/commands/test_computer.py
Original file line number Diff line number Diff line change
Expand Up @@ -514,68 +514,58 @@ def test_show(self):
assert '--username=' in result.output
assert result_cur.output == result.output

def test_computer_export_setup(self):
def test_computer_export_setup(self, tmp_path):
"""Test if 'verdi computer export setup' command works"""
self.comp_builder.label = 'test_computer_export_setup'
self.comp_builder.transport = 'core.ssh'
comp = self.comp_builder.new()
comp.store()

exported_setup_filename = 'computer-setup.yml'
try:
result = self.cli_runner(computer_export_setup, [comp.label, exported_setup_filename])
assert 'Success' in result.output, 'Command should have run successfull.'
assert (
f'{exported_setup_filename}' in result.output
), 'Filename should be in terminal output but was not found.'
assert os.path.exists(
exported_setup_filename
), f"'{exported_setup_filename}' was not created during export."

# verifying correctness by comparing internal and loaded yml object
with open(exported_setup_filename, 'r', encoding='utf-8') as yfhandle:
configure_setup_data = yaml.safe_load(yfhandle)
assert configure_setup_data == self.comp_builder.get_computer_spec(
comp
), 'Internal computer configuration does not agree with exported one.'
except Exception:
raise
finally:
if os.path.exists(exported_setup_filename):
os.remove(exported_setup_filename)

def test_computer_export_config(self):
exported_setup_filename = tmp_path / 'computer-setup.yml'
result = self.cli_runner(computer_export_setup, [comp.label, exported_setup_filename])
assert 'Success' in result.output, 'Command should have run successfull.'
assert (
f'{exported_setup_filename}' in result.output
), 'Filename should be in terminal output but was not found.'
assert os.path.exists(
exported_setup_filename
), f"'{exported_setup_filename}' was not created during export."

# verifying correctness by comparing internal and loaded yml object
with open(exported_setup_filename, 'r', encoding='utf-8') as yfhandle:
configure_setup_data = yaml.safe_load(yfhandle)
assert configure_setup_data == self.comp_builder.get_computer_spec(
comp
), 'Internal computer configuration does not agree with exported one.'

def test_computer_export_config(self, tmp_path):
"""Test if 'verdi computer export config' command works"""
self.comp_builder.label = 'test_computer_export_config'
self.comp_builder.transport = 'core.ssh'
comp = self.comp_builder.new()
comp.store()

exported_config_filename = 'computer-configure.yml'
try:
result = self.cli_runner(computer_export_config, [comp.label, exported_config_filename], raises=True)

comp.configure(safe_interval=0.0)
result = self.cli_runner(computer_export_config, [comp.label, exported_config_filename])
assert 'Success' in result.output, 'Command should have run successfull.'
assert (
f'{exported_config_filename}' in result.output
), 'Filename should be in terminal output but was not found.'
assert os.path.exists(
exported_config_filename
), f"'{exported_config_filename}' was not created during export."

# verifying correctness by comparing internal and loaded yml object
with open(exported_config_filename, 'r', encoding='utf-8') as yfhandle:
configure_config_data = yaml.safe_load(yfhandle)
assert (
configure_config_data == comp.get_configuration()
), 'Internal computer configuration does not agree with exported one.'
except Exception:
raise
finally:
if os.path.exists(exported_config_filename):
os.remove(exported_config_filename)
exported_config_filename = tmp_path / 'computer-configure.yml'
# We have not configuret the computer yet so it should raise an error
with pytest.raises(AssertionError):
self.cli_runner(computer_export_config, [comp.label, exported_config_filename], raises=True)

comp.configure(safe_interval=0.0)
result = self.cli_runner(computer_export_config, [comp.label, exported_config_filename])
assert 'Success' in result.output, 'Command should have run successfull.'
assert (
f'{exported_config_filename}' in result.output
), 'Filename should be in terminal output but was not found.'
assert os.path.exists(
exported_config_filename
), f"'{exported_config_filename}' was not created during export."

# verifying correctness by comparing internal and loaded yml object
with open(exported_config_filename, 'r', encoding='utf-8') as yfhandle:
configure_config_data = yaml.safe_load(yfhandle)
assert (
configure_config_data == comp.get_configuration()
), 'Internal computer configuration does not agree with exported one.'


class TestVerdiComputerCommands:
Expand Down

0 comments on commit 5169197

Please sign in to comment.