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_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'))
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'))
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_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)
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': '******' })
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_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_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)
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)
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)
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)
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)
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()
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_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={})
def test_should_deploy_commit_and_set_deployment_folder(self, run_mock): cfg = Config( Source.url('url'), 'fedora/28/x86_64/atomic-host', remote='fedora-atomic', stateroot='atomic-host', root_filesystem='/dev/mapper/atomic-root', ) Deploy(cfg).run() run_mock.assert_called_once_with([ 'ostree', 'admin', 'deploy', '--sysroot=/', '--os=atomic-host', 'fedora-atomic:fedora/28/x86_64/atomic-host', '--karg=root=/dev/mapper/atomic-root' ], check=True) self.assertEqual( cfg.deployment_dir, os.path.join('/ostree', 'deploy', 'atomic-host', 'deploy', 'abcdef.1'))
def test_should_deploy_into_specified_sysroot(self, listdir_mock, run_mock): sysroot = os.path.join('/mnt', 'rootfs') cfg = Config(Source.url('url'), 'ref', remote='remote', stateroot='test-stateroot', sysroot=sysroot, root_filesystem='/dev/mapper/atomic-root') listdir_mock.side_effect = [[], ['deploy', 'deploy.origin']] Deploy(cfg).run() listdir_mock.assert_called_with( os.path.join(sysroot, 'ostree', 'deploy', 'test-stateroot', 'deploy')) run_mock.assert_called_once_with([ 'ostree', 'admin', 'deploy', '--sysroot=%s' % sysroot, '--os=test-stateroot', 'remote:ref', '--karg=root=/dev/mapper/atomic-root' ], check=True)
def test_should_add_additional_kernel_args(self, run_mock): cfg = Config( Source.url('url'), 'ref', remote='remote', stateroot='os', kernel_args=['quiet', 'splash'], root_filesystem='/dev/rootfs', ) Deploy(cfg).run() run_mock.assert_called_once_with([ 'ostree', 'admin', 'deploy', '--sysroot=/', '--os=os', 'remote:ref', '--karg=root=/dev/rootfs', '--karg-append=quiet', '--karg-append=splash', ], check=True)
def test_remote_name_should_be_randomly_generated_if_not_specified(self): cfg1 = Config(Source.url('url'), 'ref') cfg2 = Config(Source.url('url'), 'ref') self.assertNotEqual(cfg1.remote, cfg2.remote)
def test_should_return_no_steps_if_no_default_provisioners(self): cfg = Config(Source.url('url'), 'ref', default_provisioners=[]) steps = get_steps(cfg) self.assertEqual(len(steps), 0)
def test_fstab_should_default_to_system_fstab(): cfg = Config(Source.url('url'), 'ref') assert cfg.fstab == Path('/', 'etc', 'fstab')
def test_default_root_filesystem_should_be_determined_by_get_root_fs(self): cfg = Config(Source.url('url'), 'ref') self.assertEqual(cfg.root_filesystem, '/dev/sda1')
def test_repo_dir_should_include_sysroot(self): sysroot = os.path.join('/mnt', 'rootfs') cfg = Config(Source.url('url'), 'ref', sysroot=sysroot) self.assertEqual(cfg.ostree_repo, os.path.join(sysroot, 'ostree', 'repo'))
def test_repo_dir_should_default_to_system_ostree_repo(self): cfg = Config(Source.url('url'), 'ref') self.assertEqual(cfg.ostree_repo, os.path.join('/ostree', 'repo'))
def test_var_dir_should_be_path_to_stateroot_var(self): cfg = Config(Source.url('url'), 'ref', stateroot='test-stateroot') self.assertEqual( cfg.var_dir, os.path.join('/ostree', 'deploy', 'test-stateroot', 'var'))
def test_deployment_path_should_raise_exception_if_name_is_not_set(self): cfg = Config(Source.url('url'), 'ref') with self.assertRaises(RuntimeError): cfg.deployment_dir
def test_stateroot_should_be_randomly_generated_if_not_specified(self): cfg1 = Config(Source.url('url'), 'ref') cfg2 = Config(Source.url('url'), 'ref') self.assertNotEqual(cfg1.stateroot, cfg2.stateroot)