def test_integration(self):
        tftproot = self.make_dir()
        config = {"tftp": {"root": tftproot}}
        config_fixture = ConfigFixture(config)
        self.useFixture(config_fixture)

        download_dir = self.make_dir()
        image_dir = os.path.join(download_dir, 'image')
        os.makedirs(image_dir)
        factory.make_file(image_dir, 'kernel')
        arch, subarch, release, purpose = make_arch_subarch_release_purpose()

        action = factory.make_name("action")
        script = MainScript(action)
        script.register(action, provisioningserver.pxe.install_image)
        script.execute(
            ("--config-file", config_fixture.filename, action, "--arch", arch,
             "--subarch", subarch, "--release", release, "--purpose", purpose,
             "--image", image_dir))

        self.assertThat(
            os.path.join(
                locate_tftp_path(
                    compose_image_path(arch, subarch, release, purpose),
                    tftproot=tftproot),
                'kernel'),
            FileExists())
Beispiel #2
0
 def test_find_image(self):
     osystem = make_osystem(self, factory.make_name("os"))
     tmpdir = self.make_dir()
     arch = factory.make_name("arch")
     subarch = factory.make_name("subarch")
     release = factory.make_name("release")
     label = factory.make_name("label")
     dir_path = os.path.join(tmpdir, osystem.name, arch, subarch, release,
                             label)
     os.makedirs(dir_path)
     if self.fname:
         factory.make_file(dir_path, self.fname)
     self.useFixture(ClusterConfigurationFixture(tftp_root=tmpdir))
     self.assertItemsEqual(
         self.expected,
         osystem._find_image(
             arch,
             subarch,
             release,
             label,
             tgz=self.tgz,
             dd=self.dd,
             squashfs=self.squashfs,
         ),
     )
Beispiel #3
0
    def test_move_file_by_glob_fails_if_multiple_files_match(self):
        source_dir = self.make_dir()
        factory.make_file(source_dir)
        factory.make_file(source_dir)

        self.assertRaises(AssertionError, move_file_by_glob, source_dir, '*',
                          self.make_dir(), factory.make_name())
Beispiel #4
0
    def test_link_bootloader_copies_previously_downloaded_files(self):
        method = PXEBootMethod()
        with tempdir() as tmp:
            new_dir = os.path.join(tmp, "new")
            current_dir = os.path.join(tmp, "current")
            os.makedirs(new_dir)
            os.makedirs(current_dir)
            factory.make_file(current_dir, method.bootloader_files[0])
            for bootloader_file in method.bootloader_files[1:]:
                factory.make_file(current_dir, bootloader_file)
            real_syslinux_dir = os.path.join(tmp, "syslinux")
            os.makedirs(real_syslinux_dir)
            atomic_symlink(
                real_syslinux_dir, os.path.join(current_dir, "syslinux")
            )

            method.link_bootloader(new_dir)

            for bootloader_file in method.bootloader_files:
                bootloader_file_path = os.path.join(new_dir, bootloader_file)
                self.assertTrue(os.path.isfile(bootloader_file_path))
            syslinux_link = os.path.join(new_dir, "syslinux")
            self.assertTrue(os.path.islink(syslinux_link))
            self.assertEqual(
                real_syslinux_dir, os.path.realpath(syslinux_link)
            )
Beispiel #5
0
 def test_get_snippet_context(self):
     contents = factory.getRandomString()
     snippets_dir = self.make_dir()
     factory.make_file(snippets_dir, 'snippet.py', contents=contents)
     self.assertItemsEqual(
         {'snippet_py': contents},
         get_snippet_context(snippets_dir=snippets_dir))
Beispiel #6
0
 def test_get_snippet_context(self):
     contents = factory.make_string()
     snippets_dir = self.make_dir()
     factory.make_file(snippets_dir, "snippet.py", contents=contents)
     snippets = get_snippet_context(snippets_dir=snippets_dir)
     self.assertItemsEqual(["base_user_data_sh", "snippet_py"],
                           snippets.keys())
     self.assertEqual(contents, snippets["snippet_py"])
 def test_install_bootloader_sweeps_aside_dot_new_if_any(self):
     contents = factory.getRandomString()
     loader = self.make_file(contents=contents)
     dest = self.make_file(contents="Old contents")
     temp_file = '%s.new' % dest
     factory.make_file(
         os.path.dirname(temp_file), name=os.path.basename(temp_file))
     install_bootloader(loader, dest)
     self.assertThat(dest, FileContains(contents))
