def test_set_directory_config(self, _): target = MockContext({ 'instance': MockNodeInstanceContext( id='terra_install-1', runtime_properties=self.get_terraform_conf_props( test_dir2).get("terraform_config") ), 'node': MockNodeContext( id='1', properties=self.get_terraform_conf_props(test_dir2) ), '_context': { 'node_id': '1' }}) source_work_dir = mkdtemp() source = MockContext({ 'instance': MockNodeInstanceContext( id='terra_module-1', runtime_properties={}), 'node': MockNodeContext( id='2', properties=self.get_terraform_module_conf_props( source_work_dir) ), '_context': { 'node_id': '2' }}) ctx = MockCloudifyContextRels(source=source, target=target) current_ctx.set(ctx=ctx) kwargs = { 'ctx': ctx } set_directory_config(**kwargs) self.assertEqual( ctx.source.instance.runtime_properties.get("executable_path"), ctx.target.instance.runtime_properties.get("executable_path"))
def test_add_microservice_proxy(self): ctx = MockCloudifyContext( source=MockContext( {'instance': MockContext({'runtime_properties': {}})}), target=MockContext({ 'node': MockContext({'type': 'cloudify.nodes.DeploymentProxy'}), 'instance': MockContext({ 'runtime_properties': { 'swarm_info': { 'manager_ip': '1.1.1.1', 'manager_port': '1231' } } }) })) current_ctx.set(ctx=ctx) tasks.add_microservice( proxy_ip_prop='["swarm_info"]["manager_ip"]', proxy_port_prop='["swarm_info"]["manager_port"]') self.assertEquals(ctx.source.instance.runtime_properties['ip'], '1.1.1.1') self.assertEquals(ctx.source.instance.runtime_properties['port'], '1231')
def setUp(self): ctx = MockCloudifyContext( target=MockContext({ 'instance': MockNodeInstanceContext( 'sg1', { OPENSTACK_ID_PROPERTY: 'test-sg', OPENSTACK_NAME_PROPERTY: 'test-sg-name' }) }), source=MockContext({ 'node': mock.MagicMock(), 'instance': MockNodeInstanceContext( 'server', { OPENSTACK_ID_PROPERTY: 'server' } )}) ) current_ctx.set(ctx) self.addCleanup(current_ctx.clear) findctx = mock.patch( 'openstack_plugin_common._find_context_in_kw', return_value=ctx, ) findctx.start() self.addCleanup(findctx.stop)
def test_bootstrap_context_key_pair(self, *_): """tests finding a key file via bootstrap context """ name = 'test_bootstrap_context_key_pair' private_key_dir = tempfile.mkdtemp() private_key_path = '{0}/{1}.pem' \ .format(private_key_dir, name) open(private_key_path, 'w').close() cloudify_agent = MockContext() cloudify_agent['agent_key_path'] = private_key_path bootstrap_ctx = BootstrapContext({'cloudify_agent': cloudify_agent}) ctx = MockCloudifyContext(bootstrap_context=bootstrap_ctx, node_name=name) current_ctx.set(ctx=ctx) with mock.patch( 'cloudify_aws.utils.get_single_connected_node_by_type') \ as mock_get_connected: with mock.patch('cloudify_aws.ec2.instance.Instance.__init__') \ as mock_instance_init: mock_instance_init.return_value = None mock_get_connected.return_value = None output = instance.Instance()._get_private_key('') self.assertEqual(private_key_path, output)
def mock_ctx(self, test_vars, test_id, test_deployment_id, runtime_properties=None): ctx = MockContext() ctx.node = MockNodeContext(properties=test_vars) ctx.bootstrap_context = BootstrapContext( common_test.BOOTSTRAP_CONTEXTS_WITHOUT_PREFIX[0]) ctx.instance = MockNodeInstanceContext( id=test_id, runtime_properties=runtime_properties or {}) ctx.deployment = mock.Mock() ctx.deployment.id = test_deployment_id ctx.type = NODE_INSTANCE ctx.logger = mock.Mock() return ctx
def test_add_microservice_node(self): ctx = MockCloudifyContext(source=MockContext( {'instance': MockContext({'runtime_properties': {}})}), target=MockContext({ 'node': MockContext({ 'type': 'cloudify.swarm.Manager', 'properties': { 'ip': '1.1.1.1', 'port': '1232' } }), 'instance': MockContext({}) })) current_ctx.set(ctx=ctx) tasks.add_microservice() self.assertEquals(ctx.source.instance.runtime_properties['ip'], '1.1.1.1') self.assertEquals(ctx.source.instance.runtime_properties['port'], '1232')
def mock_ctx(self, test_vars, test_id, test_deployment_id, runtime_properties=None): ctx = MockContext() ctx.node = MockNodeContext(properties=test_vars) ctx.instance = MockNodeInstanceContext( id=test_id, runtime_properties=runtime_properties or {}) ctx.deployment = mock.Mock() ctx.deployment.id = test_deployment_id ctx.type = NODE_INSTANCE return ctx
def mock_ctx(self, test_vars, test_id, test_deployment_id, runtime_properties=None): ctx = MockContext() ctx.node = MockNodeContext(properties=test_vars) ctx.instance = MockNodeInstanceContext( id=test_id, runtime_properties=runtime_properties or {}) ctx.deployment = mock.Mock() ctx.deployment.id = test_deployment_id ctx.bootstrap_context = mock.Mock() setattr(ctx.bootstrap_context, 'resources_prefix', '') ctx.type = NODE_INSTANCE ctx.logger = mock.Mock() return ctx
def mock_relationship_context(self, testname): instance_context = MockContext({ 'node': MockContext({ 'properties': { constants.AWS_CONFIG_PROPERTY: {}, 'use_external_resource': False, 'resource_id': '' } }), 'instance': MockContext({ 'runtime_properties': { 'aws_resource_id': 'i-abc1234', 'public_ip_address': '127.0.0.1' } }) }) elasticip_context = MockContext({ 'node': MockContext({ 'properties': { constants.AWS_CONFIG_PROPERTY: {}, 'use_external_resource': False, 'resource_id': '', 'domain': '', } }), 'instance': MockContext({ 'runtime_properties': { 'aws_resource_id': '' } }) }) relationship_context = MockCloudifyContext( node_id=testname, source=instance_context, target=elasticip_context) setattr(relationship_context.source.node, 'type_hierarchy', ['cloudify.nodes.Compute', 'cloudify.aws.nodes.Instance'] ) return relationship_context
def mock_relationship_context(self, testname): source_context = MockContext({ 'node': MockContext({ 'properties': { constants.AWS_CONFIG_PROPERTY: {}, 'use_external_resource': False, 'resource_id': '' } }), 'instance': MockContext({ 'runtime_properties': { 'aws_resource_id': 'r-abc1234' } }) }) target_context = MockContext({ 'node': MockContext({ 'properties': { constants.AWS_CONFIG_PROPERTY: {}, 'use_external_resource': False, 'resource_id': 'r-abc12345' } }), 'instance': MockContext({ 'runtime_properties': { 'aws_resource_id': 'r-abc12346', 'relationships': 'cloudify.aws.relationships.root_connected_to_root' } }) }) relationship_context = MockCloudifyContext( node_id=testname, source=source_context, target=target_context) setattr(relationship_context.source.node, 'type_hierarchy', ['cloudify.nodes.Root', 'cloudify.nodes.Root'] ) return relationship_context
def mock_volume_relationship_context(self, testname): instance_context = MockContext({ 'node': MockContext({ 'properties': { constants.AWS_CONFIG_PROPERTY: self._get_aws_config(), 'use_external_resource': False, 'resource_id': '' } }), 'instance': MockContext({ 'runtime_properties': { 'aws_resource_id': 'i-abc1234', 'public_ip_address': '127.0.0.1' } }) }) volume_context = MockContext({ 'node': MockContext({ 'properties': { constants.AWS_CONFIG_PROPERTY: self._get_aws_config(), 'use_external_resource': False, 'resource_id': '', 'zone': self.env.availability_zone, 'size': TEST_SIZE, 'device': TEST_DEVICE, } }), 'instance': MockContext({'runtime_properties': { 'aws_resource_id': '' }}) }) relationship_context = MockCloudifyContext(node_id=testname, source=volume_context, target=instance_context) return relationship_context
def mock_relationship_context(self, testname): instance_context = MockContext({ 'node': MockContext({ 'properties': { constants.AWS_CONFIG_PROPERTY: {}, 'use_external_resource': False, 'resource_id': '' } }), 'instance': MockContext( {'runtime_properties': { 'aws_resource_id': 'i-abc1234' }}) }) volume_context = MockContext({ 'node': MockContext({ 'properties': { constants.AWS_CONFIG_PROPERTY: {}, 'use_external_resource': False, 'resource_id': '', 'zone': '', 'size': '', 'device': TEST_DEVICE } }), 'instance': MockContext({'runtime_properties': { 'aws_resource_id': '' }}) }) relationship_context = MockCloudifyContext(node_id=testname, source=volume_context, target=instance_context) return relationship_context
def mock_relationship_context(self, testname): instance_context = MockContext({ 'node': MockContext({ 'properties': { constants.AWS_CONFIG_PROPERTY: self._get_aws_config(), 'use_external_resource': False, 'resource_id': '' } }), 'instance': MockContext({ 'runtime_properties': { 'aws_resource_id': 'i-abc1234', 'public_ip_address': '127.0.0.1' } }) }) elasticip_context = MockContext({ 'node': MockContext({ 'properties': { constants.AWS_CONFIG_PROPERTY: self._get_aws_config(), 'use_external_resource': False, 'resource_id': '', } }), 'instance': MockContext({'runtime_properties': { 'aws_resource_id': '' }}) }) relationship_context = MockCloudifyContext(node_id=testname, source=instance_context, target=elasticip_context) return relationship_context
def mock_relationship_context(self, testname): server_context = MockContext({ 'node': MockContext({ 'properties': { constants.AZURE_CONFIG_PROPERTY: self._get_azure_config() } }), 'instance': MockContext({ 'runtime_properties': { 'azure_resource_id': 'i-abc1234', 'public_ip_address': '127.0.0.1' } }) }) publicip_context = MockContext({ 'node': MockContext({ 'properties': { constants.AZURE_CONFIG_PROPERTY: self._get_azure_config(), 'use_external_resource': False, 'existing_pip_name': '', } }), 'instance': MockContext({'runtime_properties': { PUBLIC_IP_KEY: 'public_ip_' }}) }) relationship_context = MockCloudifyContext(node_id=testname, source=instance_context, target=publicip_context) return relationship_context
def test_set_directory_config(self, _): def get_terraform_conf_props(module_root=test_dir2): return { "terraform_config": { "executable_path": path.join(module_root, "terraform"), "storage_path": module_root, "plugins_dir": path.join(module_root, '.terraform', "plugins"), }, "resource_config": { "use_existing_resource": False, "installation_source": "https://releases.hashicorp.com/terraform/0.11.7/" "terraform_0.11.7_linux_amd64.zip", "plugins": [] } } def get_terraform_module_conf_props(module_root=test_dir2): return { "resource_config": { "source": path.join(module_root, "template"), "variables": { "a": "var1", "b": "var2" }, "environment_variables": { "EXEC_PATH": path.join(module_root, "execution"), } } } target = MockContext({ 'instance': MockNodeInstanceContext( id='terra_install-1', runtime_properties=get_terraform_conf_props().get( "terraform_config")), 'node': MockNodeContext(id='1', properties=get_terraform_conf_props()), '_context': { 'node_id': '1' } }) source_work_dir = mkdtemp() source = MockContext({ 'instance': MockNodeInstanceContext(id='terra_module-1', runtime_properties={}), 'node': MockNodeContext( id='2', properties=get_terraform_module_conf_props(source_work_dir)), '_context': { 'node_id': '2' } }) ctx = MockCloudifyContextRels(source=source, target=target) current_ctx.set(ctx=ctx) kwargs = {'ctx': ctx} set_directory_config(**kwargs) self.assertEqual( ctx.source.instance.runtime_properties.get("executable_path"), ctx.target.instance.runtime_properties.get("executable_path"))
def test_remove_external_interface_from_router(self, mock_connection): target = MockContext({ 'instance': MockNodeInstanceContext(id='router-1', runtime_properties={ RESOURCE_ID: 'a95b5509-c122-4c2f-823e-884bb559afe2', OPENSTACK_TYPE_PROPERTY: OPENSTACK_TYPE_PROPERTY, OPENSTACK_NAME_PROPERTY: 'node-router', }), 'node': MockNodeContext(id='1', properties={ 'client_config': self.client_config, 'resource_config': self.resource_config, 'use_external_resource': True, }), '_context': { 'node_id': '1' } }) source = MockContext({ 'instance': MockNodeInstanceContext(id='subnet-1', runtime_properties={ RESOURCE_ID: 'a95b5509-c122-4c2f-823e-884bb559afe8', OPENSTACK_TYPE_PROPERTY: SUBNET_OPENSTACK_TYPE, OPENSTACK_NAME_PROPERTY: 'node-subnet', }), 'node': MockNodeContext(id='1', properties={ 'client_config': self.client_config, 'resource_config': self.resource_config, 'use_external_resource': True, }), '_context': { 'node_id': '1' } }) self._pepare_relationship_context_for_operation( deployment_id='RouterTest', source=source, target=target, ctx_operation_name='cloudify.interfaces.' 'relationship_lifecycle.unlink', node_id='1') router_instance = openstack.network.v2.router.Router( **{ 'id': 'a95b5509-c122-4c2f-823e-884bb559afe8', 'name': 'test_router', 'description': 'test_description', 'availability_zone_hints': ['1'], 'availability_zones': ['2'], 'created_at': 'timestamp1', 'distributed': False, 'external_gateway_info': { 'network_id': 'a95b5509-c122-4c2f-823e-884bb559afe4' }, 'flavor_id': '5', 'ha': False, 'revision': 7, 'routes': [], 'status': '9', 'tenant_id': '10', 'updated_at': 'timestamp2', }) # Mock get router response mock_connection().network.get_router = \ mock.MagicMock(return_value=router_instance) # Call remove interface from router router.remove_interface_from_router( **{'subnet_id': 'a95b5509-c122-4c2f-823e-884bb559afe8'})
def test_add_external_interface_to_router(self, mock_connection): target = MockContext({ 'instance': MockNodeInstanceContext(id='router-1', runtime_properties={ RESOURCE_ID: 'a95b5509-c122-4c2f-823e-884bb559afe2', OPENSTACK_TYPE_PROPERTY: OPENSTACK_TYPE_PROPERTY, OPENSTACK_NAME_PROPERTY: 'node-router', }), 'node': MockNodeContext(id='1', properties={ 'client_config': self.client_config, 'resource_config': self.resource_config, 'use_external_resource': True, }), '_context': { 'node_id': '1' } }) source = MockContext({ 'instance': MockNodeInstanceContext(id='subnet-1', runtime_properties={ RESOURCE_ID: 'a95b5509-c122-4c2f-823e-884bb559afe8', OPENSTACK_TYPE_PROPERTY: SUBNET_OPENSTACK_TYPE, OPENSTACK_NAME_PROPERTY: 'node-subnet', }), 'node': MockNodeContext(id='1', properties={ 'client_config': self.client_config, 'resource_config': self.resource_config, 'use_external_resource': True, }), '_context': { 'node_id': '1' } }) self._pepare_relationship_context_for_operation( deployment_id='RouterTest', source=source, target=target, ctx_operation_name='cloudify.interfaces.' 'relationship_lifecycle.postconfigure', node_id='1') router_instance = openstack.network.v2.router.Router( **{ 'id': 'a95b5509-c122-4c2f-823e-884bb559afe8', 'name': 'test_router', 'description': 'test_description', 'availability_zone_hints': ['1'], 'availability_zones': ['2'], 'created_at': 'timestamp1', 'distributed': False, 'external_gateway_info': { 'network_id': 'a95b5509-c122-4c2f-823e-884bb559afe4' }, 'flavor_id': '5', 'ha': False, 'revision': 7, 'routes': [], 'status': '9', 'tenant_id': '10', 'updated_at': 'timestamp2', }) ports = [ openstack.network.v2.port.Port( **{ 'id': 'a95b5509-c122-4c2f-823e-884bb559afe1', 'name': 'test_port_1', 'admin_state_up': True, 'binding_host_id': '3', 'binding_profile': { '4': 4 }, 'binding_vif_details': { '5': 5 }, 'binding_vif_type': '6', 'binding_vnic_type': '7', 'created_at': '2016-03-09T12:14:57.233772', 'data_plane_status': '32', 'description': 'port_description_2', 'device_id': '9', 'device_owner': '10', 'dns_assignment': [{ '11': 11 }], 'dns_domain': 'a11', 'dns_name': '12', 'extra_dhcp_opts': [{ '13': 13 }], 'fixed_ips': [{ 'subnet_id': 'a95b5509-c122-4c2f-823e-884bb559afe8' }, { 'subnet_id': 'a95b5509-c122-4c2f-823e-884bb559afa7' }], 'allowed_address_pairs': [{ 'ip_address': '10.0.0.3' }, { 'ip_address': '10.0.0.4' }], 'mac_address': '00-14-22-01-23-45', 'network_id': '18', 'port_security_enabled': True, 'qos_policy_id': '21', 'revision_number': 22, 'security_groups': ['23'], 'status': '25', 'tenant_id': '26', 'updated_at': '2016-07-09T12:14:57.233772', }), openstack.network.v2.port.Port( **{ 'id': 'a95b5509-c122-4c2f-823e-884bb559afe2', 'name': 'test_port_1', 'admin_state_up': True, 'binding_host_id': '3', 'binding_profile': { '4': 4 }, 'binding_vif_details': { '5': 5 }, 'binding_vif_type': '6', 'binding_vnic_type': '7', 'created_at': '2016-03-09T12:14:57.233772', 'data_plane_status': '32', 'description': 'port_description_2', 'device_id': '9', 'device_owner': '10', 'dns_assignment': [{ '11': 11 }], 'dns_domain': 'a11', 'dns_name': '12', 'extra_dhcp_opts': [{ '13': 13 }], 'fixed_ips': [{ 'subnet_id': 'a95b5509-c122-4c2f-823e-884bb559afe5' }, { 'subnet_id': 'a95b5509-c122-4c2f-823e-884bb559afa4' }], 'allowed_address_pairs': [{ 'ip_address': '10.0.0.3' }, { 'ip_address': '10.0.0.4' }], 'mac_address': '00-41-23-23-23-24', 'network_id': '18', 'port_security_enabled': True, 'qos_policy_id': '21', 'revision_number': 22, 'security_groups': ['23'], 'status': '25', 'tenant_id': '26', 'updated_at': '2016-07-09T12:14:57.233772', }), ] # Mock list port response mock_connection().network.ports = mock.MagicMock(return_value=ports) # Mock get router response mock_connection().network.get_router = \ mock.MagicMock(return_value=router_instance) # Call add interface to router router.add_interface_to_router( **{'subnet_id': 'a95b5509-c122-4c2f-823e-884bb559afe8'})