def test_connect_sg_to_port(self, *_):
        mock_neutron = MockNeutronClient(update=True)
        ctx = MockCloudifyContext(
            source=MockRelationshipSubjectContext(node=mock.MagicMock(),
                                                  instance=mock.MagicMock()),
            target=MockRelationshipSubjectContext(node=mock.MagicMock(),
                                                  instance=mock.MagicMock()))

        with mock.patch('neutron_plugin.port.ctx', ctx):
            neutron_plugin.port.connect_security_group(mock_neutron)
            self.assertIsNone(ctx.operation._operation_retry)
    def test_connect_sg_to_port_race_condition(self, *_):
        mock_neutron = MockNeutronClient(update=False)

        ctx = MockCloudifyContext(
            source=MockRelationshipSubjectContext(node=mock.MagicMock(),
                                                  instance=mock.MagicMock()),
            target=MockRelationshipSubjectContext(
                node=mock.MagicMock(),
                instance=MockNodeInstanceContext(
                    runtime_properties={OPENSTACK_ID_PROPERTY: 'test-sg-id'})))
        with mock.patch('neutron_plugin.port.ctx', ctx):
            neutron_plugin.port.connect_security_group(mock_neutron, ctx=ctx)
            self.assertIsInstance(ctx.operation._operation_retry,
                                  OperationRetry)
Example #3
0
    def test_resolve_negative(self):
        # given
        argument1_name = 'var1'
        argument2_name = 'var2'

        relationship_target_node_mock = Mock()
        relationship_target_instance_mock = Mock()
        relationship_target_instance_mock.id = '6'

        relationship_source_node_mock = Mock()
        relationship_source_instance_mock = Mock()
        relationship_ctx = MockRelationshipContext(
            target=MockRelationshipSubjectContext(
                node=relationship_target_node_mock,
                instance=relationship_target_instance_mock))

        relationship_source_instance_mock.relationships = [relationship_ctx]

        rule1 = Mock()
        rule1.argument_name = argument1_name
        rule1_evaluate_value = 'value1'
        rule1_evaluate_mock = Mock(
            side_effect=lambda ctx, node, instance:
            (True, rule1_evaluate_value)
            if (node == relationship_source_node_mock and instance ==
                relationship_source_instance_mock) else (False, None))
        rule1.evaluate = rule1_evaluate_mock

        rule2 = Mock()
        rule2.argument_name = argument2_name
        rule2_evaluate_mock = Mock(return_value=(False, None))
        rule2.evaluate = rule2_evaluate_mock

        rules = [rule1, rule2]

        resolver = RelationshipInputArgumentResolver(rules)

        ctx_mock = Mock()
        ctx_mock.target = MockRelationshipSubjectContext(
            node=relationship_target_node_mock,
            instance=relationship_target_instance_mock)
        ctx_mock.source = MockRelationshipSubjectContext(
            node=relationship_source_node_mock,
            instance=relationship_source_instance_mock)

        # then
        with self.assertRaises(InputArgumentResolvingError):
            # when
            resolver.resolve(ctx_mock)
Example #4
0
    def test_resolve_negative(self):
        # given
        argument1_name = 'var1'
        rule1 = Mock()
        rule1.argument_name = argument1_name
        rule1_evaluate_value = None
        rule1_evaluate_mock = Mock(return_value=(False, rule1_evaluate_value))
        rule1.evaluate = rule1_evaluate_mock

        rules = [rule1]

        resolver = InstanceInputArgumentResolver(rules)
        relationship_target_node_mock = Mock()
        relationship_target_instance_mock = Mock()
        relationship_mock = MockRelationshipContext(
            target=MockRelationshipSubjectContext(
                node=relationship_target_node_mock,
                instance=relationship_target_instance_mock),
            type='cloudify.relationships.connected_to')
        ctx_mock = Mock()
        ctx_instance_mock = Mock()
        ctx_instance_mock.relationships = [relationship_mock]
        ctx_mock.instance = ctx_instance_mock

        # then
        with self.assertRaises(InputArgumentResolvingError):
            # when
            resolver.resolve(ctx_mock)