Beispiel #8
0
 def test_sweeps_aside_dot_new_if_any(self):
     contents = factory.make_bytes()
     loader = self.make_file(contents=contents)
     dest = self.make_file(contents="Old contents")
     temp_file = "%s.new" % dest
     factory.make_file(os.path.dirname(temp_file),
                       name=os.path.basename(temp_file))
     atomic_copy(loader, dest)
     self.assertThat(dest, FileContains(contents))
Beispiel #9
0
 def test_are_identical_dirs_returns_false_if_file_was_removed(self):
     shared_file = factory.getRandomString()
     contents = factory.getRandomString()
     old = os.path.dirname(
         self.make_file(name=shared_file, contents=contents))
     new = os.path.dirname(
         self.make_file(name=shared_file, contents=contents))
     factory.make_file(old)
     self.assertFalse(are_identical_dirs(old, new))
Beispiel #10
0
 def test_install_bootloader_sweeps_aside_dot_new_if_any(self):
     contents = factory.getRandomString()
     loader = self.make_file(contents=contents)
     dest = self.make_file(contents="Old contents")
     temp_file = '%s.new' % dest
     factory.make_file(os.path.dirname(temp_file),
                       name=os.path.basename(temp_file))
     install_bootloader(loader, dest)
     self.assertThat(dest, FileContains(contents))
    def test_move_file_by_glob_fails_if_multiple_files_match(self):
        source_dir = self.make_dir()
        factory.make_file(source_dir)
        factory.make_file(source_dir)

        self.assertRaises(
            AssertionError,
            move_file_by_glob,
            source_dir, '*', self.make_dir(), factory.make_name())
Beispiel #12
0
 def test_are_identical_dirs_returns_false_if_file_was_removed(self):
     shared_file = factory.getRandomString()
     contents = factory.getRandomString()
     old = os.path.dirname(
         self.make_file(name=shared_file, contents=contents))
     new = os.path.dirname(
         self.make_file(name=shared_file, contents=contents))
     factory.make_file(old)
     self.assertFalse(are_identical_dirs(old, new))
 def test_set_up_dns_does_not_overwrite_config(self):
     dns_conf_dir = self.make_dir()
     self.patch(conf, 'DNS_CONFIG_DIR', dns_conf_dir)
     random_content = factory.getRandomString()
     factory.make_file(location=dns_conf_dir,
                       name=MAAS_NAMED_CONF_NAME,
                       contents=random_content)
     call_command('set_up_dns', no_clobber=True)
     self.assertThat(os.path.join(dns_conf_dir, MAAS_NAMED_CONF_NAME),
                     FileContains(random_content))
Beispiel #14
0
    def test_clean_up_info_file_removes_old_info_failed_file(self):
        target_dir = self.make_dir()
        info_file = factory.make_file(target_dir, 'info')
        info_failed_file = factory.make_file(target_dir, 'info.failed')
        read_text_file(info_failed_file)
        new_text = read_text_file(info_file)

        clean_up_info_file(target_dir)

        self.assertThat(info_failed_file, FileContains(new_text))
Beispiel #15
0
    def test_clean_up_info_file_removes_old_info_failed_file(self):
        target_dir = self.make_dir()
        info_file = factory.make_file(target_dir, 'info')
        info_failed_file = factory.make_file(target_dir, 'info.failed')
        read_text_file(info_failed_file)
        new_text = read_text_file(info_file)

        clean_up_info_file(target_dir)

        self.assertThat(info_failed_file, FileContains(new_text))
Beispiel #16
0
 def test_set_up_options_conf_write_config_allows_single_override(self):
     dns_conf_dir = patch_dns_config_path(self)
     factory.make_file(
         location=dns_conf_dir, name=NAMED_CONF_OPTIONS,
         contents=NAMED_CONF_OPTIONS_WITH_ALLOW_QUERY_CONTENTS)
     set_up_options_conf()
     target_file = os.path.join(
         dns_conf_dir, MAAS_NAMED_CONF_OPTIONS_INSIDE_NAME)
     target = read_isc_file(target_file)
     self.assertIsNone(target.get('allow-query'))
