Example #1
0
def test_script_can_push_snaps_with_credentials(event_loop, monkeypatch,
                                                channel):
    push_call_counter = (n for n in range(0, 2))

    task = {
        'dependencies': ['some_snap_build_taskId'],
        'scopes': ['project:releng:snapcraft:firefox:{}'.format(channel)],
        'payload': {
            'upstreamArtifacts': [{
                'paths': ['public/build/firefox-59.0.snap'],
                'taskId': 'some_snap_build_taskId',
                'taskType': 'build'
            }],
        },
    }

    with tempfile.NamedTemporaryFile('w+') as macaroon_beta, \
            tempfile.NamedTemporaryFile('w+') as macaroon_candidate:
        config = {
            'macaroons_locations': {
                'candidate': macaroon_candidate.name,
                'beta': macaroon_beta.name,
            },
        }

        with tempfile.TemporaryDirectory() as work_dir:
            config['work_dir'] = work_dir

            with open(os.path.join(work_dir, 'task.json'), 'w') as task_file:
                json.dump(task, task_file)

            snap_artifact_dir = os.path.join(
                work_dir, 'cot/some_snap_build_taskId/public/build/')
            makedirs(snap_artifact_dir)
            snap_artifact_path = os.path.join(snap_artifact_dir,
                                              'firefox-59.0.snap')
            with open(snap_artifact_path, 'w') as snap_file:
                snap_file.write(' ')

            # config_file is not put in the TemporaryDirectory() (like the others), because it usually lives
            # elsewhere on the filesystem
            with tempfile.NamedTemporaryFile('w+') as config_file:
                json.dump(config, config_file)
                config_file.seek(0)

                def snapcraft_store_client_push_fake(snap_filename,
                                                     release_channels):
                    assert snap_filename == snap_artifact_path
                    assert release_channels == [channel]
                    next(push_call_counter)

                monkeypatch.setattr(snapcraft_store_client, 'push',
                                    snapcraft_store_client_push_fake)
                main(config_path=config_file.name)

    assert next(push_call_counter) == 1
def test_script_can_push_snaps_with_credentials(monkeypatch, channel, expected_revision):
    task = {
        "dependencies": ["some_snap_build_taskId"],
        "scopes": ["project:releng:snapcraft:firefox:{}".format(channel)],
        "payload": {"upstreamArtifacts": [{"paths": ["public/build/target.snap"], "taskId": "some_snap_build_taskId", "taskType": "build"}]},
    }

    snapcraft_store_client_mock = MagicMock()
    store_mock = MagicMock()
    store_mock.get_snap_revisions.return_value = _ALL_REVISIONS_ABSTRACT

    def cpi_get_side_effect(*args, **kwargs):
        revision = kwargs["params"]["revision"]
        cpi_get_mock = MagicMock()
        cpi_get_mock.json.return_value = {"download_sha3_384": "fake_hash_rev{}".format(revision)}
        return cpi_get_mock

    store_mock.cpi.get.side_effect = cpi_get_side_effect

    monkeypatch.setattr(snap_store, "StoreClient", lambda: store_mock)
    monkeypatch.setattr(snap_store, "get_hash", lambda *args, **kwargs: "fake_hash_rev{}".format(expected_revision))

    with tempfile.NamedTemporaryFile("w+") as macaroon_beta, tempfile.NamedTemporaryFile("w+") as macaroon_candidate:
        config = {"push_to_store": True, "macaroons_locations": {"candidate": macaroon_candidate.name, "beta": macaroon_beta.name}}

        with tempfile.TemporaryDirectory() as work_dir:
            config["work_dir"] = work_dir

            with open(os.path.join(work_dir, "task.json"), "w") as task_file:
                json.dump(task, task_file)

            snap_artifact_dir = os.path.join(work_dir, "cot/some_snap_build_taskId/public/build/")
            makedirs(snap_artifact_dir)
            snap_artifact_path = os.path.join(snap_artifact_dir, "target.snap")
            with open(snap_artifact_path, "w") as snap_file:
                snap_file.write(" ")

            # config_file is not put in the TemporaryDirectory() (like the others), because it usually lives
            # elsewhere on the filesystem
            with tempfile.NamedTemporaryFile("w+") as config_file:
                json.dump(config, config_file)
                config_file.seek(0)

                monkeypatch.setattr(snap_store, "snapcraft_store_client", snapcraft_store_client_mock)
                main(config_path=config_file.name)

    snapcraft_store_client_mock.push.assert_called_once_with(snap_filename=snap_artifact_path)
    store_mock.release.assert_called_once_with(snap_name="firefox", revision=expected_revision, channels=[channel])
