Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1860 adding tests for the executor resource deployment files #1893

Open
wants to merge 8 commits into
base: develop
Choose a base branch
from
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

- Added feature to use custom python files as modules to be used in the electron function
- Added test coverage for executor resource deployment.
Aravind-psiog marked this conversation as resolved.
Show resolved Hide resolved

### Changed

Expand Down
15 changes: 15 additions & 0 deletions tests/covalent_dispatcher_tests/_cli/groups/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Copyright 2023 Agnostiq Inc.
#
# This file is part of Covalent.
#
# Licensed under the Apache License 2.0 (the "License"). A copy of the
# License may be obtained with this software package or at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Use of this file is prohibited except in compliance with the License.
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Copyright 2023 Agnostiq Inc.
#
# This file is part of Covalent.
#
# Licensed under the Apache License 2.0 (the "License"). A copy of the
# License may be obtained with this software package or at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Use of this file is prohibited except in compliance with the License.
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from unittest.mock import patch

import pytest

from covalent_dispatcher._cli.groups.deploy_group import validate_args, validate_region


def test_validate_region(mocker):
"""Test validate region"""
region_name = "us-east-1"
with patch("covalent_dispatcher._cli.groups.deploy_group.boto3.client") as mock_boto3_client:
mock_client = mock_boto3_client.return_value
mock_client.describe_regions.return_value = {"Regions": [{"RegionName": region_name}]}

result = validate_region(region_name)

assert result is True


@pytest.mark.parametrize(
"args",
[{"region": "test pytest"}],
)
def test_validate_args(mocker, args):
"""Test validate args"""
mocker.patch(
"covalent_dispatcher._cli.groups.deploy_group.validate_region",
return_value=False,
)
result = validate_args(args)
assert result == f"Unable to find the provided region: {args['region']}"
85 changes: 82 additions & 3 deletions tests/covalent_tests/cloud_resource_manager/core_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,6 @@ def test_run_in_subprocess(mocker, test_retcode, crm):

if test_retcode != 0:
exception = subprocess.CalledProcessError(returncode=test_retcode, cmd=test_cmd)
print("some exception ", exception)
with pytest.raises(subprocess.CalledProcessError) as excinfo:
crm._run_in_subprocess(
cmd=test_cmd,
Expand Down Expand Up @@ -370,6 +369,67 @@ def test_get_tf_statefile_path(mocker, crm, executor_name):
# mock_get_config.assert_called_once_with("dispatcher.db_path")


def test_docker(mocker, crm, caplog):
mocker.patch(
"covalent.cloud_resource_manager.core.shutil.which",
return_value=None,
)
with pytest.raises(SystemExit):
crm._validation_docker()
assert caplog.levelname == "ERROR"
assert caplog.text == "Docker not found on system"


@pytest.mark.parametrize(
"cmd",
["mock-terraform init", "mock-terraform destroy"],
)
def test_log_error_msg(mocker, crm, cmd):
# mocker.patch(
# "covalent.cloud_resource_manager.core.CloudResourceManager._log_error_msg",
# )
Aravind-psiog marked this conversation as resolved.
Show resolved Hide resolved
mocker.patch(
"covalent.cloud_resource_manager.core.open",
new=mock.mock_open(read_data="test data"),
)
crm._log_error_msg(cmd)


@pytest.mark.parametrize(
"mock_exist, indicator",
[
([True, None], "On deploy"),
([True, None], "On destroy"),
([False, True], None),
([False, False], None),
],
)
def test_terraform_error_validator(mocker, crm, mock_exist, indicator):
mocker.patch(
"covalent.cloud_resource_manager.core.os.path.join",
return_value="test-mock-error-file",
)
mocker.patch(
"covalent.cloud_resource_manager.core.os.path.exists",
side_effect=mock_exist,
)
mocker.patch("covalent.cloud_resource_manager.core.os.path.getsize", return_value=2)
mocker.patch(
"covalent.cloud_resource_manager.core.open",
new=mock.mock_open(read_data=f"mock test\n{indicator}"),
)
status = crm._terraform_error_validator(tfstate_path="mock_file_path")
if mock_exist[0]:
if indicator == "On deploy":
assert status == "*up"
elif indicator == "On destroy":
assert status == "*down"
elif mock_exist[1]:
assert status == "up"
else:
assert status == "down"


@pytest.mark.parametrize(
"dry_run, executor_options",
[
Expand Down Expand Up @@ -424,6 +484,20 @@ def test_up(mocker, dry_run, executor_options, executor_name, executor_module_pa
"covalent.cloud_resource_manager.core.CloudResourceManager._update_config",
)

mocker.patch(
"covalent.cloud_resource_manager.core.Path.exists",
return_value=True,
)

mocker.patch(
"covalent.cloud_resource_manager.core.Path.unlink",
)

mocker.patch(
"covalent.cloud_resource_manager.core.os.path.getsize",
return_value=0,
)

if executor_options:
with pytest.raises(SystemExit):
CloudResourceManager(
Expand Down Expand Up @@ -573,9 +647,14 @@ def test_down(mocker, crm):

mocker.patch(
"covalent.cloud_resource_manager.core.os.path.getsize",
return_value=2,
return_value=0,
)

# mock_path_unlink = mocker.patch(
# "covalent.cloud_resource_manager.core.get_plugin_settings",
# # return_value={"test": "default"},
# )
Aravind-psiog marked this conversation as resolved.
Show resolved Hide resolved

crm.down(print_callback=None)

mock_get_tf_path.assert_called_once()
Expand All @@ -594,7 +673,7 @@ def test_down(mocker, crm):
mock_run_in_subprocess.assert_called_once_with(cmd=cmd, print_callback=None, env_vars=env_vars)

assert mock_path_exists.call_count == 5
assert mock_path_unlink.call_count == 4
assert mock_path_unlink.call_count == 5


def test_status(mocker, crm):
Expand Down
Loading