Beispiel #1
0
 def get_latest_changeset(self):
     '''
     Get the latest coverage changeset pushed to ES
     '''
     query = ActiveDataCoverage.available_revisions_query(nb=1)
     out = self.search('latest-build', query)
     if out['aggregations']:
         return out['aggregations']['revisions']['buckets'][0]['key']
async def test_list_tests_invalid(mock_secrets, mock_active_data):
    '''
    Test latest build availables from Elastic Search cluster
    '''
    from code_coverage_backend.services.active_data import ActiveDataCoverage
    activeDataCoverage = ActiveDataCoverage()

    nb, tests = await activeDataCoverage.list_tests('dummy', 'test.cpp')
    assert nb == 0
    assert tests == []
async def test_latest_build(mock_secrets, mock_active_data):
    '''
    Test latest build availables from Elastic Search cluster
    '''
    from code_coverage_backend.services.active_data import ActiveDataCoverage
    activeDataCoverage = ActiveDataCoverage()
    latest_rev, previous_rev = await activeDataCoverage.get_latest_build()

    assert latest_rev == '2d83e1843241d869a2fc5cf06f96d3af44c70e70'
    assert previous_rev == 'a0f7e5f1bea6466277ba96a2bd22eee6f72930c3'
async def test_available_revisions(mock_secrets, mock_active_data):
    '''
    Test last available revisions from Elastic Search cluster
    '''
    from code_coverage_backend.services.active_data import ActiveDataCoverage
    activeDataCoverage = ActiveDataCoverage()
    revs = await activeDataCoverage.available_revisions()

    assert len(revs) == 2
    assert revs[0]['key'] == '2d83e1843241d869a2fc5cf06f96d3af44c70e70'
    assert revs[1]['key'] == 'a0f7e5f1bea6466277ba96a2bd22eee6f72930c3'
async def test_list_tests_valid(mock_secrets, mock_active_data):
    '''
    Test latest build availables from Elastic Search cluster
    '''
    from code_coverage_backend.services.active_data import ActiveDataCoverage
    activeDataCoverage = ActiveDataCoverage()

    nb, tests = await activeDataCoverage.list_tests(
        '2d83e1843241d869a2fc5cf06f96d3af44c70e70', 'js/src/jsutil.cpp')
    assert nb == 2
    assert len(tests) == 2
    assert [
        t['_source']['source']['file']['percentage_covered']['~n~']
        for t in tests
    ] == [
        0.42,
        0.1136363636363636,
    ]
async def test_file_coverage(mock_secrets, mock_active_data):
    '''
    Test file coverage API from Elastic Search cluster
    '''
    from code_coverage_backend.services.active_data import ActiveDataCoverage
    activeDataCoverage = ActiveDataCoverage()

    file_coverage = await activeDataCoverage.get_file_coverage(
        '2d83e1843241d869a2fc5cf06f96d3af44c70e70', 'js/src/jsutil.cpp')
    assert isinstance(file_coverage, dict)
    assert file_coverage == {
        42: 1,
        53: 2,
        54: 2,
        58: 2,
        59: 2,
        60: 2,
        170: 2,
        172: 2,
        173: 2,
        176: 2,
        180: 1
    }
Beispiel #7
0
def coverage_paths(path, changeset):
    '''
    Aggregate coverage percent data from ES for a given path and changeset
    The output will be a list of typed path (directory|file) and its total coverage
    '''
    assert not path.startswith('/'), 'No absolute path'

    filters = [
        # Filter by changeset
        {
            'term': {
                ActiveDataCoverage.FIELD_CHANGESET: changeset
            }
        },

        # Filename must start with specified path
        {
            'prefix': {
                ActiveDataCoverage.FIELD_FILENAME: path
            }
        },
    ]
    excludes = [
        # Remove weird rust macros
        {
            'wildcard': {
                ActiveDataCoverage.FIELD_FILENAME: '<* macros>'
            }
        },

        # Remove weird NONE
        {
            'term': {
                ActiveDataCoverage.FIELD_FILENAME: 'NONE'
            }
        },
    ]

    query = ActiveDataCoverage.base_query(filters, excludes)
    query.update({
        'aggs': {
            # Group by filename
            'coverage': {
                'terms': {
                    'field': ActiveDataCoverage.FIELD_FILENAME,
                    'script': {
                        'source': GROUP_BY_FOLDER.format(path_len=len(path)),
                        'lang': 'painless',
                    },

                    # Sort by filename
                    'order': {
                        '_key': 'asc'
                    },

                    # Should be enough to cover every folder in one query
                    # Biggest sa-central folder has 3600+ sub elements
                    'size': 5000,
                },

                # For each filename, average the coverage percent
                # over all the tests
                'aggs': {
                    'percent': {
                        'avg': {
                            'field': ActiveDataCoverage.FIELD_TOTAL_PERCENT,
                        }
                    }
                }
            }
        },

        # No hits please, that's just extra load
        'size': 0,
    })

    out = active_data.search('coverage_paths', query)
    return [{
        'path': item['key'],
        'coverage': item['percent']['value'],
        'nb': item['doc_count'],
        'type': item['key'].endswith('/') and 'directory' or 'file'
    } for item in out['aggregations']['coverage']['buckets']]