How to use mock_fail method in toolium

Best Python code snippet using toolium_python

test_compute_manager.py

Source:test_compute_manager.py Github

copy

Full Screen

1# Copyright 2016 IBM Corp.2#3# Licensed under the Apache License, Version 2.0 (the "License"); you may4# not use this file except in compliance with the License. You may obtain5# a copy of the License at6#7# http://www.apache.org/licenses/LICENSE-2.08#9# Unless required by applicable law or agreed to in writing, software10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the12# License for the specific language governing permissions and limitations13# under the License.14import mock15from io import StringIO16from zun.common import consts17from zun.common import exception18from zun.compute import claims19from zun.compute import compute_node_tracker20from zun.compute import manager21import zun.conf22from zun.objects.container import Container23from zun.objects.image import Image24from zun.tests import base25from zun.tests.unit.container.fake_driver import FakeDriver as fake_driver26from zun.tests.unit.db import utils27class FakeResourceTracker(object):28 def container_claim(self, context, container, host, limits):29 return claims.NopClaim()30class TestManager(base.TestCase):31 def setUp(self):32 super(TestManager, self).setUp()33 zun.conf.CONF.set_override(34 'container_driver',35 'zun.tests.unit.container.fake_driver.FakeDriver')36 self.compute_manager = manager.Manager()37 @mock.patch.object(Container, 'save')38 def test_fail_container(self, mock_save):39 container = Container(self.context, **utils.get_test_container())40 self.compute_manager._fail_container(self.context, container,41 "Creation Failed")42 self.assertEqual(consts.ERROR, container.status)43 self.assertEqual("Creation Failed", container.status_reason)44 self.assertIsNone(container.task_state)45 @mock.patch.object(Container, 'save')46 @mock.patch('zun.image.driver.pull_image')47 @mock.patch.object(fake_driver, 'create')48 @mock.patch.object(fake_driver, 'create_sandbox')49 def test_container_create(self, mock_create_sandbox, mock_create,50 mock_pull, mock_save):51 container = Container(self.context, **utils.get_test_container())52 image = {'image': 'repo', 'path': 'out_path', 'driver': 'glance'}53 mock_pull.return_value = image, False54 mock_create_sandbox.return_value = 'fake_id'55 self.compute_manager._resource_tracker = FakeResourceTracker()56 self.compute_manager._do_container_create(self.context, container)57 mock_save.assert_called_with(self.context)58 mock_pull.assert_any_call(self.context, container.image, 'latest',59 'always', 'glance')60 mock_create.assert_called_once_with(self.context, container,61 'fake_id', image)62 @mock.patch.object(Container, 'save')63 @mock.patch.object(fake_driver, 'create_sandbox')64 @mock.patch('zun.image.driver.pull_image')65 @mock.patch.object(manager.Manager, '_fail_container')66 def test_container_create_pull_image_failed_docker_error(67 self, mock_fail, mock_pull, mock_create_sandbox, mock_save):68 container = Container(self.context, **utils.get_test_container())69 mock_pull.side_effect = exception.DockerError("Pull Failed")70 mock_create_sandbox.return_value = mock.MagicMock()71 self.compute_manager._do_container_create(self.context, container)72 mock_fail.assert_called_once_with(self.context,73 container, "Pull Failed")74 @mock.patch.object(Container, 'save')75 @mock.patch.object(fake_driver, 'create_sandbox')76 @mock.patch('zun.image.driver.pull_image')77 @mock.patch.object(manager.Manager, '_fail_container')78 def test_container_create_pull_image_failed_image_not_found(79 self, mock_fail, mock_pull, mock_create_sandbox, mock_save):80 container = Container(self.context, **utils.get_test_container())81 mock_pull.side_effect = exception.ImageNotFound("Image Not Found")82 mock_create_sandbox.return_value = mock.MagicMock()83 self.compute_manager._do_container_create(self.context, container)84 mock_fail.assert_called_once_with(self.context,85 container, "Image Not Found")86 @mock.patch.object(Container, 'save')87 @mock.patch.object(fake_driver, 'create_sandbox')88 @mock.patch('zun.image.driver.pull_image')89 @mock.patch.object(manager.Manager, '_fail_container')90 def test_container_create_pull_image_failed_zun_exception(91 self, mock_fail, mock_pull, mock_create_sandbox, mock_save):92 container = Container(self.context, **utils.get_test_container())93 mock_pull.side_effect = exception.ZunException(94 message="Image Not Found")95 mock_create_sandbox.return_value = mock.MagicMock()96 self.compute_manager._do_container_create(self.context, container)97 mock_fail.assert_called_once_with(self.context,98 container, "Image Not Found")99 @mock.patch.object(Container, 'save')100 @mock.patch('zun.image.driver.pull_image')101 @mock.patch.object(fake_driver, 'create')102 @mock.patch.object(fake_driver, 'create_sandbox')103 @mock.patch.object(manager.Manager, '_fail_container')104 def test_container_create_docker_create_failed(self, mock_fail,105 mock_create_sandbox,106 mock_create, mock_pull,107 mock_save):108 container = Container(self.context, **utils.get_test_container())109 image = {'image': 'repo', 'path': 'out_path', 'driver': 'glance'}110 mock_pull.return_value = image, False111 mock_create.side_effect = exception.DockerError("Creation Failed")112 mock_create_sandbox.return_value = mock.MagicMock()113 self.compute_manager._resource_tracker = FakeResourceTracker()114 self.compute_manager._do_container_create(self.context, container)115 mock_fail.assert_called_once_with(116 self.context, container, "Creation Failed", unset_host=True)117 @mock.patch.object(Container, 'save')118 @mock.patch('zun.image.driver.pull_image')119 @mock.patch.object(fake_driver, 'create')120 @mock.patch.object(fake_driver, 'start')121 def test_container_run(self, mock_start,122 mock_create, mock_pull, mock_save):123 container = Container(self.context, **utils.get_test_container())124 image = {'image': 'repo', 'path': 'out_path', 'driver': 'glance'}125 mock_create.return_value = container126 mock_pull.return_value = image, False127 container.status = 'Stopped'128 self.compute_manager._resource_tracker = FakeResourceTracker()129 self.compute_manager._do_container_run(self.context, container)130 mock_save.assert_called_with(self.context)131 mock_pull.assert_any_call(self.context, container.image, 'latest',132 'always', 'glance')133 mock_create.assert_called_once_with(self.context, container,134 None, image)135 mock_start.assert_called_once_with(container)136 @mock.patch.object(Container, 'save')137 @mock.patch('zun.image.driver.pull_image')138 @mock.patch.object(manager.Manager, '_fail_container')139 def test_container_run_image_not_found(self, mock_fail,140 mock_pull, mock_save):141 container = Container(self.context, **utils.get_test_container())142 mock_pull.side_effect = exception.ImageNotFound(143 message="Image Not Found")144 self.compute_manager._do_container_run(self.context,145 container)146 mock_save.assert_called_with(self.context)147 mock_fail.assert_called_with(self.context,148 container, 'Image Not Found')149 mock_pull.assert_called_once_with(self.context, 'kubernetes/pause',150 'latest', 'ifnotpresent', 'docker')151 @mock.patch.object(Container, 'save')152 @mock.patch('zun.image.driver.pull_image')153 @mock.patch.object(manager.Manager, '_fail_container')154 def test_container_run_image_pull_exception_raised(self, mock_fail,155 mock_pull, mock_save):156 container = Container(self.context, **utils.get_test_container())157 mock_pull.side_effect = exception.ZunException(158 message="Image Not Found")159 self.compute_manager._do_container_run(self.context,160 container)161 mock_save.assert_called_with(self.context)162 mock_fail.assert_called_with(self.context,163 container, 'Image Not Found')164 mock_pull.assert_called_once_with(self.context, 'kubernetes/pause',165 'latest', 'ifnotpresent', 'docker')166 @mock.patch.object(Container, 'save')167 @mock.patch('zun.image.driver.pull_image')168 @mock.patch.object(manager.Manager, '_fail_container')169 def test_container_run_image_pull_docker_error(self, mock_fail,170 mock_pull, mock_save):171 container = Container(self.context, **utils.get_test_container())172 mock_pull.side_effect = exception.DockerError(173 message="Docker Error occurred")174 self.compute_manager._do_container_run(self.context,175 container)176 mock_save.assert_called_with(self.context)177 mock_fail.assert_called_with(self.context,178 container, 'Docker Error occurred')179 mock_pull.assert_called_once_with(self.context, 'kubernetes/pause',180 'latest', 'ifnotpresent', 'docker')181 @mock.patch.object(Container, 'save')182 @mock.patch('zun.image.driver.pull_image')183 @mock.patch.object(manager.Manager, '_fail_container')184 @mock.patch.object(fake_driver, 'create')185 def test_container_run_create_raises_docker_error(self, mock_create,186 mock_fail,187 mock_pull, mock_save):188 container = Container(self.context, **utils.get_test_container())189 mock_pull.return_value = {'name': 'nginx', 'path': None}, True190 mock_create.side_effect = exception.DockerError(191 message="Docker Error occurred")192 self.compute_manager._resource_tracker = FakeResourceTracker()193 self.compute_manager._do_container_run(self.context,194 container)195 mock_save.assert_called_with(self.context)196 mock_fail.assert_called_with(197 self.context, container, 'Docker Error occurred', unset_host=True)198 mock_pull.assert_any_call(self.context, container.image, 'latest',199 'always', 'glance')200 mock_create.assert_called_once_with(self.context, container, None,201 {'name': 'nginx', 'path': None})202 @mock.patch.object(compute_node_tracker.ComputeNodeTracker,203 'remove_usage_from_container')204 @mock.patch.object(Container, 'destroy')205 @mock.patch.object(Container, 'save')206 @mock.patch.object(fake_driver, 'delete')207 def test_container_delete(self, mock_delete, mock_save, mock_cnt_destroy,208 mock_remove_usage):209 container = Container(self.context, **utils.get_test_container())210 self.compute_manager.container_delete(self. context, container, False)211 mock_save.assert_called_with(self.context)212 mock_delete.assert_called_once_with(container, False)213 mock_cnt_destroy.assert_called_once_with(self.context)214 mock_remove_usage.assert_called_once_with(self.context, container,215 True)216 @mock.patch.object(manager.Manager, '_fail_container')217 @mock.patch.object(Container, 'save')218 @mock.patch.object(fake_driver, 'delete')219 def test_container_delete_failed(self, mock_delete, mock_save,220 mock_fail):221 container = Container(self.context, **utils.get_test_container())222 mock_delete.side_effect = exception.DockerError(223 message="Docker Error occurred")224 self.assertRaises(exception.DockerError,225 self.compute_manager.container_delete,226 self.context, container, False)227 mock_save.assert_called_with(self.context)228 mock_fail.assert_called_with(self.context,229 container, 'Docker Error occurred')230 @mock.patch.object(manager.Manager, '_fail_container')231 @mock.patch.object(fake_driver, 'delete_sandbox')232 @mock.patch.object(fake_driver, 'get_sandbox_id')233 @mock.patch.object(Container, 'save')234 @mock.patch.object(fake_driver, 'delete')235 def test_container_delete_sandbox_failed(self, mock_delete, mock_save,236 mock_sandbox, mock_delete_sandbox,237 mock_fail):238 container = Container(self.context, **utils.get_test_container())239 mock_sandbox.return_value = "sandbox_id"240 mock_delete_sandbox.side_effect = exception.ZunException(241 message="Unexpected exception")242 self.assertRaises(exception.ZunException,243 self.compute_manager.container_delete,244 self.context, container, False)245 mock_save.assert_called_with(self.context)246 mock_fail.assert_called_with(self.context,247 container, 'Unexpected exception')248 @mock.patch.object(fake_driver, 'list')249 def test_container_list(self, mock_list):250 self.compute_manager.container_list(self.context)251 mock_list.assert_called_once_with()252 @mock.patch.object(fake_driver, 'list')253 def test_container_list_failed(self, mock_list):254 mock_list.side_effect = exception.DockerError255 self.assertRaises(exception.DockerError,256 self.compute_manager.container_list,257 self.context)258 @mock.patch.object(fake_driver, 'show')259 def test_container_show(self, mock_show):260 container = Container(self.context, **utils.get_test_container())261 self.compute_manager.container_show(self.context, container)262 mock_show.assert_called_once_with(container)263 @mock.patch.object(fake_driver, 'show')264 def test_container_show_failed(self, mock_show):265 container = Container(self.context, **utils.get_test_container())266 mock_show.side_effect = exception.DockerError267 self.assertRaises(exception.DockerError,268 self.compute_manager.container_show,269 self.context, container)270 @mock.patch.object(Container, 'save')271 @mock.patch.object(fake_driver, 'reboot')272 def test_container_reboot(self, mock_reboot, mock_save):273 container = Container(self.context, **utils.get_test_container())274 self.compute_manager._do_container_reboot(self.context, container, 10)275 mock_save.assert_called_with(self.context)276 mock_reboot.assert_called_once_with(container, 10)277 @mock.patch.object(manager.Manager, '_fail_container')278 @mock.patch.object(Container, 'save')279 @mock.patch.object(fake_driver, 'reboot')280 def test_container_reboot_failed(self, mock_reboot, mock_save,281 mock_fail):282 container = Container(self.context, **utils.get_test_container())283 mock_reboot.side_effect = exception.DockerError(284 message="Docker Error occurred")285 self.assertRaises(exception.DockerError,286 self.compute_manager._do_container_reboot,287 self.context, container, 10, reraise=True)288 mock_save.assert_called_with(self.context)289 mock_fail.assert_called_with(self.context,290 container, 'Docker Error occurred')291 @mock.patch.object(Container, 'save')292 @mock.patch.object(fake_driver, 'stop')293 def test_container_stop(self, mock_stop, mock_save):294 container = Container(self.context, **utils.get_test_container())295 self.compute_manager._do_container_stop(self.context, container, 10)296 mock_save.assert_called_with(self.context)297 mock_stop.assert_called_once_with(container, 10)298 @mock.patch.object(manager.Manager, '_fail_container')299 @mock.patch.object(Container, 'save')300 @mock.patch.object(fake_driver, 'stop')301 def test_container_stop_failed(self, mock_stop, mock_save, mock_fail):302 container = Container(self.context, **utils.get_test_container())303 mock_stop.side_effect = exception.DockerError(304 message="Docker Error occurred")305 self.assertRaises(exception.DockerError,306 self.compute_manager._do_container_stop,307 self.context, container, 10, reraise=True)308 mock_save.assert_called_with(self.context)309 mock_fail.assert_called_with(self.context,310 container, 'Docker Error occurred')311 @mock.patch.object(Container, 'save')312 @mock.patch.object(fake_driver, 'start')313 def test_container_start(self, mock_start, mock_save):314 container = Container(self.context, **utils.get_test_container())315 self.compute_manager._do_container_start(self.context, container)316 mock_save.assert_called_with(self.context)317 mock_start.assert_called_once_with(container)318 @mock.patch.object(Container, 'save')319 @mock.patch.object(manager.Manager, '_fail_container')320 @mock.patch.object(fake_driver, 'start')321 def test_container_start_failed(self, mock_start,322 mock_fail, mock_save):323 container = Container(self.context, **utils.get_test_container())324 mock_start.side_effect = exception.DockerError(325 message="Docker Error occurred")326 self.assertRaises(exception.DockerError,327 self.compute_manager._do_container_start,328 self.context, container, reraise=True)329 mock_save.assert_called_with(self.context)330 mock_fail.assert_called_with(self.context,331 container, 'Docker Error occurred')332 @mock.patch.object(fake_driver, 'pause')333 def test_container_pause(self, mock_pause):334 container = Container(self.context, **utils.get_test_container())335 self.compute_manager._do_container_pause(self.context, container)336 mock_pause.assert_called_once_with(container)337 @mock.patch.object(manager.Manager, '_fail_container')338 @mock.patch.object(fake_driver, 'pause')339 def test_container_pause_failed(self, mock_pause, mock_fail):340 container = Container(self.context, **utils.get_test_container())341 mock_pause.side_effect = exception.DockerError(342 message="Docker Error occurred")343 self.assertRaises(exception.DockerError,344 self.compute_manager._do_container_pause,345 self.context, container, reraise=True)346 mock_fail.assert_called_with(self.context,347 container, 'Docker Error occurred')348 @mock.patch.object(fake_driver, 'unpause')349 def test_container_unpause(self, mock_unpause):350 container = Container(self.context, **utils.get_test_container())351 self.compute_manager._do_container_unpause(self.context, container)352 mock_unpause.assert_called_once_with(container)353 @mock.patch.object(manager.Manager, '_fail_container')354 @mock.patch.object(fake_driver, 'unpause')355 def test_container_unpause_failed(self, mock_unpause, mock_fail):356 container = Container(self.context, **utils.get_test_container())357 mock_unpause.side_effect = exception.DockerError(358 message="Docker Error occurred")359 self.assertRaises(exception.DockerError,360 self.compute_manager._do_container_unpause,361 self.context, container, reraise=True)362 mock_fail.assert_called_with(self.context,363 container, 'Docker Error occurred')364 @mock.patch.object(fake_driver, 'show_logs')365 def test_container_logs(self, mock_logs):366 container = Container(self.context, **utils.get_test_container())367 self.compute_manager.container_logs(self.context,368 container, True, True,369 False, 'all', None)370 mock_logs.assert_called_once_with(container, stderr=True, stdout=True,371 timestamps=False, tail='all',372 since=None)373 @mock.patch.object(fake_driver, 'show_logs')374 def test_container_logs_failed(self, mock_logs):375 container = Container(self.context, **utils.get_test_container())376 mock_logs.side_effect = exception.DockerError377 self.assertRaises(exception.DockerError,378 self.compute_manager.container_logs,379 self.context, container, True, True,380 False, 'all', None)381 @mock.patch.object(fake_driver, 'execute_run')382 @mock.patch.object(fake_driver, 'execute_create')383 def test_container_execute(self, mock_execute_create, mock_execute_run):384 mock_execute_create.return_value = 'fake_exec_id'385 container = Container(self.context, **utils.get_test_container())386 self.compute_manager.container_exec(387 self.context, container, 'fake_cmd', True, False)388 mock_execute_create.assert_called_once_with(container, 'fake_cmd',389 False)390 mock_execute_run.assert_called_once_with('fake_exec_id', 'fake_cmd')391 @mock.patch.object(fake_driver, 'execute_create')392 def test_container_execute_failed(self, mock_execute_create):393 container = Container(self.context, **utils.get_test_container())394 mock_execute_create.side_effect = exception.DockerError395 self.assertRaises(exception.DockerError,396 self.compute_manager.container_exec,397 self.context, container, 'fake_cmd', True, False)398 @mock.patch.object(fake_driver, 'kill')399 def test_container_kill(self, mock_kill):400 container = Container(self.context, **utils.get_test_container())401 self.compute_manager._do_container_kill(self.context, container, None)402 mock_kill.assert_called_once_with(container, None)403 @mock.patch.object(manager.Manager, '_fail_container')404 @mock.patch.object(fake_driver, 'kill')405 def test_container_kill_failed(self, mock_kill, mock_fail):406 container = Container(self.context, **utils.get_test_container())407 mock_kill.side_effect = exception.DockerError(408 message="Docker Error occurred")409 self.assertRaises(exception.DockerError,410 self.compute_manager._do_container_kill,411 self.context, container, None, reraise=True)412 mock_fail.assert_called_with(self.context,413 container, 'Docker Error occurred')414 @mock.patch.object(Container, 'save')415 @mock.patch.object(fake_driver, 'update')416 def test_container_update(self, mock_update, mock_save):417 container = Container(self.context, **utils.get_test_container())418 self.compute_manager.container_update(self.context, container,419 {'memory': 512})420 mock_save.assert_called_with(self.context)421 mock_update.assert_called_once_with(container)422 @mock.patch.object(fake_driver, 'update')423 def test_container_update_failed(self, mock_update):424 container = Container(self.context, **utils.get_test_container())425 mock_update.side_effect = exception.DockerError426 self.assertRaises(exception.DockerError,427 self.compute_manager.container_update,428 self.context, container, {})429 @mock.patch.object(fake_driver, 'attach')430 @mock.patch.object(Container, 'save')431 def test_container_attach(self, mock_save, mock_attach):432 container = Container(self.context, **utils.get_test_container())433 mock_attach.return_value = "ws://test"434 self.compute_manager.container_attach(self.context, container)435 mock_save.assert_called_with(self.context)436 @mock.patch.object(fake_driver, 'attach')437 def test_container_attach_failed(self, mock_attach):438 container = Container(self.context, **utils.get_test_container())439 mock_attach.side_effect = Exception440 self.assertRaises(exception.ZunException,441 self.compute_manager.container_attach,442 self.context, container)443 @mock.patch.object(fake_driver, 'resize')444 def test_container_resize(self, mock_resize):445 container = Container(self.context, **utils.get_test_container())446 self.compute_manager.container_resize(self.context,447 container, "100", "100")448 mock_resize.assert_called_once_with(container, "100", "100")449 @mock.patch.object(fake_driver, 'resize')450 def test_container_resize_failed(self, mock_resize):451 container = Container(self.context, **utils.get_test_container())452 mock_resize.side_effect = exception.DockerError453 self.assertRaises(exception.DockerError,454 self.compute_manager.container_resize,455 self.context, container, "100", "100")456 @mock.patch.object(fake_driver, 'inspect_image')457 @mock.patch.object(Image, 'save')458 @mock.patch('zun.image.driver.pull_image')459 def test_image_pull(self, mock_pull, mock_save, mock_inspect):460 image = Image(self.context, **utils.get_test_image())461 ret = {'image': 'repo', 'path': 'out_path', 'driver': 'glance'}462 mock_pull.return_value = ret, True463 mock_inspect.return_value = {'Id': 'fake-id', 'Size': 512}464 self.compute_manager._do_image_pull(self.context, image)465 mock_pull.assert_any_call(self.context, image.repo, image.tag)466 mock_save.assert_called_once()467 mock_inspect.assert_called_once_with(image.repo + ":" + image.tag)468 @mock.patch.object(fake_driver, 'load_image')469 @mock.patch.object(fake_driver, 'inspect_image')470 @mock.patch.object(Image, 'save')471 @mock.patch('zun.image.driver.pull_image')472 def test_image_pull_not_loaded(self, mock_pull, mock_save,473 mock_inspect, mock_load):474 image = Image(self.context, **utils.get_test_image())475 repo_tag = image.repo + ":" + image.tag476 ret = {'image': 'repo', 'path': 'out_path', 'driver': 'glance'}477 mock_pull.return_value = ret, False478 mock_inspect.return_value = {'Id': 'fake-id', 'Size': 512}479 self.compute_manager._do_image_pull(self.context, image)480 mock_pull.assert_any_call(self.context, image.repo, image.tag)481 mock_save.assert_called_once()482 mock_inspect.assert_called_once_with(repo_tag)483 mock_load.assert_called_once_with(ret['path'])484 @mock.patch.object(fake_driver, 'execute_resize')485 def test_container_exec_resize(self, mock_resize):486 self.compute_manager.container_exec_resize(487 self.context, 'fake_exec_id', "100", "100")488 mock_resize.assert_called_once_with('fake_exec_id', "100", "100")489 @mock.patch.object(fake_driver, 'execute_resize')490 def test_container_exec_resize_failed(self, mock_resize):491 mock_resize.side_effect = exception.DockerError492 self.assertRaises(exception.DockerError,493 self.compute_manager.container_exec_resize,494 self.context, 'fake_exec_id', "100", "100")495 @mock.patch('zun.image.driver.upload_image_data')496 @mock.patch.object(fake_driver, 'get_image')497 @mock.patch.object(fake_driver, 'commit')498 def test_container_commit(self, mock_commit,499 mock_get_image, mock_upload_image_data):500 container = Container(self.context, **utils.get_test_container())501 mock_get_image_response = mock.MagicMock()502 mock_get_image_response.data = StringIO().read()503 mock_get_image.return_value = mock_get_image_response504 mock_upload_image_data.return_value = mock.MagicMock()505 self.compute_manager._do_container_commit(self.context,506 mock_get_image_response,507 container, 'repo', 'tag')508 mock_commit.assert_called_once_with(container, 'repo', 'tag')509 @mock.patch.object(fake_driver, 'commit')510 def test_container_commit_failed(self, mock_commit):511 container = Container(self.context, **utils.get_test_container())512 mock_commit.side_effect = exception.DockerError513 self.assertRaises(exception.DockerError,514 self.compute_manager._do_container_commit,...

