def test_deployment_path_should_return_path_after_setting_name(self):
        cfg = Config(Source.url('url'), 'ref', stateroot='test-stateroot')
        cfg.set_deployment_name('deployment-name')

        self.assertEqual(
            cfg.deployment_dir,
            os.path.join('/ostree', 'deploy', 'test-stateroot', 'deploy',
                         'deployment-name'))
    def test_deployment_dir_should_include_sysroot(self):
        sysroot = os.path.join('/mnt', 'rootfs')
        cfg = Config(Source.url('url'), 'ref', sysroot=sysroot)
        cfg.set_deployment_name('0123deployment.0')

        self.assertEqual(
            cfg.deployment_dir,
            os.path.join(sysroot, 'ostree', 'deploy', cfg.stateroot, 'deploy',
                         '0123deployment.0'))
 def test_should_raise_config_exception_if_both_url_and_path_are_present(
         self):
     json = '''{
         "url": "https://example.com/ostree",
         "path": "/srv/ostree",
         "ref": "fedora/28/x86_64/workstation"
     }'''
     with self.assertRaises(InvalidConfigError):
         Config.parse_json(StringIO(json))
Beispiel #4
0
    def test_should_unmount_on_cleanup(self, run_mock: mock.Mock):
        cfg = Config(Source.url('url'), 'ref', stateroot='test')
        cfg.set_deployment_name('test-deploy.0')

        MountVar(cfg).cleanup()

        run_mock.assert_called_once_with([
            'umount',
            '-l',
            os.path.join('/ostree', 'deploy', 'test', 'deploy',
                         'test-deploy.0', 'var'),
        ],
                                         check=True)
Beispiel #5
0
    def test_should_bind_mount_var_into_deployment(self, run_mock: mock.Mock):
        cfg = Config(Source.url('url'), 'ref', stateroot='test')
        cfg.set_deployment_name('test-deploy.0')

        MountVar(cfg).run()

        run_mock.assert_called_once_with([
            'mount',
            '-o',
            'bind',
            os.path.join('/ostree', 'deploy', 'test', 'var'),
            os.path.join('/ostree', 'deploy', 'test', 'deploy',
                         'test-deploy.0', 'var'),
        ],
                                         check=True)
Beispiel #6
0
    def test_should_not_error_if_instance_is_created_without_deployment_name_set(
            self):
        cfg = Config(Source.url('url'), 'ref', stateroot='test')

        step = MountVar(cfg)

        self.assertIsNotNone(step)
    def test_should_raise_exception_if_nothing_was_added_to_deployments_dir(
            self):
        cfg = Config(Source.url('url'), 'ref')

        step = Deploy(cfg)
        with self.assertRaises(DeployError):
            step.run()
    def test_var_dir_should_be_path_to_stateroot_var_for_randomly_generated_stateroot(
            self):
        cfg = Config(Source.url('url'), 'ref')

        self.assertEqual(
            cfg.var_dir, os.path.join('/ostree', 'deploy', cfg.stateroot,
                                      'var'))
