def test_should_raise_CouldNotUploadRpmsException(self):
        rpm_upload_command_before_test = get_rpm_upload_command()
        configuration.set_property(get_rpm_upload_command, "foobar")

        self.assertRaises(CouldNotUploadRpmsException, ConfigRpmMaker(None, None)._upload_rpms, [''])

        configuration.set_property(get_rpm_upload_command, rpm_upload_command_before_test)
    def test_should_not_clean_up_working_directory(self):

        configuration.set_property(is_no_clean_up_enabled, True)
        config_rpm_maker = self._given_config_rpm_maker()

        try:
            config_rpm_maker.build()
        except Exception:
            # the cleanup should be independent of the result of the build operation
            pass

        self.assert_path_exists(config_rpm_maker.work_dir)
        self.assert_path_exists(config_rpm_maker.error_log_file)
    def test_should_perform_chunked_uploads(self):
        old_config = get_rpm_upload_command()
        target_file = os.path.abspath(os.path.join(get_temporary_directory(), 'upload.txt'))
        if os.path.exists(target_file):
            os.remove(target_file)
        cmd_file = os.path.abspath(os.path.join(get_temporary_directory(), 'upload.sh'))
        with open(cmd_file, 'w') as f:
            f.write('#!/bin/bash\ndest=$1 ; shift ; echo "${#@} [email protected]" >> "$dest"')

        os.chmod(cmd_file, 0755)
        cmd = '%s %s' % (cmd_file, target_file)
        configuration.set_property(get_rpm_upload_command, cmd)
        try:
            ConfigRpmMaker(None, None)._upload_rpms(['a' for x in range(25)])
        finally:
            configuration.set_property(get_rpm_upload_command, old_config)

        self.assertTrue(os.path.exists(target_file))
        with open(target_file) as f:
            self.assertEqual(f.read(), '10 a a a a a a a a a a\n10 a a a a a a a a a a\n5 a a a a a\n')
    def test_should_build_rpms_for_hosts(self):

        configuration.set_property(is_no_clean_up_enabled, True)
        config_rpm_maker = self._given_config_rpm_maker()
        rpms = config_rpm_maker.build()

        self.assertEqual(12, len(rpms))

        hosts_to_check = {
            'devweb01': {},
            'tuvweb01': {},
            'berweb01': {
                'requires': ['all-req', 'all-req2', 'ber-req', 'ber-req2', 'host-spec-requirement', 'pro-req', 'ty-web-requirement'],
                'provides': ['all-prov', 'all-prov2', 'all-prov3', 'pro-prov', 'pro-prov2', 'typ-web-provides'],
                'files': {'files/file_from_all': '',
                          'files/file_from_ber': '',
                          'files/file_from_pro': '',
                          'files/override': 'berweb',
                          'vars/override': 'berweb',
                          'vars/var_in_var': 'berwebberweb'},
                'symlinks': {'symlinks/link1': '/foo/bar'},
            },
        }

        for host in hosts_to_check:
            host_to_check = hosts_to_check[host]
            requires = host_to_check.get('requires', None)
            provides = host_to_check.get('provides', None)
            files = host_to_check.get('files', None)
            symlinks = host_to_check.get('symlinks', None)
            self.assertRpm(host, rpms, requires=requires, provides=provides, files=files, symlinks=symlinks)

        self.assertRpm("group-dev-web",
                       rpms,
                       requires=['all-req', 'all-req2', 'ty-web-requirement', 'yadt-config-group-dev-web-repos', 'yadt-minion'],
                       provides=['all-prov', 'all-prov2', 'all-prov3', 'typ-web-provides', 'yadt-config-all', 'yadt-config-group-dev-web'],
                       exhaustive=True)
    def test_should_create_files_for_hosts(self):

        configuration.set_property(is_no_clean_up_enabled, True)
        svn_service = SvnService(base_url=self.repo_url, path_to_config=get_svn_path_to_config())

        ConfigRpmMaker('2', svn_service).build()

        self.assert_config_viewer_file('berweb01', 'berweb01.overlaying', """host/berweb01:/VARIABLES
loctyp/berweb:/VARIABLES/OVERRIDE
loc/pro:/VARIABLES/RPM_PROVIDES
host/berweb01:/VARIABLES/RPM_REQUIRES
loctyp/berweb:/VARIABLES/VAR_IN_VAR
typ/web:/data
typ/web:/data/file-with-special-character
typ/web:/data/index.html
typ/web:/data/other.html
loctyp/berweb:/files
all:/files/binary.zip
all:/files/file_from_all
loc/ber:/files/file_from_ber
loc/pro:/files/file_from_pro
loctyp/berweb:/files/override
host/berweb01:/symlinks
host/berweb01:/symlinks/link1.%symlink
loctyp/berweb:/vars
all:/vars/override
loctyp/berweb:/vars/var_in_var
""")

        self.assert_config_viewer_file('berweb01', 'berweb01.variables', """<!DOCTYPE html><html><head><title>berweb01.variables</title></head><body><pre>                                 ALIASES :
                                     ALL : all
                                    FQDN : .+
                                    HOST : berweb01
                                  HOSTNR : 01
           INSTALL_PROTECTION_DEPENDENCY : hostname-<strong title="HOST">berweb01</strong>
                                      IP : .+
                                     LOC : ber
                                  LOCTYP : berweb
                                OVERRIDE : berweb
                                REVISION : 2
                                RPM_NAME : berweb01
                            RPM_PROVIDES : pro-prov2, all-prov2, all-prov, pro-prov, typ-web-provides, all-prov3
                            RPM_REQUIRES : ber-req2, pro-req, all-req, ber-req, ty-web-requirement, all-req2, host-spec-requirement
                  RPM_REQUIRES_NON_REPOS : ber-req2, pro-req, all-req, ber-req, ty-web-requirement, all-req2, host-spec-requirement
                      RPM_REQUIRES_REPOS :
                            SHORT_HOSTNR : 1
                                     TYP : web
                              VAR_IN_VAR : <strong title="LOC">ber</strong><strong title="TYP">web</strong><strong title="OVERRIDE">berweb</strong>
</pre></body></html>""")

        self.assert_host_files_are_there('berweb01')
        self.assert_config_viewer_file('berweb01', join('files', 'file_from_ber'), "")
        self.assert_config_viewer_file('berweb01', join('files', 'file_from_pro'), "")
        self.assert_config_viewer_file('berweb01', join('files', 'override'), "<!DOCTYPE html><html><head><title>override</title></head><body><pre>berweb</pre></body></html>")
        self.assert_config_viewer_file('berweb01', join('VARIABLES', 'LOC'), "<!DOCTYPE html><html><head><title>LOC</title></head><body><pre>ber</pre></body></html>")
        self.assert_config_viewer_file('berweb01', join('VARIABLES', 'LOCTYP'), "<!DOCTYPE html><html><head><title>LOCTYP</title></head><body><pre>berweb</pre></body></html>")
        self.assert_config_viewer_file('berweb01', join('VARIABLES', 'OVERRIDE'), "<!DOCTYPE html><html><head><title>OVERRIDE</title></head><body><pre>berweb</pre></body></html>")
        self.assert_config_viewer_file('berweb01', join('VARIABLES', 'RPM_PROVIDES'), "<!DOCTYPE html><html><head><title>RPM_PROVIDES</title></head><body><pre>pro-prov2, all-prov2, all-prov, pro-prov, typ-web-provides, all-prov3</pre></body></html>")
        self.assert_config_viewer_file('berweb01', join('VARIABLES', 'RPM_REQUIRES'), "<!DOCTYPE html><html><head><title>RPM_REQUIRES</title></head><body><pre>ber-req2, pro-req, all-req, ber-req, ty-web-requirement, all-req2, host-spec-requirement</pre></body></html>")
        self.assert_config_viewer_file('berweb01', join('VARIABLES', 'RPM_REQUIRES_NON_REPOS'), "<!DOCTYPE html><html><head><title>RPM_REQUIRES_NON_REPOS</title></head><body><pre>ber-req2, pro-req, all-req, ber-req, ty-web-requirement, all-req2, host-spec-requirement</pre></body></html>")
        self.assert_config_viewer_file('berweb01', join('VARIABLES', 'RPM_REQUIRES_REPOS'), "")
        self.assert_config_viewer_file('berweb01', join('VARIABLES', 'VAR_IN_VAR'), '''<!DOCTYPE html><html><head><title>VAR_IN_VAR</title></head><body><pre><strong title="LOC">ber</strong><strong title="TYP">web</strong><strong title="OVERRIDE">berweb</strong></pre></body></html>''')
        self.assert_config_viewer_file('berweb01', join('vars', 'override'), '''<!DOCTYPE html><html><head><title>override</title></head><body><pre><strong title="OVERRIDE">berweb</strong></pre></body></html>''')
        self.assert_config_viewer_file('berweb01', join('vars', 'var_in_var'), '''<!DOCTYPE html><html><head><title>var_in_var</title></head><body><pre><strong title="VAR_IN_VAR">berwebberweb</strong></pre></body></html>''')

        self.assert_host_files_are_there('devweb01')
        self.assert_config_viewer_file('devweb01', join('files', 'override'), "<!DOCTYPE html><html><head><title>override</title></head><body><pre>all</pre></body></html>")
        self.assert_config_viewer_file('devweb01', join('VARIABLES', 'LOC'), "<!DOCTYPE html><html><head><title>LOC</title></head><body><pre>dev</pre></body></html>")
        self.assert_config_viewer_file('devweb01', join('VARIABLES', 'LOCTYP'), "<!DOCTYPE html><html><head><title>LOCTYP</title></head><body><pre>devweb</pre></body></html>")
        self.assert_config_viewer_file('devweb01', join('VARIABLES', 'OVERRIDE'), "<!DOCTYPE html><html><head><title>OVERRIDE</title></head><body><pre>all</pre></body></html>")
        self.assert_config_viewer_file('devweb01', join('VARIABLES', 'RPM_PROVIDES'), "<!DOCTYPE html><html><head><title>RPM_PROVIDES</title></head><body><pre>typ-web-provides, all-prov, all-prov2, all-prov3</pre></body></html>")
        self.assert_config_viewer_file('devweb01', join('VARIABLES', 'RPM_REQUIRES'), "<!DOCTYPE html><html><head><title>RPM_REQUIRES</title></head><body><pre>all-req2, all-req, ty-web-requirement</pre></body></html>")
        self.assert_config_viewer_file('devweb01', join('VARIABLES', 'RPM_REQUIRES_NON_REPOS'), "<!DOCTYPE html><html><head><title>RPM_REQUIRES_NON_REPOS</title></head><body><pre>all-req2, all-req, ty-web-requirement</pre></body></html>")
        self.assert_config_viewer_file('devweb01', join('VARIABLES', 'RPM_REQUIRES_REPOS'), "")
        self.assert_config_viewer_file('devweb01', join('vars', 'override'), '''<!DOCTYPE html><html><head><title>override</title></head><body><pre><strong title="OVERRIDE">all</strong></pre></body></html>''')

        self.assert_config_viewer_file('devweb01', 'devweb01.overlaying', """host/devweb01:/VARIABLES
host/devweb01:/VARIABLES/DUMMY_VAR1
all:/VARIABLES/OVERRIDE
typ/web:/VARIABLES/RPM_PROVIDES
typ/web:/VARIABLES/RPM_REQUIRES
typ/web:/data
typ/web:/data/file-with-special-character
typ/web:/data/index.html
typ/web:/data/other.html
all:/files
all:/files/binary.zip
all:/files/file_from_all
all:/files/override
host/devweb01:/host_specific_file
all:/vars
all:/vars/override
""")

        self.assert_config_viewer_file('devweb01', 'devweb01.variables', """<!DOCTYPE html><html><head><title>devweb01.variables</title></head><body><pre>                                 ALIASES :
                                     ALL : all
                              DUMMY_VAR1 :
                                    FQDN : .+
                                    HOST : devweb01
                                  HOSTNR : 01
           INSTALL_PROTECTION_DEPENDENCY : hostname-<strong title="HOST">devweb01</strong>
                                      IP : .+
                                     LOC : dev
                                  LOCTYP : devweb
                                OVERRIDE : all
                                REVISION : 2
                                RPM_NAME : devweb01
                            RPM_PROVIDES : typ-web-provides, all-prov, all-prov2, all-prov3
                            RPM_REQUIRES : all-req2, all-req, ty-web-requirement
                  RPM_REQUIRES_NON_REPOS : all-req2, all-req, ty-web-requirement
                      RPM_REQUIRES_REPOS :
                            SHORT_HOSTNR : 1
                                     TYP : web
</pre></body></html>""")

        self.assert_host_files_are_there('tuvweb01')

        self.assert_config_viewer_file_exactly('tuvweb01', 'tuvweb01.overlaying', """host/tuvweb01:/VARIABLES
host/tuvweb01:/VARIABLES/DUMMY_VAR2
all:/VARIABLES/OVERRIDE
typ/web:/VARIABLES/RPM_PROVIDES
typ/web:/VARIABLES/RPM_REQUIRES
typ/web:/data
typ/web:/data/file-with-special-character
typ/web:/data/index.html
typ/web:/data/other.html
all:/files
all:/files/binary.zip
all:/files/file_from_all
all:/files/override
all:/vars
all:/vars/override
""")

        self.assert_config_viewer_file('tuvweb01', 'tuvweb01.variables', """<!DOCTYPE html><html><head><title>tuvweb01.variables</title></head><body><pre>                                 ALIASES :
                                     ALL : all
                              DUMMY_VAR2 :
                                    FQDN : .+
                                    HOST : tuvweb01
                                  HOSTNR : 01
           INSTALL_PROTECTION_DEPENDENCY : hostname-<strong title="HOST">tuvweb01</strong>
                                      IP : .+
                                     LOC : tuv
                                  LOCTYP : tuvweb
                                OVERRIDE : all
                                REVISION : 2
                                RPM_NAME : tuvweb01
                            RPM_PROVIDES : typ-web-provides, all-prov, all-prov2, all-prov3
                            RPM_REQUIRES : all-req2, all-req, ty-web-requirement
                  RPM_REQUIRES_NON_REPOS : all-req2, all-req, ty-web-requirement
                      RPM_REQUIRES_REPOS :
                            SHORT_HOSTNR : 1
                                     TYP : web
</pre></body></html>""")

        self.assert_config_viewer_file('tuvweb01', join('files', 'override'), "<!DOCTYPE html><html><head><title>override</title></head><body><pre>all</pre></body></html>")
        self.assert_config_viewer_file('tuvweb01', join('VARIABLES', 'LOC'), "<!DOCTYPE html><html><head><title>LOC</title></head><body><pre>tuv</pre></body></html>")
        self.assert_config_viewer_file('tuvweb01', join('VARIABLES', 'LOCTYP'), "<!DOCTYPE html><html><head><title>LOCTYP</title></head><body><pre>tuvweb</pre></body></html>")
        self.assert_config_viewer_file('tuvweb01', join('VARIABLES', 'OVERRIDE'), "<!DOCTYPE html><html><head><title>OVERRIDE</title></head><body><pre>all</pre></body></html>")
        self.assert_config_viewer_file('tuvweb01', join('VARIABLES', 'RPM_PROVIDES'), "<!DOCTYPE html><html><head><title>RPM_PROVIDES</title></head><body><pre>typ-web-provides, all-prov, all-prov2, all-prov3</pre></body></html>")
        self.assert_config_viewer_file('tuvweb01', join('VARIABLES', 'RPM_REQUIRES'), "<!DOCTYPE html><html><head><title>RPM_REQUIRES</title></head><body><pre>all-req2, all-req, ty-web-requirement</pre></body></html>")
        self.assert_config_viewer_file('tuvweb01', join('VARIABLES', 'RPM_REQUIRES_NON_REPOS'), "<!DOCTYPE html><html><head><title>RPM_REQUIRES_NON_REPOS</title></head><body><pre>all-req2, all-req, ty-web-requirement</pre></body></html>")
        self.assert_config_viewer_file('tuvweb01', join('VARIABLES', 'RPM_REQUIRES_REPOS'), "")
        self.assert_config_viewer_file('tuvweb01', join('vars', 'override'), '''<!DOCTYPE html><html><head><title>override</title></head><body><pre><strong title="OVERRIDE">all</strong></pre></body></html>''')