Beispiel #17
0
    def test_install_symlink_overwrites_existing_dir(self):
        images = self.make_dir()
        installed_image = os.path.join(images, 'installed-image')
        os.makedirs(installed_image)
        linked_image = os.path.join(images, 'linked-image')
        os.makedirs(linked_image)
        factory.make_file(linked_image, 'obsolete-file')

        install_symlink(installed_image, linked_image)

        self.assertEqual(installed_image, os.readlink(linked_image))
 def test_set_up_dns_does_not_overwrite_config(self):
     dns_conf_dir = self.make_dir()
     self.patch(conf, 'DNS_CONFIG_DIR', dns_conf_dir)
     random_content = factory.getRandomString()
     factory.make_file(
         location=dns_conf_dir, name=MAAS_NAMED_CONF_NAME,
         contents=random_content)
     call_command('set_up_dns', no_clobber=True)
     self.assertThat(
         os.path.join(dns_conf_dir, MAAS_NAMED_CONF_NAME),
         FileContains(random_content))
Beispiel #19
0
    def test_overwrites_existing_obsolete_file(self):
        text = factory.getRandomString()
        legacy_config = self.make_legacy_config(text)
        obsolete_config = legacy_config + '.obsolete'
        factory.make_file(os.path.dirname(obsolete_config),
                          os.path.basename(obsolete_config))

        retire_legacy_config()

        self.assertThat(legacy_config, Not(FileExists()))
        self.assertThat(obsolete_config, FileContains(text))
Beispiel #20
0
 def test_atomic_symlink_overwrites_dest_file(self):
     filename = self.make_file(contents=factory.make_string())
     target_dir = self.make_dir()
     link_name = factory.make_name("link")
     # Create a file that will be overwritten.
     factory.make_file(location=target_dir, name=link_name)
     target = os.path.join(target_dir, link_name)
     atomic_symlink(filename, target)
     self.assertTrue(os.path.islink(target),
                     "atomic_symlink didn't create a symlink")
     self.assertThat(target, SamePath(filename))
Beispiel #21
0
 def test_does_not_overwrite_config(self):
     dns_conf_dir = self.make_dir()
     patch_dns_config_path(self, dns_conf_dir)
     random_content = factory.make_string()
     factory.make_file(
         location=dns_conf_dir, name=MAAS_NAMED_CONF_NAME,
         contents=random_content)
     self.run_command('--no-clobber')
     self.assertThat(
         os.path.join(dns_conf_dir, MAAS_NAMED_CONF_NAME),
         FileContains(random_content))
Beispiel #22
0
    def test_install_symlink_overwrites_existing_dir(self):
        images = self.make_dir()
        installed_image = os.path.join(images, 'installed-image')
        os.makedirs(installed_image)
        linked_image = os.path.join(images, 'linked-image')
        os.makedirs(linked_image)
        factory.make_file(linked_image, 'obsolete-file')

        install_symlink(installed_image, linked_image)

        self.assertEqual(installed_image, os.readlink(linked_image))
Beispiel #23
0
 def test_install_dir_replaces_existing_dir(self):
     download_image = os.path.join(self.make_dir(), 'download-image')
     published_image = os.path.join(self.make_dir(), 'published-image')
     os.makedirs(download_image)
     sample_file = factory.make_file(download_image)
     os.makedirs(published_image)
     obsolete_file = factory.make_file(published_image)
     install_dir(download_image, published_image)
     self.assertThat(
         os.path.join(published_image, os.path.basename(sample_file)),
         FileExists())
     self.assertThat(obsolete_file, Not(FileExists()))
Beispiel #24
0
    def test_install_symlink_sweeps_aside_dot_old(self):
        images = self.make_dir()
        installed_image = os.path.join(images, 'installed-image')
        os.makedirs(installed_image)
        linked_image = os.path.join(images, 'linked-image')
        os.makedirs(linked_image + '.old')
        factory.make_file(linked_image + '.old')

        install_symlink(installed_image, linked_image)

        self.assertEqual(installed_image, os.readlink(linked_image))
        self.assertThat(linked_image + '.old', Not(DirExists()))
Beispiel #25
0
    def test_overwrites_existing_obsolete_file(self):
        text = factory.getRandomString()
        legacy_config = self.make_legacy_config(text)
        obsolete_config = legacy_config + '.obsolete'
        factory.make_file(
            os.path.dirname(obsolete_config),
            os.path.basename(obsolete_config))

        retire_legacy_config()

        self.assertThat(legacy_config, Not(FileExists()))
        self.assertThat(obsolete_config, FileContains(text))