class TestBuiltinProvisioner(TestCase):
    cfg = Config(Source.url('url'), 'ref', stateroot='test')
    cfg.set_deployment_name('test-deploy.0')
    deploy_dir = os.path.join('/', 'ostree', 'deploy', 'test', 'deploy',
                              'test-deploy.0')

    def test_title_should_be_str_instance(self):
        self.assertIsInstance(
            BuiltinProvisioner(mock.Mock(), mock.Mock()).title, str)

    @mock.patch('deploy_ostree.steps.provisioners.builtin.run')
    def test_should_run_provisioner_script_with_no_arguments(
            self, run_mock: mock.Mock):
        provisioner = ProvisionerConfig('etc-fstab', {})

        BuiltinProvisioner(self.cfg, provisioner).run()

        run_mock.assert_called_once_with(
            [os.path.join(PROVISIONERS_DIR, 'etc-fstab'), self.deploy_dir],
            check=True,
            env={})

    @mock.patch('deploy_ostree.steps.provisioners.builtin.run')
    def test_should_run_provisioner_script_with_specified_sysroot(
            self, run_mock: mock.Mock):
        sysroot = os.path.join('/mnt', 'rootfs')
        cfg = Config(Source.url('url'),
                     'ref',
                     stateroot='test',
                     sysroot=sysroot)
        cfg.set_deployment_name('test-deploy.0')
        provisioner = ProvisionerConfig('etc-fstab', {})

        BuiltinProvisioner(cfg, provisioner).run()

        run_mock.assert_called_once_with([
            os.path.join(PROVISIONERS_DIR, 'etc-fstab'),
            os.path.join(sysroot, 'ostree', 'deploy', 'test', 'deploy',
                         'test-deploy.0')
        ],
                                         check=True,
                                         env={})

    @mock.patch('deploy_ostree.steps.provisioners.builtin.run')
    def test_should_run_provisioner_script_with_arguments(
            self, run_mock: mock.Mock):
        provisioner = ProvisionerConfig('create-user', {
            'username': '******',
            'password': '******'
        })

        BuiltinProvisioner(self.cfg, provisioner).run()

        run_mock.assert_called_once_with(
            [os.path.join(PROVISIONERS_DIR, 'create-user'), self.deploy_dir],
            check=True,
            env={
                'DEPLOY_OSTREE_username': '******',
                'DEPLOY_OSTREE_password': '******'
            })
Beispiel #10
0
    def test_should_return_FileRemote_if_path_source(self):
        cfg = Config(Source.path('/ostree/repo'), 'ref')

        steps = get_steps(cfg)

        self.assertEqual(len(steps), 1)
        self.assertIsInstance(steps[0], FileRemote)
    def test_should_parse_config_with_multiple_provisioners_and_arguments(
            self):
        json = '''{
            "path": "/srv/ostree",
            "ref": "ref",

            "default-provisioners": [
                {
                    "provisioner": "prov-1"
                },
                {
                    "arg1": "value1",
                    "arg2": 5,
                    "provisioner": "prov-3"
                },
                {
                    "provisioner": "prov-2",
                    "arg": true
                }
            ]
        }'''
        cfg = Config.parse_json(StringIO(json))

        self.assertEqual(cfg.default_provisioners, [
            ProvisionerConfig('prov-1', {}),
            ProvisionerConfig('prov-3', {
                'arg1': 'value1',
                'arg2': 5
            }),
            ProvisionerConfig('prov-2', {'arg': True}),
        ])
    def test_var_dir_should_include_sysroot(self):
        sysroot = os.path.join('/mnt', 'rootfs')
        cfg = Config(Source.url('url'), 'ref', sysroot=sysroot)

        self.assertEqual(
            cfg.var_dir,
            os.path.join(sysroot, 'ostree', 'deploy', cfg.stateroot, 'var'))