Full Screen

Full Screen

test_testcase.py

Source:test_testcase.py Github

copy

Full Screen

1#!/usr/bin/env python2# -*- coding: utf-8 -*-3# pylint: disable=protected-access,invalid-name4from __future__ import absolute_import, unicode_literals, division5import unittest6import logging7from pignacio_scripts.testing.testcase import TestCase8from pignacio_scripts.testing.mock import patch, sentinel, Mock9logger = logging.getLogger(__name__) # pylint: disable=invalid-name10class TestCaseTest(unittest.TestCase):11 class MyTestCase(TestCase):12 def runTest(self):13 pass14 def setUp(self):15 self.testcase = self.MyTestCase()16 fail_patcher = patch.object(self.testcase, 'fail', autospec=True)17 self.addCleanup(fail_patcher.stop)18 self.mock_fail = fail_patcher.start()19 cleanup_patcher = patch.object(self.testcase, 'addCleanup',20 autospec=True)21 self.addCleanup(cleanup_patcher.stop)22 self.mock_cleanup = cleanup_patcher.start()23 def test_is_unittest_subclass(self):24 self.assertIsInstance(self.testcase, unittest.TestCase)25 @patch('pignacio_scripts.testing.testcase.patch', autospec=True)26 def test_patch(self, mock_patch):27 patcher = Mock()28 patcher.start.return_value = sentinel.patched29 mock_patch.return_value = patcher30 patched = self.testcase.patch(sentinel.arg, kwarg=sentinel.kwvalue)31 self.assertEqual(patched, sentinel.patched)32 mock_patch.assert_called_once_with(sentinel.arg,33 kwarg=sentinel.kwvalue)34 patcher.start.assert_called_once_with()35 self.mock_cleanup.assert_any_call(patcher.stop)36 @patch('pignacio_scripts.testing.testcase.patch')37 def test_patch_object(self, mock_patch):38 patcher = Mock()39 patcher.start.return_value = sentinel.patched40 mock_patch.object.return_value = patcher41 patched = self.testcase.patch_object(sentinel.arg,42 kwarg=sentinel.kwvalue)43 self.assertEqual(patched, sentinel.patched)44 mock_patch.object.assert_called_once_with(sentinel.arg,45 kwarg=sentinel.kwvalue)46 patcher.start.assert_called_once_with()47 self.mock_cleanup.assert_any_call(patcher.stop)48 @patch('pignacio_scripts.testing.testcase.capture_stdout')49 def test_capture_stdout(self, mock_capture):50 patcher = Mock()51 patcher.start.return_value = sentinel.patched52 mock_capture.return_value = patcher53 patched = self.testcase.capture_stdout()54 self.assertEqual(patched, sentinel.patched)55 mock_capture.assert_called_once_with()56 patcher.start.assert_called_once_with()57 self.mock_cleanup.assert_any_call(patcher.stop)58 def test_assert_size_good(self):59 self.testcase.assertSize([1, 2, 3], 3)60 self.assertFalse(self.mock_fail.called)61 def test_assert_size_bad(self):62 self.testcase.assertSize([1, 2], 3)63 self.assertTrue(self.mock_fail.called)64 self.assertIn('size is 2 != 3', self.mock_fail.call_args[0][0])65 def test_assert_size_fails_with_no_len(self):66 self.testcase.assertSize(1, 1)67 self.assertTrue(self.mock_fail.called)68 self.assertIn('Could not get 1 size', self.mock_fail.call_args[0][0])69class SoftCalledWithTest(unittest.TestCase):70 '''Tests for `Testcase.assertSoftCalledWith`.'''71 class MyTestCase(TestCase):72 def runTest(self):73 pass74 def setUp(self):75 self.testcase = self.MyTestCase()76 fail_patcher = patch.object(self.testcase, 'fail', autospec=True)77 self.addCleanup(fail_patcher.stop)78 self.mock_fail = fail_patcher.start()79 self.mock_func = Mock()80 def test_no_call(self):81 self.testcase.assertSoftCalledWith(self.mock_func)82 self.assertTrue(self.mock_fail.called)83 def test_no_args(self):84 self.mock_func()85 self.testcase.assertSoftCalledWith(self.mock_func)86 self.assertFalse(self.mock_fail.called)87 def test_arg(self):88 self.mock_func(sentinel.arg)89 self.testcase.assertSoftCalledWith(self.mock_func, sentinel.arg)90 self.assertFalse(self.mock_fail.called)91 def test_args(self):92 self.mock_func(sentinel.arg_1, sentinel.arg_2)93 self.testcase.assertSoftCalledWith(self.mock_func, sentinel.arg_1,94 sentinel.arg_2)95 self.assertFalse(self.mock_fail.called)96 def test_different_arg(self):97 self.mock_func(sentinel.arg_1)98 self.testcase.assertSoftCalledWith(self.mock_func, sentinel.arg_2)99 self.assertTrue(self.mock_fail.called)100 def test_missing_arg(self):101 self.mock_func()102 self.testcase.assertSoftCalledWith(self.mock_func, sentinel.arg)103 self.assertTrue(self.mock_fail.called)104 def test_kwarg(self):105 self.mock_func(kwarg=sentinel.kwarg)106 self.testcase.assertSoftCalledWith(self.mock_func,107 kwarg=sentinel.kwarg)108 self.assertFalse(self.mock_fail.called)109 def test_kwargs(self):110 self.mock_func(kwarg_1=sentinel.kwarg_1, kwarg_2=sentinel.kwarg_2)111 self.testcase.assertSoftCalledWith(self.mock_func,112 kwarg_1=sentinel.kwarg_1,113 kwarg_2=sentinel.kwarg_2)114 self.assertFalse(self.mock_fail.called)115 def test_kwarg_wrong_name_fail(self):116 self.mock_func(kwarg=sentinel.kwarg)117 self.testcase.assertSoftCalledWith(self.mock_func,118 kwarg_2=sentinel.kwarg)119 self.assertTrue(self.mock_fail.called)120 def test_kwarg_wrong_value_fail(self):121 self.mock_func(kwarg=sentinel.kwarg)122 self.testcase.assertSoftCalledWith(self.mock_func,123 kwarg=sentinel.value)124 self.assertTrue(self.mock_fail.called)125 def test_arg_and_kwarg(self):126 self.mock_func(sentinel.arg, kwarg=sentinel.kwarg)127 self.testcase.assertSoftCalledWith(self.mock_func, sentinel.arg,128 kwarg=sentinel.kwarg)129 self.assertFalse(self.mock_fail.called)130 def test_no_call_message(self):131 self.testcase.assertSoftCalledWith(self.mock_func)132 self.assertTrue(self.mock_fail.called)133 self.assertIn('was not called', self.mock_fail.call_args[0][0])134 def test_message_has_current_call(self):135 self.mock_func("<arg>", kwarg="<kwarg>")136 self.testcase.assertSoftCalledWith(self.mock_func, missing=None)137 self.assertTrue(self.mock_fail.called)138 message = self.mock_fail.call_args[0][0]139 self.assertIn('<arg>', message)140 self.assertIn('<kwarg>', message)141 def test_message_has_expected_call(self):142 self.mock_func()143 self.testcase.assertSoftCalledWith(self.mock_func, '<arg>',144 kwarg='<kwarg>')145 self.assertTrue(self.mock_fail.called)146 message = self.mock_fail.call_args[0][0]147 self.assertIn('<arg>', message)...