Beispiel #26
0
 def test_make_destination_returns_existing_directory(self):
     tftproot = self.make_dir()
     arch, subarch, release, purpose = make_arch_subarch_release_purpose()
     expected_dest = locate_tftp_path(
         compose_image_path(arch, subarch, release, purpose),
         tftproot=tftproot)
     os.makedirs(expected_dest)
     contents = factory.getRandomString()
     testfile = factory.make_name('testfile')
     factory.make_file(expected_dest, contents=contents, name=testfile)
     dest = make_destination(tftproot, arch, subarch, release, purpose)
     self.assertThat(os.path.join(dest, testfile), FileContains(contents))
Beispiel #27
0
 def test_make_destination_returns_existing_directory(self):
     tftproot = self.make_dir()
     arch, subarch, release, purpose = make_arch_subarch_release_purpose()
     expected_dest = locate_tftp_path(compose_image_path(
         arch, subarch, release, purpose),
                                      tftproot=tftproot)
     os.makedirs(expected_dest)
     contents = factory.getRandomString()
     testfile = factory.make_name('testfile')
     factory.make_file(expected_dest, contents=contents, name=testfile)
     dest = make_destination(tftproot, arch, subarch, release, purpose)
     self.assertThat(os.path.join(dest, testfile), FileContains(contents))
Beispiel #28
0
 def test_install_dir_replaces_existing_dir(self):
     download_image = os.path.join(self.make_dir(), 'download-image')
     published_image = os.path.join(self.make_dir(), 'published-image')
     os.makedirs(download_image)
     sample_file = factory.make_file(download_image)
     os.makedirs(published_image)
     obsolete_file = factory.make_file(published_image)
     install_dir(download_image, published_image)
     self.assertThat(
         os.path.join(published_image, os.path.basename(sample_file)),
         FileExists())
     self.assertThat(obsolete_file, Not(FileExists()))
Beispiel #29
0
 def make_image_dir(self, image_params, tftproot):
     """Fake a boot image matching `image_params` under `tftproot`."""
     image_dir = os.path.join(
         tftproot,
         compose_image_path(osystem=image_params['osystem'],
                            arch=image_params['architecture'],
                            subarch=image_params['subarchitecture'],
                            release=image_params['release'],
                            label=image_params['label']))
     os.makedirs(image_dir)
     factory.make_file(image_dir, 'linux')
     factory.make_file(image_dir, 'initrd.gz')
Beispiel #30
0
 def make_image_dir(self, image_params, tftproot):
     """Fake a boot image matching `image_params` under `tftproot`."""
     image_dir = locate_tftp_path(
         compose_image_path(
             arch=image_params['architecture'],
             subarch=image_params['subarchitecture'],
             release=image_params['release'],
             purpose=image_params['purpose']),
         tftproot)
     os.makedirs(image_dir)
     factory.make_file(image_dir, 'linux')
     factory.make_file(image_dir, 'initrd.gz')
Beispiel #31
0
 def test_write_config_skips_writing_if_overwrite_false(self):
     # If DNSConfig is created with overwrite=False, it won't
     # overwrite an existing config file.
     target_dir = patch_dns_config_path(self)
     random_content = factory.make_string()
     factory.make_file(location=target_dir,
                       name=MAAS_NAMED_CONF_NAME,
                       contents=random_content)
     dnsconfig = DNSConfig()
     dnsconfig.write_config(overwrite=False)
     self.assertThat(os.path.join(target_dir, MAAS_NAMED_CONF_NAME),
                     FileContains(random_content))
Beispiel #32
0
    def test_install_symlink_sweeps_aside_dot_old(self):
        images = self.make_dir()
        installed_image = os.path.join(images, 'installed-image')
        os.makedirs(installed_image)
        linked_image = os.path.join(images, 'linked-image')
        os.makedirs(linked_image + '.old')
        factory.make_file(linked_image + '.old')

        install_symlink(installed_image, linked_image)

        self.assertEqual(installed_image, os.readlink(linked_image))
        self.assertThat(linked_image + '.old', Not(DirExists()))