Beispiel #13
0
    def test_should_return_HttpRemote_if_url_source(self):
        cfg = Config(Source.url('http://example.com/ostree'), 'ref')

        steps = get_steps(cfg)

        self.assertEqual(len(steps), 1)
        self.assertIsInstance(steps[0], HttpRemote)
    def test_kernel_args_should_be_empty_if_not_specified(self):
        json = '''{
            "path": "/srv/ostree",
            "ref": "ref"
        }'''
        cfg = Config.parse_json(StringIO(json))

        self.assertEqual(cfg.kernel_args, [])
    def test_should_parse_config_with_empty_kernel_args(self):
        json = '''{
            "path": "/srv/ostree",
            "ref": "ref",
            "kernel-args": []
        }'''
        cfg = Config.parse_json(StringIO(json))

        self.assertEqual(cfg.kernel_args, [])
    def test_should_parse_config_with_empty_provisioners_list(self):
        json = '''{
            "path": "/srv/ostree",
            "ref": "ref",

            "default-provisioners": []
        }'''
        cfg = Config.parse_json(StringIO(json))

        self.assertEqual(cfg.default_provisioners, [])
    def test_should_parse_config_with_path_and_ref(self):
        json = '''{
            "path": "/srv/ostree",
            "ref": "fedora/28/x86_64/workstation"
        }'''
        cfg = Config.parse_json(StringIO(json))

        self.assertEqual('/srv/ostree', cfg.path)
        self.assertIsNone(cfg.url)
        self.assertEqual('fedora/28/x86_64/workstation', cfg.ref)
    def test_should_run_provisioner_script_with_specified_sysroot(
            self, run_mock: mock.Mock):
        sysroot = os.path.join('/mnt', 'rootfs')
        cfg = Config(Source.url('url'),
                     'ref',
                     stateroot='test',
                     sysroot=sysroot)
        cfg.set_deployment_name('test-deploy.0')
        provisioner = ProvisionerConfig('etc-fstab', {})

        BuiltinProvisioner(cfg, provisioner).run()

        run_mock.assert_called_once_with([
            os.path.join(PROVISIONERS_DIR, 'etc-fstab'),
            os.path.join(sysroot, 'ostree', 'deploy', 'test', 'deploy',
                         'test-deploy.0')
        ],
                                         check=True,
                                         env={})
Beispiel #19
0
    def test_should_mount_with_specified_sysroot(self, run_mock: mock.Mock):
        sysroot = os.path.join('/mnt', 'rootfs')
        cfg = Config(Source.url('url'),
                     'ref',
                     stateroot='test',
                     sysroot=sysroot)
        cfg.set_deployment_name('test-deploy.0')

        MountVar(cfg).run()

        run_mock.assert_called_once_with([
            'mount',
            '-o',
            'bind',
            os.path.join(sysroot, 'ostree', 'deploy', 'test', 'var'),
            os.path.join(sysroot, 'ostree', 'deploy', 'test', 'deploy',
                         'test-deploy.0', 'var'),
        ],
                                         check=True)
    def test_should_parse_config_with_url_and_ref(self):
        json = '''{
            "url": "https://example.com/ostree",
            "ref": "fedora/28/x86_64/workstation",

            "ignored key": "ignored value"
        }'''
        cfg = Config.parse_json(StringIO(json))

        self.assertEqual('https://example.com/ostree', cfg.url)
        self.assertIsNone(cfg.path)
        self.assertEqual('fedora/28/x86_64/workstation', cfg.ref)
Beispiel #21
0
    def test_should_pull_ref(self, run_mock: mock.Mock):
        cfg = Config(Source.url('url'),
                     'fedora/28/x86_64/workstation',
                     remote='ostree-remote')

        PullRef(cfg).run()

        run_mock.assert_called_once_with([
            'ostree', 'pull',
            '--repo=%s' % os.path.join('/ostree', 'repo'), 'ostree-remote',
            'fedora/28/x86_64/workstation'
        ],
                                         check=True)
    def test_should_parse_config_with_kernel_args(self):
        json = '''{
            "path": "/srv/ostree",
            "ref": "ref",
            "kernel-args": [
                "arg1",
                "arg2",
                "arg3"
            ]
        }'''
        cfg = Config.parse_json(StringIO(json))

        self.assertEqual(cfg.kernel_args, ['arg1', 'arg2', 'arg3'])
    def test_should_parse_config_with_remote_and_stateroot_names(self):
        json = '''{
            "url": "https://example.com/ostree",
            "ref": "fedora/28/x86_64/workstation",

            "remote": "atomicws",
            "stateroot": "fedora-atomic-workstation"
        }'''
        cfg = Config.parse_json(StringIO(json))

        self.assertEqual('https://example.com/ostree', cfg.url)
        self.assertEqual('fedora/28/x86_64/workstation', cfg.ref)
        self.assertEqual('atomicws', cfg.remote)
        self.assertEqual('fedora-atomic-workstation', cfg.stateroot)
