Best Python code snippet using tempest_python
test_security_groups.py
Source:test_security_groups.py
1# Licensed under the Apache License, Version 2.0 (the "License"); you may2# not use this file except in compliance with the License. You may obtain3# a copy of the License at4#5# http://www.apache.org/licenses/LICENSE-2.06#7# Unless required by applicable law or agreed to in writing, software8# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT9# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the10# License for the specific language governing permissions and limitations11# under the License.12import ddt13import mock14from rally import exceptions as rally_exceptions15from rally.plugins.openstack.scenarios.neutron import security_groups16from tests.unit import test17@ddt.ddt18class NeutronSecurityGroup(test.TestCase):19 @ddt.data(20 {},21 {"security_group_create_args": {}},22 {"security_group_create_args": {"description": "fake-description"}},23 )24 @ddt.unpack25 def test_create_and_list_security_groups(26 self, security_group_create_args=None):27 scenario = security_groups.CreateAndListSecurityGroups()28 security_group_data = security_group_create_args or {}29 scenario._create_security_group = mock.Mock()30 scenario._list_security_groups = mock.Mock()31 scenario.run(security_group_create_args=security_group_create_args)32 scenario._create_security_group.assert_called_once_with(33 **security_group_data)34 scenario._list_security_groups.assert_called_once_with()35 @ddt.data(36 {},37 {"security_group_create_args": {}},38 {"security_group_create_args": {"description": "fake-description"}},39 )40 @ddt.unpack41 def test_create_and_show_security_group(42 self, security_group_create_args=None):43 scenario = security_groups.CreateAndShowSecurityGroup()44 security_group = mock.Mock()45 security_group_data = security_group_create_args or {}46 scenario._create_security_group = mock.Mock()47 scenario._show_security_group = mock.Mock()48 # Positive case49 scenario._create_security_group.return_value = security_group50 scenario.run(security_group_create_args=security_group_create_args)51 scenario._create_security_group.assert_called_once_with(52 **security_group_data)53 scenario._show_security_group.assert_called_once_with(54 scenario._create_security_group.return_value)55 @ddt.data(56 {},57 {"security_group_create_args": {}},58 {"security_group_create_args": {"description": "fake-description"}},59 )60 @ddt.unpack61 def test_create_and_show_security_group_with_none_group(62 self, security_group_create_args=None):63 scenario = security_groups.CreateAndShowSecurityGroup()64 security_group_data = security_group_create_args or {}65 scenario._create_security_group = mock.Mock()66 scenario._show_security_group = mock.Mock()67 # Negative case: security_group isn't created68 scenario._create_security_group.return_value = None69 self.assertRaises(rally_exceptions.RallyAssertionError,70 scenario.run, security_group_create_args)71 scenario._create_security_group.assert_called_with(72 **security_group_data)73 @ddt.data(74 {},75 {"security_group_create_args": {}},76 {"security_group_create_args": {"description": "fake-description"}},77 )78 @ddt.unpack79 def test_create_and_delete_security_groups(80 self, security_group_create_args=None):81 scenario = security_groups.CreateAndDeleteSecurityGroups()82 security_group_data = security_group_create_args or {}83 scenario._create_security_group = mock.Mock()84 scenario._delete_security_group = mock.Mock()85 scenario.run(security_group_create_args=security_group_create_args)86 scenario._create_security_group.assert_called_once_with(87 **security_group_data)88 scenario._delete_security_group.assert_called_once_with(89 scenario._create_security_group.return_value)90 @ddt.data(91 {},92 {"security_group_create_args": {}},93 {"security_group_create_args": {"description": "fake-description"}},94 {"security_group_update_args": {}},95 {"security_group_update_args": {"description": "fake-updated-descr"}},96 )97 @ddt.unpack98 def test_create_and_update_security_groups(99 self, security_group_create_args=None,100 security_group_update_args=None):101 scenario = security_groups.CreateAndUpdateSecurityGroups()102 security_group_data = security_group_create_args or {}103 security_group_update_data = security_group_update_args or {}104 scenario._create_security_group = mock.Mock()105 scenario._update_security_group = mock.Mock()106 scenario.run(security_group_create_args=security_group_create_args,107 security_group_update_args=security_group_update_args)108 scenario._create_security_group.assert_called_once_with(109 **security_group_data)110 scenario._update_security_group.assert_called_once_with(111 scenario._create_security_group.return_value,112 **security_group_update_data)113 @ddt.data(114 {},115 {"security_group_args": {}},116 {"security_group_args": {"description": "fake-description"}},117 {"security_group_rule_args": {}},118 {"security_group_rule_args": {"description": "fake-rule-descr"}},119 )120 @ddt.unpack121 def test_create_and_list_security_group_rules(122 self, security_group_args=None,123 security_group_rule_args=None):124 scenario = security_groups.CreateAndListSecurityGroupRules()125 security_group_data = security_group_args or {}126 security_group_rule_data = security_group_rule_args or {}127 security_group = mock.MagicMock()128 security_group_rule = {"security_group_rule": {"id": 1, "name": "f1"}}129 scenario._create_security_group = mock.MagicMock()130 scenario._create_security_group_rule = mock.MagicMock()131 scenario._list_security_group_rules = mock.MagicMock()132 # Positive case133 scenario._create_security_group.return_value = security_group134 scenario._create_security_group_rule.return_value = security_group_rule135 scenario._list_security_group_rules.return_value = {136 "security_group_rules": [{"id": 1, "name": "f1"},137 {"id": 2, "name": "f2"},138 {"id": 3, "name": "f3"}]}139 scenario.run(security_group_args=security_group_data,140 security_group_rule_args=security_group_rule_data)141 scenario._create_security_group.assert_called_once_with(142 **security_group_data)143 scenario._create_security_group_rule.assert_called_once_with(144 security_group["security_group"]["id"],145 **security_group_rule_data)146 scenario._list_security_group_rules.assert_called_once_with()147 @ddt.data(148 {},149 {"security_group_args": {}},150 {"security_group_args": {"description": "fake-description"}},151 {"security_group_rule_args": {}},152 {"security_group_rule_args": {"description": "fake-rule-descr"}},153 )154 @ddt.unpack155 def test_create_and_list_security_group_rules_with_fails(156 self, security_group_args=None,157 security_group_rule_args=None):158 scenario = security_groups.CreateAndListSecurityGroupRules()159 security_group_data = security_group_args or {}160 security_group_rule_data = security_group_rule_args or {}161 security_group = mock.MagicMock()162 security_group_rule = {"security_group_rule": {"id": 1, "name": "f1"}}163 scenario._create_security_group = mock.MagicMock()164 scenario._create_security_group_rule = mock.MagicMock()165 scenario._list_security_group_rules = mock.MagicMock()166 scenario._create_security_group_rule.return_value = security_group_rule167 scenario._list_security_group_rules.return_value = {168 "security_group_rules": [{"id": 1, "name": "f1"},169 {"id": 2, "name": "f2"},170 {"id": 3, "name": "f3"}]}171 # Negative case1: security_group isn't created172 scenario._create_security_group.return_value = None173 self.assertRaises(rally_exceptions.RallyAssertionError,174 scenario.run,175 security_group_data,176 security_group_rule_data)177 scenario._create_security_group.assert_called_with(178 **security_group_data)179 # Negative case2: security_group_rule isn't created180 scenario._create_security_group.return_value = security_group181 scenario._create_security_group_rule.return_value = None182 self.assertRaises(rally_exceptions.RallyAssertionError,183 scenario.run,184 security_group_data,185 security_group_rule_data)186 scenario._create_security_group.assert_called_with(187 **security_group_data)188 scenario._create_security_group_rule.assert_called_with(189 security_group["security_group"]["id"],190 **security_group_rule_data)191 # Negative case3: security_group_rule isn't listed192 scenario._create_security_group.return_value = security_group193 scenario._create_security_group_rule.return_value = mock.MagicMock()194 self.assertRaises(rally_exceptions.RallyAssertionError,195 scenario.run,196 security_group_data,197 security_group_rule_data)198 scenario._create_security_group.assert_called_with(199 **security_group_data)200 scenario._create_security_group_rule.assert_called_with(201 security_group["security_group"]["id"],202 **security_group_rule_data)203 scenario._list_security_group_rules.assert_called_with()204 @ddt.data(205 {},206 {"security_group_args": {}},207 {"security_group_args": {"description": "fake-description"}},208 {"security_group_rule_args": {}},209 {"security_group_rule_args": {"description": "fake-rule-descr"}}210 )211 @ddt.unpack212 def test_create_and_show_security_group_rule(213 self, security_group_args=None,214 security_group_rule_args=None):215 scenario = security_groups.CreateAndShowSecurityGroupRule()216 security_group_data = security_group_args or {}217 security_group_rule_data = security_group_rule_args or {}218 security_group = mock.MagicMock()219 security_group_rule = {"security_group_rule": {"id": 1, "name": "f1"}}220 scenario._create_security_group = mock.MagicMock()221 scenario._create_security_group_rule = mock.MagicMock()222 scenario._show_security_group_rule = mock.MagicMock()223 # Positive case224 scenario._create_security_group.return_value = security_group225 scenario._create_security_group_rule.return_value = security_group_rule226 scenario.run(security_group_args=security_group_data,227 security_group_rule_args=security_group_rule_data)228 scenario._create_security_group.assert_called_once_with(229 **security_group_data)230 scenario._create_security_group_rule.assert_called_once_with(231 security_group["security_group"]["id"],232 **security_group_rule_data)233 scenario._show_security_group_rule.assert_called_once_with(234 security_group_rule["security_group_rule"]["id"])235 @ddt.data(236 {},237 {"security_group_args": {}},238 {"security_group_args": {"description": "fake-description"}},239 {"security_group_rule_args": {}},240 {"security_group_rule_args": {"description": "fake-rule-descr"}}241 )242 @ddt.unpack243 def test_create_and_delete_security_group_rule(244 self, security_group_args=None,245 security_group_rule_args=None):246 scenario = security_groups.CreateAndDeleteSecurityGroupRule()247 security_group_data = security_group_args or {}248 security_group_rule_data = security_group_rule_args or {}249 security_group = mock.MagicMock()250 security_group_rule = {"security_group_rule": {"id": 1, "name": "f1"}}251 scenario._create_security_group = mock.MagicMock()252 scenario._create_security_group_rule = mock.MagicMock()253 scenario._delete_security_group_rule = mock.MagicMock()254 scenario._delete_security_group = mock.MagicMock()255 # Positive case256 scenario._create_security_group.return_value = security_group257 scenario._create_security_group_rule.return_value = security_group_rule258 scenario.run(security_group_args=security_group_data,259 security_group_rule_args=security_group_rule_data)260 scenario._create_security_group.assert_called_once_with(261 **security_group_data)262 scenario._create_security_group_rule.assert_called_once_with(263 security_group["security_group"]["id"],264 **security_group_rule_data)265 scenario._delete_security_group_rule.assert_called_once_with(266 security_group_rule["security_group_rule"]["id"])267 scenario._delete_security_group.assert_called_once_with(268 security_group)269 @ddt.data(270 {},271 {"security_group_args": {}},272 {"security_group_args": {"description": "fake-description"}},273 {"security_group_rule_args": {}},274 {"security_group_rule_args": {"description": "fake-rule-descr"}},275 )276 @ddt.unpack277 def test_create_and_show_security_group_rule_with_fails(278 self, security_group_args=None,279 security_group_rule_args=None):280 scenario = security_groups.CreateAndShowSecurityGroupRule()281 security_group_data = security_group_args or {}282 security_group_rule_data = security_group_rule_args or {}283 security_group = mock.MagicMock()284 security_group_rule = {"security_group_rule": {"id": 1, "name": "f1"}}285 scenario._create_security_group = mock.MagicMock()286 scenario._create_security_group_rule = mock.MagicMock()287 scenario._show_security_group_rule = mock.MagicMock()288 scenario._create_security_group_rule.return_value = security_group_rule289 # Negative case1: security_group isn't created290 scenario._create_security_group.return_value = None291 self.assertRaises(rally_exceptions.RallyAssertionError,292 scenario.run,293 security_group_data,294 security_group_rule_data)295 scenario._create_security_group.assert_called_with(296 **security_group_data)297 # Negative case2: security_group_rule isn't created298 scenario._create_security_group.return_value = security_group299 scenario._create_security_group_rule.return_value = None300 self.assertRaises(rally_exceptions.RallyAssertionError,301 scenario.run,302 security_group_data,303 security_group_rule_data)304 scenario._create_security_group.assert_called_with(305 **security_group_data)306 scenario._create_security_group_rule.assert_called_with(307 security_group["security_group"]["id"],...
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!!