<s> import mock <EOL> import random <EOL> import string <EOL> import unittest <EOL> from cloudify . exceptions import NonRecoverableError <EOL> from cloudify . context import BootstrapContext <EOL> from cloudify . mocks import MockCloudifyContext <EOL> import openstack_plugin_common as common <EOL> import openstack_plugin_common . tests . test as common_test <EOL> import neutron_plugin <EOL> import neutron_plugin . network <EOL> import neutron_plugin . port <EOL> import neutron_plugin . router <EOL> import neutron_plugin . security_group <EOL> class ResourcesRenamingTest ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> neutron_plugin . port . _find_network_in_related_nodes = mock . Mock ( ) <EOL> common . Config . get = mock . Mock ( ) <EOL> common . Config . get . return_value = { } <EOL> self . neutron_mock = mock . Mock ( ) <EOL> def neutron_mock_connect ( unused_self , unused_cfg ) : <EOL> return self . neutron_mock <EOL> common . NeutronClient . connect = neutron_mock_connect <EOL> self . neutron_mock . cosmo_list = mock . Mock ( ) <EOL> self . neutron_mock . cosmo_list . return_value = [ ] <EOL> def _setup_ctx ( self , obj_type ) : <EOL> ctx = common_test . create_mock_ctx_with_provider_info ( <EOL> node_id = '<STR_LIT>' , <EOL> properties = { <EOL> obj_type : { <EOL> '<STR_LIT:name>' : obj_type + '<STR_LIT>' , <EOL> } , <EOL> '<STR_LIT>' : [ ] <EOL> } <EOL> ) <EOL> return ctx <EOL> def _test ( self , obj_type ) : <EOL> ctx = self . _setup_ctx ( obj_type ) <EOL> attr = getattr ( self . neutron_mock , '<STR_LIT>' + obj_type ) <EOL> attr . return_value = { <EOL> obj_type : { <EOL> '<STR_LIT:id>' : obj_type + '<STR_LIT>' , <EOL> } <EOL> } <EOL> getattr ( neutron_plugin , obj_type ) . create ( ctx ) <EOL> calls = attr . mock_calls <EOL> self . assertEquals ( len ( calls ) , <NUM_LIT:1> ) <EOL> arg = calls [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] <EOL> self . assertEquals ( arg [ obj_type ] [ '<STR_LIT:name>' ] , '<STR_LIT>' + obj_type + '<STR_LIT>' ) <EOL> def test_network ( self ) : <EOL> self . _test ( '<STR_LIT>' ) <EOL> def test_port ( self ) : <EOL> self . _test ( '<STR_LIT:port>' ) <EOL> def test_router ( self ) : <EOL> self . _test ( '<STR_LIT>' ) <EOL> def test_security_group ( self ) : <EOL> self . _test ( '<STR_LIT>' ) <EOL> def test_network_no_prefix ( self ) : <EOL> ctx = self . _setup_ctx ( '<STR_LIT>' ) <EOL> for pctx in common_test . BOOTSTRAP_CONTEXTS_WITHOUT_PREFIX : <EOL> ctx . _bootstrap_context = BootstrapContext ( pctx ) <EOL> self . neutron_mock . create_network . reset_mock ( ) <EOL> self . neutron_mock . create_network . return_value = { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:id>' : '<STR_LIT>' , <EOL> } <EOL> } <EOL> neutron_plugin . network . create ( ctx ) <EOL> calls = self . neutron_mock . create_network . mock_calls <EOL> self . assertEquals ( len ( calls ) , <NUM_LIT:1> ) <EOL> arg = calls [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] <EOL> self . assertEquals ( arg [ '<STR_LIT>' ] [ '<STR_LIT:name>' ] , '<STR_LIT>' , <EOL> "<STR_LIT>" + str ( pctx ) ) <EOL> def _rand_str ( n ) : <EOL> chars = string . ascii_uppercase + string . digits <EOL> return '<STR_LIT>' . join ( random . choice ( chars ) for _ in range ( n ) ) <EOL> class SecurityGroupTest ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> common . Config . get = mock . Mock ( ) <EOL> common . Config . get . return_value = { } <EOL> self . neutron_mock = mock . Mock ( ) <EOL> def neutron_mock_connect ( unused_self , unused_cfg ) : <EOL> return self . neutron_mock <EOL> common . NeutronClient . connect = neutron_mock_connect <EOL> neutron_plugin . security_group . _rules_for_sg_id = mock . Mock ( ) <EOL> neutron_plugin . security_group . _rules_for_sg_id . return_value = [ ] <EOL> def _setup_ctx ( self ) : <EOL> sg_name = _rand_str ( <NUM_LIT:6> ) + '<STR_LIT>' <EOL> ctx = MockCloudifyContext ( properties = { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:name>' : sg_name , <EOL> '<STR_LIT:description>' : '<STR_LIT>' <EOL> } , <EOL> '<STR_LIT>' : [ { '<STR_LIT:port>' : <NUM_LIT> } ] , <EOL> '<STR_LIT>' : True , <EOL> } ) <EOL> return ctx <EOL> def test_sg_new ( self ) : <EOL> ctx = self . _setup_ctx ( ) <EOL> self . neutron_mock . cosmo_list = mock . Mock ( ) <EOL> self . neutron_mock . cosmo_list . return_value = [ ] <EOL> self . neutron_mock . create_security_group = mock . Mock ( ) <EOL> self . neutron_mock . create_security_group . return_value = { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:description>' : '<STR_LIT>' , <EOL> '<STR_LIT:id>' : ctx [ '<STR_LIT>' ] [ '<STR_LIT:name>' ] + '<STR_LIT>' , <EOL> } <EOL> } <EOL> neutron_plugin . security_group . create ( ctx ) <EOL> self . assertTrue ( self . neutron_mock . create_security_group . mock_calls ) <EOL> def test_sg_use_existing ( self ) : <EOL> ctx = self . _setup_ctx ( ) <EOL> self . neutron_mock . cosmo_list = mock . Mock ( ) <EOL> self . neutron_mock . cosmo_list . return_value = [ { <EOL> '<STR_LIT:id>' : ctx [ '<STR_LIT>' ] [ '<STR_LIT:name>' ] + '<STR_LIT>' , <EOL> '<STR_LIT:description>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : [ { <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } ] <EOL> } ] <EOL> self . neutron_mock . create_security_group = mock . Mock ( ) <EOL> self . neutron_mock . create_security_group . return_value = { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:description>' : '<STR_LIT>' , <EOL> '<STR_LIT:id>' : ctx [ '<STR_LIT>' ] [ '<STR_LIT:name>' ] + '<STR_LIT>' , <EOL> } <EOL> } <EOL> neutron_plugin . security_group . create ( ctx ) <EOL> self . assertFalse ( self . neutron_mock . create_security_group . mock_calls ) <EOL> def test_sg_use_existing_with_other_rules ( self ) : <EOL> ctx = self . _setup_ctx ( ) <EOL> self . neutron_mock . cosmo_list = mock . Mock ( ) <EOL> self . neutron_mock . cosmo_list . return_value = [ { <EOL> '<STR_LIT:id>' : ctx [ '<STR_LIT>' ] [ '<STR_LIT:name>' ] + '<STR_LIT>' , <EOL> '<STR_LIT:description>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : [ { <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } ] <EOL> } ] <EOL> self . neutron_mock . create_security_group = mock . Mock ( ) <EOL> self . neutron_mock . create_security_group . return_value = { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:description>' : '<STR_LIT>' , <EOL> '<STR_LIT:id>' : ctx [ '<STR_LIT>' ] [ '<STR_LIT:name>' ] + '<STR_LIT>' , <EOL> } <EOL> } <EOL> self . assertRaises ( <EOL> NonRecoverableError , <EOL> neutron_plugin . security_group . create , <EOL> ctx <EOL> ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> unittest . main ( ) </s>
