def test_writes_source_keyrings(self): # Stop import_images() from actually doing anything. self.patch(boot_resources, "maaslog") self.patch(boot_resources, "try_send_rack_event") self.patch(boot_resources, "call_and_check") self.patch(boot_resources, "download_all_boot_resources") self.patch(boot_resources, "download_all_image_descriptions") self.patch(boot_resources, "install_boot_loaders") self.patch(boot_resources, "update_current_symlink") self.patch(boot_resources, "write_snapshot_metadata") self.patch(boot_resources, "write_targets_conf") self.patch(boot_resources, "update_targets_conf") fake_write_all_keyrings = self.patch(boot_resources, "write_all_keyrings") sources = ([{ "keyring_data": self.getUniqueString(), "url": factory.make_name("something"), "selections": [{ "os": factory.make_name("os"), "release": factory.make_name("release"), "arches": [factory.make_name("arch")], "subarches": [factory.make_name("subarch")], "labels": [factory.make_name("label")], }], }], ) boot_resources.import_images(sources) self.assertThat(fake_write_all_keyrings, MockCalledWith(mock.ANY, sources))
def test_writes_source_keyrings(self): # Stop import_images() from actually doing anything. self.patch(boot_resources, 'maaslog') self.patch(boot_resources, 'try_send_rack_event') self.patch(boot_resources, 'call_and_check') self.patch(boot_resources, 'download_all_boot_resources') self.patch(boot_resources, 'download_all_image_descriptions') self.patch(boot_resources, 'install_boot_loaders') self.patch(boot_resources, 'update_current_symlink') self.patch(boot_resources, 'write_snapshot_metadata') self.patch(boot_resources, 'write_targets_conf') self.patch(boot_resources, 'update_targets_conf') fake_write_all_keyrings = self.patch(boot_resources, 'write_all_keyrings') sources = [ { 'keyring_data': self.getUniqueString(), 'url': factory.make_name("something"), 'selections': [ { 'os': factory.make_name("os"), 'release': factory.make_name("release"), 'arches': [factory.make_name("arch")], 'subarches': [factory.make_name("subarch")], 'labels': [factory.make_name("label")], }, ], }, ], boot_resources.import_images(sources) self.assertThat(fake_write_all_keyrings, MockCalledWith(mock.ANY, sources))
def _run_import(sources, maas_url, http_proxy=None, https_proxy=None): """Run the import. This is function is synchronous so it must be called with deferToThread. """ # Fix the sources to download from the IP address defined in the cluster # configuration, instead of the URL that the region asked it to use. sources = fix_sources_for_cluster(sources, maas_url) variables = {"GNUPGHOME": get_maas_user_gpghome()} if http_proxy is not None: variables["http_proxy"] = http_proxy if https_proxy is not None: variables["https_proxy"] = https_proxy # Communication to the sources and loopback should not go through proxy. no_proxy_hosts = [ "localhost", "::ffff:127.0.0.1", "127.0.0.1", "::1", "[::ffff:127.0.0.1]", "[::1]", ] no_proxy_hosts += list(get_hosts_from_sources(sources)) variables["no_proxy"] = ",".join(no_proxy_hosts) with environment_variables(variables): imported = boot_resources.import_images(sources) # Update the boot images cache so `list_boot_images` returns the # correct information. reload_boot_images() # Tell callers if anything happened. return imported
def test_returns_false_when_no_new_images(self): # Stop import_images() from actually doing anything. self.patch(boot_resources, "maaslog") fake_download_all_image_descriptions = self.patch( boot_resources, "download_all_image_descriptions") fake_image_descriptions = MagicMock() fake_image_descriptions.is_empty.return_value = False fake_download_all_image_descriptions.return_value = ( fake_image_descriptions) self.patch(boot_resources, "meta_contains").return_value = True self.patch(boot_resources, "write_all_keyrings") sources = ([{ "keyring_data": self.getUniqueString(), "url": factory.make_name("something"), "selections": [{ "os": factory.make_name("os"), "release": factory.make_name("release"), "arches": [factory.make_name("arch")], "subarches": [factory.make_name("subarch")], "labels": [factory.make_name("label")], }], }], ) self.assertFalse(boot_resources.import_images(sources))
def test__returns_false_when_no_new_images(self): # Stop import_images() from actually doing anything. self.patch(boot_resources, 'maaslog') fake_download_all_image_descriptions = self.patch( boot_resources, 'download_all_image_descriptions') fake_image_descriptions = MagicMock() fake_image_descriptions.is_empty.return_value = False fake_download_all_image_descriptions.return_value = ( fake_image_descriptions) self.patch(boot_resources, 'meta_contains').return_value = True fake_update_iscsi_targets = self.patch(boot_resources, 'update_iscsi_targets') self.patch(boot_resources, 'write_all_keyrings') sources = [ { 'keyring_data': self.getUniqueString(), 'url': factory.make_name("something"), 'selections': [ { 'os': factory.make_name("os"), 'release': factory.make_name("release"), 'arches': [factory.make_name("arch")], 'subarches': [factory.make_name("subarch")], 'labels': [factory.make_name("label")], }, ], }, ], self.assertFalse(boot_resources.import_images(sources)) self.assertThat(fake_update_iscsi_targets, MockCalledOnce())
def test__returns_false_when_no_images(self): # Stop import_images() from actually doing anything. self.patch(boot_resources, 'maaslog') fake_download_all_image_descriptions = self.patch( boot_resources, 'download_all_image_descriptions') fake_download_all_image_descriptions.return_value = MagicMock() self.patch(boot_resources, 'write_all_keyrings') sources = [ { 'keyring_data': self.getUniqueString(), 'url': factory.make_name("something"), 'selections': [ { 'os': factory.make_name("os"), 'release': factory.make_name("release"), 'arches': [factory.make_name("arch")], 'subarches': [factory.make_name("subarch")], 'labels': [factory.make_name("label")], }, ], }, ], self.assertFalse(boot_resources.import_images(sources))
def test__runs_import_and_returns_true(self): # Stop import_images() from actually doing anything. self.patch(boot_resources, 'maaslog') fake_download_all_image_descriptions = self.patch( boot_resources, 'download_all_image_descriptions') fake_image_descriptions = MagicMock() fake_image_descriptions.is_empty.return_value = False fake_download_all_image_descriptions.return_value = ( fake_image_descriptions) self.patch(boot_resources, 'meta_contains').return_value = False self.patch(boot_resources, 'map_products') self.patch(boot_resources, 'download_all_boot_resources') fake_write_snapshot_metadata = self.patch(boot_resources, 'write_snapshot_metadata') fake_targets_conf = self.patch(boot_resources, 'write_targets_conf') fake_link_bootloaders = self.patch(boot_resources, 'link_bootloaders') fake_update_current_symlink = self.patch(boot_resources, 'update_current_symlink') fake_update_iscsi_targets = self.patch(boot_resources, 'update_iscsi_targets') fake_cleanup_snapshots_and_cache = self.patch( boot_resources, 'cleanup_snapshots_and_cache') self.patch(boot_resources, 'write_all_keyrings') sources = [ { 'keyring_data': self.getUniqueString(), 'url': factory.make_name("something"), 'selections': [ { 'os': factory.make_name("os"), 'release': factory.make_name("release"), 'arches': [factory.make_name("arch")], 'subarches': [factory.make_name("subarch")], 'labels': [factory.make_name("label")], }, ], }, ], self.assertTrue(boot_resources.import_images(sources)) self.assertThat(fake_write_snapshot_metadata, MockCalledOnce()) self.assertThat(fake_targets_conf, MockCalledOnce()) self.assertThat(fake_link_bootloaders, MockCalledOnce()) self.assertThat(fake_update_current_symlink, MockCalledOnce()) self.assertThat(fake_update_iscsi_targets, MockCalledOnce()) self.assertThat(fake_cleanup_snapshots_and_cache, MockCalledOnce())
def test_runs_import_and_returns_true(self): # Stop import_images() from actually doing anything. self.patch(boot_resources, "maaslog") fake_download_all_image_descriptions = self.patch( boot_resources, "download_all_image_descriptions") fake_image_descriptions = MagicMock() fake_image_descriptions.is_empty.return_value = False fake_download_all_image_descriptions.return_value = ( fake_image_descriptions) self.patch(boot_resources, "meta_contains").return_value = False self.patch(boot_resources, "map_products") self.patch(boot_resources, "download_all_boot_resources") fake_write_snapshot_metadata = self.patch(boot_resources, "write_snapshot_metadata") fake_link_bootloaders = self.patch(boot_resources, "link_bootloaders") fake_update_current_symlink = self.patch(boot_resources, "update_current_symlink") fake_cleanup_snapshots_and_cache = self.patch( boot_resources, "cleanup_snapshots_and_cache") self.patch(boot_resources, "write_all_keyrings") sources = ([{ "keyring_data": self.getUniqueString(), "url": factory.make_name("something"), "selections": [{ "os": factory.make_name("os"), "release": factory.make_name("release"), "arches": [factory.make_name("arch")], "subarches": [factory.make_name("subarch")], "labels": [factory.make_name("label")], }], }], ) self.assertTrue(boot_resources.import_images(sources)) self.assertThat(fake_write_snapshot_metadata, MockCalledOnce()) self.assertThat(fake_link_bootloaders, MockCalledOnce()) self.assertThat(fake_update_current_symlink, MockCalledOnce()) self.assertThat(fake_cleanup_snapshots_and_cache, MockCalledOnce())