Beispiel #33
0
 def test_request_from_http(self):
     # We can't just call self.make_file because HTTPServerFixture will only
     # serve content from the current WD. And we don't want to create random
     # content in the original WD.
     self.useFixture(TempWDFixture())
     name = factory.getRandomString()
     content = factory.getRandomString().encode('ascii')
     factory.make_file(location='.', name=name, contents=content)
     with HTTPServerFixture() as httpd:
         url = urljoin(httpd.url, name)
         response = MAASDispatcher().dispatch_query(url, {})
         self.assertEqual(200, response.code)
         self.assertEqual(content, response.read())
Beispiel #34
0
 def test_request_from_http(self):
     # We can't just call self.make_file because HTTPServerFixture will only
     # serve content from the current WD. And we don't want to create random
     # content in the original WD.
     self.useFixture(TempWDFixture())
     name = factory.getRandomString()
     content = factory.getRandomString().encode('ascii')
     factory.make_file(location='.', name=name, contents=content)
     with HTTPServerFixture() as httpd:
         url = urljoin(httpd.url, name)
         response = MAASDispatcher().dispatch_query(url, {})
         self.assertEqual(200, response.code)
         self.assertEqual(content, response.read())
Beispiel #35
0
 def make_resource_path(self, files=[]):
     tmpdir = self.make_dir()
     arch = factory.make_name("arch")
     subarch = factory.make_name("subarch")
     release = factory.make_name("release")
     label = factory.make_name("label")
     dirpath = os.path.join(tmpdir, "windows", arch, subarch, release,
                            label)
     os.makedirs(dirpath)
     for fname in files:
         factory.make_file(dirpath, fname)
     self.useFixture(ClusterConfigurationFixture(tftp_root=tmpdir))
     return arch, subarch, release, label
Beispiel #36
0
 def test_set_up_options_conf_write_config_allows_overrides(self):
     dns_conf_dir = patch_dns_config_path(self)
     factory.make_file(location=dns_conf_dir,
                       name=NAMED_CONF_OPTIONS,
                       contents=NAMED_CONF_OPTIONS_CONTENTS)
     set_up_options_conf()
     target_file = os.path.join(dns_conf_dir,
                                MAAS_NAMED_CONF_OPTIONS_INSIDE_NAME)
     target = read_isc_file(target_file)
     self.assertThat([
         target.get('allow-query'),
         target.get('allow-recursion'),
         target.get('allow-query-cache'),
     ], AllMatch(Is(None)))
    def make_downloads(self, release=None, arch=None):
        """Set up a directory with an image for "download" by the script.

        Returns an "ARCHIVE" URL for the download.
        """
        if release is None:
            release = factory.make_name('release')
        archive = self.make_dir()
        basepath, download = compose_download_dir(archive, arch, release)
        os.makedirs(download)
        for filename in compose_download_filenames(arch, release):
            factory.make_file(download, filename)
        generate_md5sums(basepath)
        return archive
Beispiel #38
0
 def test_set_up_options_conf_write_config_allows_zero_overrides(self):
     dns_conf_dir = patch_dns_config_path(self)
     factory.make_file(location=dns_conf_dir,
                       name=NAMED_CONF_OPTIONS,
                       contents=NAMED_CONF_OPTIONS_NO_ALLOW_CONTENTS)
     set_up_options_conf()
     target_file = os.path.join(dns_conf_dir,
                                MAAS_NAMED_CONF_OPTIONS_INSIDE_NAME)
     target = read_isc_file(target_file)
     self.assertThat([
         target['allow-query']['any'],
         target['allow-recursion']['trusted'],
         target['allow-query-cache']['trusted'],
     ], AllMatch(Equals(True)))
Beispiel #39
0
 def test_write_config_skips_writing_if_overwrite_false(self):
     # If DNSConfig is created with overwrite=False, it won't
     # overwrite an existing config file.
     target_dir = self.make_dir()
     self.patch(DNSConfig, 'target_dir', target_dir)
     random_content = factory.getRandomString()
     factory.make_file(
         location=target_dir, name=MAAS_NAMED_CONF_NAME,
         contents=random_content)
     dnsconfig = DNSConfig()
     dnsconfig.write_config(overwrite=False)
     self.assertThat(
         os.path.join(target_dir, MAAS_NAMED_CONF_NAME),
         FileContains(random_content))
