Exemplo n.º 1
0
def test_exclude_dirs(aggregator):
    with temp_directory() as td:
        exclude = ['node_modules', 'vendor']
        instance = {'directory': td, 'recursive': True, 'countonly': True, 'exclude_dirs': exclude}

        for ed in exclude:
            create_file(os.path.join(td, ed, 'file'))

        dir_check = DirectoryCheck('directory', {}, [instance])
        dir_check.check(instance)

    assert len(aggregator.metric_names) == 1
Exemplo n.º 2
0
def test_no_recursive_symlink_loop(aggregator):
    with temp_directory() as tdir:

        # Setup dir and files
        dir2 = os.path.join(tdir, 'fixture_dir2')
        dir3 = os.path.join(tdir, 'fixture_dir2', 'fixture_dir3')
        mkdir(dir2)
        mkdir(dir3)
        open(os.path.join(tdir, 'level1file'), 'w').close()
        open(os.path.join(dir2, 'level2file'), 'w').close()
        open(os.path.join(dir3, 'level3file'), 'w').close()
        os.symlink(tdir, os.path.join(dir3, 'symdir'))

        # Run Check
        instance = {
            'directory': tdir,
            'recursive': True,
            'filegauges': True,
            'follow_symlinks': False
        }
        check = DirectoryCheck('directory', {}, [instance])
        check.check(instance)

        # Assert no warning
        assert len(check.warnings) == 0

        # Assert metrics
        files = [
            ['level1file'],
            ['fixture_dir2', 'level2file'],
            ['fixture_dir2', 'fixture_dir3', 'level3file'],
            ['fixture_dir2', 'fixture_dir3', 'symdir'],
        ]
        for file in files:
            for metric in common.FILE_METRICS:
                tags = [
                    'name:{}'.format(tdir),
                    'filename:{}'.format(os.path.join(tdir, *file))
                ]
                aggregator.assert_metric(metric, count=1, tags=tags)
        for metric in common.DIR_METRICS:
            tags = ['name:{}'.format(tdir)]
            aggregator.assert_metric(metric, count=1, tags=tags)

    aggregator.assert_all_metrics_covered()
Exemplo n.º 3
0
def test_stat_follow_symlinks(aggregator, stat_follow_symlinks,
                              expected_dir_size, expected_file_sizes):
    def flatten_value(value):
        if callable(value):
            return value(tdir)
        return value

    with temp_directory() as tdir:

        # Setup dir and files
        file50 = os.path.join(tdir, 'file50')
        file100 = os.path.join(tdir, 'file100')
        file100sym = os.path.join(tdir, 'file100sym')

        with open(file50, 'w') as f:
            f.write('0' * 50)
        with open(file100, 'w') as f:
            f.write('0' * 100)

        os.symlink(file100, file100sym)

        # Run Check
        instance = {
            'directory': tdir,
            'recursive': True,
            'filegauges': True,
            'stat_follow_symlinks': stat_follow_symlinks,
        }
        check = DirectoryCheck('directory', {}, [instance])
        check.check(instance)

        common_tags = ['name:{}'.format(tdir)]
        aggregator.assert_metric('system.disk.directory.bytes',
                                 value=flatten_value(expected_dir_size),
                                 tags=common_tags)
        for filename, size in expected_file_sizes:
            tags = common_tags + [
                'filename:{}'.format(os.path.join(tdir, filename))
            ]
            aggregator.assert_metric('system.disk.directory.file.bytes',
                                     value=flatten_value(size),
                                     tags=tags)