Beispiel #1
0
def test_unregister_resets_default():
    """Unregistering a backend unsets it as the default backend (if it was set)."""
    class CountingCollector(object):
        CONSTRUCTED_COUNT = 0

        def __init__(self):
            CountingCollector.CONSTRUCTED_COUNT += 1

    Collector.register_backend("counter", CountingCollector)
    Collector.set_default_backend("counter")

    # Calling get() should construct custom backend once.
    Collector.get()
    assert CountingCollector.CONSTRUCTED_COUNT == 1

    # Calling get() should construct custom backend again.
    Collector.get()
    assert CountingCollector.CONSTRUCTED_COUNT == 2

    Collector.register_backend("counter", None)

    # Calling get() after unregister should succeed
    assert Collector.get()

    # And it should not have constructed the custom backend
    assert CountingCollector.CONSTRUCTED_COUNT == 2
Beispiel #2
0
def test_set_default():
    """Can set and use a default collector."""
    class MyCollector(object):
        INSTANCES = []

        def __init__(self):
            MyCollector.INSTANCES.append(self)
            self.pushed = []

        def update_push_items(self, items):
            self.pushed.extend(items)

    Collector.register_backend("my-collector", MyCollector)
    Collector.set_default_backend("my-collector")

    items = [
        {
            "filename": "file1",
            "state": "PENDING"
        },
        {
            "filename": "file2",
            "state": "PENDING"
        },
    ]

    # Updating push items through default collector should succeed
    Collector.get().update_push_items(items).result()

    # It should have used the class we installed as the default
    assert len(MyCollector.INSTANCES) == 1
    assert MyCollector.INSTANCES[0].pushed == items
Beispiel #3
0
def reset_backend():
    """Resets the default backend both before and after each test,
    and clears my-collector backend.
    """
    Collector.set_default_backend(None)
    yield
    Collector.set_default_backend(None)
    Collector.register_backend("my-collector", None)
def test_base_class_context_manager():
    """Exercise the __enter__ and __exit__ methods of Collector."""
    collector = Collector()
    # use my-collector name because the autouse fixture `reset_backend`
    # will clean it up for us
    Collector.register_backend("my-collector", lambda: collector)
    Collector.set_default_backend("my-collector")

    # empty with just to exercise __enter__ and __exit__
    with Collector.get():
        pass
Beispiel #5
0
def test_context_manager_backend():
    """Test a backend's context manager protocol methods are called properly."""
    collector = MagicMock(spec=Collector)

    # use my-collector name because the autouse fixture `reset_backend`
    # will clean it up for us
    Collector.register_backend("my-collector", lambda: collector)
    Collector.set_default_backend("my-collector")

    with Collector.get():
        pass

    collector.__enter__.assert_called_once()
    collector.__exit__.assert_called_once_with(*(None, ) * 3)
Beispiel #6
0
def fake_collector():
    """Install fake in-memory backend for pushcollector library.
    Recorded push items can be tested via this instance.

    This is an autouse fixture so that all tests will automatically
    use the fake backend.
    """
    collector = FakeCollector()

    Collector.register_backend("pubtools-pulp-test", lambda: collector)
    Collector.set_default_backend("pubtools-pulp-test")

    yield collector

    Collector.set_default_backend(None)
Beispiel #7
0
def stub_collector():
    """Installs a custom pushcollector backend which records all push items
    onto a plain old list.

    Yields the list; it can be inspected to see which push items were recorded."""
    itemlist = []

    Collector.register_backend("pubtools-pulp-test",
                               lambda: StubCollector(itemlist))
    Collector.set_default_backend("pubtools-pulp-test")

    yield itemlist

    Collector.set_default_backend(None)
    Collector.register_backend("pubtools-pulp-test", None)