Page MenuHomeSoftware Heritage

Added LaunchpadLister

Authored by legau on Mar 10 2020, 3:11 PM.


Group Reviewers

First draft for (T1734)

It uses only launchpadlib and no http requests. This is because we've been discussing with the people at launchpad and they're
planning on adding a new endpoint git_repositories.getAllRepositories just for this. Using only the library it will be easier to implement
once they're done.

It is functionnal but tests are not ready yet. Looking for improvements and remarks on its design as it overrides almost everything.

Diff Detail

rDLS Listers
No Linters Available
No Unit Test Coverage
Build Status
Buildable 11632
Build 17640: Phabricator diff pipeline on jenkinsJenkins console · Jenkins
Build 17639: arc lint + arc unit

Event Timeline

legau created this revision.Mar 10 2020, 3:11 PM
legau updated this revision to Diff 10135.Mar 18 2020, 4:28 PM

Updated with git_repositories.getRepositories

Tasks tests added, Still no lister tests

ardumont added a subscriber: ardumont.EditedMar 24 2020, 5:58 PM


Thanks for your interest and work on this.

Still no lister tests

Please add at least one integration test like the gnu one [1].
You could check other lister, they all have at least one integration test like this one.

That does not delve into the intricacies of the lister contrary to some other tests.
It allows to directly check the scheduled tasks (what's been listed and outputted in the scheduler's db).

You can then check the task's shape is somewhat in the form you expect.



legau updated this revision to Diff 10254.Mar 25 2020, 4:29 PM
  • Added Tests and annotations
ardumont added inline comments.Mar 25 2020, 6:21 PM
6 ↗(On Diff #10254)

you should not need it for tests as it's runtime required already.




What are those parameters?

You might want to use some defined at [1]



Please remove it.


I did not fully realize earlier.

Does that test currently load a real launchpad instance?
(i believe so and the diff description seems to agree ;)

I don't see the data test file.

requests_mock_datadir is a fixture to transform http requests into lookup within the ./data/<https_server-name>/remaining-file-named-after-remaining-http-query-parameters...

legau added inline comments.Mar 25 2020, 8:27 PM

These arguments are what the launchpad instance need to be set up as described here


Yes it does, what would you recommend to use to mock launchpadlib data ?

ardumont added inline comments.Mar 26 2020, 9:25 AM

We are using pytest and this allows to define fixtures like requests_mock_datadir.

So as a first step, i'd look if there is any existing fixture around launchpadlib [1]
Note that checking how they are testing themselves should be interesting.
As that would help in understanding how to define the fixture if we need to do it ourselves.

If there is no fixture yet, you can try to define one which would mock the launchpadlib calls to return a fixed dataset of repositories to list. Just define a json file in the "tests/data/launchpadlib/dataset.json" and make the fixture returns it. Then in the tests, you can further ensure the data listed and the test dataset are consistent together.

We use pytest-mocker for that, for example [2]



ardumont accepted this revision.Mar 27 2020, 10:52 AM

Requiring changes for the sake of the change on the testing part.
We don't want to depend on third party dataset for the testing.
That could fail for no good reasons.

I think i'm fine with the implementation otherwise.

This revision is now accepted and ready to land.Mar 27 2020, 10:52 AM
ardumont requested changes to this revision.Mar 27 2020, 10:53 AM

wrong selection sorry

This revision now requires changes to proceed.Mar 27 2020, 10:53 AM
legau updated this revision to Diff 10535.Apr 4 2020, 6:56 PM

Updated tests to mock launchpadlib

legau updated this revision to Diff 10536.Apr 4 2020, 7:05 PM


ardumont added inline comments.Apr 5 2020, 11:10 AM

When you'll have your fixture working as you want.
Define said lister fixture in swh/lister/launchpad/tests/ (or even just before the test that uses it, i would not mind).

def lister_launchpad(swh_listers):
    with patch('launchpadlib.launchpad.Launchpad', MagicMock()):
        lister = swh_listers['launchpad']
    # define how the mock should behave when calling given methods...
    # what you actually defined below in the beginning of the tests.
    lister.launchpad.git_repositories.getRepositories = MagicMock(
        side_effect=[mock_lp_response(i) for i in range(3)])

    return lister

And then instead of using swh_listers in your test definition, use the fixture define here:

def test_launchpad_lister(lister_launchpad):

That way, when reading the test, we can concentrate on checking the expected behavior of the lister.
And not the setup of the fixture (for that we will read the fixture definition ;)


Please, make it readable (pretty print).

Personally, i use within emacs, M-x json-pretty-print ;)


we have a datadir fixture [1] for that, "inject" datadir in the test signature and:

