Python testtools.matchers.HasLength() Examples

The following are 30 code examples of testtools.matchers.HasLength(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module testtools.matchers , or try the search function .
Example #1
Source File: test_mark_down.py    From vitrage with Apache License 2.0 6 votes vote down vote up
def test_get_do_recipe(self):

        # Test Action
        action_steps = MarkDown.get_do_recipe(self.action_spec)

        # Test Assertions

        # expecting for one step: [update_vertex]
        self.assertThat(action_steps, matchers.HasLength(1))

        self.assertEqual(UPDATE_VERTEX, action_steps[0].type)
        update_vertex_step_params = action_steps[0].params
        self.assertThat(update_vertex_step_params, matchers.HasLength(3))

        is_marked_down = update_vertex_step_params[VProps.IS_MARKED_DOWN]
        self.assertTrue(is_marked_down)

        vitrage_id = update_vertex_step_params[VProps.VITRAGE_ID]
        self.assertEqual(self.target_vertex.vertex_id, vitrage_id)

        is_real_vitrage_id = \
            update_vertex_step_params[VProps.IS_REAL_VITRAGE_ID]
        self.assertTrue(is_real_vitrage_id) 
Example #2
Source File: test_container.py    From zun with Apache License 2.0 6 votes vote down vote up
def test_list_with_filters(self):
        with mock.patch.object(self.dbapi, 'list_containers',
                               autospec=True) as mock_get_list:
            mock_get_list.return_value = [self.fake_container]
            filt = {'status': 'Running'}
            containers = objects.Container.list(self.context,
                                                filters=filt)
            self.assertEqual(1, mock_get_list.call_count)
            self.assertThat(containers, HasLength(1))
            self.assertIsInstance(containers[0], objects.Container)
            self.assertEqual(self.context, containers[0]._context)
            mock_get_list.assert_called_once_with(self.context,
                                                  consts.TYPE_CONTAINER,
                                                  filters=filt,
                                                  limit=None, marker=None,
                                                  sort_key=None, sort_dir=None) 
Example #3
Source File: test_capsule.py    From zun with Apache License 2.0 6 votes vote down vote up
def test_list_with_filters(self):
        with mock.patch.object(self.dbapi, 'list_containers',
                               autospec=True) as mock_get_list:
            mock_get_list.return_value = [self.fake_capsule]
            filt = {'status': 'Running'}
            capsules = objects.Capsule.list(self.context,
                                            filters=filt)
            self.assertEqual(1, mock_get_list.call_count)
            self.assertThat(capsules, HasLength(1))
            self.assertIsInstance(capsules[0], objects.Capsule)
            self.assertEqual(self.context, capsules[0]._context)
            mock_get_list.assert_called_once_with(self.context,
                                                  consts.TYPE_CAPSULE,
                                                  filters=filt,
                                                  limit=None, marker=None,
                                                  sort_key=None, sort_dir=None) 
Example #4
Source File: test_contexts.py    From yaql with Apache License 2.0 6 votes vote down vote up
def test_collect_functions(self):
        def f():
            pass

        def f_():
            pass

        def f__():
            pass

        context = contexts.Context()
        context2 = context.create_child_context()
        context3 = context2.create_child_context()
        context.register_function(f)
        context.register_function(f_)
        context3.register_function(f__)
        functions = context3.collect_functions('f')
        self.assertThat(functions, testtools.matchers.HasLength(2))
        self.assertThat(functions[0], testtools.matchers.HasLength(1))
        self.assertThat(functions[1], testtools.matchers.HasLength(2)) 
Example #5
Source File: test_contexts.py    From yaql with Apache License 2.0 6 votes vote down vote up
def test_delete_function(self):
        def f():
            pass

        def f_():
            pass

        context = contexts.Context()
        context.register_function(f)
        context2 = context.create_child_context()
        context2.register_function(f_)

        functions, is_exclusive = context2.get_functions('f')
        spec = functions.pop()
        self.assertIn(spec, context2)
        context2.delete_function(spec)
        self.assertNotIn(spec, context2)
        functions, is_exclusive = context.get_functions('f')
        self.assertThat(functions, matchers.HasLength(1)) 
Example #6
Source File: test_federation.py    From magnum with Apache License 2.0 6 votes vote down vote up
def test_list_with_filters(self):
        with mock.patch.object(self.dbapi, 'get_federation_list',
                               autospec=True) as mock_get_list:
            mock_get_list.return_value = [self.fake_federation]
            filters = {'name': 'federation1'}
            federations = objects.Federation.list(self.context,
                                                  filters=filters)

            mock_get_list.assert_called_once_with(self.context, sort_key=None,
                                                  sort_dir=None,
                                                  filters=filters, limit=None,
                                                  marker=None)
            self.assertEqual(1, mock_get_list.call_count)
            self.assertThat(federations, HasLength(1))
            self.assertIsInstance(federations[0], objects.Federation)
            self.assertEqual(self.context, federations[0]._context) 
Example #7
Source File: test_cluster.py    From magnum with Apache License 2.0 6 votes vote down vote up
def test_list_with_filters(self, mock_cluster_template_get):
        with mock.patch.object(self.dbapi, 'get_cluster_list',
                               autospec=True) as mock_get_list:
            mock_get_list.return_value = [self.fake_cluster]
            mock_cluster_template_get.return_value = self.fake_cluster_template
            filters = {'name': 'cluster1'}
            clusters = objects.Cluster.list(self.context, filters=filters)

            mock_get_list.assert_called_once_with(self.context, sort_key=None,
                                                  sort_dir=None,
                                                  filters=filters, limit=None,
                                                  marker=None)
            self.assertEqual(1, mock_get_list.call_count)
            self.assertThat(clusters, HasLength(1))
            self.assertIsInstance(clusters[0], objects.Cluster)
            self.assertEqual(self.context, clusters[0]._context) 
Example #8
Source File: test_common.py    From masakari with Apache License 2.0 6 votes vote down vote up
def test_items_equals_given_limit(self, href_link_mock):
        items = [
            {"uuid": "123"}
        ]
        req = mock.MagicMock()
        params = mock.PropertyMock(return_value=dict(limit=1))
        type(req).params = params

        builder = common.ViewBuilder()
        results = builder._get_collection_links(req, items,
                                                mock.sentinel.coll_key,
                                                "uuid")

        href_link_mock.assert_called_once_with(req, "123",
                                               mock.sentinel.coll_key)
        self.assertThat(results, matchers.HasLength(1)) 
Example #9
Source File: test_common.py    From masakari with Apache License 2.0 6 votes vote down vote up
def test_items_equals_default_limit(self, href_link_mock):
        items = [
            {"uuid": "123"}
        ]
        req = mock.MagicMock()
        params = mock.PropertyMock(return_value=dict())
        type(req).params = params
        self.flags(osapi_max_limit=1)

        builder = common.ViewBuilder()
        results = builder._get_collection_links(req, items,
                                                mock.sentinel.coll_key,
                                                "uuid")

        href_link_mock.assert_called_once_with(req, "123",
                                               mock.sentinel.coll_key)
        self.assertThat(results, matchers.HasLength(1)) 
Example #10
Source File: test_common.py    From masakari with Apache License 2.0 6 votes vote down vote up
def test_items_equals_default_limit_with_given(self, href_link_mock):
        items = [
            {"uuid": "123"}
        ]
        req = mock.MagicMock()
        # Given limit is greater than default max, only return default max
        params = mock.PropertyMock(return_value=dict(limit=2))
        type(req).params = params
        self.flags(osapi_max_limit=1)

        builder = common.ViewBuilder()
        results = builder._get_collection_links(req, items,
                                                mock.sentinel.coll_key,
                                                "uuid")

        href_link_mock.assert_called_once_with(req, "123",
                                               mock.sentinel.coll_key)
        self.assertThat(results, matchers.HasLength(1)) 
Example #11
Source File: test_apis.py    From vitrage with Apache License 2.0 6 votes vote down vote up
def test_get_topology_with_not_admin_project(self):
        # Setup
        graph = self._create_graph()
        apis = TopologyApis(graph, self.api_lock)
        ctx = {'tenant': 'project_2', 'is_admin': False}

        # Action
        graph_topology = apis.get_topology(
            ctx,
            graph_type='graph',
            depth=10,
            query=None,
            root=None,
            all_tenants=False)
        graph_topology = decompress_obj(graph_topology)

        # Test assertions
        self.assertThat(graph_topology['nodes'], matchers.HasLength(7))
        self._check_projects_entities(graph_topology['nodes'],
                                      'project_2',
                                      False) 
Example #12
Source File: test_apis.py    From vitrage with Apache License 2.0 6 votes vote down vote up
def test_get_topology_with_all_tenants(self):
        # Setup
        graph = self._create_graph()
        apis = TopologyApis(graph, self.api_lock)
        ctx = {'tenant': 'project_1', 'is_admin': False}

        # Action
        graph_topology = apis.get_topology(
            ctx,
            graph_type='graph',
            depth=10,
            query=None,
            root=None,
            all_tenants=True)
        graph_topology = decompress_obj(graph_topology)

        # Test assertions
        self.assertThat(graph_topology['nodes'], matchers.HasLength(12)) 
Example #13
Source File: test_apis.py    From vitrage with Apache License 2.0 6 votes vote down vote up
def test_resource_list_with_admin_project_and_query(self):
        # Setup
        graph = self._create_graph()
        apis = ResourceApis(graph, self.api_lock)
        ctx = {'tenant': 'project_2', 'is_admin': True}

        # Action
        resources = apis.get_resources(
            ctx,
            resource_type=NOVA_INSTANCE_DATASOURCE,
            all_tenants=False,
            query={'==': {'id': 'instance_3'}})
        resources = decompress_obj(resources)['resources']

        # Test assertions
        self.assertThat(resources, matchers.HasLength(1)) 
Example #14
Source File: test_templates.py    From vitrage with Apache License 2.0 6 votes vote down vote up
def _add_template_with_missing_param(self, template_filename):
        # Setup
        files_content = self._load_template_content(template_filename)
        params = {'template_name': 'template_with_params_3',
                  'alarm_name': 'My alarm', 'new_state': 'SUBOPTIMAL'}

        # Action
        added_templates = \
            self.apis.add_template(ctx=None, templates=files_content,
                                   template_type=None, params=params)
        self.added_template = added_templates[0]['uuid']

        # Test assertions
        self.assertThat(added_templates, matchers.HasLength(1))
        self.assertEqual('ERROR', added_templates[0]['status'])
        self.assert_starts_with('Failed to resolve parameter',
                                added_templates[0]['status details']) 
Example #15
Source File: test_templates.py    From vitrage with Apache License 2.0 6 votes vote down vote up
def _add_template_with_actual_params(self, template_filename):
        # Setup
        files_content = self._load_template_content(template_filename)
        params = {'template_name': 'template_with_params_4',
                  'alarm_type': 'zabbix', 'alarm_name': 'My alarm',
                  'new_state': 'SUBOPTIMAL'}

        # Action
        added_templates = \
            self.apis.add_template(ctx=None, templates=files_content,
                                   template_type=None, params=params)
        self.added_template = added_templates[0]['uuid']

        # Test assertions
        self.assertThat(added_templates, matchers.HasLength(1))
        self.assertEqual('LOADING', added_templates[0]['status']) 
Example #16
Source File: test_templates.py    From vitrage with Apache License 2.0 6 votes vote down vote up
def _add_template_with_extra_actual_param(self, template_filename):
        # Setup
        files_content = self._load_template_content(template_filename)
        params = {'template_name': 'template_with_extra_actual_param',
                  'alarm_type': 'zabbix', 'alarm_name': 'My alarm',
                  'new_state': 'SUBOPTIMAL',
                  'non_existing_param': 'some value'}

        # Action
        added_templates = \
            self.apis.add_template(ctx=None, templates=files_content,
                                   template_type=None, params=params)
        self.added_template = added_templates[0]['uuid']

        # Test assertions
        self.assertThat(added_templates, matchers.HasLength(1))
        self.assertEqual('LOADING', added_templates[0]['status']) 
Example #17
Source File: test_consistency.py    From vitrage with Apache License 2.0 6 votes vote down vote up
def _assert_vertices_status(self, category, vitrage_type,
                                num_vertices, num_marked_deleted):
        vertices = \
            self.processor.entity_graph.get_vertices({
                VProps.VITRAGE_CATEGORY: category,
                VProps.VITRAGE_TYPE: vitrage_type,
            })
        self.assertThat(vertices, matchers.HasLength(num_vertices))

        marked_deleted_vertices = \
            self.processor.entity_graph.get_vertices({
                VProps.VITRAGE_CATEGORY: category,
                VProps.VITRAGE_TYPE: vitrage_type,
                VProps.VITRAGE_IS_DELETED: True
            })
        self.assertThat(marked_deleted_vertices,
                        matchers.HasLength(num_marked_deleted)) 
Example #18
Source File: test_templates.py    From vitrage with Apache License 2.0 6 votes vote down vote up
def _add_template_with_extra_param_def(self, template_filename):
        # Setup
        files_content = self._load_template_content(template_filename)
        params = {'template_name': 'template_with_extra_param_def',
                  'alarm_type': 'zabbix', 'alarm_name': 'My alarm',
                  'new_state': 'SUBOPTIMAL'}

        # Action
        added_templates = \
            self.apis.add_template(ctx=None, templates=files_content,
                                   template_type=None, params=params)
        self.added_template = added_templates[0]['uuid']

        # Test assertions
        self.assertThat(added_templates, matchers.HasLength(1))
        self.assertEqual('LOADING', added_templates[0]['status']) 
Example #19
Source File: test_connection_pool.py    From oslo.cache with Apache License 2.0 6 votes vote down vote up
def test_cleanup_pool(self):
        self.test_get_context_manager()
        newtime = time.time() + self.unused_timeout * 2
        non_expired_connection = _memcache_pool._PoolItem(
            ttl=(newtime * 2),
            connection=mock.MagicMock())
        self.connection_pool.queue.append(non_expired_connection)
        self.assertThat(self.connection_pool.queue, matchers.HasLength(2))
        with mock.patch.object(time, 'time', return_value=newtime):
            conn = self.connection_pool.queue[0].connection
            with self.connection_pool.acquire():
                pass
            conn.assert_has_calls(
                [mock.call(self.connection_pool.destroyed_value)])
        self.assertThat(self.connection_pool.queue, matchers.HasLength(1))
        self.assertEqual(0, non_expired_connection.connection.call_count) 
Example #20
Source File: test_compute_node.py    From zun with Apache License 2.0 5 votes vote down vote up
def test_list(self):
        with mock.patch.object(self.dbapi, 'list_compute_nodes',
                               autospec=True) as mock_get_list:
            mock_get_list.return_value = [self.fake_compute_node]
            compute_nodes = objects.ComputeNode.list(self.context)
            self.assertEqual(1, mock_get_list.call_count)
            self.assertThat(compute_nodes, HasLength(1))
            self.assertIsInstance(compute_nodes[0], objects.ComputeNode)
            self.assertEqual(self.context, compute_nodes[0]._context) 
Example #21
Source File: test_consistency.py    From vitrage with Apache License 2.0 5 votes vote down vote up
def _add_entities_with_different_timestamps(self, consistency_interval,
                                                create_func,
                                                category,
                                                datasource_name,
                                                resource_type):
        # add resources to the graph:
        # - updated_resource
        # - outdated_resource with an old timestamp
        # - deleted_resource with an old timestamp and is_deleted==true

        future_timestamp = str(datetime.datetime_delta(
            2 * consistency_interval))
        past_timestamp = str(datetime.datetime_delta(
            -2 * consistency_interval + 1))

        updated_resource = create_func(
            v_id=resource_type + '1234', v_type=resource_type,
            ds_name=datasource_name, timestamp=future_timestamp)
        outdated_resource = create_func(
            v_id=resource_type + '5678', v_type=resource_type,
            ds_name=datasource_name, timestamp=past_timestamp)
        deleted_resource = create_func(
            v_id=resource_type + '9999', v_type=resource_type,
            ds_name=datasource_name, timestamp=past_timestamp, is_deleted=True)

        self.graph.add_vertex(updated_resource)
        self.graph.add_vertex(outdated_resource)
        self.graph.add_vertex(deleted_resource)

        # get the list of vertices
        resource_vertices = self.processor.entity_graph.get_vertices({
            VProps.VITRAGE_CATEGORY: category,
            VProps.VITRAGE_TYPE: resource_type
        })

        self.assertThat(resource_vertices, matchers.HasLength(3),
                        'Wrong number of vertices of type %s', resource_type) 
Example #22
Source File: test_compute_node.py    From zun with Apache License 2.0 5 votes vote down vote up
def test_list_with_filters(self):
        with mock.patch.object(self.dbapi, 'list_compute_nodes',
                               autospec=True) as mock_get_list:
            mock_get_list.return_value = [self.fake_compute_node]
            filt = {'hostname': 'test'}
            compute_nodes = objects.ComputeNode.list(
                self.context, filters=filt)
            self.assertEqual(1, mock_get_list.call_count)
            self.assertThat(compute_nodes, HasLength(1))
            self.assertIsInstance(compute_nodes[0], objects.ComputeNode)
            self.assertEqual(self.context, compute_nodes[0]._context)
            mock_get_list.assert_called_once_with(
                self.context, filters=filt, limit=None, marker=None,
                sort_key=None, sort_dir=None) 
Example #23
Source File: test_nagios.py    From vitrage with Apache License 2.0 5 votes vote down vote up
def test_nagios_validity(self):
        # Setup
        processor = self._create_processor_with_graph()
        self.assertThat(processor.entity_graph,
                        matchers.HasLength(
                            self._num_total_expected_vertices())
                        )

        spec_list = mock_driver.simple_nagios_alarm_generators(
            host_num=1,
            events_num=1)
        static_events = mock_driver.generate_random_events_list(spec_list)
        nagios_event = static_events[0]
        nagios_event['resource_name'] = \
            self._find_entity_id_by_type(processor.entity_graph,
                                         NOVA_HOST_DATASOURCE)
        nagios_event['status'] = NagiosTestStatus.CRITICAL

        # Action
        processor.process_event(nagios_event)

        # Test assertions
        self.assertThat(processor.entity_graph,
                        matchers.HasLength(
                            self._num_total_expected_vertices() + 1)
                        )

        nagios_vertices = processor.entity_graph.get_vertices(
            vertex_attr_filter={
                VProps.VITRAGE_CATEGORY: EntityCategory.ALARM,
                VProps.VITRAGE_TYPE: NAGIOS_DATASOURCE
            })
        self.assertThat(nagios_vertices, matchers.HasLength(1))

        nagios_neighbors = processor.entity_graph.neighbors(
            nagios_vertices[0].vertex_id)
        self.assertThat(nagios_neighbors, matchers.HasLength(1))

        self.assertEqual(NOVA_HOST_DATASOURCE,
                         nagios_neighbors[0][VProps.VITRAGE_TYPE]) 
Example #24
Source File: test_apis.py    From vitrage with Apache License 2.0 5 votes vote down vote up
def test_get_alarms_with_admin_project(self):
        # Setup
        graph = self._create_graph()
        apis = AlarmApis(graph, self.api_lock, self._db)
        ctx = {'tenant': 'project_1', 'is_admin': True}

        # Action
        alarms = apis.get_alarms(ctx, vitrage_id='all', all_tenants=False)
        alarms = decompress_obj(alarms)['alarms']

        # Test assertions
        self.assertThat(alarms, matchers.HasLength(3))
        self._check_projects_entities(alarms, 'project_1', True) 
Example #25
Source File: test_apis.py    From vitrage with Apache License 2.0 5 votes vote down vote up
def test_get_alarms_with_all_tenants(self):
        # Setup
        graph = self._create_graph()
        apis = AlarmApis(graph, self.api_lock, self._db)
        ctx = {'tenant': 'project_1', 'is_admin': False}

        # Action
        alarms = apis.get_alarms(ctx, vitrage_id='all', all_tenants=True)
        alarms = decompress_obj(alarms)['alarms']

        # Test assertions
        self.assertThat(alarms, matchers.HasLength(5))
        self._check_projects_entities(alarms, None, True) 
Example #26
Source File: test_apis.py    From vitrage with Apache License 2.0 5 votes vote down vote up
def test_get_rca_with_admin_project(self):
        # Setup
        graph = self._create_graph()
        apis = RcaApis(graph, self.api_lock, self._db)
        ctx = {'tenant': 'project_1', 'is_admin': True}

        # Action
        graph_rca = apis.get_rca(ctx, root='alarm_on_host', all_tenants=False)
        graph_rca = json.loads(graph_rca)

        # Test assertions
        self.assertThat(graph_rca['nodes'], matchers.HasLength(3))
        self._check_projects_entities(graph_rca['nodes'], 'project_1', True) 
Example #27
Source File: test_apis.py    From vitrage with Apache License 2.0 5 votes vote down vote up
def test_get_rca_with_not_admin_project(self):
        # Setup
        graph = self._create_graph()
        apis = RcaApis(graph, self.api_lock, self._db)
        ctx = {'tenant': 'project_2', 'is_admin': False}

        # Action
        graph_rca = apis.get_rca(ctx,
                                 root='alarm_on_instance_3',
                                 all_tenants=False)
        graph_rca = json.loads(graph_rca)

        # Test assertions
        self.assertThat(graph_rca['nodes'], matchers.HasLength(2))
        self._check_projects_entities(graph_rca['nodes'], 'project_2', True) 
Example #28
Source File: test_apis.py    From vitrage with Apache License 2.0 5 votes vote down vote up
def test_get_rca_with_not_admin_bla_project(self):
        # Setup
        graph = self._create_graph()
        apis = RcaApis(graph, self.api_lock, self._db)
        ctx = {'tenant': 'project_2', 'is_admin': False}

        # Action
        graph_rca = apis.get_rca(ctx, root='alarm_on_host', all_tenants=False)
        graph_rca = json.loads(graph_rca)

        # Test assertions
        self.assertThat(graph_rca['nodes'], matchers.HasLength(3))
        self._check_projects_entities(graph_rca['nodes'], 'project_2', True) 
Example #29
Source File: test_apis.py    From vitrage with Apache License 2.0 5 votes vote down vote up
def test_get_rca_with_all_tenants(self):
        # Setup
        graph = self._create_graph()
        apis = RcaApis(graph, self.api_lock, self._db)
        ctx = {'tenant': 'project_1', 'is_admin': False}

        # Action
        graph_rca = apis.get_rca(ctx, root='alarm_on_host', all_tenants=True)
        graph_rca = json.loads(graph_rca)

        # Test assertions
        self.assertThat(graph_rca['nodes'], matchers.HasLength(5))
        self._check_projects_entities(graph_rca['nodes'], None, True) 
Example #30
Source File: test_cinder_volume.py    From vitrage with Apache License 2.0 5 votes vote down vote up
def test_cinder_volume_validity(self):
        # Setup
        processor = self._create_processor_with_graph()
        self.assertThat(processor.entity_graph,
                        matchers.HasLength(
                            self._num_total_expected_vertices())
                        )

        spec_list = mock_driver.simple_volume_generators(
            volume_num=1,
            instance_num=1,
            snapshot_events=1)
        static_events = mock_driver.generate_random_events_list(spec_list)
        cinder_volume_event = static_events[0]
        cinder_volume_event['attachments'][0]['server_id'] = \
            self._find_entity_id_by_type(processor.entity_graph,
                                         NOVA_INSTANCE_DATASOURCE)

        # Action
        processor.process_event(cinder_volume_event)

        # Test assertions
        self.assertThat(processor.entity_graph,
                        matchers.HasLength(
                            self._num_total_expected_vertices() + 1)
                        )

        cinder_vertices = processor.entity_graph.get_vertices(
            vertex_attr_filter={
                VProps.VITRAGE_CATEGORY: EntityCategory.RESOURCE,
                VProps.VITRAGE_TYPE: CINDER_VOLUME_DATASOURCE
            })
        self.assertThat(cinder_vertices, matchers.HasLength(1))

        cinder_neighbors = processor.entity_graph.neighbors(
            cinder_vertices[0].vertex_id)
        self.assertThat(cinder_neighbors, matchers.HasLength(1))
        self.assertEqual(NOVA_INSTANCE_DATASOURCE,
                         cinder_neighbors[0][VProps.VITRAGE_TYPE])