Beispiel #24
0
    def test_should_return_BuiltinProvisioner_per_configured_provisioner(self):
        cfg = Config(Source.url('url'),
                     'ref',
                     stateroot='test',
                     default_provisioners=[
                         ProvisionerConfig('create-user', {}),
                         ProvisionerConfig('etc-fstab', {'arg': 'value'}),
                     ])

        steps = get_steps(cfg)

        self.assertEqual(len(steps), 2)
        self.assertIsInstance(steps[0], BuiltinProvisioner)
        self.assertIsInstance(steps[1], BuiltinProvisioner)
Beispiel #25
0
    def test_should_delete_ostree_remote(self, mock_run: mock.Mock):
        cfg = Config(Source.url('url'), 'ref', remote='remote-name')

        DeleteRemote(cfg).run()

        mock_run.assert_called_once_with([
            'ostree',
            'remote',
            'delete',
            '--repo=%s' % os.path.join('/ostree', 'repo'),
            '--if-exists',
            'remote-name',
        ],
                                         check=True)
Beispiel #26
0
    def test_should_add_ostree_remote_for_url_in_config(
            self, mock_run: mock.Mock):
        cfg = Config(Source.url('https://example.com/ostree'),
                     'debian/9/i386/desktop',
                     remote='remote-name')

        HttpRemote(cfg).run()

        mock_run.assert_called_once_with([
            'ostree', 'remote', 'add',
            '--repo=%s' % os.path.join('/ostree', 'repo'), '--no-gpg-verify',
            'remote-name', 'https://example.com/ostree'
        ],
                                         check=True)
Beispiel #27
0
    def test_should_add_ostree_remote_for_path_in_config(
            self, mock_run: mock.Mock):
        cfg = Config(Source.path('/srv/ostree/debian/repo'),
                     'debian/9/i386/desktop',
                     remote='remote-name')

        FileRemote(cfg).run()

        mock_run.assert_called_once_with([
            'ostree', 'remote', 'add',
            '--repo=%s' % os.path.join('/ostree', 'repo'), '--no-gpg-verify',
            'remote-name', 'file:///srv/ostree/debian/repo'
        ],
                                         check=True)
    def test_should_parse_config_with_one_default_provisioner(self):
        json = '''{
            "url": "http://example.com",
            "ref": "ref",

            "default-provisioners": [
                {
                    "provisioner": "some-provisioner"
                }
            ]
        }'''
        cfg = Config.parse_json(StringIO(json))

        self.assertEqual(cfg.default_provisioners,
                         [ProvisionerConfig('some-provisioner', {})])
Beispiel #29
0
def should_not_create_stateroot_if_path_exists():
    sysroot = Path('/', 'mnt', 'rootfs')
    cfg = Config(Source.url('url'),
                 'ref',
                 stateroot='stateroot-name',
                 sysroot=str(sysroot))

    with ExitStack() as stack:
        exists = stack.enter_context(
            mock.patch('os.path.exists', mock.Mock(return_value=True)))
        run = stack.enter_context(
            mock.patch('deploy_ostree.steps.create_stateroot.run'))
        CreateStateroot(cfg).run()

    exists.assert_called_once_with('/mnt/rootfs/ostree/deploy/stateroot-name')
    run.assert_not_called()
Beispiel #30
0
    def test_should_add_ostree_remote_with_absolute_path(
            self, mock_run: mock.Mock):
        cfg = Config(Source.path('repo'),
                     'debian/9/i386/desktop',
                     remote='remote-name',
                     base_dir='ostree')
        repo_abs_path = os.path.join(os.getcwd(), 'ostree', 'repo')

        FileRemote(cfg).run()

        mock_run.assert_called_once_with([
            'ostree', 'remote', 'add',
            '--repo=%s' % os.path.join('/ostree', 'repo'), '--no-gpg-verify',
            'remote-name',
            'file://%s' % repo_abs_path
        ],
                                         check=True)