Example #5
0
    def get_mock_relationship_ctx_for_node(self, rel_specs):
        """
        This method will generate list of mock relationship associated with
        certain node
        :param rel_specs: Relationships is an ordered collection of
        relationship specs - dicts with the keys "node" and "instance" used
        to construct the MockNodeContext and the MockNodeInstanceContext,
        and optionally a "type" key.
        Examples: [
            {},
            {"node": {"id": 5}},
            {
                "type": "some_type",
                "instance": {
                    "id": 3,
                    "runtime_properties":{}
                }
            }
        ]
        :return list: Return list of "MockRelationshipContext" instances
        """

        relationships = []
        for rel_spec in rel_specs:
            node = rel_spec.get('node', {})
            node_id = node.pop('id', uuid.uuid4().hex)

            instance = rel_spec.get('instance', {})
            instance_id = instance.pop(
                'id', '{0}_{1}'.format(node_id,
                                       uuid.uuid4().hex))
            if 'properties' not in node:
                node['properties'] = {}

            mock_data = {
                'id': node_id,
                'properties': node['properties'],
            }

            if rel_spec.get('type_hierarchy'):
                mock_data['type_hierarchy'] = rel_spec['type_hierarchy']

            node_ctx = CustomMockNodeContext(**mock_data)
            instance_ctx = MockNodeInstanceContext(id=instance_id, **instance)

            rel_subject_ctx = MockRelationshipSubjectContext(
                node=node_ctx, instance=instance_ctx)
            rel_type = rel_spec.get('type')
            rel_ctx = MockRelationshipContext(target=rel_subject_ctx,
                                              type=rel_type)
            relationships.append(rel_ctx)

        return relationships
 def _get_ctx_mock(self, instance_id, boot):
     rel_specs = [MockRelationshipContext(
         target=MockRelationshipSubjectContext(node=MockNodeContext(
             properties={'boot': boot}), instance=MockNodeInstanceContext(
             runtime_properties={
                 OPENSTACK_TYPE_PROPERTY: VOLUME_OPENSTACK_TYPE,
                 OPENSTACK_ID_PROPERTY: instance_id,
                 VOLUME_BOOTABLE: False
             })))]
     ctx = mock.MagicMock()
     ctx.instance = MockNodeInstanceContext(relationships=rel_specs)
     ctx.logger = setup_logger('mock-logger')
     return ctx
Example #7
0
    def test_resolve_positive(self):
        # given
        argument1_name = 'var1'
        argument2_name = 'var2'

        rule1 = Mock()
        rule1.argument_name = argument1_name
        rule1_evaluate_value = 'value1'
        rule1_evaluate_mock = Mock(return_value=(True, rule1_evaluate_value))
        rule1.evaluate = rule1_evaluate_mock

        rule2 = Mock()
        rule2.argument_name = argument2_name
        rule2_evaluate_value = 'value2'
        rule2_evaluate_mock = Mock(return_value=(True, rule2_evaluate_value))
        rule2.evaluate = rule2_evaluate_mock

        rules = [rule1, rule2]

        resolver = InstanceInputArgumentResolver(rules)
        relationship_target_node_mock = Mock()
        relationship_target_instance_mock = Mock()
        relationship_mock = MockRelationshipContext(
            target=MockRelationshipSubjectContext(
                node=relationship_target_node_mock,
                instance=relationship_target_instance_mock),
            type='cloudify.relationships.connected_to')
        ctx_mock = Mock()
        ctx_instance_mock = Mock()
        ctx_instance_mock.relationships = [relationship_mock]
        ctx_mock.instance = ctx_instance_mock

        # when
        result = resolver.resolve(ctx_mock)

        # then
        self.assertEquals(
            result, {
                argument1_name: rule1_evaluate_value,
                argument2_name: rule2_evaluate_value
            })

        rule1_evaluate_mock.assert_called_once_with(
            relationship_mock, relationship_target_node_mock,
            relationship_target_instance_mock)

        rule2_evaluate_mock.assert_called_once_with(
            relationship_mock, relationship_target_node_mock,
            relationship_target_instance_mock)
