def test_push(monkeypatch, channel): push_call_count = (n for n in range(0, 2)) login_call_count = (n for n in range(0, 3)) context = MagicMock() store_client_mock = MagicMock() monkeypatch.setattr('snapcraft.storeapi.StoreClient', lambda: store_client_mock) with tempfile.NamedTemporaryFile('w+') as fake_macaroon: context.config = {'macaroons_locations': {channel: fake_macaroon.name}} def snapcraft_store_client_login_fake(store, config_fd): assert store == store_client_mock assert config_fd.name == fake_macaroon.name next(login_call_count) def snapcraft_store_client_push_fake(snap_filename, release_channels): assert snap_filename == '/some/file.snap' assert release_channels == [channel] next(push_call_count) monkeypatch.setattr(snapcraft_store_client, 'login', snapcraft_store_client_login_fake) monkeypatch.setattr(snapcraft_store_client, 'push', snapcraft_store_client_push_fake) push(context, '/some/file.snap', channel) assert next(push_call_count) == 1 assert next(login_call_count) == 1 store_client_mock.logout.assert_called_once_with()
async def async_main(context): context.task = client.get_task(context.config) _log_warning_forewords(context) # TODO Sanity checks on the file snap_file_path = artifacts.get_snap_file_path(context) channel = task.pluck_channel(context.task) snap_store.push(context, snap_file_path, channel)
def test_push_early_return_if_not_allowed(monkeypatch): call_count = (n for n in range(0, 2)) context = MagicMock() def increase_call_count(_, __): next(call_count) monkeypatch.setattr(snapcraft_store_client, 'push', increase_call_count) push(context, '/some/file.snap', channel='mock') assert next(call_count) == 0
def test_push(monkeypatch, channel, expected_macaroon_location, raises, exception_message, bubbles_up_exception): fake_release_if_needed_count = (n for n in range(0, 2)) context = MagicMock() context.config = { "push_to_store": True, "macaroons_locations": { "beta": "/path/to/macaroon_beta", "candidate": "/path/to/macaroon_candidate" } } store = MagicMock() store_client_mock = MagicMock() @contextlib.contextmanager def fake_store_session(macaroon_location): assert macaroon_location == expected_macaroon_location yield store monkeypatch.setattr(snap_store, "_store_session", fake_store_session) monkeypatch.setattr(snap_store, "snapcraft_store_client", store_client_mock) store_client_mock.push.side_effect = (snap_store.StoreReviewError({ "errors": [{ "message": exception_message }], "code": "processing_error" }) if raises else None) def fake_release_if_needed(store_, channel_, snap_file_path): assert store_ is store assert channel_ == channel_ assert snap_file_path == "/path/to/snap" next(fake_release_if_needed_count) monkeypatch.setattr(snap_store, "_release_if_needed", fake_release_if_needed) if bubbles_up_exception: with pytest.raises(snap_store.StoreReviewError): snap_store.push(context, "/path/to/snap", channel) assert next(fake_release_if_needed_count) == 0 else: snap_store.push(context, "/path/to/snap", channel) assert next(fake_release_if_needed_count) == 1
def test_push(monkeypatch, channel, expected_macaroon_location, raises, exception_message, bubbles_up_exception): fake_release_if_needed_count = (n for n in range(0, 2)) context = MagicMock() context.config = { 'push_to_store': True, 'macaroons_locations': { 'beta': '/path/to/macaroon_beta', 'candidate': '/path/to/macaroon_candidate', } } store = MagicMock() store_client_mock = MagicMock() @contextlib.contextmanager def fake_store_session(macaroon_location): assert macaroon_location == expected_macaroon_location yield store monkeypatch.setattr(snap_store, '_store_session', fake_store_session) monkeypatch.setattr(snap_store, 'snapcraft_store_client', store_client_mock) store_client_mock.push.side_effect = snap_store.StoreReviewError({ 'errors': [{'message': exception_message}], 'code': 'processing_error', }) if raises else None def fake_release_if_needed(store_, channel_, snap_file_path): assert store_ is store assert channel_ == channel_ assert snap_file_path == '/path/to/snap' next(fake_release_if_needed_count) monkeypatch.setattr(snap_store, '_release_if_needed', fake_release_if_needed) if bubbles_up_exception: with pytest.raises(snap_store.StoreReviewError): snap_store.push(context, '/path/to/snap', channel) assert next(fake_release_if_needed_count) == 0 else: snap_store.push(context, '/path/to/snap', channel) assert next(fake_release_if_needed_count) == 1
def test_push(monkeypatch, channel): call_count = (n for n in range(0, 2)) context = MagicMock() with tempfile.NamedTemporaryFile('w+') as macaroon: context.config = { 'macaroons_locations': {channel: macaroon.name} } macaroon.write(SNAPCRAFT_SAMPLE_CONFIG) with tempfile.TemporaryDirectory() as temp_dir: def snapcraft_store_client_push_fake(snap_file_path, channel): # This function can't be a regular mock because of the following check: assert os.getcwd() == temp_dir # Push must be done from a disposable dir assert snap_file_path == '/some/file.snap' assert channel == channel next(call_count) @contextlib.contextmanager def TemporaryDirectory(): try: yield temp_dir finally: pass monkeypatch.setattr(tempfile, 'TemporaryDirectory', TemporaryDirectory) monkeypatch.setattr(snapcraft_store_client, 'push', snapcraft_store_client_push_fake) push(context, '/some/file.snap', channel) assert os.getcwd() != temp_dir snapcraft_cred_file = os.path.join(temp_dir, '.snapcraft', 'snapcraft.cfg') assert not os.path.exists(snapcraft_cred_file) assert next(call_count) == 1