Beispiel #40
0
    def test_link_simplestream_bootloaders_creates_lpxelinux_and_links(self):
        method = PXEBootMethod()
        with tempdir() as tmp:
            stream_path = os.path.join(tmp, 'bootloader',
                                       method.bios_boot_method,
                                       method.bootloader_arches[0])
            os.makedirs(stream_path)
            for bootloader_file in method.bootloader_files:
                factory.make_file(stream_path, bootloader_file)

            method.link_bootloader(tmp)

            self.assertTrue(os.path.exists(os.path.join(tmp, 'lpxelinux.0')))
            self.assertTrue(os.path.islink(os.path.join(tmp, 'pxelinux.0')))
    def make_downloads(self, release=None, arch=None):
        """Set up a directory with an image for "download" by the script.

        Returns an "ARCHIVE" URL for the download.
        """
        if release is None:
            release = factory.make_name('release')
        archive = self.make_dir()
        basepath, download = compose_download_dir(archive, arch, release)
        os.makedirs(download)
        for filename in compose_download_filenames(arch, release):
            factory.make_file(download, filename)
        generate_md5sums(basepath)
        return archive
Beispiel #42
0
    def test__ignores_other_files(self):
        tarball = make_tarball_name()
        self.patch(uec2roottar, 'check_call')
        self.patch(uec2roottar, 'check_output')
        # Make the function think that it found two files in the tarball: an
        # image and some other file.
        image = make_image(self)
        working_dir = os.path.dirname(image)
        # This other file doesn't upset things, because it doesn't look like
        # an image file.
        factory.make_file(working_dir)

        self.assertEqual(
            image,
            uec2roottar.extract_image_from_tarball(tarball, working_dir))
Beispiel #43
0
 def make_resource_path(self, filename):
     self.useFixture(ClusterConfigurationFixture())
     tmpdir = self.make_dir()
     arch = factory.make_name('arch')
     subarch = factory.make_name('subarch')
     release = factory.make_name('release')
     label = factory.make_name('label')
     current_dir = os.path.join(tmpdir, 'current') + '/'
     dirpath = os.path.join(current_dir, 'custom', arch, subarch, release,
                            label)
     os.makedirs(dirpath)
     factory.make_file(dirpath, filename)
     with ClusterConfiguration.open_for_update() as config:
         config.tftp_root = current_dir
     return arch, subarch, release, label
Beispiel #44
0
 def make_resource_path(self, filename):
     self.useFixture(ClusterConfigurationFixture())
     tmpdir = self.make_dir()
     arch = factory.make_name("arch")
     subarch = factory.make_name("subarch")
     release = factory.make_name("release")
     label = factory.make_name("label")
     current_dir = os.path.join(tmpdir, "current") + "/"
     dirpath = os.path.join(current_dir, "custom", arch, subarch, release,
                            label)
     os.makedirs(dirpath)
     factory.make_file(dirpath, filename)
     with ClusterConfiguration.open_for_update() as config:
         config.tftp_root = current_dir
     return arch, subarch, release, label
Beispiel #45
0
    def test_link_bootloader_links_simplestream_bootloader_files(self):
        method = FakeBootMethod()
        with tempdir() as tmp:
            stream_path = os.path.join(tmp, 'bootloader',
                                       method.bios_boot_method,
                                       method.bootloader_arches[0])
            os.makedirs(stream_path)
            for bootloader_file in method.bootloader_files:
                factory.make_file(stream_path, bootloader_file)

            method.link_bootloader(tmp)

            for bootloader_file in method.bootloader_files:
                bootloader_file_path = os.path.join(tmp, bootloader_file)
                self.assertTrue(os.path.islink(bootloader_file_path))
Beispiel #46
0
    def test_link_bootloader_copies_previous_downloaded_files(self):
        method = FakeBootMethod()
        with tempdir() as tmp:
            new_dir = os.path.join(tmp, "new")
            current_dir = os.path.join(tmp, "current")
            os.makedirs(new_dir)
            os.makedirs(current_dir)
            for bootloader_file in method.bootloader_files:
                factory.make_file(current_dir, bootloader_file)

            method.link_bootloader(new_dir)

            for bootloader_file in method.bootloader_files:
                bootloader_file_path = os.path.join(new_dir, bootloader_file)
                self.assertTrue(os.path.isfile(bootloader_file_path))
