Best Python code snippet using tempest_python
test_neutron_metering.py
Source:test_neutron_metering.py
1#2# Licensed under the Apache License, Version 2.0 (the "License"); you may3# not use this file except in compliance with the License. You may obtain4# a copy of the License at5#6# http://www.apache.org/licenses/LICENSE-2.07#8# Unless required by applicable law or agreed to in writing, software9# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT10# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the11# License for the specific language governing permissions and limitations12# under the License.13import mock14import six15from neutronclient.common import exceptions16from neutronclient.v2_0 import client as neutronclient17from heat.common import exception18from heat.common import template_format19from heat.engine.resources.openstack.neutron import metering20from heat.engine import scheduler21from heat.tests import common22from heat.tests import utils23metering_template = '''24heat_template_version: 2015-04-3025description: Template to test metering resources26resources:27 label:28 type: OS::Neutron::MeteringLabel29 properties:30 name: TestLabel31 description: Description of TestLabel32 shared: True33 rule:34 type: OS::Neutron::MeteringRule35 properties:36 metering_label_id: { get_resource: label }37 remote_ip_prefix: 10.0.3.0/2438 direction: ingress39 excluded: false40'''41class MeteringLabelTest(common.HeatTestCase):42 def setUp(self):43 super(MeteringLabelTest, self).setUp()44 self.mockclient = mock.Mock(spec=neutronclient.Client)45 self.patchobject(neutronclient, 'Client', return_value=self.mockclient)46 def create_metering_label(self):47 self.mockclient.create_metering_label.return_value = {48 'metering_label': {'id': '1234'}49 }50 snippet = template_format.parse(metering_template)51 self.stack = utils.parse_stack(snippet)52 resource_defns = self.stack.t.resource_definitions(self.stack)53 return metering.MeteringLabel(54 'label', resource_defns['label'], self.stack)55 def test_create(self):56 rsrc = self.create_metering_label()57 scheduler.TaskRunner(rsrc.create)()58 self.assertEqual((rsrc.CREATE, rsrc.COMPLETE), rsrc.state)59 self.mockclient.create_metering_label.assert_called_once_with({60 'metering_label': {61 'name': 'TestLabel',62 'description': 'Description of TestLabel',63 'shared': True64 }65 })66 def test_create_failed(self):67 self.mockclient.create_metering_label.side_effect = (68 exceptions.NeutronClientException())69 snippet = template_format.parse(metering_template)70 stack = utils.parse_stack(snippet)71 resource_defns = stack.t.resource_definitions(stack)72 rsrc = metering.MeteringLabel(73 'label', resource_defns['label'], stack)74 error = self.assertRaises(exception.ResourceFailure,75 scheduler.TaskRunner(rsrc.create))76 self.assertEqual(77 'NeutronClientException: resources.label: '78 'An unknown exception occurred.',79 six.text_type(error))80 self.assertEqual((rsrc.CREATE, rsrc.FAILED), rsrc.state)81 self.mockclient.create_metering_label.assert_called_once_with({82 'metering_label': {83 'name': 'TestLabel',84 'description': 'Description of TestLabel',85 'shared': True86 }87 })88 def test_delete(self):89 rsrc = self.create_metering_label()90 self.mockclient.delete_metering_label.return_value = None91 self.mockclient.show_metering_label.side_effect = (92 exceptions.NeutronClientException(status_code=404))93 scheduler.TaskRunner(rsrc.create)()94 scheduler.TaskRunner(rsrc.delete)()95 self.assertEqual((rsrc.DELETE, rsrc.COMPLETE), rsrc.state)96 self.mockclient.create_metering_label.assert_called_once_with({97 'metering_label': {98 'name': 'TestLabel',99 'description': 'Description of TestLabel',100 'shared': True101 }102 })103 self.mockclient.delete_metering_label.assert_called_once_with('1234')104 self.mockclient.show_metering_label.assert_called_once_with('1234')105 def test_delete_already_gone(self):106 rsrc = self.create_metering_label()107 self.mockclient.delete_metering_label.side_effect = (108 exceptions.NeutronClientException(status_code=404))109 scheduler.TaskRunner(rsrc.create)()110 scheduler.TaskRunner(rsrc.delete)()111 self.assertEqual((rsrc.DELETE, rsrc.COMPLETE), rsrc.state)112 self.mockclient.create_metering_label.assert_called_once_with({113 'metering_label': {114 'name': 'TestLabel',115 'description': 'Description of TestLabel',116 'shared': True117 }118 })119 self.mockclient.delete_metering_label.assert_called_once_with('1234')120 self.mockclient.show_metering_label.assert_not_called()121 def test_delete_failed(self):122 rsrc = self.create_metering_label()123 self.mockclient.delete_metering_label.side_effect = (124 exceptions.NeutronClientException(status_code=400))125 scheduler.TaskRunner(rsrc.create)()126 error = self.assertRaises(exception.ResourceFailure,127 scheduler.TaskRunner(rsrc.delete))128 self.assertEqual(129 'NeutronClientException: resources.label: '130 'An unknown exception occurred.',131 six.text_type(error))132 self.assertEqual((rsrc.DELETE, rsrc.FAILED), rsrc.state)133 self.mockclient.create_metering_label.assert_called_once_with({134 'metering_label': {135 'name': 'TestLabel',136 'description': 'Description of TestLabel',137 'shared': True138 }139 })140 self.mockclient.delete_metering_label.assert_called_once_with('1234')141 def test_attribute(self):142 rsrc = self.create_metering_label()143 self.mockclient.show_metering_label.return_value = {144 'metering_label': {145 'name': 'TestLabel',146 'description': 'Description of TestLabel',147 'shared': True148 }149 }150 scheduler.TaskRunner(rsrc.create)()151 self.assertEqual('TestLabel', rsrc.FnGetAtt('name'))152 self.assertEqual('Description of TestLabel',153 rsrc.FnGetAtt('description'))154 self.assertTrue(rsrc.FnGetAtt('shared'))155 self.mockclient.create_metering_label.assert_called_once_with({156 'metering_label': {157 'name': 'TestLabel',158 'description': 'Description of TestLabel',159 'shared': True160 }161 })162 self.mockclient.show_metering_label.assert_called_with('1234')163class MeteringRuleTest(common.HeatTestCase):164 def setUp(self):165 super(MeteringRuleTest, self).setUp()166 self.mockclient = mock.Mock(spec=neutronclient.Client)167 self.patchobject(neutronclient, 'Client', return_value=self.mockclient)168 def create_metering_label_rule(self):169 self.mockclient.create_metering_label_rule.return_value = {170 'metering_label_rule': {'id': '5678'}171 }172 snippet = template_format.parse(metering_template)173 self.stack = utils.parse_stack(snippet)174 self.patchobject(self.stack['label'], 'FnGetRefId',175 return_value='1234')176 resource_defns = self.stack.t.resource_definitions(self.stack)177 return metering.MeteringRule(178 'rule', resource_defns['rule'], self.stack)179 def test_create(self):180 rsrc = self.create_metering_label_rule()181 scheduler.TaskRunner(rsrc.create)()182 self.assertEqual((rsrc.CREATE, rsrc.COMPLETE), rsrc.state)183 self.mockclient.create_metering_label_rule.assert_called_once_with({184 'metering_label_rule': {185 'metering_label_id': '1234',186 'remote_ip_prefix': '10.0.3.0/24',187 'direction': 'ingress',188 'excluded': False189 }190 })191 def test_create_failed(self):192 self.mockclient.create_metering_label_rule.side_effect = (193 exceptions.NeutronClientException())194 snippet = template_format.parse(metering_template)195 stack = utils.parse_stack(snippet)196 self.patchobject(stack['label'], 'FnGetRefId',197 return_value='1234')198 resource_defns = stack.t.resource_definitions(stack)199 rsrc = metering.MeteringRule(200 'rule', resource_defns['rule'], stack)201 error = self.assertRaises(exception.ResourceFailure,202 scheduler.TaskRunner(rsrc.create))203 self.assertEqual(204 'NeutronClientException: resources.rule: '205 'An unknown exception occurred.',206 six.text_type(error))207 self.assertEqual((rsrc.CREATE, rsrc.FAILED), rsrc.state)208 self.mockclient.create_metering_label_rule.assert_called_once_with({209 'metering_label_rule': {210 'metering_label_id': '1234',211 'remote_ip_prefix': '10.0.3.0/24',212 'direction': 'ingress',213 'excluded': False214 }215 })216 def test_delete(self):217 rsrc = self.create_metering_label_rule()218 self.mockclient.delete_metering_label_rule.return_value = None219 self.mockclient.show_metering_label_rule.side_effect = (220 exceptions.NeutronClientException(status_code=404))221 scheduler.TaskRunner(rsrc.create)()222 scheduler.TaskRunner(rsrc.delete)()223 self.assertEqual((rsrc.DELETE, rsrc.COMPLETE), rsrc.state)224 self.mockclient.create_metering_label_rule.assert_called_once_with({225 'metering_label_rule': {226 'metering_label_id': '1234',227 'remote_ip_prefix': '10.0.3.0/24',228 'direction': 'ingress',229 'excluded': False230 }231 })232 self.mockclient.delete_metering_label_rule.assert_called_once_with(233 '5678')234 self.mockclient.show_metering_label_rule.assert_called_once_with(235 '5678')236 def test_delete_already_gone(self):237 rsrc = self.create_metering_label_rule()238 self.mockclient.delete_metering_label_rule.side_effect = (239 exceptions.NeutronClientException(status_code=404))240 scheduler.TaskRunner(rsrc.create)()241 scheduler.TaskRunner(rsrc.delete)()242 self.assertEqual((rsrc.DELETE, rsrc.COMPLETE), rsrc.state)243 self.mockclient.create_metering_label_rule.assert_called_once_with({244 'metering_label_rule': {245 'metering_label_id': '1234',246 'remote_ip_prefix': '10.0.3.0/24',247 'direction': 'ingress',248 'excluded': False249 }250 })251 self.mockclient.delete_metering_label_rule.assert_called_once_with(252 '5678')253 self.mockclient.show_metering_label_rule.assert_not_called()254 def test_delete_failed(self):255 rsrc = self.create_metering_label_rule()256 self.mockclient.delete_metering_label_rule.side_effect = (257 exceptions.NeutronClientException(status_code=400))258 scheduler.TaskRunner(rsrc.create)()259 error = self.assertRaises(exception.ResourceFailure,260 scheduler.TaskRunner(rsrc.delete))261 self.assertEqual(262 'NeutronClientException: resources.rule: '263 'An unknown exception occurred.',264 six.text_type(error))265 self.assertEqual((rsrc.DELETE, rsrc.FAILED), rsrc.state)266 self.mockclient.create_metering_label_rule.assert_called_once_with({267 'metering_label_rule': {268 'metering_label_id': '1234',269 'remote_ip_prefix': '10.0.3.0/24',270 'direction': 'ingress',271 'excluded': False272 }273 })274 self.mockclient.delete_metering_label_rule.assert_called_once_with(275 '5678')276 self.mockclient.show_metering_label_rule.assert_not_called()277 def test_attribute(self):278 rsrc = self.create_metering_label_rule()279 self.mockclient.show_metering_label_rule.return_value = {280 'metering_label_rule': {281 'metering_label_id': '1234',282 'remote_ip_prefix': '10.0.3.0/24',283 'direction': 'ingress',284 'excluded': False285 }286 }287 scheduler.TaskRunner(rsrc.create)()288 self.assertEqual('10.0.3.0/24', rsrc.FnGetAtt('remote_ip_prefix'))289 self.assertEqual('ingress', rsrc.FnGetAtt('direction'))290 self.assertIs(False, rsrc.FnGetAtt('excluded'))291 self.mockclient.create_metering_label_rule.assert_called_once_with({292 'metering_label_rule': {293 'metering_label_id': '1234',294 'remote_ip_prefix': '10.0.3.0/24',295 'direction': 'ingress',296 'excluded': False297 }298 })...
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!!