Esempio n. 1
0
    def _maybe_inject_config_version(self, lock: Lock, config: OSConfig,
                                     raw_config: dict):
        if config.get_version() != 'latest':
            return

        latest_config = self.config.api.get_highest_artifact(
            'config', config.get_name())
        with lock:
            if latest_config:
                raw_config['os']['version'] = int(
                    latest_config.get('version')) + 1
            else:
                raw_config['os']['version'] = 1
Esempio n. 2
0
class ConfigTest(unittest.TestCase):
    def setUp(self):
        test_yaml = {'os': {'name': 'test', 'version': 1}}

        self.test_config = OSConfig(MagicMock(), MagicMock(), test_yaml)

    def test_config_is_valid(self):
        self.assertIsNone(self.test_config.validate())

    def test_config_content_type(self):
        self.assertEqual(self.test_config.get_content_type(), 'text/x-yaml')

    def test_config_type(self):
        self.assertEqual(self.test_config.get_type(), 'config')

    def test_config_pretty_type(self):
        self.assertEqual(self.test_config.get_pretty_type(), 'OS Config')

    def test_config_sub_type(self):
        self.assertIsNone(self.test_config.get_sub_type())

    def test_config_name(self):
        self.assertEqual(self.test_config.get_name(),
                         str(self.test_config.ecosystem['os']['name']))

    def test_config_version(self):
        self.assertEqual(self.test_config.get_version(),
                         str(self.test_config.ecosystem['os']['version']))

    def test_config_meta_data(self):
        self.assertIsNone(self.test_config.get_registry_meta_data())
Esempio n. 3
0
    def test__upload_artifact__config_requests_are_correct(self):
        config_file = os.path.join(__tests_root__, 'res/config.yml')
        artifact = OSConfig.parse(MagicMock(), config_file)
        self.handler.get = MagicMock(return_value={
            'signed_request': 'signed_request',
            'url': 'signed_url'
        })

        self.api.upload_artifact(config_file, artifact)

        self.handler.get.assert_called_with(
            'url_root/mason-test/project-id/1?type=config&noContentType=true',
            headers={
                'Content-Type': 'application/json',
                'Content-MD5': 'BtYkQIi96WeIVrTFcPaYtQ==',
                'Authorization': 'Bearer Foobar'
            }
        )
        self.handler.put.assert_called_with(
            'signed_request',
            config_file,
            headers={'Content-MD5': 'BtYkQIi96WeIVrTFcPaYtQ=='}
        )
        self.handler.post.assert_called_with(
            'url_root/mason-test',
            headers={'Content-Type': 'application/json', 'Authorization': 'Bearer Foobar'},
            json={
                'name': 'project-id',
                'version': '1',
                'customer': 'mason-test',
                'url': 'signed_url',
                'type': 'config',
                'checksum': {'sha1': '1061ecf0b0a7269c352439e5f7c0819b268d8e6f'}
            }
        )
Esempio n. 4
0
    def _rewritten_config(self, raw_config_file, apks, medias):
        raw_config = OSConfig.parse(self.config, raw_config_file).ecosystem
        config = copy.deepcopy(raw_config)
        apps = raw_config.get('apps') or []
        media = raw_config.get('media') or {}

        config['from'] = raw_config_file
        for apk in apks:
            package_name = apk.get_name()
            version = apk.get_version()

            if self._has_app_presence(package_name, apps):
                for app in config.get('apps'):
                    if package_name == app.get('package_name'):
                        app['version_code'] = int(version)
                        break

        for medium in medias:
            name = medium.get_name()
            version = medium.get_version()
            type_ = medium.get_sub_type()

            if self._has_media_animation_presence(media, name, type_):
                config.get('media').get(type_)['version'] = int(version)

        config_file = os.path.join(self.working_dir,
                                   os.path.basename(raw_config_file))
        with open(config_file, 'w') as f:
            f.write(yaml.safe_dump(config))
        return config_file
