Best Python code snippet using tempest_python
test_server_groups.py
Source:test_server_groups.py
1# Copyright 2017: Inc.2# All Rights Reserved.3#4# Licensed under the Apache License, Version 2.0 (the "License"); you may5# not use this file except in compliance with the License. You may obtain6# a copy of the License at7#8# http://www.apache.org/licenses/LICENSE-2.09#10# Unless required by applicable law or agreed to in writing, software11# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT12# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the13# License for the specific language governing permissions and limitations14# under the License.15from unittest import mock16import ddt17from rally import exceptions as rally_exceptions18from rally_openstack.task.scenarios.nova import server_groups19from tests.unit import test20SERVER_GROUPS_MODULE = "rally_openstack.task.scenarios.nova.server_groups"21NOVA_SERVER_GROUPS = SERVER_GROUPS_MODULE + ".NovaServerGroups"22@ddt.ddt23class NovaServerGroupsTestCase(test.ScenarioTestCase):24 def test_create_and_list_server_groups(self):25 scenario = server_groups.CreateAndListServerGroups(self.context)26 fake_server_group = mock.MagicMock()27 all_projects = False28 scenario._create_server_group = mock.MagicMock()29 scenario._list_server_groups = mock.MagicMock()30 scenario._list_server_groups.return_value = [mock.MagicMock(),31 fake_server_group,32 mock.MagicMock()]33 # Positive case and kwargs is None34 scenario._create_server_group.return_value = fake_server_group35 scenario.run(policies="fake_policy", all_projects=False, kwargs=None)36 kwargs = {37 "policies": "fake_policy"38 }39 scenario._create_server_group.assert_called_once_with(**kwargs)40 scenario._list_server_groups.assert_called_once_with(all_projects)41 # Positive case and kwargs is not None42 foo_kwargs = {43 "policies": "fake_policy"44 }45 scenario._create_server_group.return_value = fake_server_group46 scenario.run(policies=None, all_projects=False,47 kwargs=foo_kwargs)48 scenario._create_server_group.assert_called_with(**foo_kwargs)49 scenario._list_server_groups.assert_called_with(all_projects)50 # Negative case1: server group isn't created51 scenario._create_server_group.return_value = None52 self.assertRaises(rally_exceptions.RallyAssertionError,53 scenario.run,54 **kwargs)55 scenario._create_server_group.assert_called_with(**kwargs)56 # Negative case2: server group not in the list of available server57 # groups58 scenario._create_server_group.return_value = mock.MagicMock()59 self.assertRaises(rally_exceptions.RallyAssertionError,60 scenario.run,61 **kwargs)62 scenario._create_server_group.assert_called_with(**kwargs)63 scenario._list_server_groups.assert_called_with(all_projects)64 def test_create_and_get_server_group_positive(self):65 scenario = server_groups.CreateAndGetServerGroup(self.context)66 fake_server_group = mock.MagicMock()67 fake_server_group_info = mock.MagicMock()68 fake_server_group.id = 12369 fake_server_group_info.id = 12370 scenario._create_server_group = mock.MagicMock()71 scenario._get_server_group = mock.MagicMock()72 # Positive case and kwargs is None73 kwargs = {74 "policies": "fake_policy"75 }76 scenario._create_server_group.return_value = fake_server_group77 scenario._get_server_group.return_value = fake_server_group_info78 scenario.run(policies="fake_policy", kwargs=None)79 scenario._create_server_group.assert_called_once_with(**kwargs)80 scenario._get_server_group.assert_called_once_with(81 fake_server_group.id)82 # Positive case and kwargs is not None83 scenario._create_server_group.return_value = fake_server_group84 scenario._get_server_group.return_value = fake_server_group_info85 foo_kwargs = {86 "policies": "fake_policy"87 }88 scenario.run(policies=None, kwargs=foo_kwargs)89 scenario._create_server_group.assert_called_with(**foo_kwargs)90 scenario._get_server_group.assert_called_with(91 fake_server_group.id)92 def test_create_and_get_server_group_negative(self):93 scenario = server_groups.CreateAndGetServerGroup(self.context)94 fake_server_group = mock.MagicMock()95 fake_server_group_info = mock.MagicMock()96 fake_server_group.id = 12397 fake_server_group_info.id = 12398 kwargs = {99 "policies": "fake_policy"100 }101 scenario._create_server_group = mock.MagicMock()102 scenario._get_server_group = mock.MagicMock()103 # Negative case1: server group isn't created104 scenario._create_server_group.return_value = None105 self.assertRaises(rally_exceptions.RallyAssertionError,106 scenario.run,107 **kwargs)108 scenario._create_server_group.assert_called_with(**kwargs)109 # Negative case2: server group to get information not the created one110 fake_server_group_info.id = 456111 scenario._create_server_group.return_value = fake_server_group112 self.assertRaises(rally_exceptions.RallyAssertionError,113 scenario.run,114 **kwargs)115 scenario._create_server_group.assert_called_with(**kwargs)116 scenario._get_server_group.assert_called_with(117 fake_server_group.id)118 def test_create_and_delete_server_group(self):119 scenario = server_groups.CreateAndDeleteServerGroup(self.context)120 fake_server_group = mock.MagicMock()121 scenario._create_server_group = mock.MagicMock()122 scenario._delete_server_group = mock.MagicMock()123 # Positive case and kwargs is None124 kwargs = {125 "policies": "fake_policy"126 }127 scenario._create_server_group.return_value = fake_server_group128 scenario.run(policies="fake_policy", kwargs=None)129 scenario._create_server_group.assert_called_once_with(**kwargs)130 scenario._delete_server_group.assert_called_once_with(131 fake_server_group.id)132 # Positive case and kwargs is not None133 scenario._create_server_group.return_value = fake_server_group134 foo_kwargs = {135 "policies": "fake_policy"136 }137 scenario.run(policies=None, kwargs=foo_kwargs)138 scenario._create_server_group.assert_called_with(**foo_kwargs)139 scenario._delete_server_group.assert_called_with(140 fake_server_group.id)141 # Negative case: server group isn't created142 scenario._create_server_group.return_value = None143 self.assertRaises(rally_exceptions.RallyAssertionError,144 scenario.run,145 **kwargs)...
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!