def test_warns_if_no_boot_resources_found(self):
        # The import code used to crash when no resources were found in the
        # Simplestreams repositories (bug 1305758).  This could happen easily
        # with mistakes in the sources.  Now, you just get a logged warning.
        sources_fixture = self.useFixture(
            BootSourcesFixture([{
                "url":
                self.make_dir(),
                "keyring":
                factory.make_name("keyring"),
                "selections": [{
                    "release": factory.make_name("release")
                }],
            }]))
        self.patch(boot_resources, "download_all_image_descriptions")
        boot_resources.download_all_image_descriptions.return_value = (
            BootImageMapping())
        self.patch_maaslog()
        self.patch(boot_resources, "RepoWriter")
        args = self.make_args(sources_file=sources_fixture.filename)

        boot_resources.main(args)

        self.assertThat(
            boot_resources.maaslog.warning,
            MockAnyCall(
                "Finished importing boot images, the region does not have "
                "any boot images available."),
        )
Exemple #2
0
    def test_warns_if_no_sources_selected(self):
        self.patch_maaslog()
        sources_fixture = self.useFixture(BootSourcesFixture([]))
        args = self.make_args(sources_file=sources_fixture.filename)

        boot_resources.main(args)

        self.assertThat(
            boot_resources.maaslog.warning,
            MockAnyCall("Can't import: region did not provide a source."))
    def test_successful_run(self):
        """Integration-test a successful run of the importer.

        This runs as much realistic code as it can, exercising most of the
        integration points for a real import.
        """
        # Patch out things that we don't want running during the test.  Patch
        # at a low level, so that we exercise all the function calls that a
        # unit test might not put to the test.
        self.patch_maaslog()
        self.patch(boot_resources, "call_and_check")
        self.patch(boot_resources, "service_monitor")

        # We'll go through installation of a PXE boot loader here, but skip
        # all other boot loader types.  Testing them all is a job for proper
        # unit tests.
        for method_name, boot_method in BootMethodRegistry:
            if method_name != "pxe":
                self.patch(boot_method, "install_bootloader")

        args = self.make_working_args()
        osystem = self.os
        arch = self.arch
        subarch = self.subarch
        kflavor = self.kflavor
        release = self.release
        label = self.label

        # Run the import code.
        boot_resources.main(args)

        # Verify the results.
        self.assertThat(os.path.join(self.storage, "cache"), DirExists())
        current = os.path.join(self.storage, "current")
        self.assertTrue(os.path.islink(current))
        self.assertThat(current, DirExists())
        self.assertThat(os.path.join(current, "pxelinux.0"), FileExists())
        self.assertThat(os.path.join(current, "maas.meta"), FileExists())
        self.assertThat(
            os.path.join(current, osystem, arch, subarch, self.release,
                         self.label),
            DirExists(),
        )

        # Verify the contents of the "meta" file.
        meta_file_path = os.path.join(current, "maas.meta")
        with open(meta_file_path, "r", encoding="ascii") as meta_file:
            meta_data = json.load(meta_file)
        self.assertEqual([osystem], list(meta_data))
        self.assertEqual([arch], list(meta_data[osystem]))
        self.assertEqual([subarch], list(meta_data[osystem][arch]))
        self.assertEqual([kflavor], list(meta_data[osystem][arch][subarch]))
        self.assertEqual([release],
                         list(meta_data[osystem][arch][subarch][kflavor]))
        self.assertEqual(
            [label], list(meta_data[osystem][arch][subarch][kflavor][release]))
        self.assertItemsEqual(
            [
                "content_id",
                "kflavor",
                "path",
                "product_name",
                "version_name",
                "subarches",
            ],
            list(meta_data[osystem][arch][subarch][kflavor][release][label]),
        )