Full Screen

Full Screen

test_tasks.py

Source:test_tasks.py Github

copy

Full Screen

1# -*- coding: utf-8 -*-2from datetime import date3from githubflow import tasks, settings4from . import TestCase, patch, make_pull_request5class TestHandlers(TestCase):6 unrelated_pr = make_pull_request("some-branch")7 stable_pr = make_pull_request(settings.STABLE_BRANCH)8 development_pr = make_pull_request(settings.DEVELOPMENT_BRANCH)9 @patch("githubflow.tasks.check_release_pr")10 def test_handle_pr_modified(self, mock_check):11 tasks.handle_pr_modified(self.unrelated_pr)12 self.assertEqual(mock_check.call_count, 0)13 tasks.handle_pr_modified(self.stable_pr)14 mock_check.assert_called_with(self.stable_pr)15 @patch("githubflow.tasks.create_release")16 @patch("githubflow.tasks.suggest_release_note")17 def test_handle_pr_merged(self, mock_suggest, mock_create):18 tasks.handle_pr_merged(self.unrelated_pr)19 self.assertEqual(mock_suggest.call_count, 0)20 self.assertEqual(mock_create.call_count, 0)21 tasks.handle_pr_merged(self.stable_pr)22 mock_create.assert_called_once_with(self.stable_pr)23 tasks.handle_pr_merged(self.development_pr)24 mock_suggest.assert_called_once_with(self.development_pr)25@patch("githubflow.tasks.create_or_fail")26class TestValidators(TestCase):27 @patch("githubflow.tasks.assert_valid_tag")28 @patch("githubflow.tasks.set_pr_status")29 def test_check_release_pr(self, mock_status, mock_assert, mock_create):30 pull_request = make_pull_request()31 with patch("githubflow.tasks.fail_pr") as mock_fail:32 tasks.check_release_pr(pull_request)33 mock_fail.assert_called()34 pull_request["body"] = "some-body"35 with patch("githubflow.tasks.fail_pr") as mock_fail:36 tasks.check_release_pr(pull_request)37 mock_fail.assert_called()38 pull_request["title"] = "1.2.3"39 with patch("githubflow.tasks.approve_pr") as mock_approve:40 tasks.check_release_pr(pull_request)41 mock_approve.assert_called()42 pull_request["title"] = "20010101.1"43 with patch("githubflow.tasks.fail_pr") as mock_fail:44 tasks.check_release_pr(pull_request)45 mock_fail.assert_called()46 pull_request["title"] = "{}.1".format(date.today().strftime('%Y%m%d'))47 with patch("githubflow.tasks.approve_pr") as mock_approve:48 tasks.check_release_pr(pull_request)49 mock_approve.assert_called()50 mock_assert.side_effect = tasks.ValidationError('foo')51 with patch("githubflow.tasks.fail_pr") as mock_fail:52 tasks.check_release_pr(pull_request)53 mock_fail.assert_called()54 mock_assert.side_effect = KeyError('foo')55 tasks.check_release_pr(pull_request)...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run toolium automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful