Esempio n. 1
0
    def test_parse_only_alive_interfaces(self):
        container = SystemManager()
        container._discover_objects()

        os_obj = container.objects.find_all(types=container.types)[0]
        collector = SystemMetaCollector(object=os_obj)
        collector.collect()

        # get interfaces info
        all_interfaces = netifaces.interfaces()
        alive_interfaces = set()
        down_interfaces = set()
        for interface_name, interface in psutil.net_if_stats().iteritems():
            if interface.isup:
                alive_interfaces.add(interface_name)
            else:
                down_interfaces.add(interface_name)

        # check interfaces
        collected_interfaces = os_obj.metad.current['network']['interfaces']
        for interface_info in collected_interfaces:
            assert_that(interface_info, has_key('name'))
            assert_that(interface_info, has_key('mac'))

            assert_that(interface_info, has_key('ipv4'))
            ipv4 = interface_info['ipv4']
            assert_that(ipv4, has_key('netmask'))
            assert_that(ipv4, has_key('address'))
            assert_that(ipv4, has_key('prefixlen'))

            name = interface_info['name']
            assert_that(all_interfaces, has_item(name))
            assert_that(alive_interfaces, has_item(name))
            assert_that(down_interfaces, not_(has_item(name)))
Esempio n. 2
0
    def test_parse_only_alive_interfaces(self):
        container = SystemManager()
        container._discover_objects()

        os_obj = container.objects.objects[container.objects.objects_by_type[
            container.type][0]]
        collector = SystemCommonMetaCollector(object=os_obj)
        collector.collect()

        # get interfaces info
        all_interfaces = netifaces.interfaces()
        alive_interfaces = set()
        down_interfaces = set()
        for interface_name, interface in psutil.net_if_stats().iteritems():
            if interface.isup:
                alive_interfaces.add(interface_name)
            else:
                down_interfaces.add(interface_name)

        # check intefaces
        collected_interfaces = os_obj.metad.current['network']['interfaces']
        for interface_info in collected_interfaces:
            name = interface_info['name']
            assert_that(all_interfaces, has_item(name))
            assert_that(alive_interfaces, has_item(name))
            assert_that(down_interfaces, not_(has_item(name)))
Esempio n. 3
0
    def test_parse_only_alive_interfaces(self):
        container = SystemManager()
        container._discover_objects()

        os_obj = container.objects.find_all(types=container.types)[0]
        collector = SystemMetaCollector(object=os_obj)
        collector.collect()

        # get interfaces info
        all_interfaces = netifaces.interfaces()
        alive_interfaces = set()
        down_interfaces = set()
        for interface_name, interface in psutil.net_if_stats().iteritems():
            if interface.isup:
                alive_interfaces.add(interface_name)
            else:
                down_interfaces.add(interface_name)

        # check interfaces
        collected_interfaces = os_obj.metad.current['network']['interfaces']
        for interface_info in collected_interfaces:
            assert_that(interface_info, has_key('name'))
            assert_that(interface_info, has_key('mac'))

            assert_that(interface_info, has_key('ipv4'))
            ipv4 = interface_info['ipv4']
            assert_that(ipv4, has_key('netmask'))
            assert_that(ipv4, has_key('address'))
            assert_that(ipv4, has_key('prefixlen'))

            name = interface_info['name']
            assert_that(all_interfaces, has_item(name))
            assert_that(alive_interfaces, has_item(name))
            assert_that(down_interfaces, not_(has_item(name)))
Esempio n. 4
0
    def test_collect_only_alive_interfaces(self):
        container = SystemManager()
        container._discover_objects()

        os_obj = container.objects.objects[container.objects.objects_by_type[container.type][0]]
        collector = SystemMetricsCollector(object=os_obj)
        collector.collect()
        collector.collect()  # double collect is needed, because otherwise we do not collect metrics properly

        # get interfaces info
        all_interfaces = netifaces.interfaces()
        alive_interfaces = set()
        down_interfaces = set()
        for interface_name, interface in psutil.net_if_stats().iteritems():
            if interface.isup:
                alive_interfaces.add(interface_name)
            else:
                down_interfaces.add(interface_name)

        # check
        collected_metrics = os_obj.statsd.current
        net_metrics_found = False
        for metric in collected_metrics['counter'].keys():
            if metric.startswith('system.net.') and '|' in metric:
                net_metrics_found = True
                metric_name, label_name = metric.split('|')
                assert_that(all_interfaces, has_item(label_name))
                assert_that(alive_interfaces, has_item(label_name))
                assert_that(down_interfaces, not_(has_item(label_name)))
        assert_that(net_metrics_found, equal_to(True))
Esempio n. 5
0
 def get_collector(self, collect=True):
     manager = SystemManager()
     manager._discover_objects()
     obj = manager.objects.find_all(types=manager.types)[0]
     collector = SystemMetaCollector(object=obj)
     if collect:
         collector.collect()
     return collector
Esempio n. 6
0
    def test_discover(self):
        system_manager = SystemManager()
        system_manager._discover_objects()
        assert_that(system_manager.objects.find_all(types=system_manager.types), has_length(1))

        # get the system object
        system_obj = system_manager.objects.find_all(types=system_manager.types)[0]

        assert_that(system_obj.type, equal_to('system'))
Esempio n. 7
0
 def get_collector(self, collect=True):
     manager = SystemManager()
     manager._discover_objects()
     obj = manager.objects.find_all(types=manager.types)[0]
     collector = SystemMetricsCollector(object=obj)
     if collect:
         collector.collect()
         collector.collect()  # second collect is needed to properly collect all metrics
     return collector
Esempio n. 8
0
 def setup_method(self, method):
     super(SystemMetricsCollectorTestCase, self).setup_method(method)
     manager = SystemManager()
     manager._discover_objects()
     system_obj = manager.objects.objects[manager.objects.objects_by_type[manager.type][0]]
     collector = SystemMetricsCollector(object=system_obj)
     collector.collect()
     collector.collect() # second collect is needed to properly collect all metrics
     self.metrics = system_obj.statsd.current
Esempio n. 9
0
    def test_discover(self):
        system_manager = SystemManager()
        system_manager._discover_objects()
        assert_that(system_manager.objects.find_all(types=system_manager.types), has_length(1))

        # get the system object
        system_obj = system_manager.objects.find_all(types=system_manager.types)[0]

        assert_that(system_obj.type, equal_to('container'))
        assert_that(system_obj.imagename, equal_to('DockerTest'))
        assert_that(system_obj.uuid, equal_to('container-DockerTest'))
Esempio n. 10
0
    def test_docker_discover(self):
        context.app_config['credentials']['imagename'] = 'DockerTest'
        system_manager = SystemManager()
        system_manager._discover_objects()
        assert_that(system_manager.objects.find_all(types=system_manager.types), has_length(1))

        # get the system object
        system_obj = system_manager.objects.find_all(types=system_manager.types)[0]

        assert_that(system_obj.type, equal_to('container'))
        assert_that(system_obj.imagename, equal_to('DockerTest'))
Esempio n. 11
0
    def test_collect_each_interface_once(self):
        container = SystemManager()
        container._discover_objects()

        os_obj = container.objects.find_all(types=container.types)[0]
        collector = SystemMetaCollector(object=os_obj)

        num_interfaces = len(psutil.net_if_stats())
        for x in xrange(3):
            collector.collect()
            collected_interfaces = os_obj.metad.current['network']['interfaces']
            assert_that(collected_interfaces, only_contains(contains_inanyorder('mac', 'name', 'ipv4', 'ipv6')))
            assert_that(collected_interfaces, has_length(num_interfaces))
Esempio n. 12
0
    def test_special_parse_restrictions(self):
        container = SystemManager()
        container._discover_objects()

        os_obj = container.objects.find_all(types=container.types)[0]
        collector = SystemMetaCollector(object=os_obj)
        assert_that(not_(collector.in_container))

        collector.collect()
        collected_meta = os_obj.metad.current

        assert_that(collected_meta, has_key('boot'))
        assert_that(collected_meta, has_key('hostname'))
        assert_that(collected_meta, has_key('ec2'))
Esempio n. 13
0
    def test_special_parse_restrictions(self):
        container = SystemManager()
        container._discover_objects()

        os_obj = container.objects.find_all(types=container.types)[0]
        collector = SystemMetaCollector(object=os_obj)
        assert_that(not_(collector.in_container))

        collector.collect()
        collected_meta = os_obj.metad.current

        assert_that(collected_meta, has_key('boot'))
        assert_that(collected_meta, has_key('hostname'))
        assert_that(collected_meta, has_key('ec2'))
Esempio n. 14
0
    def test_discover(self):
        system_manager = SystemManager()
        system_manager._discover_objects()
        assert_that(
            system_manager.objects.find_all(types=system_manager.types),
            has_length(1))

        # get the system object
        system_obj = system_manager.objects.find_all(
            types=system_manager.types)[0]

        assert_that(system_obj.type, equal_to('container'))
        assert_that(system_obj.imagename, equal_to('DockerTest'))
        assert_that(system_obj.uuid, equal_to('container-DockerTest'))
Esempio n. 15
0
    def test_discover(self):
        system_manager = SystemManager()
        system_manager._discover_objects()
        assert_that(
            system_manager.objects.find_all(types=system_manager.types),
            has_length(1))

        # get the system object
        system_obj = system_manager.objects.find_all(
            types=system_manager.types)[0]

        assert_that(system_obj.type, equal_to('system'))
        assert_that(system_obj.uuid, equal_to(DEFAULT_UUID))
        assert_that(system_obj.hostname, equal_to(DEFAULT_HOST))
Esempio n. 16
0
    def test_default_interface(self):
        container = SystemManager()
        container._discover_objects()

        os_obj = container.objects.find_all(types=container.types)[0]
        collector = SystemMetaCollector(object=os_obj)
        collector.collect()

        default_from_netstat, _ = subp.call(
            'netstat -nr | egrep -i "^0.0.0.0|default" | head -1 | sed "s/.*[ ]\([^ ][^ ]*\)$/\\1/"'
        )[0]

        default_interface = os_obj.metad.current['network']['default']

        assert_that(default_interface, equal_to(default_from_netstat))
Esempio n. 17
0
    def test_flush_aggregation(self):
        container = SystemManager()
        container._discover_objects()

        os_obj = container.objects.objects[container.objects.objects_by_type[container.type][0]]
        collector = SystemMetricsCollector(object=os_obj)
        collector.collect()
        collector.collect()  # double collect is needed, because otherwise we do not collect metrics properly

        flush = collector.object.statsd.flush()

        for type in flush['metrics'].keys():  # e.g. 'timer', 'counter', 'gauge', 'average'
            for key in flush['metrics'][type].keys():
                # Make sure there is only one item per item in the flush.
                assert_that(len(flush['metrics'][type][key]), equal_to(1))
Esempio n. 18
0
    def test_default_interface(self):
        container = SystemManager()
        container._discover_objects()

        os_obj = container.objects.find_all(types=container.types)[0]
        collector = SystemMetaCollector(object=os_obj)
        collector.collect()

        default_from_netstat, _ = subp.call(
            'netstat -nr | egrep -i "^0.0.0.0|default" | head -1 | sed "s/.*[ ]\([^ ][^ ]*\)$/\\1/"'
        )[0]

        default_interface = os_obj.metad.current['network']['default']

        assert_that(default_interface, equal_to(default_from_netstat))
Esempio n. 19
0
    def test_collect_each_interface_once(self):
        container = SystemManager()
        container._discover_objects()

        os_obj = container.objects.find_all(types=container.types)[0]
        collector = SystemMetaCollector(object=os_obj)

        num_interfaces = len(psutil.net_if_stats())
        for x in xrange(3):
            collector.collect()
            collected_interfaces = os_obj.metad.current['network'][
                'interfaces']
            assert_that(collected_interfaces,
                        only_contains(contains_inanyorder('mac', 'name')))
            assert_that(collected_interfaces, has_length(num_interfaces))
Esempio n. 20
0
    def setup_method(self, method):
        super(PsutilsTestCase, self).setup_method(method)

        self.system_manager = SystemManager()
        self.system_manager._discover_objects()
        self.nginx_manager = NginxManager()
        self.nginx_manager._discover_objects()

        self.system_obj = self.system_manager.objects.objects[
            self.system_manager.objects.objects_by_type[
                self.system_manager.type][0]]
        self.system_metrics_collector = self.system_obj.collectors[1]

        self.nginx_obj = self.nginx_manager.objects.objects[
            self.nginx_manager.objects.objects_by_type[
                self.nginx_manager.type][0]]
        self.nginx_metrics_collector = self.nginx_obj.collectors[2]
Esempio n. 21
0
    def setup_method(self, method):
        super(PsutilsTestCase, self).setup_method(method)

        self.system_manager = SystemManager()
        self.system_manager._discover_objects()
        self.nginx_manager = NginxManager()
        self.nginx_manager._discover_objects()

        self.system_obj = self.system_manager.objects.objects[
            self.system_manager.objects.objects_by_type[self.system_manager.type][0]
        ]
        self.system_metrics_collector = self.system_obj.collectors[1]

        self.nginx_obj = self.nginx_manager.objects.objects[
            self.nginx_manager.objects.objects_by_type[self.nginx_manager.type][0]
        ]
        self.nginx_metrics_collector = self.nginx_obj.collectors[1]
Esempio n. 22
0
class PsutilsTestCase(RealNginxTestCase):
    """
    Overall test are for testing our calls to psutils and making sure they work.
    """
    def setup_method(self, method):
        super(PsutilsTestCase, self).setup_method(method)

        self.system_manager = SystemManager()
        self.system_manager._discover_objects()
        self.nginx_manager = NginxManager()
        self.nginx_manager._discover_objects()

        self.system_obj = self.system_manager.objects.objects[
            self.system_manager.objects.objects_by_type[self.system_manager.type][0]
        ]
        self.system_metrics_collector = self.system_obj.collectors[1]

        self.nginx_obj = self.nginx_manager.objects.objects[
            self.nginx_manager.objects.objects_by_type[self.nginx_manager.type][0]
        ]
        self.nginx_metrics_collector = self.nginx_obj.collectors[1]

    def teardown_method(self, method):
        self.system_manager = None
        self.nginx_manager = None
        super(PsutilsTestCase, self).teardown_method(method)

    def test_system_virtual_memory(self):
        assert_that(calling(self.system_metrics_collector.virtual_memory), not_(raises(Exception)))

    def test_system_swap(self):
        assert_that(calling(self.system_metrics_collector.swap), not_(raises(Exception)))

    def test_system_cpu(self):
        assert_that(calling(self.system_metrics_collector.cpu), not_(raises(Exception)))

    def test_system_disk_partitions(self):
        assert_that(calling(self.system_metrics_collector.disk_partitions), not_(raises(Exception)))

    def test_system_disk_io_counters(self):
        assert_that(calling(self.system_metrics_collector.disk_io_counters), not_(raises(Exception)))

    def test_system_net_io_counters(self):
        assert_that(calling(self.system_metrics_collector.net_io_counters), not_(raises(Exception)))

    def test_nginx_memory_info(self):
        assert_that(calling(self.nginx_metrics_collector.memory_info), not_(raises(Exception)))

    def test_nginx_workers_fds_count(self):
        assert_that(calling(self.nginx_metrics_collector.workers_fds_count), not_(raises(Exception)))

    # These next two tests have to be skipped due to calls to .handle_zombie() which raises a hamcrest exception.
    @future_test
    def test_nginx_workers_rlimit_nofile(self):
        assert_that(calling(self.nginx_metrics_collector.workers_rlimit_nofile), not_(raises(Exception)))

    @future_test
    def test_nginx_workers_io(self):
        assert_that(calling(self.nginx_metrics_collector.workers_io), not_(raises(Exception)))

    def test_nginx_workers_cpu(self):
        assert_that(calling(self.nginx_metrics_collector.workers_cpu), not_(raises(Exception)))
Esempio n. 23
0
class PsutilsTestCase(RealNginxTestCase):
    """
    Overall test are for testing our calls to psutils and making sure they work.
    """
    def setup_method(self, method):
        super(PsutilsTestCase, self).setup_method(method)

        self.system_manager = SystemManager()
        self.system_manager._discover_objects()
        self.nginx_manager = NginxManager()
        self.nginx_manager._discover_objects()

        self.system_obj = self.system_manager.objects.objects[
            self.system_manager.objects.objects_by_type[
                self.system_manager.type][0]]
        self.system_metrics_collector = self.system_obj.collectors[1]

        self.nginx_obj = self.nginx_manager.objects.objects[
            self.nginx_manager.objects.objects_by_type[
                self.nginx_manager.type][0]]
        self.nginx_metrics_collector = self.nginx_obj.collectors[2]

    def teardown_method(self, method):
        self.system_manager = None
        self.nginx_manager = None
        super(PsutilsTestCase, self).teardown_method(method)

    def test_system_virtual_memory(self):
        assert_that(calling(self.system_metrics_collector.virtual_memory),
                    not_(raises(Exception)))

    def test_system_swap(self):
        assert_that(calling(self.system_metrics_collector.swap),
                    not_(raises(Exception)))

    def test_system_cpu(self):
        assert_that(calling(self.system_metrics_collector.cpu),
                    not_(raises(Exception)))

    def test_system_disk_partitions(self):
        assert_that(calling(self.system_metrics_collector.disk_partitions),
                    not_(raises(Exception)))

    def test_system_disk_io_counters(self):
        assert_that(calling(self.system_metrics_collector.disk_io_counters),
                    not_(raises(Exception)))

    def test_system_net_io_counters(self):
        assert_that(calling(self.system_metrics_collector.net_io_counters),
                    not_(raises(Exception)))

    def test_nginx_memory_info(self):
        assert_that(calling(self.nginx_metrics_collector.memory_info),
                    not_(raises(Exception)))

    def test_nginx_workers_fds_count(self):
        assert_that(calling(self.nginx_metrics_collector.workers_fds_count),
                    not_(raises(Exception)))

    # These next two tests have to be skipped due to calls to .handle_zombie() which raises a hamcrest exception.
    @future_test
    def test_nginx_workers_rlimit_nofile(self):
        assert_that(
            calling(self.nginx_metrics_collector.workers_rlimit_nofile),
            not_(raises(Exception)))

    @future_test
    def test_nginx_workers_io(self):
        assert_that(calling(self.nginx_metrics_collector.workers_io),
                    not_(raises(Exception)))

    def test_nginx_workers_cpu(self):
        assert_that(calling(self.nginx_metrics_collector.workers_cpu),
                    not_(raises(Exception)))