Beispiel #47
0
 def setUp(self):
     super(TestOOPSService, self).setUp()
     # OOPSService relies upon LogService.
     self.tempdir = self.make_dir()
     self.log_filename = factory.make_file(
         location=self.tempdir, name="test.log")
     self.log_service = LogService(self.log_filename)
     self.log_service.setServiceParent(self.services)
Beispiel #48
0
    def make_file(self, name=None, contents=None):
        """Create, and write to, a file.

        This is a convenience wrapper around `make_dir` and a factory
        call.  It ensures that the file is in a directory that will be
        cleaned up at the end of the test.
        """
        return factory.make_file(self.make_dir(), name, contents)
Beispiel #49
0
 def test_doesnt_override_accept_encoding_headers(self):
     # If someone passes their own Accept-Encoding header, then dispatch
     # just passes it through.
     self.useFixture(TempWDFixture())
     name = factory.getRandomString()
     content = factory.getRandomString(300).encode('ascii')
     factory.make_file(location='.', name=name, contents=content)
     with HTTPServerFixture() as httpd:
         url = urljoin(httpd.url, name)
         headers = {'Accept-encoding': 'gzip'}
         res = MAASDispatcher().dispatch_query(url, headers)
         self.assertEqual(200, res.code)
         self.assertEqual('gzip', res.info().get('Content-Encoding'))
         raw_content = res.read()
     read_content = gzip.GzipFile(
         mode='rb', fileobj=BytesIO(raw_content)).read()
     self.assertEqual(content, read_content)
Beispiel #50
0
 def create_ephemeral_info(self, name, arch, release):
     """Create a pseudo-real ephemeral info file."""
     ephemeral_info = """
         release=%s
         stream=ephemeral
         label=release
         serial=20120424
         arch=%s
         name=%s
         """ % (release, arch, name)
     ephemeral_root = self.make_dir()
     config = {"boot": {"ephemeral": {"directory": ephemeral_root}}}
     self.useFixture(ConfigFixture(config))
     ephemeral_dir = os.path.join(
         ephemeral_root, release, 'ephemeral', arch, release)
     os.makedirs(ephemeral_dir)
     factory.make_file(
         ephemeral_dir, name='info', contents=ephemeral_info)
Beispiel #51
0
    def test_supports_any_method(self):
        # urllib2, which MAASDispatcher uses, only supports POST and
        # GET. There is some extra code that makes sure the passed
        # method is honoured which is tested here.
        self.useFixture(TempWDFixture())
        name = factory.getRandomString()
        content = factory.getRandomString(300).encode('ascii')
        factory.make_file(location='.', name=name, contents=content)

        method = "PUT"
        # The test httpd doesn't like PUT, so we'll look for it bitching
        # about that for the purposes of this test.
        with HTTPServerFixture() as httpd:
            url = urljoin(httpd.url, name)
            e = self.assertRaises(
                urllib2.HTTPError, MAASDispatcher().dispatch_query, url, {},
                method=method)
            self.assertIn("Unsupported method ('PUT')", e.reason)
Beispiel #52
0
 def test_install_dir_moves_dir_into_place(self):
     download_image = os.path.join(self.make_dir(), 'download-image')
     published_image = os.path.join(self.make_dir(), 'published-image')
     contents = factory.getRandomString()
     os.makedirs(download_image)
     sample_file = factory.make_file(download_image, contents=contents)
     install_dir(download_image, published_image)
     self.assertThat(
         os.path.join(published_image, os.path.basename(sample_file)),
         FileContains(contents))
Beispiel #53
0
 def test_get_pxe_template_gets_default_if_available(self):
     # If there is no template matching the purpose, arch, and subarch,
     # but there is a completely generic template, then get_pxe_template()
     # falls back to that as the default.
     templates_dir = self.make_fake_templates_dir()
     generic_template = factory.make_file(templates_dir, 'config.template')
     purpose = factory.make_name("purpose")
     arch, subarch = factory.make_names("arch", "subarch")
     self.assertEqual(
         generic_template,
         config.get_pxe_template(purpose, arch, subarch).name)
 def make_original_dir(self):
     """Create a directory with the kernel, initrd and root tarball."""
     original_dir = self.make_dir()
     factory.make_file(original_dir, 'linux')
     factory.make_file(original_dir, 'initrd.gz')
     factory.make_file(original_dir, 'dist-root.tar.gz')
     return original_dir
 def prepare_storage_dir(self):
     """Set up a storage directory with kernel, initrd, and root tarball."""
     storage = self.make_dir()
     factory.make_file(storage, 'linux')
     factory.make_file(storage, 'initrd.gz')
     factory.make_file(storage, 'dist-root.tar.gz')
     return storage
