'Test repacking all binaries.'

@test_lib.RequiresPackage('grr-response-templates')
def testRepackAll(self):

    self.executables_dir = config_lib.Resource().Filter('executables')
    with utils.TempDirectory() as tmp_dir:
        new_dir = os.path.join(tmp_dir, 'grr', 'executables')
        os.makedirs(new_dir)
        shutil.copy(os.path.join(self.executables_dir, 'windows/templates/unzipsfx/unzipsfx-i386.exe'), new_dir)
        shutil.copy(os.path.join(self.executables_dir, 'windows/templates/unzipsfx/unzipsfx-amd64.exe'), new_dir)
        with test_lib.ConfigOverrider({'ClientBuilder.executables_dir': new_dir, 'ClientBuilder.unzipsfx_stub_dir': new_dir}):
            repacking.TemplateRepacker().RepackAllTemplates()
        self.assertEqual(len(glob.glob(os.path.join(new_dir, 'installers/*.deb'))), 2)
        self.assertEqual(len(glob.glob(os.path.join(new_dir, 'installers/*.rpm'))), 2)
        self.assertEqual(len(glob.glob(os.path.join(new_dir, 'installers/*.exe'))), 4)
        self.assertEqual(len(glob.glob(os.path.join(new_dir, 'installers/*.pkg'))), 1)
        zf = zipfile.ZipFile(glob.glob(os.path.join(new_dir, 'installers/*.pkg')).pop(), mode='r')
        fd = zf.open('config.yaml')
        loaded = yaml.safe_load(fd)
        loaded.pop('Config.includes')
        packaged_config = config.CONFIG.MakeNewConfig()
        packaged_config.Initialize(parser=config_lib.YamlParser, data=yaml.safe_dump(loaded))
        packaged_config.Validate(sections=build.ClientRepacker.CONFIG_SECTIONS)
        repacker = build.ClientRepacker()
        repacker.ValidateEndConfig(packaged_config)
