def test_string_config(self, custom_mock_cfg): cmc = MockChroot(config=custom_mock_cfg) output = cmc.chroot('/usr/bin/yum', '-q', 'list', 'google-chrome-beta') # If the package is not there, or any other error, yum will return a # nonzero value, which will raise an exception. So output will contain a # non blank string only if the package is found assert output
def test_chroot_by_target(self): mc = MockChroot(config=from_koji( target='rhevm-3.5-rhel-6-mead-candidate')) expected = ('Red Hat Enterprise Linux Server release 6.3 Beta ' '(Santiago)\nKernel \\r on an \\m\n\n') output = mc.chroot('cat', '/etc/issue') assert output == expected
def test__setup_mock_build_options(self, args, expected): if isinstance(expected, type) and issubclass(expected, Exception): with pytest.raises(expected): MockChroot._setup_mock_build_options(**args) else: output = MockChroot._setup_mock_build_options(**args) assert output == expected
def test_buildsrpm(self, monkeypatch): # Set to list to get it passed by reference to clusures checked_cmd = [[]] def check_output(cmd): checked_cmd[0] = list(cmd) return 'some output' monkeypatch.setattr(mock_chroot, 'check_output', check_output) mc = MockChroot(root='some_root') output = mc.buildsrpm(spec='some.spec', sources='/some/sources') expected = [ MockChroot.mock_exe(), '--root=some_root', '--buildsrpm', '--spec', 'some.spec', '--sources', '/some/sources', ] assert checked_cmd[0] == expected assert output == 'some output' output = mc.buildsrpm( spec='some.spec', sources='/some/sources', define='def1' ) expected = [ MockChroot.mock_exe(), '--root=some_root', '--buildsrpm', '--spec', 'some.spec', '--sources', '/some/sources', '--define', 'def1', ] assert checked_cmd[0] == expected assert output == 'some output'
def test_bind_mount(self, custom_mock_cfg, dir_to_mount): cmc = MockChroot(config=mock_chroot.config.compose( custom_mock_cfg, mock_chroot.config.bind_mount( dir_to_mount['pair']))) output = cmc.chroot('cat', dir_to_mount['test_file_path']) # If we got here, we've no exception so the file is there assert output
def test_use_host_resolv(self, custom_mock_cfg): host_name = 'www.google.com' cmc = MockChroot(config=mock_chroot.config.compose( custom_mock_cfg, mock_chroot.config.use_host_resolv())) output = cmc.chroot('getent', 'hosts', host_name) # If we got here, we've no exception so DNS worked assert output
def test_bind_mount(self, custom_mock_cfg, dir_to_mount): cmc = MockChroot(config=mock_chroot.config.compose( custom_mock_cfg, mock_chroot.config.bind_mount(dir_to_mount['pair']) )) output = cmc.chroot('cat', dir_to_mount['test_file_path']) # If we got here, we've no exception so the file is there assert output
def test_chroot_by_tag(self): mc = MockChroot(config=from_koji(tag='rhevm-3.5-rhel-6-mead-build')) expected = ( 'Red Hat Enterprise Linux Server release 6.3 Beta ' '(Santiago)\nKernel \\r on an \\m\n\n' ) output = mc.chroot('cat', '/etc/issue') assert output == expected
def test_env_vars(self, custom_mock_cfg): var_name = 'MY_VAR' var_value = 'my value' cmc = MockChroot(config=mock_chroot.config.compose( custom_mock_cfg, mock_chroot.config.env_vars(**{var_name: var_value}))) output = cmc.chroot('bash', '-c', 'echo "${0}"'.format(var_name)) # If we got here, we've no exception so the file is there assert output.rstrip() == var_value
def test_use_host_resolv(self, custom_mock_cfg): host_name = 'www.google.com' cmc = MockChroot(config=mock_chroot.config.compose( custom_mock_cfg, mock_chroot.config.use_host_resolv() )) output = cmc.chroot('getent', 'hosts', host_name) # If we got here, we've no exception so DNS worked assert output
def test_file(self, custom_mock_cfg): file_path = '/a_generated_file.txt' file_content = 'just some generated text' cmc = MockChroot(config=mock_chroot.config.compose( custom_mock_cfg, mock_chroot.config.file(file_path, file_content))) # Must clean the chroot for files to be created cmc.clean() output = cmc.chroot('cat', file_path) # If we got here, we've no exception so the file is there assert output == file_content
def test_env_vars(self, custom_mock_cfg): var_name = 'MY_VAR' var_value = 'my value' cmc = MockChroot(config=mock_chroot.config.compose( custom_mock_cfg, mock_chroot.config.env_vars(**{var_name: var_value}) )) output = cmc.chroot('bash', '-c', 'echo "${0}"'.format(var_name)) # If we got here, we've no exception so the file is there assert output.rstrip() == var_value
def test_file(self, custom_mock_cfg): file_path = '/a_generated_file.txt' file_content = 'just some generated text' cmc = MockChroot(config=mock_chroot.config.compose( custom_mock_cfg, mock_chroot.config.file(file_path, file_content) )) # Must clean the chroot for files to be created cmc.clean() output = cmc.chroot('cat', file_path) # If we got here, we've no exception so the file is there assert output == file_content
def build_in_mock(self, branch): """Build the sources in self.package_workdir as rhpkg woulb build them if they were pushed to the specified distgit branch The following environment variables can affect how this method works: - KOJI_TOPURL: Specifies a Koji package mirror to use - KOJI_PROFILE: Specifies which Koji configuration profile to use (defaults to 'brew') """ print("Building branch '{0}' in Mock".format(branch)) out_dir = os.path.join( self.builder.rpmbuild_basedir, "{0}-{1}".format(self.project_name, self.builder.build_version), branch ) getoutput("mkdir -p %s" % out_dir) print("build output will be written to {0}".format(out_dir)) # Logic taken from pyrpkg sources: target = "%s-candidate" % branch mock_conf = mock_config.compose( mock_config.from_koji( target=target, topurl=os.environ.get("KOJI_TOPURL", None), koji_profile=os.environ.get("KOJI_PROFILE", "brew"), ), mock_config.to["resultdir"].set(out_dir), mock_config.to["root_cache_enable"].set(True), mock_config.to["yum_cache_enable"].set(True), ) extra_yum_config = self._build_extra_yum_config(branch) if extra_yum_config: print("Injecting extra yum configuration to Mock") mock_conf.append(mock_config.to["yum.conf"].add(extra_yum_config)) debug("Dumping mock configuration") debug(str(mock_conf)) mock = MockChroot(config=mock_conf) print("Building SRPM in Mock") mock.buildsrpm(spec=self.builder.spec_file, sources=self.package_workdir) srpms = glob("{0}/*.src.rpm".format(out_dir)) if len(srpms) == 0: raise RuntimeError("no srpms found in {0}".format(out_dir)) elif len(srpms) > 1: raise RuntimeError("multiple srpms found in {0}".format(out_dir)) else: srpm = srpms[0] print("Building RPM in Mock") mock.rebuild(src_rpm=srpm, no_clean=True)
def test_rebuild(self, monkeypatch): # Set to list to get it passed by reference to clusures checked_cmd = [[]] def check_output(cmd): checked_cmd[0] = list(cmd) return 'some output' monkeypatch.setattr(mock_chroot, 'check_output', check_output) mc = MockChroot(root='some_root') output = mc.rebuild(src_rpm='some.src.rpm') expected = [ MockChroot.mock_exe(), '--root=some_root', '--rebuild', 'some.src.rpm' ] assert checked_cmd[0] == expected assert output == 'some output' mc.rebuild(src_rpm='some.src.rpm', define='def1') expected = [ MockChroot.mock_exe(), '--root=some_root', '--rebuild', 'some.src.rpm', '--define', 'def1', ] assert checked_cmd[0] == expected assert output == 'some output'
def test_clean(self): mc = MockChroot(root='epel-6-x86_64') expected = 'testing\n' output = mc.chroot('bash', '-c', 'echo testing | tee test.txt') assert output == expected output = mc.chroot('cat', 'test.txt') assert output == expected mc.clean() with pytest.raises(CalledProcessError): output = mc.chroot('cat', 'test.txt') expected = 'not there\n' output = mc.chroot('bash', '-c', 'test -e test.txt || echo not there') assert output == expected
def test_chroot(self): mc = MockChroot(root='epel-6-x86_64') expected = 'CentOS release 6.7 (Final)\nKernel \\r on an \\m\n\n' output = mc.chroot('cat', '/etc/issue') assert output == expected expected = 'testing\n' output = mc.chroot('bash', '-c', 'echo testing | tee test.txt') assert output == expected output = mc.chroot('cat', 'test.txt') assert output == expected with pytest.raises(CalledProcessError): output = mc.chroot('false') # the following will raise an exception if we're in the wrong directory output = mc.chroot('cat', 'hosts', cwd='etc')
def build_in_mock(self, branch): """Build the sources in self.package_workdir as rhpkg woulb build them if they were pushed to the specified distgit branch The following environment variables can affect how this method works: - KOJI_TOPURL: Specifies a Koji package mirror to use - KOJI_PROFILE: Specifies which Koji configuration profile to use (defaults to 'brew') """ print("Building branch '{0}' in Mock".format(branch)) out_dir = os.path.join( self.builder.rpmbuild_basedir, '{0}-{1}'.format(self.project_name, self.builder.build_version), branch) getoutput("mkdir -p %s" % out_dir) print('build output will be written to {0}'.format(out_dir)) # Logic taken from pyrpkg sources: target = '%s-candidate' % branch mock_conf = mock_config.compose( mock_config.from_koji( target=target, topurl=os.environ.get('KOJI_TOPURL', None), koji_profile=os.environ.get('KOJI_PROFILE', 'brew'), ), mock_config.to['resultdir'].set(out_dir), mock_config.to['root_cache_enable'].set(True), mock_config.to['yum_cache_enable'].set(True)) extra_yum_config = self._build_extra_yum_config(branch) if extra_yum_config: print('Injecting extra yum configuration to Mock') mock_conf.append(mock_config.to['yum.conf'].add(extra_yum_config)) debug('Dumping mock configuration') debug(str(mock_conf)) mock = MockChroot(config=mock_conf) print('Building SRPM in Mock') mock.buildsrpm( spec=self.builder.spec_file, sources=self.package_workdir, ) srpms = glob('{0}/*.src.rpm'.format(out_dir)) if len(srpms) == 0: raise RuntimeError('no srpms found in {0}'.format(out_dir)) elif len(srpms) > 1: raise RuntimeError('multiple srpms found in {0}'.format(out_dir)) else: srpm = srpms[0] print('Building RPM in Mock') mock.rebuild(src_rpm=srpm, no_clean=True)
def test_buildsrpm(self, monkeypatch): # Set to list to get it passed by reference to clusures checked_cmd = [[]] def check_output(cmd): checked_cmd[0] = list(cmd) return 'some output' monkeypatch.setattr(mock_chroot, 'check_output', check_output) mc = MockChroot(root='some_root') output = mc.buildsrpm(spec='some.spec', sources='/some/sources') expected = [ MockChroot.mock_exe(), '--root=some_root', '--buildsrpm', '--spec', 'some.spec', '--sources', '/some/sources', ] assert checked_cmd[0] == expected assert output == 'some output' output = mc.buildsrpm(spec='some.spec', sources='/some/sources', define='def1') expected = [ MockChroot.mock_exe(), '--root=some_root', '--buildsrpm', '--spec', 'some.spec', '--sources', '/some/sources', '--define', 'def1', ] assert checked_cmd[0] == expected assert output == 'some output'
def test_init_bad_params(self, custom_mock_cfg): with pytest.raises(RuntimeError): MockChroot(root='epel-6-x86_64', config=custom_mock_cfg)
class TestMockChroot(object): @pytest.mark.parametrize(('init_args', 'more_args', 'expected'), [ ( dict(root='a_root_file'), ('foo', 'bar'), (MockChroot.mock_exe(), '--root=a_root_file', 'foo', 'bar'), ), ]) def test__mock_cmd(self, init_args, more_args, expected): mock = MockChroot(**init_args) result = mock._mock_cmd(*more_args) assert result == expected def test_chroot(self): mc = MockChroot(root='epel-6-x86_64') expected = 'CentOS release 6.7 (Final)\nKernel \\r on an \\m\n\n' output = mc.chroot('cat', '/etc/issue') assert output == expected expected = 'testing\n' output = mc.chroot('bash', '-c', 'echo testing | tee test.txt') assert output == expected output = mc.chroot('cat', 'test.txt') assert output == expected with pytest.raises(CalledProcessError): output = mc.chroot('false') # the following will raise an exception if we're in the wrong directory output = mc.chroot('cat', 'hosts', cwd='etc') def test_string_config(self, custom_mock_cfg): cmc = MockChroot(config=custom_mock_cfg) output = cmc.chroot('/usr/bin/yum', '-q', 'list', 'google-chrome-beta') # If the package is not there, or any other error, yum will return a # nonzero value, which will raise an exception. So output will contain a # non blank string only if the package is found assert output def test_init_bad_params(self, custom_mock_cfg): with pytest.raises(RuntimeError): MockChroot(root='epel-6-x86_64', config=custom_mock_cfg) def test_clean(self): mc = MockChroot(root='epel-6-x86_64') expected = 'testing\n' output = mc.chroot('bash', '-c', 'echo testing | tee test.txt') assert output == expected output = mc.chroot('cat', 'test.txt') assert output == expected mc.clean() with pytest.raises(CalledProcessError): output = mc.chroot('cat', 'test.txt') expected = 'not there\n' output = mc.chroot('bash', '-c', 'test -e test.txt || echo not there') assert output == expected @pytest.mark.parametrize(('args', 'expected'), [ ( dict(no_clean=True), ['--no-clean'], ), ( dict(no_clean=False), [], ), ( dict(define='def1'), ['--define', 'def1'], ), ( dict(define=('def1', 'def2')), ['--define', 'def1', '--define', 'def2'], ), ( dict(resultdir='/a/dir'), ['--resultdir', '/a/dir'], ), ( dict(define=7), TypeError, ), ( dict(no_clean=False, define='def1', resultdir='/a/dir'), ['--define', 'def1', '--resultdir', '/a/dir'], ), ]) def test__setup_mock_build_options(self, args, expected): if isinstance(expected, type) and issubclass(expected, Exception): with pytest.raises(expected): MockChroot._setup_mock_build_options(**args) else: output = MockChroot._setup_mock_build_options(**args) assert output == expected def test_rebuild(self, monkeypatch): # Set to list to get it passed by reference to clusures checked_cmd = [[]] def check_output(cmd): checked_cmd[0] = list(cmd) return 'some output' monkeypatch.setattr(mock_chroot, 'check_output', check_output) mc = MockChroot(root='some_root') output = mc.rebuild(src_rpm='some.src.rpm') expected = [ MockChroot.mock_exe(), '--root=some_root', '--rebuild', 'some.src.rpm' ] assert checked_cmd[0] == expected assert output == 'some output' mc.rebuild(src_rpm='some.src.rpm', define='def1') expected = [ MockChroot.mock_exe(), '--root=some_root', '--rebuild', 'some.src.rpm', '--define', 'def1', ] assert checked_cmd[0] == expected assert output == 'some output' def test_buildsrpm(self, monkeypatch): # Set to list to get it passed by reference to clusures checked_cmd = [[]] def check_output(cmd): checked_cmd[0] = list(cmd) return 'some output' monkeypatch.setattr(mock_chroot, 'check_output', check_output) mc = MockChroot(root='some_root') output = mc.buildsrpm(spec='some.spec', sources='/some/sources') expected = [ MockChroot.mock_exe(), '--root=some_root', '--buildsrpm', '--spec', 'some.spec', '--sources', '/some/sources', ] assert checked_cmd[0] == expected assert output == 'some output' output = mc.buildsrpm(spec='some.spec', sources='/some/sources', define='def1') expected = [ MockChroot.mock_exe(), '--root=some_root', '--buildsrpm', '--spec', 'some.spec', '--sources', '/some/sources', '--define', 'def1', ] assert checked_cmd[0] == expected assert output == 'some output'
def test__mock_cmd(self, init_args, more_args, expected): mock = MockChroot(**init_args) result = mock._mock_cmd(*more_args) assert result == expected