def test_adds_release_codename_title_and_support_eol(self): source = factory.make_BootSource(keyring_data=b"1234") os = factory.make_name("os") release = factory.make_name("release") release_codename = factory.make_name("codename") release_title = factory.make_name("title") support_eol = factory.make_date().strftime("%Y-%m-%d") image_spec = make_image_spec(os=os, release=release) image_mapping = BootImageMapping() image_mapping.setdefault( image_spec, { "release_codename": release_codename, "release_title": release_title, "support_eol": support_eol, }, ) mock_download = self.patch(bootsources, "download_all_image_descriptions") mock_download.return_value = image_mapping cache_boot_sources() cached = BootSourceCache.objects.filter(boot_source=source).first() self.assertEqual(release_codename, cached.release_codename) self.assertEqual(release_title, cached.release_title) self.assertEqual(support_eol, cached.support_eol.strftime("%Y-%m-%d"))
def test_adds_title_with_gadget_to_extra(self): source = factory.make_BootSource(keyring_data=b"1234") os = factory.make_name("os") release = factory.make_name("release") os_title = factory.make_name("os_title") release_title = factory.make_name("release_title") gadget_title = factory.make_name("gadget_title") image_spec = make_image_spec(os=os, release=release) image_mapping = BootImageMapping() image_mapping.setdefault( image_spec, { "os_title": os_title, "release_title": release_title, "gadget_title": gadget_title, }, ) mock_download = self.patch(bootsources, "download_all_image_descriptions") mock_download.return_value = image_mapping cache_boot_sources() cached = BootSourceCache.objects.filter(boot_source=source).first() self.assertDictEqual( {"title": "%s %s %s" % (os_title, release_title, gadget_title)}, cached.extra, )
def test_catches_connection_errors_and_sets_component_error(self): sources = [ factory.make_BootSource(keyring_data=b"1234") for _ in range(3) ] download_image_descriptions = self.patch( download_descriptions_module, "download_image_descriptions" ) error_text_one = factory.make_name("<error1>") error_text_two = factory.make_name("<error2>") # Make two of the downloads fail. download_image_descriptions.side_effect = [ ConnectionError(error_text_one), BootImageMapping(), IOError(error_text_two), ] cache_boot_sources() base_error = "Failed to import images from {url}: {err}" error_part_one = base_error.format( url=sources[0].url, err=html.escape(error_text_one) ) error_part_two = base_error.format( url=sources[2].url, err=html.escape(error_text_two) ) expected_error = error_part_one + "<br>" + error_part_two actual_error = get_persistent_error(COMPONENT.REGION_IMAGE_IMPORT) self.assertEqual(expected_error, actual_error)
def test_has_env_GNUPGHOME_set(self): capture = patch_and_capture_env_for_download_all_image_descriptions( self) factory.make_BootSource(keyring_data=b"1234") cache_boot_sources() self.assertEqual(bootsources.get_maas_user_gpghome(), capture.env["GNUPGHOME"])
def test_notifies_missing_commissioning_os(self): cache_boot_sources() self.assertTrue( Notification.objects.filter( ident="commissioning_series_unselected").exists()) self.assertTrue( Notification.objects.filter( ident="commissioning_series_unavailable").exists())
def test_returns_clears_entire_cache(self): source = factory.make_BootSource(keyring_data=b"1234") factory.make_BootSourceCache(source) mock_download = self.patch(bootsources, "download_all_image_descriptions") mock_download.return_value = make_boot_image_mapping() cache_boot_sources() self.assertEqual(0, BootSourceCache.objects.all().count())
def test__passes_user_agent_with_maas_version(self): mock_download = self.patch(bootsources, 'download_all_image_descriptions') factory.make_BootSource(keyring_data=b'1234') cache_boot_sources() self.assertThat( mock_download, MockCalledOnceWith(ANY, user_agent=get_maas_user_agent()))
def test_clears_component_error_when_successful(self): register_persistent_error(COMPONENT.REGION_IMAGE_IMPORT, factory.make_string()) [factory.make_BootSource(keyring_data=b"1234") for _ in range(3)] download_image_descriptions = self.patch( download_descriptions_module, "download_image_descriptions") # Make all of the downloads successful. download_image_descriptions.return_value = BootImageMapping() cache_boot_sources() self.assertIsNone(get_persistent_error(COMPONENT.REGION_IMAGE_IMPORT))
def test__doesnt_have_env_http_and_https_proxy_set_if_disabled(self): proxy_address = factory.make_name('proxy') Config.objects.set_config('http_proxy', proxy_address) Config.objects.set_config('enable_http_proxy', False) capture = ( patch_and_capture_env_for_download_all_image_descriptions(self)) factory.make_BootSource(keyring_data=b'1234') cache_boot_sources() self.assertEqual(("", ""), (capture.env.get( 'http_proxy', ''), capture.env.get('https_proxy', '')))
def test__has_env_http_and_https_proxy_set(self): proxy_address = factory.make_name('proxy') Config.objects.set_config('http_proxy', proxy_address) capture = ( patch_and_capture_env_for_download_all_image_descriptions(self)) factory.make_BootSource(keyring_data=b'1234') cache_boot_sources() self.assertEqual((proxy_address, proxy_address, '127.0.0.1,localhost'), (capture.env['http_proxy'], capture.env['https_proxy'], capture.env['no_proxy']))
def test__has_env_http_and_https_proxy_set_with_custom_no_proxy(self): proxy_address = factory.make_name('proxy') Config.objects.set_config('http_proxy', proxy_address) Config.objects.set_config('boot_images_no_proxy', True) capture = ( patch_and_capture_env_for_download_all_image_descriptions(self)) factory.make_BootSource(keyring_data=b'1234', url=b'http://192.168.1.100:8080/ephemeral-v3/') cache_boot_sources() no_proxy_hosts = '127.0.0.1,localhost,192.168.1.100' self.assertEqual((proxy_address, proxy_address, no_proxy_hosts), (capture.env['http_proxy'], capture.env['https_proxy'], capture.env['no_proxy']))
def test_has_env_http_and_https_proxy_set(self): proxy_address = factory.make_name("proxy") Config.objects.set_config("http_proxy", proxy_address) capture = patch_and_capture_env_for_download_all_image_descriptions( self) factory.make_BootSource(keyring_data=b"1234") cache_boot_sources() self.assertEqual( (proxy_address, proxy_address, "127.0.0.1,localhost"), ( capture.env["http_proxy"], capture.env["https_proxy"], capture.env["no_proxy"], ), )
def test__returns_adds_entries_to_cache_for_source(self): source = factory.make_BootSource(keyring_data=b'1234') os = factory.make_name('os') releases = [factory.make_name('release') for _ in range(3)] image_specs = [ make_image_spec(os=os, release=release) for release in releases] mock_download = self.patch( bootsources, 'download_all_image_descriptions') mock_download.return_value = make_boot_image_mapping(image_specs) cache_boot_sources() cached_releases = [ cache.release for cache in BootSourceCache.objects.filter(boot_source=source) if cache.os == os ] self.assertItemsEqual(releases, cached_releases)
def test_has_env_http_and_https_proxy_set_with_custom_no_proxy(self): proxy_address = factory.make_name("proxy") Config.objects.set_config("http_proxy", proxy_address) Config.objects.set_config("boot_images_no_proxy", True) capture = patch_and_capture_env_for_download_all_image_descriptions( self) factory.make_BootSource(keyring_data=b"1234", url="http://192.168.1.100:8080/ephemeral-v3/") cache_boot_sources() no_proxy_hosts = "127.0.0.1,localhost,192.168.1.100" self.assertEqual( (proxy_address, proxy_address, no_proxy_hosts), ( capture.env["http_proxy"], capture.env["https_proxy"], capture.env["no_proxy"], ), )
def test__adds_title_to_extra(self): source = factory.make_BootSource(keyring_data=b'1234') os = factory.make_name('os') release = factory.make_name('release') os_title = factory.make_name('os_title') release_title = factory.make_name('release_title') image_spec = make_image_spec(os=os, release=release) image_mapping = BootImageMapping() image_mapping.setdefault(image_spec, { 'os_title': os_title, 'release_title': release_title, }) mock_download = self.patch(bootsources, 'download_all_image_descriptions') mock_download.return_value = image_mapping cache_boot_sources() cached = BootSourceCache.objects.filter(boot_source=source).first() self.assertDictEqual({'title': '%s %s' % (os_title, release_title)}, cached.extra)