def _prepare_for_find_rel(self): mock_relation = MagicMock() mock_relation.type_hierarchy = 'cloudify.relationships.contained_in' mock_relation.target.instance.runtime_properties = { 'aws_resource_id': 'b' } mock_relation.target.node.type_hierarchy = ['cloudify.nodes.Compute'] mock_relation.target.node.id = 'cloud-sample-node' mock_child = MagicMock() mock_child.type_hierarchy = 'cloudify.relationships.contained_in' mock_child.target.instance.runtime_properties = { 'aws_resource_id': 'a' } mock_child.target.node.type_hierarchy = ['cloudify.nodes.Root'] mock_child.target.node.id = 'aws-sample-node' mock_child.target.instance.relationships = [mock_relation] mock_instance = MockCloudifyContext( 'parent_id', deployment_id='deployment_id', properties={'a': 'b'}, runtime_properties={'c': 'd'}, relationships=[mock_child] ) current_ctx.set(mock_instance) return mock_instance, mock_child
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_create(self): """This tests that create runs""" test_properties = self.get_mock_properties() ctx = self.security_group_mock('test_create', test_properties) current_ctx.set(ctx=ctx) securitygroup.create(ctx=ctx)
def test_create(self): _ctx = self.get_mock_ctx( 'test_create', test_properties=NODE_PROPERTIES, test_runtime_properties=DEFAULT_RUNTIME_PROPERTIES, type_hierarchy=POLICY_TH ) current_ctx.set(_ctx) self.fake_client.create_policy = MagicMock(return_value={ 'Policy': { 'PolicyName': 'policy_name_id', 'Arn': 'arn_id' } }) policy.create(ctx=_ctx, resource_config=None, iface=None) self.fake_boto.assert_called_with('iam', **CLIENT_CONFIG) self.assertEqual( _ctx.instance.runtime_properties, RUNTIME_PROPERTIES_AFTER_CREATE )
def ctx(): ctx = Mock() current_ctx.set(ctx) yield ctx current_ctx.clear()
def test_create_policy_str(self): _ctx = self.get_mock_ctx( 'test_create', test_properties=NODE_PROPERTIES_POLICY_STR, test_runtime_properties=DEFAULT_RUNTIME_PROPERTIES, type_hierarchy=POLICY_TH ) current_ctx.set(_ctx) self.fake_client.create_policy = MagicMock(return_value={ 'Policy': { 'PolicyName': 'policy_name_id', 'Arn': 'arn_id' } }) policy.create(ctx=_ctx, resource_config=None, iface=None) self.fake_boto.assert_called_with('iam', **CLIENT_CONFIG) self.fake_client.create_policy.assert_called_with( Description='Grants access to EC2 network components', Path='/service-role/', PolicyDocument=POLICY_STR, PolicyName='aws_resource' ) self.assertEqual( _ctx.instance.runtime_properties, RUNTIME_PROPERTIES_AFTER_CREATE )
def _prepare_context(self, runtime_prop=None): mock_group = MagicMock() mock_group.type_hierarchy = 'cloudify.relationships.depends_on' mock_group.target.instance.runtime_properties = { 'aws_resource_id': 'group_id', 'resource_id': 'group_name_id' } mock_group.target.node.properties = {} mock_group.target.node.type_hierarchy = [ 'cloudify.nodes.Root', 'cloudify.nodes.aws.autoscaling.Group' ] _ctx = self.get_mock_ctx( 'test_create', test_properties=NODE_PROPERTIES, test_runtime_properties=runtime_prop if runtime_prop else { 'resource_config': {} }, type_hierarchy=POLICY_TH, test_relationships=[mock_group] ) current_ctx.set(_ctx) return _ctx
def test_attach_external_interface_instance_failed(self): """ Tries to attach existing interface to existing instance """ ctx = self.mock_relationship_context( 'test_attach_external_interface_instance') vpc_client = self.create_vpc_client() vpc = vpc_client.create_vpc('10.10.10.0/16') subnet = vpc_client.create_subnet(vpc.id, '10.10.10.0/16') ec2_client = self.get_client() ein = ec2_client.create_network_interface(subnet.id) reservation = ec2_client.run_instances( image_id=TEST_AMI_IMAGE_ID, instance_type=TEST_INSTANCE_TYPE) ctx.source.instance.runtime_properties['aws_resource_id'] = \ ein.id ctx.target.instance.runtime_properties['aws_resource_id'] = \ reservation.instances[0].id current_ctx.set(ctx=ctx) with mock.patch( 'cloudify_aws.base.AwsBase.execute', return_value=False): output = self.assertRaises( RecoverableError, eni.InterfaceAttachment().associate, ctx=ctx ) self.assertIn('Failed to attach eni', output.message)
def test_create_group_rules_both_src_group_id_cidr(self): """ This tests that either src_group_id or cidr_ip is error is raised when neither is given. """ ec2_client = connection.EC2ConnectionClient().client() group = ec2_client.create_security_group( 'test_create_group_rules_both_src_group_id_or_cidr', 'this is test') test_properties = self.get_mock_properties() ctx = self.security_group_mock( 'test_create_group_rules_both_src_group_id_or_cidr', test_properties) current_ctx.set(ctx=ctx) group_object = ec2_client.create_security_group( 'dummy', 'this is test') ctx.node.properties['rules'][0]['src_group_id'] = group_object ex = self.assertRaises( NonRecoverableError, securitygroup._create_group_rules, group) self.assertIn( 'You need to pass either src_group_id OR cidr_ip.', ex.message)
def test_delete_unexpected_client_error(self): _test_name = 'test_delete' _ctx = self.get_mock_ctx( _test_name, test_properties=NODE_PROPERTIES, test_runtime_properties=RUNTIME_PROPERTIES_AFTER_CREATE, type_hierarchy=OPTION_GROUP_TH ) current_ctx.set(_ctx) self.fake_client.delete_option_group = self._gen_client_error( 'test_delete', message='SomeMessage', code='InvalidFault' ) with self.assertRaises(ClientError) as error: option_group.delete( ctx=_ctx, resource_config=None, iface=None ) self.assertEqual( str(error.exception), ( 'An error occurred (InvalidFault) when calling the ' + 'client_error_test_delete operation: SomeMessage' ) )
def test_detach_from(self): _source_ctx, _target_ctx, _ctx = self._create_common_relationships( 'test_detach_from', source_type_hierarchy=OPTION_GROUP_TH, target_type_hierarchy=['cloudify.nodes.Root', 'cloudify.nodes.aws.rds.Option'] ) current_ctx.set(_ctx) self.fake_client.modify_option_group = MagicMock( return_value={ 'OptionGroup': 'abc' } ) option_group.detach_from( ctx=_ctx, resource_config=None, iface=None ) self.assertEqual(_target_ctx.instance.runtime_properties, { 'resource_id': 'prepare_attach_target', 'aws_resource_id': 'aws_target_mock_id', 'aws_resource_arn': 'aws_resource_mock_arn' }) self.fake_client.modify_option_group.assert_called_with( ApplyImmediately=True, OptionGroupName='aws_resource_mock_id', OptionsToRemove=['aws_target_mock_id'] )
def test_delete(self): _test_name = 'test_delete' _ctx = self.get_mock_ctx( _test_name, test_properties=NODE_PROPERTIES, test_runtime_properties=RUNTIME_PROPERTIES_AFTER_CREATE, type_hierarchy=OPTION_GROUP_TH ) current_ctx.set(_ctx) self.fake_client.delete_option_group = MagicMock( return_value={} ) option_group.delete( ctx=_ctx, resource_config=None, iface=None ) self.fake_boto.assert_called_with( 'rds', **CLIENT_CONFIG ) self.fake_client.delete_option_group.assert_called_with( OptionGroupName='dev-db-option-group' ) self.assertEqual( _ctx.instance.runtime_properties, { '__deleted': True, } )
def test_immortal_delete(self): _test_name = 'test_delete' _ctx = self.get_mock_ctx( _test_name, test_properties=NODE_PROPERTIES, test_runtime_properties=RUNTIME_PROPERTIES_AFTER_CREATE, type_hierarchy=OPTION_GROUP_TH ) current_ctx.set(_ctx) self.fake_client.delete_option_group = MagicMock( return_value={} ) self.fake_client.describe_option_groups = MagicMock( return_value={ 'OptionGroupsList': [{ 'OptionGroupName': 'dev-db-option-group', 'OptionGroupArn': 'OptionGroupArn' }] } ) with self.assertRaises(OperationRetry) as error: option_group.delete( ctx=_ctx, resource_config=None, iface=None ) self.assertEqual( str(error.exception), ( 'RDS Option Group ID# "dev-db-option-group" is still ' + 'in a pending state.' ) )
def test_disassociate_response_error(self, *_): """this checks that disassociate raises an error when disassociate_address fails due to response error. """ ctx = self.mock_relationship_context( 'test_disassociate_response_error') current_ctx.set(ctx=ctx) address = self.get_address() with mock.patch( 'cloudify_aws.ec2.elasticip.ElasticIPInstanceConnection' '.get_target_resource') \ as mock_get_target_resource: mock_get_target_resource.return_value = \ self.get_client().get_all_addresses(addresses=[address]) with mock.patch( 'cloudify_aws.base.AwsBase.execute') \ as mock_execute_disassociate_address: mock_execute_disassociate_address.side_effect = \ EC2ResponseError( mock.Mock(return_value={'status': 404}), 'error') ex = self.assertRaises( NonRecoverableError, elasticip.ElasticIPInstanceConnection().disassociate) self.assertIn( 'error', ex.message)
def test_configure_without_resource_id(self): _test_name = 'test_create_UnknownServiceError' _test_node_properties = { 'use_external_resource': False, "resource_config": { 'ParameterName': 'ParameterName', "kwargs": { "ParameterName": "log_timestamps", "ParameterValue": "UTC", "ApplyMethod": "immediate" } }, 'client_config': CLIENT_CONFIG } _ctx = self.get_mock_ctx( _test_name, test_properties=_test_node_properties, test_runtime_properties={}, type_hierarchy=PARAMETER_TH ) current_ctx.set(_ctx) parameter.configure(ctx=_ctx, resource_config=None, iface=None) self.assertEqual( _ctx.instance.runtime_properties, { 'aws_resource_id': 'log_timestamps', 'resource_config': { 'ParameterName': 'log_timestamps', 'ParameterValue': 'UTC', 'ApplyMethod': 'immediate' } } )
def test_create_raises_UnknownServiceError(self): _test_node_properties = { 'use_external_resource': False, 'client_config': CLIENT_CONFIG } _test_runtime_properties = { 'resource_config': {} } _ctx = self.get_mock_ctx( 'test_create_UnknownServiceError', test_properties=_test_node_properties, test_runtime_properties=_test_runtime_properties, type_hierarchy=SUBNET_GROUP_TH ) current_ctx.set(_ctx) resource_config = {'SubnetIds': ['test_subnet_id_1']} with self.assertRaises(UnknownServiceError) as error: subnet_group.create(ctx=_ctx, resource_config=resource_config, iface=None) self.assertEqual( str(error.exception), "Unknown service: 'rds'. Valid service names are: ['rds']" ) self.fake_boto.assert_called_with('rds', aws_access_key_id='xxx', aws_secret_access_key='yyy', region_name='aq-testzone-1')
def install_script(name, windows, user, manager_ip): ctx = MockCloudifyContext( node_id='node', properties={'agent_config': { 'user': user, 'windows': windows, 'install_method': 'provided', 'manager_ip': manager_ip, 'name': name }}) try: current_ctx.set(ctx) os.environ['MANAGER_FILE_SERVER_URL'] = 'http://{0}:53229'.format( manager_ip) init_script = script.init_script(cloudify_agent={}) finally: os.environ.pop('MANAGER_FILE_SERVER_URL') current_ctx.clear() result = '\n'.join(init_script.split('\n')[:-1]) if windows: return '{0}\n' \ 'DownloadAndExtractAgentPackage\n' \ 'ExportDaemonEnv\n' \ 'ConfigureAgent'.format(result) else: return '{0}\n' \ 'install_agent'.format(result)
def test_disassociate_external_elasticip(self): """ Tests that when an address that is in the user's EC2 account is provided to the disassociate function and use_external_resource is true no errors are raised """ ctx = self.mock_relationship_context( 'test_disassociate_external_elasticip') current_ctx.set(ctx=ctx) address = self.get_address() instance_id = self.get_instance_id() ctx.target.node.properties['use_external_resource'] = False ctx.target.node.properties['resource_id'] = address.public_ip ctx.source.node.properties['use_external_resource'] = False ctx.source.node.properties['resource_id'] = instance_id ctx.source.instance.runtime_properties['aws_resource_id'] = \ instance_id test_elasticipinstanceconnection = \ self.create_elasticipinstanceconnection_for_checking() output = \ test_elasticipinstanceconnection\ .disassociate_external_resource_naively() self.assertEqual(False, output)
def test_delete(self): _ctx = self.get_mock_ctx( 'test_delete', test_properties=NODE_PROPERTIES, test_runtime_properties=RUNTIME_PROPERTIES_AFTER_CREATE, type_hierarchy=QUEUE_TH ) current_ctx.set(_ctx) self.fake_client.delete_queue = self.mock_return(DELETE_RESPONSE) queue.delete(ctx=_ctx, resource_config=None, iface=None) self.fake_boto.assert_called_with('sqs', **CLIENT_CONFIG) self.fake_client.delete_queue.assert_called_with( QueueUrl='fake_QueueUrl' ) self.assertEqual( _ctx.instance.runtime_properties, { 'aws_resource_arn': 'fake_QueueArn', 'aws_resource_id': 'fake_QueueUrl', 'resource_config': {} } )
def test_detach_external_interface_instance(self): """ tries to detach cleanly """ ctx = self.mock_relationship_context( 'test_attach_external_instance') vpc_client = self.create_vpc_client() vpc = vpc_client.create_vpc('10.10.10.0/16') subnet = vpc_client.create_subnet(vpc.id, '10.10.10.0/16') ec2_client = self.get_client() ein = ec2_client.create_network_interface(subnet.id) reservation = ec2_client.run_instances( image_id=TEST_AMI_IMAGE_ID, instance_type=TEST_INSTANCE_TYPE) ctx.source.node.properties['use_external_resource'] = True ctx.source.node.properties['resource_id'] = ein.id ctx.source.instance.runtime_properties['aws_resource_id'] = \ ein.id ctx.target.node.properties['use_external_resource'] = True ctx.target.node.properties['resource_id'] = \ reservation.instances[0].id ctx.target.instance.runtime_properties['aws_resource_id'] = \ reservation.instances[0].id current_ctx.set(ctx=ctx) eni.associate(ctx=ctx) eni.disassociate(ctx=ctx) self.assertNotIn('attachment_id', ctx.source.instance.runtime_properties)
def test_create(self): _ctx = self.get_mock_ctx( 'test_create', test_properties=NODE_PROPERTIES, test_runtime_properties=RUNTIME_PROPERTIES, type_hierarchy=QUEUE_TH ) current_ctx.set(_ctx) self.fake_client.create_queue = MagicMock(return_value={ 'QueueUrl': 'fake_QueueUrl' }) queue.create(ctx=_ctx, resource_config=None, iface=None) self.fake_boto.assert_called_with('sqs', **CLIENT_CONFIG) self.fake_client.get_queue_attributes.assert_called_with( AttributeNames=['QueueArn'], QueueUrl='fake_QueueUrl' ) self.assertEqual( _ctx.instance.runtime_properties, { 'aws_resource_arn': 'None', 'aws_resource_id': 'fake_QueueUrl', 'resource_config': {} } )
def test_assoc(self): _ctx = self.get_mock_relationship_ctx( "elb", test_target=self.get_mock_ctx("elb", NODE_PROPERTIES, {EXTERNAL_RESOURCE_ID: 'ext_id'}), test_source=self.get_mock_ctx("elb", NODE_PROPERTIES, {EXTERNAL_RESOURCE_ID: 'ext_id'})) self.fake_client.register_instances_with_load_balancer = \ self.mock_return(DELETE_RESPONSE) self.fake_client.describe_load_balancers = \ self.mock_return({ 'LoadBalancerDescriptions': [{ 'Instances': [{ 'InstanceId': 'ext_id' }] }] }) _ctx._operation = Mock() _ctx.operation.retry_number = 0 current_ctx.set(_ctx) load_balancer.assoc(ctx=_ctx) self.fake_boto.assert_called_with('elb', **CLIENT_CONFIG) self.fake_client.register_instances_with_load_balancer.\ assert_called_with(Instances=[{'InstanceId': 'ext_id'}], LoadBalancerName='ext_id') self.fake_client.describe_load_balancers.assert_called_with( LoadBalancerNames=['ext_id'] )
def test_delete(self): ctx = self.mock_ctx('testdeletenic') current_ctx.set(ctx=ctx) ctx.logger.info("BEGIN delete NIC test") ctx.logger.info("create NIC") status_code = nic.create(ctx=ctx) ctx.logger.debug("status_code = " + str(status_code) ) self.assertTrue(bool((status_code == 200) or (status_code == 201))) current_ctx.set(ctx=ctx) utils.wait_status(ctx, "nic",constants.SUCCEEDED, 600) ctx.logger.info("trying to delete an non-deletable nic") ctx.node.properties[constants.DELETABLE_KEY] = False current_ctx.set(ctx=ctx) self.assertEqual(0, nic.delete(ctx=ctx)) ctx.logger.info("delete NIC") ctx.node.properties[constants.DELETABLE_KEY] = True current_ctx.set(ctx=ctx) self.assertEqual(202, nic.delete(ctx=ctx)) ctx.logger.info("check is nic is release") try: current_ctx.set(ctx=ctx) utils.wait_status(ctx, "nic","deleting", 600) except utils.WindowsAzureError: pass ctx.logger.info("END delete NIC test")
def test_delete(self): _ctx = self.get_mock_ctx( 'test_delete', test_properties=NODE_PROPERTIES, test_runtime_properties=RUNTIME_PROPERTIES_AFTER_CREATE, type_hierarchy=POLICY_TH ) current_ctx.set(_ctx) self.fake_client.delete_policy = self.mock_return(DELETE_RESPONSE) policy.delete(ctx=_ctx, resource_config=None, iface=None) self.fake_boto.assert_called_with('iam', **CLIENT_CONFIG) self.fake_client.delete_policy.assert_called_with( PolicyArn='arn_id' ) self.assertEqual( _ctx.instance.runtime_properties, { '__deleted': True } )
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('ec2.utils.get_single_connected_node_by_type') \ as mock_get_connected: mock_get_connected.return_value = None output = instance._get_private_key('') self.assertEqual(private_key_path, output)
def test_create_group_rules_ruleset(self): ctx = self.mock_cloudify_context( 'test_create_group_rules_ruleset') ctx.node.properties['rules'] = [ { 'ip_protocol': 'tcp', 'from_port': 22, 'to_port': 22, 'cidr_ip': '0.0.0.0/0' } ] current_ctx.set(ctx=ctx) client = self._get_ec2_client() group = client.create_security_group( 'test_create_group_rules', 'some description') self.addCleanup(group.delete) securitygroup._create_group_rules(group) groups_from_test = \ client.get_all_security_groups(groupnames=[group.name]) self.assertEqual(group.id, groups_from_test[0].id) self.assertEqual( str(groups_from_test[0].rules[0]), 'IPPermissions:tcp(22-22)' )
def test_associate_external_elasticip_or_instance_external(self): ctx = self.mock_relationship_context( 'test_associate_external_elasticip_or_instance_external') current_ctx.set(ctx=ctx) client = self._get_ec2_client() address_object = client.allocate_address() self.addCleanup(address_object.delete) ctx.source.node.properties['use_external_resource'] = True ctx.target.node.properties['use_external_resource'] = True ctx.source.instance.runtime_properties['public_ip_address'] = \ '127.0.0.1' output = \ elasticip._associate_external_elasticip_or_instance( address_object.public_ip) self.assertEqual(True, output) self.assertIn( 'public_ip_address', ctx.source.instance.runtime_properties) self.assertEqual( address_object.public_ip, ctx.source.instance.runtime_properties['public_ip_address'])
def test_allocate(self): """ This tests that allocate adds the runtime_properties.""" ctx = self.mock_ctx('test_allocate') current_ctx.set(ctx=ctx) elasticip.allocate(ctx=ctx) self.assertIn('aws_resource_id', ctx.instance.runtime_properties)
def test_invalid_source_resource(self): """ Tests that NonRecoverableError: Instance NotFound is raised when a source instance that is not in the user's EC2 account is provided to the detach function NEW """ ctx = self.mock_relationship_context('test_invalid_source_resource') current_ctx.set(ctx=ctx) ctx.target.instance.runtime_properties['aws_resource_id'] = '0.0.0.0' ctx.source.instance.runtime_properties['public_ip_address'] = '0.0.0.0' with mock.patch( 'cloudify_aws.base.AwsBase.execute') \ as mock_execute: mock_execute.side_effect = EC2ResponseError( mock.Mock(return_value={'status': 404}), 'InvalidInstanceID.NotFound') with mock.patch( 'boto.ec2.connection.EC2Connection.get_all_instances') \ as mock_get_all_instances: with self.assertRaisesRegexp( EC2ResponseError, 'InvalidInstanceID.NotFound'): mock_get_all_instances.side_effect = EC2ResponseError( mock.Mock(return_value={'status': 404}), 'InvalidInstanceID.NotFound') output = elasticip.ElasticIPInstanceConnection()\ .get_source_resource() self.assertIsNone(output)
def test_disassoc_raises(self): _ctx = self.get_mock_relationship_ctx( "elb", test_target=self.get_mock_ctx("elb", NODE_PROPERTIES, {EXTERNAL_RESOURCE_ID: 'ext_id', 'instances': ['ext_id']}), test_source=self.get_mock_ctx("elb", NODE_PROPERTIES, {EXTERNAL_RESOURCE_ID: 'ext_id'})) self.fake_client.deregister_instances_from_load_balancer = \ self.mock_return(DELETE_RESPONSE) self.fake_client.describe_load_balancers = \ self.mock_return({ 'LoadBalancerDescriptions': [{ 'Instances': [{ 'InstanceId': 'ext_id' }] }] }) current_ctx.set(_ctx) with self.assertRaises(OperationRetry): load_balancer.disassoc(ctx=_ctx) self.fake_boto.assert_called_with('elb', **CLIENT_CONFIG) self.fake_client.deregister_instances_from_load_balancer.\ assert_called_with(Instances=[{'InstanceId': 'ext_id'}], LoadBalancerName='ext_id') self.fake_client.describe_load_balancers.assert_called_with( LoadBalancerNames=['ext_id'] )
def test_save_key_pair_missing_property(self): """ This tests that _save_key_pair raises an error when private_key_path is not set. """ ctx = self.mock_ctx('test_save_key_pair_missing_property') current_ctx.set(ctx=ctx) test_keypair = self.create_kp_for_checking() ec2_client = connection.EC2ConnectionClient().client() del(ctx.node.properties['private_key_path']) kp = ec2_client.create_key_pair('test_create_use_external') ex = self.assertRaises( NonRecoverableError, test_keypair._save_key_pair, kp) self.assertIn( 'Unable to get key file path, private_key_path not set', ex.message)
def test_start(self): """This tests that start adds tags""" test_properties = self.get_mock_properties() ctx = self.security_group_mock( 'test_start', test_properties) current_ctx.set(ctx=ctx) ec2_client = connection.EC2ConnectionClient().client() group = ec2_client.create_security_group('test_start', 'this is test') group_id = group.id ctx.instance.runtime_properties['aws_resource_id'] = group_id securitygroup.start(ctx=ctx) group_list = ec2_client.get_all_security_groups(group_ids=group_id) group_object = group_list[0] self.assertEquals(group_object.tags.get('resource_id'), ctx.instance.id) self.assertEquals(group_object.tags.get('deployment_id'), ctx.deployment.id)
def test_key_pair_and_relationship(self, *_): """tests that key pair is not provided by both a property and via relationship """ name = 'test_key_pair_and_relationship' ctx = self.mock_ctx(name) current_ctx.set(ctx=ctx) ctx.node.properties['use_password'] = True private_key_dir = tempfile.mkdtemp() private_key_path = '{0}/{1}.pem' \ .format(private_key_dir, name) test_instance = self.create_instance_for_checking() ex = self.assertRaises(NonRecoverableError, test_instance._get_private_key, private_key_path=private_key_path) self.assertIn("server can't both have a", ex.message)
def _prepare_context_for_operation(self, test_name, test_properties={}, test_runtime_properties={}, test_relationships=None, type_hierarchy=['cloudify.nodes.Root'], test_source=None, test_target=None, ctx_operation_name=None): self._ctx = self.get_mock_ctx( test_name=test_name, test_properties=test_properties, test_runtime_properties=test_runtime_properties, test_relationships=test_relationships, type_hierarchy=type_hierarchy, test_source=test_source, test_target=test_target, ctx_operation_name=ctx_operation_name) current_ctx.set(self._ctx)
def test_start_clean(self): """ this tests that the instance start function starts an instance. """ ctx = self.mock_ctx('test_start_clean') current_ctx.set(ctx=ctx) ec2_client = connection.EC2ConnectionClient().client() reservation = ec2_client.run_instances( TEST_AMI_IMAGE_ID, instance_type=TEST_INSTANCE_TYPE) instance_id = reservation.instances[0].id ctx.instance.runtime_properties['aws_resource_id'] = instance_id ec2_client.stop_instances(instance_id) instance.Instance().start(ctx=ctx) reservations = ec2_client.get_all_reservations(instance_id) instance_object = reservations[0].instances[0] state = instance_object.update() self.assertEqual(state, 'running')
def test_configure_static_no_path_or_inline(self): """ Tests that if conf.type is static, but conf.path is empty then an error will be raised. """ ERROR = \ 'either logstash property conf.path ' \ 'or conf.inline are required when conf.type is "static".' ctx = self.get_mock_context( 'test_configure_static_no_path_or_inline') current_ctx.set(ctx=ctx) ctx.node.properties['conf']['type'] = 'static' ex = self.assertRaises( exceptions.NonRecoverableError, tasks.configure, ctx.node.properties['conf'], ctx=ctx) self.assertIn(ERROR, ex.message)
def setUp(self): super(BaseInitScriptTest, self).setUp() ctx = MockCloudifyContext(node_id='d', properties={ 'agent_config': { 'user': self.username, 'install_method': 'init_script', 'rest_host': 'localhost', 'windows': self.windows, 'basedir': self.temp_folder } }) current_ctx.set(ctx) self.addCleanup(lambda: current_ctx.clear()) self.input_cloudify_agent = { 'broker_ip': 'localhost', 'ssl_cert_path': self._rest_cert_path }
def _prepare_context(self, runtime_prop=None): mock_group = MagicMock() mock_group.type_hierarchy = 'cloudify.relationships.depends_on' mock_group.target.instance.runtime_properties = { 'aws_resource_id': 'aws_id' } mock_group.target.node.type_hierarchy = [ 'cloudify.nodes.Root', 'cloudify.nodes.aws.autoscaling.Group' ] _ctx = self.get_mock_ctx('test_create', test_properties=NODE_PROPERTIES, test_runtime_properties=runtime_prop if runtime_prop else {'resource_config': {}}, type_hierarchy=LIFECYCLE_HOOK_TH, test_relationships=[mock_group]) current_ctx.set(_ctx) return _ctx
def test_configure_template_no_path(self): """ Tests that if conf.type is template, but conf.path is empty then an error will be raised. """ ERROR = \ 'logstash property conf.path ' \ 'cannot be empty if conf.type is "template".' ctx = self.get_mock_context( 'test_configure_template_no_path') current_ctx.set(ctx=ctx) ctx.node.properties['conf']['type'] = 'template' ex = self.assertRaises( exceptions.NonRecoverableError, tasks.configure, ctx.node.properties['conf'], ctx=ctx) self.assertIn(ERROR, ex.message)
def _prepare_context(self, runtime_prop=None): mock_fs = MagicMock() mock_fs.type_hierarchy = 'cloudify.relationships.depends_on' mock_fs.target.instance.runtime_properties = { 'aws_resource_id': 'aws_net_id' } mock_fs.target.node.type_hierarchy = [ 'cloudify.nodes.Root', 'cloudify.nodes.aws.efs.FileSystem' ] _ctx = self.get_mock_ctx('test_create', test_properties=NODE_PROPERTIES, test_runtime_properties=runtime_prop if runtime_prop else {'resource_config': {}}, type_hierarchy=TAGS_TH, test_relationships=[mock_fs]) current_ctx.set(_ctx) return _ctx
def test_resource_definition_from_file_properties(self): _ctx = MockCloudifyContext( node_id="test_id", node_name="test_name", deployment_id="test_name", properties={'file': { 'resource_path': 'path2' }}, runtime_properties={ 'kubernetes': { 'metadata': { 'name': "kubernetes_id" } } }, relationships=[], operation={'retry_number': 0}) _ctx._node.type = 'cloudify.kubernetes.resources.Pod' current_ctx.set(_ctx) def _mocked_yaml_from_file(resource_path, target_path=None, template_variables=None): if resource_path == 'path2': return { 'apiVersion': 'v1', 'kind': 'ReplicaSet', 'metadata': 'aa', 'spec': 'bb' } with patch('cloudify_kubernetes.utils._yaml_from_file', _mocked_yaml_from_file): result = utils.resource_definition_from_file() self.assertTrue(isinstance(result, KubernetesResourceDefinition)) self.assertEquals(result.kind, 'ReplicaSet') self.assertEquals(result.api_version, 'v1') self.assertEquals(result.metadata, 'aa') self.assertEquals(result.spec, 'bb')
def test_start(self): _ctx = self.get_mock_ctx( 'test_start', test_properties=NODE_PROPERTIES, test_runtime_properties=RUNTIME_PROPERTIES_AFTER_CREATE, type_hierarchy=LOADBALANCER_TH, type_node=LOADBALANCER_TYPE, ) current_ctx.set(_ctx) self.fake_client.modify_load_balancer_attributes = self.mock_return( DELETE_RESPONSE) # should be used resource config from inputs load_balancer.start(ctx=_ctx, resource_config={'a': 'b'}, iface=None) self.fake_boto.assert_called_with('elb', **CLIENT_CONFIG) self.fake_client.modify_load_balancer_attributes.assert_called_with( LoadBalancerName='aws_resource', a='b') # start without resource configs _ctx = self.get_mock_ctx( 'test_start', test_properties=NODE_PROPERTIES, test_runtime_properties=RUNTIME_PROPERTIES_AFTER_CREATE, type_hierarchy=LOADBALANCER_TH, type_node=LOADBALANCER_TYPE, ) current_ctx.set(_ctx) self.fake_client.modify_load_balancer_attributes = self.mock_return( DELETE_RESPONSE) # should be used resource config from inputs load_balancer.start(ctx=_ctx, resource_config=None, iface=None) self.fake_boto.assert_called_with('elb', **CLIENT_CONFIG) self.fake_client.modify_load_balancer_attributes.assert_not_called()
def test_vm_memory_overcommit(self): ctx = mock.Mock() current_ctx.set(ctx) vm_name = 'vm_overcommit_test' task = self.start_vm_deploy( vm_name, vm_memory=4177920, # vSphere max memory size vm_template_key='linux_template', ) while task.info.state in ( vim.TaskInfo.State.queued, vim.TaskInfo.State.running, ): time.sleep(0.5) vm = self.client._get_obj_by_name( vim.VirtualMachine, vm_name, use_cache=False, ) self.addCleanup( self.client.delete_server, vm, ) candidates = self.client.find_candidate_hosts( resource_pool='Resources', vm_cpus=1, vm_memory=2048, vm_networks={}, ) for candidate in candidates: if candidate[0].name == vm.summary.runtime.host.name: # Should be less than 0 memory because we created a huge VM self.assertLess(candidate[2], 0) break else: raise ValueError("didn't find the host for the test VM. " "Something bad is going on")
def test_create(self): _ctx = self.get_mock_ctx( 'test_create', test_properties=NODE_PROPERTIES, test_runtime_properties=DEFAULT_RUNTIME_PROPERTIES, type_hierarchy=PIPELINE_TH, ctx_operation_name='cloudify.interfaces.lifecycle.create', ) current_ctx.set(_ctx) self.fake_client.create_pipeline = MagicMock( return_value={'pipeline': { 'name': PIPELINE_NAME, 'version': 1 }}) self.fake_client.get_pipeline_state = MagicMock( return_value={ 'pipelineName': PIPELINE_NAME, 'pipelineVersion': 1, 'created': TEST_DATE }) pipeline.create(ctx=_ctx, iface=None, params=None) self.fake_boto.assert_called_with('codepipeline', **CLIENT_CONFIG) self.fake_client.create_pipeline.assert_called_with(pipeline={ "name": PIPELINE_NAME, "version": 1 }) updated_runtime_prop = copy.deepcopy(RUNTIME_PROPERTIES_AFTER_CREATE) updated_runtime_prop['create_response'] = { 'pipelineName': PIPELINE_NAME, 'pipelineVersion': 1, 'created': str(TEST_DATE) } self.assertEqual(_ctx.instance.runtime_properties, updated_runtime_prop)
def test_base_operation_handler_functions(self): """ Tests that the base operation helpers create_helper, start_helper, stop_helper, delete_helper calls cloudify_resource_state_change_handler function and run as expected. """ ctx = self.get_mock_ctx('test_base_operation_handler_functions') current_ctx.set(ctx=ctx) resource = AwsBaseNode('root', [], resource_states=[]) with mock.patch('cloudify_aws.base.AwsBaseNode' '.get_and_filter_resources_by_matcher') \ as mock_get_and_filter_resources_by_matcher: mock_get_and_filter_resources_by_matcher.return_value = [] for operation in ('create', 'start', 'stop', 'delete'): ctx.operation._operation_context['name'] = operation with mock.patch('cloudify_aws.base.AwsBaseNode.{0}' .format(operation)) as mock_operation: function = getattr(resource, '{0}_helper' .format(operation)) output = function() if operation in ('create_helper', 'start_helper', 'modify_helper', 'stop_helper'): self.assertIsNone(output) elif operation == 'delete_helper': self.assertEqual(output, True) mock_operation.return_value = False function = getattr(resource, '{0}_helper' .format(operation)) with self.assertRaisesRegexp( NonRecoverableError, 'Neither external resource, nor Cloudify ' 'resource'): function() with mock.patch('cloudify_aws.base.AwsBaseNode.modify_attributes', return_value=True): ctx.operation._operation_context['name'] = 'modify' function = getattr(resource, 'modify_helper') self.assertEqual(True, function({'key': 'value'}))
def test_prepare_assoc(self): _source_ctx, _target_ctx, _ctx = self._create_subnet_relationships( 'test_prepare_assoc' ) current_ctx.set(_ctx) subnet_group.prepare_assoc( ctx=_ctx, resource_config=None, iface=None ) self.assertEqual(_source_ctx.instance.runtime_properties, { 'resource_config': { 'SubnetIds': [ 'subnet-xxxxxxxx', 'subnet-yyyyyyyy', 'aws_resource_mock_id' ], 'DBSubnetGroupDescription': 'MySQL5.7 Subnet Group', 'DBSubnetGroupName': 'zzzzzz-subnet-group' } })
def test_discover_and_deploy(self, _, __, ___, get_rest_client): mock_rest_client = self.get_mock_rest_client() get_rest_client.return_value = mock_rest_client ctx = self.get_mock_ctx('foo', reltype=NODE_INSTANCE) ctx.instance.runtime_properties['subclouds'] = { 'subcloud1': { 'external_id': 'taco', 'name': 'bell', 'oam_floating_ip': '10.10.10.10' } } current_ctx.set(ctx) node = get_rest_client().node_instances.list()[0] with patch('cloudify_starlingx.utils.wtx', side_effect=ctx): with patch( 'cloudify_starlingx.workflows.discover' '.get_controller_node_instance', return_value=node): discover.discover_and_deploy(ctx=ctx) assert mock_rest_client.deployment_groups.put.called
def test_do_resource_status_check_service_fail(self): # raise exception on empty balancer _, _ctx = self._prepare_master_node() current_ctx.set(_ctx) with self.assertRaises(OperationRetry) as error: tasks._do_resource_status_check( "Service", { 'spec': { 'type': 'Ingress' }, 'status': { 'load_balancer': { 'ingress': None } } }) self.assertEqual( str(error.exception), "status {'load_balancer': {'ingress': None}} in phase " "[{'load_balancer': {'ingress': None}}]")
def test_delete(self): _ctx = self.get_mock_ctx( 'test_delete', test_properties=NODE_PROPERTIES, test_runtime_properties=RUNTIME_PROPERTIES_AFTER_CREATE, type_hierarchy=PIPELINE_TH) current_ctx.set(_ctx) self.fake_client.delete_pipeline = self.mock_return(DELETE_RESPONSE) pipeline.delete(ctx=_ctx, resource_config=None, iface=None) self.fake_boto.assert_called_with('codepipeline', **CLIENT_CONFIG) self.fake_client.delete_pipeline.assert_called_with(name=PIPELINE_NAME) self.assertEqual(_ctx.instance.runtime_properties, { '__deleted': True, })
def test_create_deployment_exists(self): # Tests that create deployment exists test_name = 'test_create_deployment_exists' _ctx = self.get_mock_ctx(test_name) current_ctx.set(_ctx) with mock.patch('cloudify.manager.get_rest_client') as mock_client: cfy_mock_client = MockCloudifyRestClient() list_response = cfy_mock_client.deployments.list() list_response[0]['id'] = test_name def mock_return(*args, **kwargs): del args, kwargs return list_response cfy_mock_client.deployments.list = mock_return mock_client.return_value = cfy_mock_client output = create_deployment(operation='create_deployment', timeout=.01) self.assertFalse(output)
def test_detach_external_volume(self): """ Tests that when an address that is in the user's EC2 account is provided to the disassociate function and use_external_resource is true no errors are raised """ ctx = self.mock_relationship_context('test_detach_external_volume') current_ctx.set(ctx=ctx) volume = self.get_volume() instance_id = self.get_instance_id() ctx.target.node.properties['use_external_resource'] = False ctx.target.node.properties['resource_id'] = volume.id ctx.source.node.properties['use_external_resource'] = False ctx.source.node.properties['resource_id'] = instance_id ctx.source.instance.runtime_properties['aws_resource_id'] = \ instance_id output = \ ebs._detach_external_volume_or_instance() self.assertEqual(False, output)
def _prepare_create_raises_UnknownServiceError(self, type_hierarchy, type_name, type_class): _ctx = self.get_mock_ctx( 'test_create', test_properties=DEFAULT_NODE_PROPERTIES, test_runtime_properties=DEFAULT_RUNTIME_PROPERTIES, type_hierarchy=type_hierarchy) current_ctx.set(_ctx) fake_boto, fake_client = self.fake_boto_client(type_name) with patch('boto3.client', fake_boto): with self.assertRaises(UnknownServiceError) as error: type_class.create(ctx=_ctx, resource_config=None, iface=None) self.assertEqual(str(error.exception), ("Unknown service: '" + type_name + "'. Valid service names are: ['rds']")) fake_boto.assert_called_with(type_name, **CLIENT_CONFIG)
def test_install(self, _): conf = self.get_terraform_conf_props(test_dir1) ctx = self.mock_ctx("test_install", conf) current_ctx.set(ctx=ctx) kwargs = { 'ctx': ctx } install(**kwargs) self.assertEqual( ctx.instance.runtime_properties.get("executable_path"), conf.get("terraform_config").get("executable_path")) self.assertEqual( ctx.instance.runtime_properties.get("storage_path"), conf.get("terraform_config").get("storage_path")) self.assertEqual( ctx.instance.runtime_properties.get("plugins_dir"), conf.get("terraform_config").get("plugins_dir")) self.assertTrue( path.isfile(ctx.instance.runtime_properties.get( "executable_path")))
def test_configure_operation(self): testname = __name__ ctx = self.get_mock_context(testname) current_ctx.set(ctx=ctx) ctx.instance.runtime_properties['key'] = '~/.ssh/agent_key.pem' ctx.instance.runtime_properties['user'] = os.getlogin() tasks.configure(ctx=ctx) self.assertEqual(os.environ.get('ANSIBLE_CONFIG'), os.path.expanduser('~/.ansible.cfg')) self.assertEqual(os.environ.get('USER'), os.getlogin()) self.assertEqual(os.environ.get('HOME'), os.path.expanduser('~')) configuration = '[defaults]\n' \ 'host_key_checking=False\n' \ 'private_key_file={0}\n'.format( os.path.expanduser( ctx.instance.runtime_properties['key'])) with open(os.environ.get('ANSIBLE_CONFIG'), 'r') as f: content = f.read() self.assertEqual(content, configuration)
def test_delete(self): _ctx = self.get_mock_ctx( 'test_delete', test_properties=NODE_PROPERTIES, test_runtime_properties=RUNTIME_PROPERTIES_AFTER_CREATE, type_hierarchy=BUCKET_TH, type_node=BUCKET_TYPE, ) current_ctx.set(_ctx) self.fake_client.delete_bucket = self.mock_return(DELETE_RESPONSE) bucket.delete(ctx=_ctx, resource_config=None, iface=None) self.fake_boto.assert_called_with('s3', **CLIENT_CONFIG) self.fake_client.delete_bucket.assert_called_with( Bucket='bucket' )
def test_create_deployment_rest_client_error(self): # Tests that deployments create fails on rest client error test_name = 'test_create_deployment_rest_client_error' _ctx = self.get_mock_ctx(test_name) current_ctx.set(_ctx) _ctx.instance.runtime_properties['deployment'] = {} _ctx.instance.runtime_properties['deployment']['id'] = test_name _ctx.instance.runtime_properties['deployment']['outputs'] = {} with mock.patch('cloudify.manager.get_rest_client') as mock_client: cfy_mock_client = MockCloudifyRestClient() cfy_mock_client.deployments.create = REST_CLIENT_EXCEPTION mock_client.return_value = cfy_mock_client error = self.assertRaises(NonRecoverableError, create_deployment, deployment_id='test_deployments_create', blueprint_id='test_deployments_create', timeout=.01) self.assertIn('action create failed', error.message)
def test_create_raises_UnknownServiceError(self): _test_name = 'test_create_UnknownServiceError' _test_runtime_properties = {'resource_config': {}} _ctx = self.get_mock_ctx( _test_name, test_properties=NODE_PROPERTIES, test_runtime_properties=_test_runtime_properties, type_hierarchy=INSTANCE_READ_REPLICA_TH) current_ctx.set(_ctx) with self.assertRaises(UnknownServiceError) as error: instance_read_replica.create(ctx=_ctx, resource_config=None, iface=None) self.assertEqual( str(error.exception), "Unknown service: 'rds'. Valid service names are: ['rds']") self.fake_boto.assert_called_with('rds', **CLIENT_CONFIG)
def test_execute_start_succeeds(self): # Tests that execute start succeeds test_name = 'test_execute_start_succeeds' _ctx = self.get_mock_ctx(test_name) current_ctx.set(_ctx) _ctx.instance.runtime_properties['deployment'] = {} with mock.patch('cloudify.manager.get_rest_client') as mock_client: mock_client.return_value = MockCloudifyRestClient() poll_with_timeout_test = \ 'cloudify_deployment_proxy.polling.poll_with_timeout' with mock.patch(poll_with_timeout_test) as poll: poll.return_value = True output = execute_start(operation='execute_workflow', deployment_id=test_name, workflow_id='install', timeout=.001) self.assertTrue(output) del _ctx, mock_client
def _gen_decorators_context(self, _test_name, runtime_prop=None, prop=None, op_name=None): _test_node_properties = prop if prop else { 'use_external_resource': False } _test_runtime_properties = runtime_prop if runtime_prop else { 'resource_config': {} } _ctx = self.get_mock_ctx( _test_name, test_properties=_test_node_properties, test_runtime_properties=_test_runtime_properties, type_hierarchy=['cloudify.nodes.Root'], ctx_operation_name=None if not op_name else op_name) current_ctx.set(_ctx) return _ctx
def test_poll_with_timeout_expected(self): test_name = 'test_poll_with_timeout_expected' _ctx = self.get_mock_ctx(test_name) current_ctx.set(_ctx) mock_timeout = .001 mock_interval = .001 def mock_return(*args, **kwargs): del args, kwargs return True output = \ poll_with_timeout( mock_return, mock_timeout, mock_interval, {}, True) self.assertTrue(output)