Esempio n. 5
0
    def _sanitize_config_for_upload(self, config: OSConfig):
        raw_config = copy.deepcopy(config.ecosystem)
        raw_config.pop('from', None)

        lock = Lock()
        rewrite_ops = []

        rewrite_ops.append(
            self.config.executor.submit(self._maybe_inject_config_version,
                                        lock, config, raw_config))
        for app in raw_config.get('apps') or []:
            rewrite_ops.append(
                self.config.executor.submit(self._maybe_inject_app_version,
                                            lock, app))
        rewrite_ops.append(
            self.config.executor.submit(self._maybe_inject_media_versions,
                                        lock, raw_config, 'bootanimation'))
        rewrite_ops.append(
            self.config.executor.submit(self._maybe_inject_media_versions,
                                        lock, raw_config, 'splash'))

        wait_for_futures(self.config.executor, rewrite_ops)

        config_file = os.path.join(self.working_dir,
                                   os.path.basename(config.binary))
        with open(config_file, 'w') as f:
            f.write(yaml.safe_dump(raw_config))
        rewritten_config = OSConfig.parse(self.config, config_file)
        rewritten_config.user_binary = config.user_binary
        return rewritten_config
Esempio n. 6
0
    def test_config_registers_successfully(self):
        input_config_file = os.path.join(__tests_root__, 'res', 'config.yml')
        working_dir = tempfile.mkdtemp()
        config_file = os.path.join(working_dir, 'config.yml')
        command = RegisterConfigCommand(self.config, [input_config_file], working_dir)

        command.run()

        self.config.api.upload_artifact.assert_called_with(
            config_file, OSConfig.parse(self.config, config_file))
Esempio n. 7
0
    def prepare(self):
        configs = []

        for file in self._expand_files(self.config_files):
            config = OSConfig.parse(self.config, file)
            config = self._sanitize_config_for_upload(config)

            self.config.analytics.log_config(config.ecosystem)
            configs.append(config)

        return configs, configs
Esempio n. 8
0
    def test_registers_and_uploads_config(self):
        input_config_file = os.path.join(__tests_root__, 'res', 'config.yml')
        working_dir = tempfile.mkdtemp()
        config_file = os.path.join(working_dir, 'config.yml')
        command = StageCommand(self.config, [input_config_file], False, None, working_dir)

        command.run()

        self.config.api.upload_artifact.assert_called_with(
            config_file, OSConfig.parse(self.config, config_file))
        self.config.api.start_build.assert_called_with('project-id', '1', None)
Esempio n. 9
0
    def test_registers_and_uploads_rewritten_config(self):
        self.config.api.get_latest_artifact = MagicMock(return_value={'version': '12'})
        self.config.api.get_highest_artifact = MagicMock(return_value={'version': '12'})
        input_config_file = os.path.join(__tests_root__, 'res', 'config4.yml')
        working_dir = tempfile.mkdtemp()
        config_file = os.path.join(working_dir, 'config4.yml')
        command = StageCommand(self.config, [input_config_file], False, None, working_dir)

        command.run()

        self.config.api.upload_artifact.assert_called_with(
            config_file, OSConfig.parse(self.config, config_file))
        self.config.api.start_build.assert_called_with('project-id4', '13', None)
Esempio n. 10
0
    def test_project_registers_successfully(self):
        self.config.endpoints_store.__getitem__ = MagicMock(return_value='https://google.com')
        self.config.api.get_build = MagicMock(return_value={'data': {'status': 'COMPLETED'}})
        simple_project = os.path.join(__tests_root__, 'res', 'simple-project')
        apk_file = os.path.join(__tests_root__, 'res', 'simple-project', 'v1.apk')
        working_dir = tempfile.mkdtemp()
        config_file = os.path.join(working_dir, 'mason.yml')
        command = RegisterProjectCommand(self.config, simple_project, working_dir)

        command.run()

        self.config.api.upload_artifact.assert_has_calls([
            call(apk_file, Apk.parse(self.config, apk_file)),
            call(config_file, OSConfig.parse(self.config, config_file))
        ])
        self.config.api.start_build.assert_called_with('project-id2', '2', None)
Esempio n. 11
0
    def setUp(self):
        test_yaml = {'os': {'name': 'test', 'version': 1}}

        self.test_config = OSConfig(MagicMock(), MagicMock(), test_yaml)