response_filepath  = os.path.join(datadir, f'response{page}.json')
with open(response_filepath, encoding='utf-8') as f:


legau marked 5 inline comments as done.Apr 5 2020, 4:38 PM
legau added inline comments.

That was my first thought but swh_listers instantiates all the listers at once so in LaunchpadLister.init login to launchpad is already called when you want to retrieve swh_listers['launchpad']

legau updated this revision to Diff 10537.Apr 5 2020, 4:58 PM

Fixed JSON and added datadir fixture

legau added a comment.Apr 5 2020, 4:59 PM

I think I broke Phabricator with my commits

I think I broke Phabricator with my commits

I'm not sure what that means.

In any case, I see a merge within your commits, please use git rebase to update a branch instead [1]

Quoting [1]
rewriting old commits with git rebase (to preserve a nice, easy to bisect history)

And then update the diff again, maybe that will also help jenkins to trigger the build diff.



That should not be an issue. Have you tried?

ardumont added inline comments.Apr 6 2020, 10:31 AM

And If that becomes an issue nonetheless, do not hesitate to split the existing fixtures to be able to reuse some part within your own (as long as other tests are still working, it's fine ;):

Something like, in the main conftest:

def lister_db_url(postgresql_proc):
    db_url = 'postgresql://{user}@{host}:{port}/{dbname}'.format(,
    logger.debug('lister db_url: %s', db_url)
    return db_url

def swh_listers(request, lister_db_url, swh_scheduler):
    listers = {}

    # Prepare schema for all listers
    for lister_name in SUPPORTED_LISTERS:
        lister = get_lister(lister_name, db_url=lister_db_url)
        lister.scheduler = swh_scheduler  # inject scheduler fixture
        listers[lister_name] = lister
    initialize(create_engine(lister_db_url), drop_tables=True)

    # Add the load-archive-files expected by some listers (gnu, cran, ...)
        'type': 'load-archive-files',
        'description': 'Load archive files.',
        'backend_name': 'swh.loader.package.tasks.LoadArchive',
        'default_interval': '1 day',

    return listers

In launchpadlister one:

def launchpad_lister(request, lister_db_url, swh_scheduler):
    # all the necessary cogs for mocking launchpad...
    # ...
    with patch('launchpadlib.launchpad.Launchpad', MagicMock()):
        lister = get_lister('launchpad', db_url=lister_db_url)
        lister.scheduler = swh_scheduler  # inject scheduler fixture
        initialize(create_engine(lister_db_url), drop_tables=True)
    return lister
legau added inline comments.Apr 6 2020, 5:35 PM

Indeed it was an issue because when you use swh_listers fixture it is called before any patch so swh_listers['launchpad'].launchpad is already instanciated with a real Launchpad instance.
I'm using what you wrote it works great

legau updated this revision to Diff 10543.Apr 6 2020, 5:48 PM

Modified Fixtures

legau added a comment.Apr 6 2020, 5:49 PM

Build is still broken even with rebasing

ardumont added inline comments.Apr 6 2020, 6:01 PM

And actually, you are making the mock being injected to swh_listers without using it later.

That feels strange.


drop swh_listers, you don't need it any longer.


you are monkey-patching here and no longer mocking.

Why not the initial with patch stanza you used earlier, that felt more standard, well at least on how we define it here in the team ;)

Build is still broken even with rebasing

yes, that may have nothing to do with you ;)

How do you update your diff, something like?

arc diff origin/master --update D2799
legau added a comment.Apr 6 2020, 6:50 PM

Yes that's how I do it

legau added inline comments.Apr 6 2020, 7:00 PM

Yes that's right


lister.launchpad.git_repositories.getRepositories is already a mocked object, would it be ok just to redefine its side_effect like

lister.launchpad.git_repositories.getRepositories.side_effect =[mock_lp_response(i) for i in range(3)]

Yes that's how I do it

Ok, thanks.

I think you built that diff prior to some changes in the jenkins configuration regarding diff (the dates roughly match in my head).
(Again, that's not on you)

I'm thinking that change in between (plus the current error message in the build) made it so it won't build (somehow, jenkins is missing some stuff from the staging repository).
The "new" build never got green but it never started, it systematically fail at applying the patch altogether.

If that's ok with you, could you please abandon this diff and make a new one?

Here, select "Abandon Revision" at the bottom of the page (selection box).

And then, from your machine, create a new diff with:

arc diff --create origin/master

The drawback is that we'll lost the history exchange of the diff, but i think you mostly, if not all, answered to everything already ;)



I don't know yet, i'd like to see the current build's status first ;)
So i'm focusing on that first, see my main comment ;)

legau abandoned this revision.Apr 7 2020, 5:34 PM