Refactor to use unittest.mock instead of mock

Simple refactor to eliminate use of mock library and instead use the
built-in library.

Change-Id: Id2c176c9c67a9043ea5f54d62044423518a3446b
This commit is contained in:
Bryan Strassner 2018-07-25 10:15:53 -05:00
parent 038f958501
commit cf9684377a
30 changed files with 87 additions and 83 deletions

View File

@ -1,7 +1,6 @@
# Testing
pytest==3.4
pytest-cov==2.5.1
mock==2.0.0
responses==0.8.1
testfixtures==5.1.1
apache-airflow[crypto,celery,postgres,hive,hdfs,jdbc]==1.9.0

View File

@ -14,7 +14,7 @@
"""Tests for the default node_lookup provided with the deployment group
functionality.
"""
import mock
from unittest import mock
import pytest

View File

@ -12,8 +12,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.
"""Tests for the DocumentValidationManager"""
import mock
from mock import MagicMock
from unittest import mock
from unittest.mock import MagicMock
import pytest

View File

@ -11,7 +11,8 @@
# 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 mock import patch
from unittest.mock import patch
import pytest
from shipyard_airflow.control.helpers.workflow_helper import (

View File

@ -12,11 +12,11 @@
# See the License for the specific language governing permissions and
# limitations under the License.
"""Tests for the action validators run when an action is created"""
import mock
from mock import MagicMock
import yaml
from unittest import mock
from unittest.mock import MagicMock
import pytest
import yaml
from shipyard_airflow.common.deployment_group.errors import (
DeploymentGroupCycleError,

View File

@ -12,14 +12,15 @@
# See the License for the specific language governing permissions and
# limitations under the License.
from datetime import datetime
from falcon import testing
from mock import patch
from oslo_config import cfg
import falcon
import json
import logging
import mock
import os
from unittest import mock
from unittest.mock import patch
import falcon
from falcon import testing
from oslo_config import cfg
import pytest
import responses
@ -241,7 +242,7 @@ def test_on_get(mock_get_all_actions, mock_authorize):
act_resource.on_get(req, resp)
mock_authorize.assert_called_once_with(
'workflow_orchestrator:list_actions', context)
mock_get_all_actions.assert_called_once()
assert mock_get_all_actions.call_count == 1
assert resp.body is not None
assert resp.status == '200 OK'
@ -406,21 +407,21 @@ def test_create_action_validator_error():
def test_get_all_actions_db(mock_get_all_submitted_actions):
act_resource = ActionsResource()
act_resource.get_all_actions_db()
mock_get_all_submitted_actions.assert_called()
assert mock_get_all_submitted_actions.called
@patch('shipyard_airflow.db.airflow_db.AirflowDbAccess.get_all_dag_runs')
def test_get_all_dag_runs_db(mock_get_all_dag_runs):
act_resource = ActionsResource()
act_resource.get_all_dag_runs_db()
mock_get_all_dag_runs.assert_called()
assert mock_get_all_dag_runs.called
@patch('shipyard_airflow.db.airflow_db.AirflowDbAccess.get_all_tasks')
def test_get_all_tasks_db(mock_get_all_tasks):
act_resource = ActionsResource()
act_resource.get_all_tasks_db()
mock_get_all_tasks.assert_called()
assert mock_get_all_tasks.called
@patch('shipyard_airflow.db.shipyard_db.ShipyardDbAccess.insert_action')

View File

@ -12,7 +12,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.
from datetime import datetime
import mock
from unittest import mock
import pytest
from shipyard_airflow.control.action.actions_id_api import (ActionsIdResource)

View File

@ -11,8 +11,9 @@
# 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 mock import patch
from datetime import datetime
from unittest.mock import patch
import pytest
from shipyard_airflow.errors import ApiError

View File

@ -11,9 +11,9 @@
# 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.
import mock
from datetime import datetime
from mock import patch
from unittest import mock
from unittest.mock import patch
from shipyard_airflow.control.action.actions_steps_id_logs_api import \
ActionsStepsLogsResource

View File

@ -11,7 +11,8 @@
# 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 mock import patch
from unittest.mock import patch
import pytest
from shipyard_airflow.control.action.actions_validations_id_api import \

View File

@ -13,8 +13,8 @@
# limitations under the License.
""" Tests for the configdocs_api"""
import json
import mock
from mock import ANY, patch
from unittest import mock
from unittest.mock import ANY, patch
import pytest

View File

@ -12,12 +12,12 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import json
import mock
from unittest import mock
from unittest.mock import patch
import yaml
import pytest
import responses
import yaml
from .fake_response import FakeResponse
from shipyard_airflow.control.base import ShipyardRequestContext

View File

@ -11,7 +11,7 @@
# 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 mock import patch
from unittest.mock import patch
import pytest

View File

@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
"""Tests for check_k8s_node_status functions"""
import mock
from unittest import mock
from shipyard_airflow.plugins.check_k8s_node_status import (
check_node_status

View File

@ -11,7 +11,8 @@
# 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.
import mock
from unittest import mock
import pytest
import yaml

View File

@ -13,11 +13,11 @@
# limitations under the License.
"""Tests for drydock_nodes operator functions"""
import copy
import mock
import os
import yaml
from unittest import mock
import pytest
import yaml
from airflow.exceptions import AirflowException
@ -274,9 +274,9 @@ class TestDrydockNodesOperator:
)
op.design_ref = {}
op.do_execute()
get_dgm.assert_called_once()
nl.assert_called_once()
pdg.assert_called_once()
assert get_dgm.call_count == 1
assert nl.call_count == 1
assert pdg.call_count == 1
assert "critical groups have met their success criteria" in caplog.text
@mock.patch("shipyard_airflow.plugins.drydock_nodes."
@ -297,9 +297,9 @@ class TestDrydockNodesOperator:
op.design_ref = {}
op.do_execute()
get_dgm.assert_called_once()
nl.assert_called_once()
pdg.assert_called_once()
assert get_dgm.call_count == 1
assert nl.call_count == 1
assert pdg.call_count == 1
def test_execute_prepare(self):
op = DrydockNodesOperator(main_dag_name="main",
@ -313,7 +313,7 @@ class TestDrydockNodesOperator:
group = DeploymentGroup(GROUP_DICT, mock.MagicMock())
group.actionable_nodes = ['node1', 'node2', 'node3']
op._execute_prepare(group)
op._execute_task.assert_called_once()
assert op._execute_task.call_count == 1
@mock.patch("shipyard_airflow.plugins.check_k8s_node_status."
"check_node_status", return_value=[])
@ -330,8 +330,8 @@ class TestDrydockNodesOperator:
group = DeploymentGroup(GROUP_DICT, mock.MagicMock())
group.actionable_nodes = ['node1', 'node2', 'node3']
op._execute_deployment(group)
op._execute_task.assert_called_once()
cns.assert_called_once()
assert op._execute_task.call_count == 1
assert cns.call_count == 1
@mock.patch("shipyard_airflow.plugins.check_k8s_node_status."
"check_node_status", return_value=['node2', 'node4'])
@ -348,8 +348,8 @@ class TestDrydockNodesOperator:
group = DeploymentGroup(GROUP_DICT, mock.MagicMock())
group.actionable_nodes = ['node1', 'node2', 'node3']
task_res = op._execute_deployment(group)
op._execute_task.assert_called_once()
cns.assert_called_once()
assert op._execute_task.call_count == 1
assert cns.call_count == 1
assert 'node4 failed to join Kubernetes' in caplog.text
assert len(task_res.successes) == 2

View File

@ -1,7 +1,6 @@
# Testing
pytest==3.4
pytest-cov==2.5.1
mock==2.0.0
responses==0.8.1
testfixtures==5.1.1

View File

@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import json
import mock
from unittest import mock
from shipyard_client.api_client.base_client import BaseClient
from shipyard_client.api_client.shipyard_api_client import ShipyardClient

View File

@ -11,7 +11,7 @@
# 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.
import mock
from unittest import mock
import responses

View File

@ -11,9 +11,10 @@
# 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.
import mock
from unittest import mock
from unittest.mock import patch, ANY
from click.testing import CliRunner
from mock import patch, ANY
from shipyard_client.cli.commit.actions import CommitConfigdocs
from shipyard_client.cli.commands import shipyard

View File

@ -11,7 +11,7 @@
# 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.
import mock
from unittest import mock
import responses

View File

@ -11,9 +11,9 @@
# 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, ANY
from click.testing import CliRunner
from mock import patch, ANY
from shipyard_client.cli.control.actions import Control
from shipyard_client.cli.commands import shipyard

View File

@ -11,11 +11,10 @@
# 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.
import mock
import yaml
from unittest import mock
import responses
import yaml
from shipyard_client.api_client.base_client import BaseClient
from shipyard_client.cli.create.actions import CreateAction

View File

@ -11,8 +11,9 @@
# 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, ANY
from click.testing import CliRunner
from mock import patch, ANY
from shipyard_client.cli.create.actions import CreateAction, CreateConfigdocs
from shipyard_client.cli.commands import shipyard

View File

@ -11,7 +11,7 @@
# 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.
import mock
from unittest import mock
import responses

View File

@ -11,9 +11,9 @@
# 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, ANY
from click.testing import CliRunner
from mock import patch, ANY
from shipyard_client.cli.describe.actions import DescribeAction
from shipyard_client.cli.describe.actions import DescribeStep

View File

@ -11,11 +11,11 @@
# 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.
import mock
from mock import patch
import pytest
from unittest import mock
from unittest.mock import patch
import responses
import pytest
from shipyard_client.api_client.base_client import BaseClient
from shipyard_client.cli import cli_format_common

View File

@ -11,9 +11,9 @@
# 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, ANY
from click.testing import CliRunner
from mock import patch, ANY
from shipyard_client.cli.get.actions import (
GetActions, GetConfigdocs, GetConfigdocsStatus, GetRenderedConfigdocs,

View File

@ -11,15 +11,14 @@
# 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.
"""Unit tests for input_checks helper module"""
from unittest.mock import Mock
from unittest import mock
from shipyard_client.cli import input_checks
def test_check_workflow_id_valid():
"""Check that a valid formatted id passes"""
ctx = Mock(side_effect=Exception("failed"))
ctx = mock.Mock(side_effect=Exception("failed"))
input_checks.check_workflow_id(
ctx, 'something__2017-01-01T12:34:56.000000')
ctx.fail.assert_not_called()
@ -32,7 +31,7 @@ def test_check_workflow_id_valid_tricky():
date if the code is not properly set up to separate the date
first.
"""
ctx = Mock(side_effect=Exception("failed"))
ctx = mock.Mock(side_effect=Exception("failed"))
input_checks.check_workflow_id(
ctx, '2017-01-01T12:34:99.000__2017-01-01T12:34:56.000000')
ctx.fail.assert_not_called()
@ -40,7 +39,7 @@ def test_check_workflow_id_valid_tricky():
def test_check_workflow_id_no_date():
"""Check tha a missing date portion of the string is rejected."""
ctx = Mock(side_effect=Exception("failed"))
ctx = mock.Mock(side_effect=Exception("failed"))
try:
input_checks.check_workflow_id(ctx, 'something__')
except Exception:
@ -52,7 +51,7 @@ def test_check_workflow_id_no_date():
def test_check_workflow_id_none():
"""Check that the workflow id check invokes the context.fail on None"""
ctx = Mock(side_effect=Exception("failed"))
ctx = mock.Mock(side_effect=Exception("failed"))
try:
input_checks.check_workflow_id(
ctx, None)
@ -64,7 +63,7 @@ def test_check_workflow_id_none():
def test_check_workflow_id_invalid_separator():
"""Check that the separator check invokes the context.fail"""
ctx = Mock(side_effect=Exception("failed"))
ctx = mock.Mock(side_effect=Exception("failed"))
try:
input_checks.check_workflow_id(
ctx, 'something20170101T12:34:56.000000')
@ -76,7 +75,7 @@ def test_check_workflow_id_invalid_separator():
def test_check_workflow_id_invalid_date():
"""Check that the date format check invokes the context.fail"""
ctx = Mock(side_effect=Exception("failed"))
ctx = mock.Mock(side_effect=Exception("failed"))
try:
input_checks.check_workflow_id(
ctx, 'something__blah0101 12:34:56.000000')
@ -88,7 +87,7 @@ def test_check_workflow_id_invalid_date():
def test_check_workflow_id_invalid_date_format():
"""Check that the date format check invokes the context.fail"""
ctx = Mock(side_effect=Exception("failed"))
ctx = mock.Mock(side_effect=Exception("failed"))
try:
input_checks.check_workflow_id(
ctx, 'something__2017-01-01T12:34:56')
@ -99,13 +98,13 @@ def test_check_workflow_id_invalid_date_format():
def test_check_id_valid():
ctx = Mock(side_effect=Exception("failed"))
ctx = mock.Mock(side_effect=Exception("failed"))
input_checks.check_id(ctx, "12345678901234567890123456")
ctx.fail.assert_not_called()
def test_check_id_too_long():
ctx = Mock(side_effect=Exception("failed"))
ctx = mock.Mock(side_effect=Exception("failed"))
try:
input_checks.check_id(ctx, "TOOLONGTOOLONGTOOLONGTOOLONGTOOLONG")
except Exception:
@ -115,7 +114,7 @@ def test_check_id_too_long():
def test_check_id_too_short():
ctx = Mock(side_effect=Exception("failed"))
ctx = mock.Mock(side_effect=Exception("failed"))
try:
input_checks.check_id(ctx, "TOOSHORT")
except Exception:
@ -125,7 +124,7 @@ def test_check_id_too_short():
def test_check_id_bad_chars():
ctx = Mock(side_effect=Exception("failed"))
ctx = mock.Mock(side_effect=Exception("failed"))
try:
input_checks.check_id(ctx, "_ARENOTALLOWED-")
except Exception:
@ -135,7 +134,7 @@ def test_check_id_bad_chars():
def test_check_id_none():
ctx = Mock(side_effect=Exception("failed"))
ctx = mock.Mock(side_effect=Exception("failed"))
try:
input_checks.check_id(ctx, None)
except Exception:
@ -145,7 +144,7 @@ def test_check_id_none():
def test_check_control_action_valid():
ctx = Mock(side_effect=Exception("failed"))
ctx = mock.Mock(side_effect=Exception("failed"))
input_checks.check_control_action(ctx, 'pause')
input_checks.check_control_action(ctx, 'unpause')
input_checks.check_control_action(ctx, 'stop')
@ -153,7 +152,7 @@ def test_check_control_action_valid():
def test_check_control_action_invalid():
ctx = Mock(side_effect=Exception("failed"))
ctx = mock.Mock(side_effect=Exception("failed"))
try:
input_checks.check_control_action(ctx, 'completely_bogus')
except Exception:
@ -163,7 +162,7 @@ def test_check_control_action_invalid():
def test_check_control_action_none():
ctx = Mock(side_effect=Exception("failed"))
ctx = mock.Mock(side_effect=Exception("failed"))
try:
input_checks.check_control_action(ctx, None)
except Exception:
@ -173,7 +172,7 @@ def test_check_control_action_none():
def test_check_action_commands():
ctx = Mock(side_effect=Exception("failed"))
ctx = mock.Mock(side_effect=Exception("failed"))
input_checks.check_action_command(ctx, 'deploy_site')
input_checks.check_action_command(ctx, 'update_site')
input_checks.check_action_command(ctx, 'update_software')
@ -182,7 +181,7 @@ def test_check_action_commands():
def test_check_action_commands_invalid():
ctx = Mock(side_effect=Exception("failed"))
ctx = mock.Mock(side_effect=Exception("failed"))
try:
input_checks.check_action_command(ctx, "burger_and_fries")
except Exception:
@ -192,7 +191,7 @@ def test_check_action_commands_invalid():
def test_check_action_commands_none():
ctx = Mock(side_effect=Exception("failed"))
ctx = mock.Mock(side_effect=Exception("failed"))
try:
input_checks.check_action_command(ctx, None)
except Exception:
@ -202,7 +201,7 @@ def test_check_action_commands_none():
def test_check_reformat_parameter_valid():
ctx = Mock(side_effect=Exception("failed"))
ctx = mock.Mock(side_effect=Exception("failed"))
param = ['this=that']
input_checks.check_reformat_parameter(ctx, param)
param = []
@ -219,7 +218,7 @@ def test_check_reformat_parameter_valid():
def test_check_reformat_parameter_no_equals_second():
ctx = Mock(side_effect=Exception("failed"))
ctx = mock.Mock(side_effect=Exception("failed"))
param = ['this=that', 'someanother']
try:
input_checks.check_reformat_parameter(ctx, param)
@ -230,7 +229,7 @@ def test_check_reformat_parameter_no_equals_second():
def test_check_reformat_parameter_no_equals_first():
ctx = Mock(side_effect=Exception("failed"))
ctx = mock.Mock(side_effect=Exception("failed"))
param = ['thisthat', 'some=another']
try:
input_checks.check_reformat_parameter(ctx, param)

View File

@ -11,9 +11,9 @@
# 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
from click.testing import CliRunner
from mock import patch
from shipyard_client.cli.commands import shipyard
from shipyard_client.api_client.shipyardclient_context import \