How to use test_empty_profile method in prospector

Best Python code snippet using prospector_python

test_policy.py

Source:test_policy.py Github

copy

Full Screen

1# Copyright 2021 Open Source Robotics Foundation, Inc.2#3# Licensed under the Apache License, Version 2.0 (the "License");4# you may not use this file except in compliance with the License.5# You may obtain 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,11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.12# See the License for the specific language governing permissions and13# limitations under the License.14from lxml import etree15import nodl16import nodl._parsing17import nodl_to_policy.policy as policy18import pytest19import sros220def test_init_policy():21 """Test that an initial empty policy is well-formed."""22 test_policy = policy.init_policy()23 assert test_policy.tag == 'policy'24 assert test_policy.attrib['version'] == sros2.policy.POLICY_VERSION25 assert len(test_policy) == 126 assert test_policy[0].tag == 'enclaves'27 assert len(test_policy[0]) == 028def test_get_profile_minimal():29 """Test an XML tree with no pre-existing `enclaves`/`profiles`/`profile` tags."""30 test_policy = policy.init_policy()31 test_profile = policy.get_profile(test_policy, node_name='foo')32 # empty tags get generated after `get_profile` is called33 test_enclaves = test_policy[0]34 test_enclave = test_enclaves[0]35 test_profiles = test_enclave[0]36 # check that a single enclave tag was created37 assert len(test_enclaves) == 138 assert test_enclave.tag == 'enclave'39 assert test_enclave.attrib['path'] == '/foo'40 # check that a single profiles tag was created41 assert len(test_enclave) == 142 assert test_profiles.tag == 'profiles'43 # check that a single profile tag was created44 assert len(test_profiles) == 145 assert test_profiles[0] == test_profile46 assert test_profile.tag == 'profile'47 assert test_profile.attrib['ns'] == '/'48 assert test_profile.attrib['node'] == 'foo'49 assert len(test_profile) == 050def test_get_profile_exists(test_policy_tree):51 """52 Test an XML tree with existing `enclaves`/`profiles`/`profile` tags.53 A lot of checks in this test are to ensure that the54 `get_profile` function does not alter an existing policy tree.55 """56 test_policy = test_policy_tree57 test_enclaves = test_policy.find(path='enclaves')58 test_enclave = test_enclaves.find(path=f'enclave[@path="/node_1"]')59 test_profiles = test_enclave.find(path='profiles')60 test_profile = policy.get_profile(test_policy, node_name='node_1')61 # check that a single enclaves tag exists62 assert test_policy[0] == test_enclaves63 assert len(test_enclaves) == 2 # check that 'enclaves' tree contains two children64 assert test_enclaves.tag == 'enclaves'65 # check that a single enclave tag exists66 assert test_enclaves[0] == test_enclave67 assert len(test_enclave) == 1 # check that 'enclave' tree contains one child ('profiles')68 assert test_enclave.tag == 'enclave'69 assert test_enclave.attrib['path'] == '/node_1'70 # check that a single profiles tag exists71 assert test_enclave[0] == test_profiles72 assert len(test_profiles) == 1 # one node per enclave, one profile tag for each73 assert test_profiles.tag == 'profiles'74 # check that a <profile node='node1'> tag exists as expected75 assert test_profiles.find(path=f'profile[@ns="/"][@node="node_1"]') == test_profile76 assert test_profile.tag == 'profile'77 assert test_profile.attrib['ns'] == '/'78 assert test_profile.attrib['node'] == 'node_1'79 assert len(test_profile) == 4 # four child tags in the <profile node="node_1> tree80def test_get_permissions_minimal():81 """Test a profile tree with no permissions tag."""82 test_empty_profile = etree.Element('profile', attrib={'ns': '/', 'node': 'foo'})83 test_permissions = policy.get_permissions(84 profile=test_empty_profile,85 permission_type='permission',86 rule_type='rule',87 rule_expression='ALLOW')88 assert len(test_empty_profile) == 1 # only one 'permissions' tag89 assert test_empty_profile.find(path='permissions[@rule="ALLOW"]') == test_permissions90 assert test_permissions.tag == 'permissions'91 assert test_permissions.attrib['rule'] == 'ALLOW'92 assert len(test_permissions) == 0 # no permissions added93def test_get_permissions_exists(test_policy_tree):94 """95 Test an XML tree with existing `enclaves`/`profiles`/`profile` tags.96 A lot of checks in this test are to ensure that the97 `get_profile` function does not alter an existing policy tree.98 """99 test_profile = test_policy_tree.find(100 path='enclaves/enclave[@path="/node_1"]/profiles/profile[@ns="/"][@node="node_1"]')101 test_permissions = policy.get_permissions(102 profile=test_profile,103 permission_type='topic',104 rule_type='publish',105 rule_expression='ALLOW')106 assert len(test_profile) == 4 # number of rule tags is unchanged107 assert test_profile.find(path='topics[@publish="ALLOW"]') == test_permissions108 assert test_permissions.tag == 'topics'109 assert test_permissions.attrib['publish'] == 'ALLOW'110 assert len(test_permissions) == 3 # number of publish topics is unchanged111def test_add_permissions_no_items(mocker):112 """113 Test that permissions are correctly added when no allowed items are provided.114 It checks that the `get_permissions` function is run 0 times.115 """116 get_permissions_mock = mocker.patch('nodl_to_policy.policy.get_permissions', autospec=True)117 policy.add_permissions(118 profile=etree.Element('foo'),119 node=nodl.types.Node(name='bar', executable='prog'),120 permission_type='fizz',121 rule_type='buzz',122 expressions=[])123 assert not get_permissions_mock.call_count124def test_add_permissions_minimal():125 """Test a profile tree with no permissions tag."""126 test_empty_profile = etree.Element('profile', attrib={'ns': '/', 'node': 'foo'})127 policy.add_permissions(128 profile=test_empty_profile,129 node=nodl.types.Node(name='node', executable='prog'), # foo?130 permission_type='topic',131 rule_type='publish',132 expressions=['item'])133 test_permissions = test_empty_profile.find(path='topics[@publish="ALLOW"]')134 assert test_permissions.tag == 'topics'135 assert test_permissions.attrib['publish'] == 'ALLOW'136 assert len(test_permissions) == 1137 test_permission = test_permissions.find(path='topic')138 assert test_permission.tag == 'topic'139 assert test_permission.text == 'item'140def test_add_permissions_exists(test_policy_tree):141 """Test a profile tree with pre-existing permissions."""142 test_profile = test_policy_tree.find(143 path='enclaves/enclave[@path="/node_1"]/profiles/profile[@ns="/"][@node="node_1"]')144 policy.add_permissions(145 profile=test_profile,146 node=nodl.types.Node(name='node', executable='prog'),147 permission_type='topic',148 rule_type='publish',149 expressions=['item'])150 test_permissions = test_profile.find(path='topics[@publish="ALLOW"]')151 assert test_permissions.tag == 'topics'152 assert test_permissions.attrib['publish'] == 'ALLOW'153 assert len(test_permissions) == 4154 test_permission_items = test_permissions.findall(path='topic')155 assert 'item' in [item.text for item in test_permission_items]156def test_add_common_permissions_minimal(helpers, common_profile_tree):157 """Test addition of common permissions to an empty profile tree."""158 test_empty_profile = etree.Element('profile', attrib={'ns': '/', 'node': 'foo'})159 policy.add_common_permissions(160 profile=test_empty_profile,161 node=nodl.types.Node(name='node', executable='prog') # foo162 )163 assert helpers.xml_trees_equal(common_profile_tree, test_empty_profile)164def test_add_common_permissions_exists(helpers, test_policy_tree):165 """Test addition of common permissions to a profile tree with pre-existing permission tags."""166 test_profile = test_policy_tree.find(167 path='enclaves/enclave[@path="/node_1"]/profiles/profile[@ns="/"][@node="node_1"]')168 policy.add_common_permissions(169 test_profile, node=nodl.types.Node(name='node', executable='prog'))170 assert helpers.xml_trees_equal(171 test_profile,172 test_policy_tree.find(173 path='enclaves/enclave[@path="/node_1"]/profiles/profile[@ns="/"][@node="node_1"]')174 )175def test_convert_to_policy_invalid(empty_nodl_path):176 """Test NoDL conversion with an invalid path."""177 with pytest.raises(nodl.errors.NoDLError) as _:178 policy.convert_to_policy(179 nodl_description=nodl.parse(empty_nodl_path))180def test_convert_to_policy(helpers, test_nodl_path, test_policy_tree):181 """Test NoDL conversion with a fully formed description."""182 test_nodl_description = nodl.parse(test_nodl_path)183 test_converted_policy = policy.convert_to_policy(test_nodl_description)184 policy_xsl_path = sros2.policy.get_policy_template('policy.xsl')185 policy_xsl = etree.XSLT(etree.parse(str(policy_xsl_path)))186 test_converted_policy = policy_xsl(test_converted_policy).getroot()187 assert helpers.xml_trees_equal(test_converted_policy, test_policy_tree)188def test_print_policy(capfd, test_policy_tree):189 """190 Test the policy printing functionality.191 This works by comparing the standard output to the expected string form of the policy XML.192 """193 # the `capfd` fixture captures `stdout`/`stderr`194 policy.print_policy(test_policy_tree)195 out, _ = capfd.readouterr() # capture console output196 # assert that standard output is equivalent to pretty printed string form of policy XML tree197 assert out == etree.tostring(test_policy_tree, pretty_print=True).decode()198def test__get_topics_by_role_no_topics():199 """Test that `_get_topics_by_role` returns empty lists for an empty input."""200 # empty dict of topics should return two empty dicts201 test_publish_topics, test_subscribe_topics = policy._get_topics_by_role({})202 assert not test_publish_topics203 assert not test_subscribe_topics204def test__get_topics_by_role():205 """Test that `_get_topics_by_role` separates an input dict of topics correctly."""206 test_topics = {207 'foo': nodl.types.Topic(208 name='foo', message_type='footype', role=nodl.types.PubSubRole('publisher')),209 'bar': nodl.types.Topic(210 name='bar', message_type='bartype', role=nodl.types.PubSubRole('subscription')),211 'fizz': nodl.types.Topic(212 name='fizz', message_type='fizztype', role=nodl.types.PubSubRole('both')),213 }214 test_publish_topics_expected = {215 'foo': nodl.types.Topic(216 name='foo', message_type='footype', role=nodl.types.PubSubRole('publisher')),217 'fizz': nodl.types.Topic(218 name='fizz', message_type='fizztype', role=nodl.types.PubSubRole('both')),219 }220 test_subscribe_topics_expected = {221 'bar': nodl.types.Topic(222 name='bar', message_type='bartype', role=nodl.types.PubSubRole('subscription')),223 'fizz': nodl.types.Topic(224 name='fizz', message_type='fizztype', role=nodl.types.PubSubRole('both')),225 }226 test_subscribe_topics, test_publish_topics = policy._get_topics_by_role(test_topics)227 assert test_publish_topics == test_publish_topics_expected228 assert test_subscribe_topics == test_subscribe_topics_expected229def test__get_services_by_role_no_services():230 """Test that `_get_services_by_role` returns empty lists for an empty input."""231 # empty dict of services should return two empty dicts232 test_reply_services, test_request_services = policy._get_services_by_role({})233 assert not test_reply_services234 assert not test_request_services235def test__get_services_by_role():236 """Test that `_get_services_by_role` separates an input dict of services correctly."""237 test_services = {238 'foo': nodl.types.Service(239 name='foo', service_type='footype', role=nodl.types.ServerClientRole('server')),240 'bar': nodl.types.Service(241 name='bar', service_type='bartype', role=nodl.types.ServerClientRole('client')),242 'fizz': nodl.types.Service(243 name='fizz', service_type='fizztype', role=nodl.types.ServerClientRole('both')),244 }245 test_server_services_expected = {246 'foo': nodl.types.Service(247 name='foo', service_type='footype', role=nodl.types.ServerClientRole('server')),248 'fizz': nodl.types.Service(249 name='fizz', service_type='fizztype', role=nodl.types.ServerClientRole('both')),250 }251 test_client_services_expected = {252 'bar': nodl.types.Service(253 name='bar', service_type='bartype', role=nodl.types.ServerClientRole('client')),254 'fizz': nodl.types.Service(255 name='fizz', service_type='fizztype', role=nodl.types.ServerClientRole('both')),256 }257 test_server_services, test_client_services = policy._get_services_by_role(test_services)258 assert test_server_services == test_server_services_expected259 assert test_client_services == test_client_services_expected260def test__get_actions_by_role(mocker):261 """Test that `_get_actions_by_role` correctly calls `_get_services_by_role`."""262 get_services_mock = mocker.patch('nodl_to_policy.policy._get_services_by_role', autospec=True)263 policy._get_actions_by_role({})264 assert get_services_mock.call_count == 1...

Full Screen

Full Screen

cloud_provider_test.py

Source:cloud_provider_test.py Github

copy

Full Screen

...5 def setup(self):6 pass7 def tearDown(self):8 pass9 def test_empty_profile(self):10 with self.assertRaises(AssertionError) as context:11 p = CloudProvider({})12 self.assertTrue('Profile does not have a "cloudProvider" attribute'13 in str(context.exception))14 def test_ec2_profile(self):15 p = CloudProvider({'cloudProvider': 'ec2'})...

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 prospector 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