Beispiel #56
0
    def test_supports_content_encoding_gzip(self):
        # The client will set the Accept-Encoding: gzip header, and it will
        # also decompress the response if it comes back with Content-Encoding:
        # gzip.
        self.useFixture(TempWDFixture())
        name = factory.getRandomString()
        content = factory.getRandomString(300).encode('ascii')
        factory.make_file(location='.', name=name, contents=content)
        called = []
        orig_urllib = urllib2.urlopen

        def logging_urlopen(*args, **kwargs):
            called.append((args, kwargs))
            return orig_urllib(*args, **kwargs)
        self.patch(urllib2, 'urlopen', logging_urlopen)
        with HTTPServerFixture() as httpd:
            url = urljoin(httpd.url, name)
            res = MAASDispatcher().dispatch_query(url, {})
            self.assertEqual(200, res.code)
            self.assertEqual(content, res.read())
        request = called[0][0][0]
        self.assertEqual([((request,), {})], called)
        self.assertEqual('gzip', request.headers.get('Accept-encoding'))
Beispiel #57
0
    def test_install_dir_replaces_existing_symlink(self):
        download_image = os.path.join(self.make_dir(), 'download-image')
        published_image = os.path.join(self.make_dir(), 'published-image')
        linked_image = os.path.join(self.make_dir(), 'linked-image')
        os.makedirs(download_image)
        sample_file = factory.make_file(download_image)
        os.makedirs(published_image)
        os.symlink(published_image, linked_image)

        install_dir(download_image, linked_image)

        self.assertThat(linked_image, DirExists())
        self.assertThat(
            os.path.join(linked_image, os.path.basename(sample_file)),
            FileExists())
Beispiel #58
0
    def patch_template(self, template_content=sample_template):
        """Patch the DHCP config template with the given contents.

        Returns a `tempita.Template` of the given template, so that a test
        can make its own substitutions and compare to those made by the
        code being tested.
        """
        fake_etc_maas = self.make_dir()
        self.useFixture(EnvironmentVariableFixture(
            'MAAS_CONFIG_DIR', fake_etc_maas))
        template_dir = path.join(fake_etc_maas, 'templates', 'dhcp')
        makedirs(template_dir)
        template = factory.make_file(
            template_dir, 'dhcpd.conf.template', contents=template_content)
        return tempita.Template(template_content, name=template)
    def test_move_file_by_glob_ignores_nonmatching_files(self):
        content = factory.getRandomString()
        source_dir, source_name = split_path(self.make_file(contents=content))
        other_content = factory.getRandomString()
        other_file = factory.make_file(source_dir, contents=other_content)
        target_dir, target_name = self.make_target()

        move_file_by_glob(source_dir, source_name, target_dir, target_name)

        self.assertThat(other_file, FileContains(other_content))
        self.assertThat(
            os.path.join(target_dir, target_name),
            FileContains(content))
        self.assertItemsEqual(
            [os.path.basename(other_file)],
            os.listdir(source_dir))
        self.assertItemsEqual([target_name], os.listdir(target_dir))
Beispiel #60
0
 def test_install_dir_sweeps_aside_dot_new_and_dot_old_if_any(self):
     # If directories <old>.old or <old>.new already exist, they're
     # probably from an aborted previous run.  They won't stop
     # install_dir from doing its work.
     download_image = os.path.join(self.make_dir(), 'download-image')
     published_image = os.path.join(
         self.make_dir(), factory.getRandomString())
     contents = factory.getRandomString()
     os.makedirs(download_image)
     sample_file = factory.make_file(download_image, contents=contents)
     os.makedirs('%s.old' % published_image)
     os.makedirs('%s.new' % published_image)
     install_dir(download_image, published_image)
     self.assertThat(
         os.path.join(published_image, os.path.basename(sample_file)),
         FileContains(contents))
     self.assertThat('%s.old' % published_image, Not(DirExists()))
     self.assertThat('%s.new' % published_image, Not(DirExists()))