Example #3
0
def test_script_can_push_snaps_with_credentials(monkeypatch, channel,
                                                expected_revision):
    task = {
        'dependencies': ['some_snap_build_taskId'],
        'scopes': ['project:releng:snapcraft:firefox:{}'.format(channel)],
        'payload': {
            'upstreamArtifacts': [{
                'paths': ['public/build/target.snap'],
                'taskId': 'some_snap_build_taskId',
                'taskType': 'build'
            }],
        },
    }

    snapcraft_store_client_mock = MagicMock()
    store_mock = MagicMock()
    store_mock.get_snap_revisions.return_value = _ALL_REVISIONS_ABSTRACT

    def cpi_get_side_effect(*args, **kwargs):
        revision = kwargs['params']['revision']
        cpi_get_mock = MagicMock()
        cpi_get_mock.json.return_value = {
            'download_sha3_384': 'fake_hash_rev{}'.format(revision)
        }
        return cpi_get_mock

    store_mock.cpi.get.side_effect = cpi_get_side_effect

    monkeypatch.setattr(snap_store, 'StoreClient', lambda: store_mock)
    monkeypatch.setattr(
        snap_store, 'get_hash',
        lambda *args, **kwargs: 'fake_hash_rev{}'.format(expected_revision))

    with tempfile.NamedTemporaryFile('w+') as macaroon_beta, \
            tempfile.NamedTemporaryFile('w+') as macaroon_candidate:
        config = {
            'macaroons_locations': {
                'candidate': macaroon_candidate.name,
                'beta': macaroon_beta.name,
            },
        }

        with tempfile.TemporaryDirectory() as work_dir:
            config['work_dir'] = work_dir

            with open(os.path.join(work_dir, 'task.json'), 'w') as task_file:
                json.dump(task, task_file)

            snap_artifact_dir = os.path.join(
                work_dir, 'cot/some_snap_build_taskId/public/build/')
            makedirs(snap_artifact_dir)
            snap_artifact_path = os.path.join(snap_artifact_dir, 'target.snap')
            with open(snap_artifact_path, 'w') as snap_file:
                snap_file.write(' ')

            # config_file is not put in the TemporaryDirectory() (like the others), because it usually lives
            # elsewhere on the filesystem
            with tempfile.NamedTemporaryFile('w+') as config_file:
                json.dump(config, config_file)
                config_file.seek(0)

                monkeypatch.setattr(snap_store, 'snapcraft_store_client',
                                    snapcraft_store_client_mock)
                main(config_path=config_file.name)

    snapcraft_store_client_mock.push.assert_called_once_with(
        snap_filename=snap_artifact_path)
    store_mock.release.assert_called_once_with(snap_name='firefox',
                                               revision=expected_revision,
                                               channels=[channel])
Example #4
0
def test_script_can_push_snaps_with_credentials(event_loop, monkeypatch,
                                                channel):
    function_call_counter = (n for n in range(0, 2))

    task = {
        'dependencies': ['some_snap_build_taskId'],
        'scopes': ['project:releng:snapcraft:firefox:{}'.format(channel)],
        'payload': {
            'upstreamArtifacts': [{
                'paths': ['public/build/firefox-59.0.snap'],
                'taskId': 'some_snap_build_taskId',
                'taskType': 'build'
            }],
        },
    }

    with tempfile.NamedTemporaryFile('w+') as macaroon_beta, \
            tempfile.NamedTemporaryFile('w+') as macaroon_candidate:
        macaroon_beta.write(SNAPCRAFT_SAMPLE_CONFIG)
        macaroon_candidate.write(SNAPCRAFT_SAMPLE_CONFIG)
        macaroon_beta.seek(0)
        macaroon_candidate.seek(0)

        config = {
            'macaroons_locations': {
                'candidate': macaroon_candidate.name,
                'beta': macaroon_beta.name,
            },
        }

        with tempfile.TemporaryDirectory() as work_dir:
            config['work_dir'] = work_dir

            with open(os.path.join(work_dir, 'task.json'), 'w') as task_file:
                json.dump(task, task_file)

            snap_artifact_dir = os.path.join(
                work_dir, 'cot/some_snap_build_taskId/public/build/')
            makedirs(snap_artifact_dir)
            snap_artifact_path = os.path.join(snap_artifact_dir,
                                              'firefox-59.0.snap')
            with open(snap_artifact_path, 'w') as snap_file:
                snap_file.write(' ')

            # config_file is not put in the TemporaryDirectory() (like the others), because it usually lives
            # elsewhere on the filesystem
            with tempfile.NamedTemporaryFile('w+') as config_file:
                json.dump(config, config_file)
                config_file.seek(0)

                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 == snap_artifact_path
                        assert channel == channel
                        next(function_call_counter)

                    @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)
                    main(config_path=config_file.name)

                    snapcraft_cred_file = os.path.join(temp_dir, '.snapcraft',
                                                       'snapcraft.cfg')

                    with open(os.path.join(
                            snapcraft_cred_file)) as snapcraft_login_config:
                        assert snapcraft_login_config.read(
                        ) == SNAPCRAFT_SAMPLE_CONFIG

    assert not os.path.exists(snapcraft_cred_file)
    assert next(
        function_call_counter) == 1  # Check fake function was called once