def test_stop():
    from cloudify_healer.stopper import stop

    # start process then stop it
    p = subprocess.Popen(["exec sleep 120"], shell=True)
    ctx = MockCloudifyContext(source=MockRelationshipSubjectContext(
        None, MockNodeInstanceContext(runtime_properties={"pid": str(p.pid)})))

    current_ctx.set(ctx)
    stop()
    sleep(2)
    if p.poll() is None:
        p.terminate()
        p.wait()
        assert False
    def _make_vm_ctx_with_relationships(self, rel_specs, properties=None):
        """Prepare a mock CloudifyContext from the given relationship spec.

        rel_specs is an ordered collection of relationship specs - dicts
        with the keys "node" and "instance" used to construct the
        MockNodeContext and the MockNodeInstanceContext, and optionally a
        "type" key.
        Examples: [
            {},
            {"node": {"id": 5}},
            {
                "type": "some_type",
                "instance": {
                    "id": 3,
                    "runtime_properties":{}
                }
            }
        ]
        """
        if properties is None:
            properties = {}
        relationships = []
        for rel_spec in rel_specs:
            node = rel_spec.get('node', {})
            node_id = node.pop('id', uuid.uuid4().hex)

            instance = rel_spec.get('instance', {})
            instance_id = instance.pop(
                'id', '{0}_{1}'.format(node_id,
                                       uuid.uuid4().hex))
            if 'properties' not in node:
                node['properties'] = {}
            node_ctx = MockNodeContext(id=node_id, **node)
            instance_ctx = MockNodeInstanceContext(id=instance_id, **instance)

            rel_subject_ctx = MockRelationshipSubjectContext(
                node=node_ctx, instance=instance_ctx)
            rel_type = rel_spec.get('type')
            rel_ctx = MockRelationshipContext(target=rel_subject_ctx,
                                              type=rel_type)
            relationships.append(rel_ctx)
        return MockCloudifyContext(node_id='vm',
                                   properties=properties,
                                   relationships=relationships)
    def mock_relationship(self,
                          type=RBAC_POLICY_APPLIED_FOR_RELATIONSHIP_TYPE,
                          runtime_properties=None):

        class _MockRelationshipContext(MockRelationshipContext):

            @property
            def type_hierarchy(self):
                return [self.type]

        return _MockRelationshipContext(
            MockRelationshipSubjectContext(
                node=None,
                instance=MockNodeInstanceContext(
                    runtime_properties=runtime_properties or {},
                )
            ),
            type=type
        )
    def _mock_item_to_resources_list_rel_ctx(self):
        # target
        tar_rel_subject_ctx = MockRelationshipSubjectContext(
            node=MockNodeContext(
                id='test_resources',
                type='cloudify.nodes.resources.List',
                properties={
                    'resource_config': [
                        '10.0.1.0/24',
                        '10.0.2.0/24',
                        '10.0.3.0/24'
                    ]
                }
            ),
            instance=MockNodeInstanceContext(
                id='test_resources_123456',
                runtime_properties={},
            )
        )

        rel_ctx = MockRelationshipContext(
            type='cloudify.relationships.resources.reserve_list_item',
            target=tar_rel_subject_ctx
        )

        # source
        src_ctx = MockCloudifyContext(
            node_id='test_item_123456',
            node_type='cloudify.nodes.resources.ListItem',
            source=self,
            target=tar_rel_subject_ctx,
            relationships=rel_ctx
        )

        current_ctx.set(src_ctx)
        return src_ctx
Example #12
0
    def test_resolve_positive(self):
        # given
        argument1_name = 'var1'
        argument2_name = 'var2'

        relationship_target_node_mock = Mock()
        relationship_target_instance_mock = Mock()
        relationship_target_instance_mock.id = '5'

        relationship_source_node_mock = Mock()
        relationship_source_instance_mock = Mock()
        relationship_ctx = MockRelationshipContext(
            target=MockRelationshipSubjectContext(
                node=relationship_target_node_mock,
                instance=relationship_target_instance_mock))

        relationship_source_instance_mock.relationships = [relationship_ctx]

        rule1 = Mock()
        rule1.argument_name = argument1_name
        rule1_evaluate_value = 'value1'
        rule1_evaluate_mock = Mock(
            side_effect=lambda ctx, node, instance:
            (True, rule1_evaluate_value)
            if (node == relationship_source_node_mock and instance ==
                relationship_source_instance_mock) else (False, None))
        rule1.evaluate = rule1_evaluate_mock

        rule2 = Mock()
        rule2.argument_name = argument2_name
        rule2_evaluate_value = 'value2'
        rule2_evaluate_mock = Mock(
            side_effect=lambda ctx, node, instance:
            (True, rule2_evaluate_value)
            if (node == relationship_target_node_mock and instance ==
                relationship_target_instance_mock) else (False, None))
        rule2.evaluate = rule2_evaluate_mock

        rules = [rule1, rule2]

        resolver = RelationshipInputArgumentResolver(rules)

        ctx_mock = Mock()
        ctx_mock.target = MockRelationshipSubjectContext(
            node=relationship_target_node_mock,
            instance=relationship_target_instance_mock)
        ctx_mock.source = MockRelationshipSubjectContext(
            node=relationship_source_node_mock,
            instance=relationship_source_instance_mock)
        # when
        result = resolver.resolve(ctx_mock)

        # then
        self.assertEquals(
            result, {
                argument1_name: rule1_evaluate_value,
                argument2_name: rule2_evaluate_value
            })

        rule1_evaluate_mock_expected_calls = [
            call(relationship_ctx, relationship_target_node_mock,
                 relationship_target_instance_mock),
            call(relationship_ctx, relationship_source_node_mock,
                 relationship_source_instance_mock)
        ]

        rule1_evaluate_mock.assert_has_calls(
            rule1_evaluate_mock_expected_calls)

        rule2_evaluate_mock_expected_calls = [
            call(relationship_ctx, relationship_target_node_mock,
                 relationship_target_instance_mock)
        ]

        rule2_evaluate_mock.assert_has_calls(
            rule2_evaluate_mock_expected_calls)