def test_GET_returns_boot_source_list(self): self.become_admin() sources = [factory.make_BootSource() for _ in range(3)] response = self.client.get(reverse('boot_sources_handler')) self.assertEqual(http.client.OK, response.status_code, response.content) parsed_result = json_load_bytes(response.content) self.assertItemsEqual( [boot_source.id for boot_source in sources], [boot_source.get('id') for boot_source in parsed_result])
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_edits_boot_source_object(self): boot_source = factory.make_BootSource() params = { "url": "http://example.com/", "keyring_filename": factory.make_name("keyring_filename"), } form = BootSourceForm(instance=boot_source, data=params) self.assertTrue(form.is_valid(), form._errors) form.save() boot_source = reload_object(boot_source) self.assertAttributes(boot_source, params)
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_deleting_boot_source_deletes_its_selections(self): # BootSource deletion cascade-deletes related # BootSourceSelections. This is implicit in Django but it's # worth adding a test for it all the same. boot_source = factory.make_BootSource() boot_source_selection = factory.make_BootSourceSelection( boot_source=boot_source) boot_source.delete() self.assertNotIn( boot_source_selection.id, [selection.id for selection in BootSourceSelection.objects.all()])
def test_ensure_boot_source_definition_skips_if_already_present(self): sources = [ factory.make_BootSource() for _ in range(3) ] created = ensure_boot_source_definition() self.assertFalse( created, "Should have returned False signaling that the " "sources where not added.") self.assertItemsEqual(sources, BootSource.objects.all())
def test_PUT_updates_boot_source(self): self.become_admin() boot_source = factory.make_BootSource() new_values = { 'url': 'http://example.com/', 'keyring_filename': factory.make_name('filename'), } response = self.client.put(get_boot_source_uri(boot_source), new_values) self.assertEqual(http.client.OK, response.status_code) boot_source = reload_object(boot_source) self.assertAttributes(boot_source, new_values)
def test_to_dict_returns_dict(self): boot_source = factory.make_BootSource( keyring_data=b"123445", keyring_filename="" ) boot_source_selection = factory.make_BootSourceSelection( boot_source=boot_source ) boot_source_dict = boot_source.to_dict() self.assertEqual(boot_source.url, boot_source_dict["url"]) self.assertEqual( [boot_source_selection.to_dict()], boot_source_dict["selections"] )
def test_cannot_create_duplicate_entry(self): boot_source = factory.make_BootSource() params = self.make_valid_source_selection_params(boot_source) form = BootSourceSelectionForm(boot_source=boot_source, data=params) self.assertTrue(form.is_valid(), form._errors) form.save() # Duplicates should be detected for the same boot_source, os and # release, the other fields are irrelevant. dup_params = {"os": params["os"], "release": params["release"]} form = BootSourceSelectionForm(boot_source=boot_source, data=dup_params) self.assertRaises(ValidationError, form.save)
def test_validates_if_boot_source_cache_has_subarch(self): boot_source = factory.make_BootSource() os = factory.make_name('os') release = factory.make_name('release') boot_caches = self.make_some_caches(boot_source, os, release) # Request subarches that are in two of the cache records. params = { 'os': os, 'release': release, 'subarches': [boot_caches[0].subarch, boot_caches[2].subarch], } form = BootSourceSelectionForm(boot_source=boot_source, data=params) self.assertTrue(form.is_valid(), form._errors)
def test_star_values_in_request_validate_against_any_cache(self): boot_source = factory.make_BootSource() os = factory.make_name("os") release = factory.make_name("release") factory.make_BootSourceCache(boot_source, os=os, release=release) params = { "os": os, "release": release, "arches": ["*"], "subarches": ["*"], "labels": ["*"], } form = BootSourceSelectionForm(boot_source=boot_source, data=params) self.assertTrue(form.is_valid(), form._errors)
def test_star_values_in_request_validate_against_any_cache(self): boot_source = factory.make_BootSource() os = factory.make_name('os') release = factory.make_name('release') factory.make_BootSourceCache(boot_source, os=os, release=release) params = { 'os': os, 'release': release, 'arches': ['*'], 'subarches': ['*'], 'labels': ['*'], } form = BootSourceSelectionForm(boot_source=boot_source, data=params) self.assertTrue(form.is_valid(), form._errors)
def test_validates_if_boot_source_cache_has_label(self): boot_source = factory.make_BootSource() os = factory.make_name("os") release = factory.make_name("release") boot_caches = self.make_some_caches(boot_source, os, release) # Request labels that are in two of the cache records. params = { "os": os, "release": release, "labels": [boot_caches[0].label, boot_caches[2].label], } form = BootSourceSelectionForm(boot_source=boot_source, data=params) self.assertTrue(form.is_valid(), form._errors)
def test_rejects_if_boot_source_cache_has_different_os(self): boot_source = factory.make_BootSource() boot_cache = factory.make_BootSourceCache(boot_source) params = {"os": factory.make_name("os"), "release": boot_cache.release} form = BootSourceSelectionForm(boot_source=boot_source, data=params) self.assertFalse(form.is_valid()) self.assertEqual( { "os": [ "OS %s with release %s has no available images " "for download" % (params["os"], boot_cache.release) ] }, form._errors, )
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_POST_requires_admin(self): boot_source = factory.make_BootSource() new_release = factory.make_name('release') params = { 'release': new_release, 'arches': [factory.make_name('arch'), factory.make_name('arch')], 'subarches': [factory.make_name('subarch'), factory.make_name('subarch')], 'labels': [factory.make_name('label')], } response = self.client.post( reverse('boot_source_selections_handler', args=[boot_source.id]), params) self.assertEqual(http.client.FORBIDDEN, response.status_code)
def test_POST_requires_admin(self): boot_source = factory.make_BootSource() new_release = factory.make_name("release") params = { "release": new_release, "arches": [factory.make_name("arch"), factory.make_name("arch")], "subarches": [ factory.make_name("subarch"), factory.make_name("subarch"), ], "labels": [factory.make_name("label")], } response = self.client.post( reverse("boot_source_selections_handler", args=[boot_source.id]), params, ) self.assertEqual(http.client.FORBIDDEN, response.status_code)
def test_rejects_if_boot_source_cache_has_different_release(self): boot_source = factory.make_BootSource() boot_cache = factory.make_BootSourceCache(boot_source) params = { 'os': boot_cache.os, 'release': factory.make_name('release'), } form = BootSourceSelectionForm(boot_source=boot_source, data=params) self.assertFalse(form.is_valid()) self.assertEqual( { "os": [ "OS %s with release %s has no available images " "for download" % (boot_cache.os, params['release']) ] }, form._errors)
def test_GET_returns_boot_source_selection_list(self): self.become_admin() boot_source = factory.make_BootSource() selections = [ factory.make_BootSourceSelection(boot_source=boot_source) for _ in range(3) ] # Create boot source selections in another boot source. [factory.make_BootSourceSelection() for _ in range(3)] response = self.client.get( reverse('boot_source_selections_handler', args=[boot_source.id])) self.assertEqual(http.client.OK, response.status_code, response.content) parsed_result = json_load_bytes(response.content) self.assertItemsEqual( [selection.id for selection in selections], [selection.get('id') for selection in parsed_result])
def test__returns_sources_and_sets_of_releases_and_architectures(self): os = factory.make_name('os') sources = [ factory.make_BootSource(keyring_data='1234') for _ in range(2)] releases = set() arches = set() for source in sources: for _ in range(3): release = factory.make_name('release') arch = factory.make_name('arch') factory.make_BootSourceCache( source, os=os, release=release, arch=arch) releases.add(release) arches.add(arch) self.assertEqual( (sources, releases, arches), get_os_info_from_boot_sources(os))
def test_rejects_if_boot_source_cache_does_not_have_label(self): boot_source = factory.make_BootSource() os = factory.make_name('os') release = factory.make_name('release') factory.make_BootSourceCache(boot_source, os=os, release=release) params = { 'os': os, 'release': release, 'labels': [factory.make_name('label')], } form = BootSourceSelectionForm(boot_source=boot_source, data=params) self.assertFalse(form.is_valid()) self.assertEqual( { "labels": ["No available images to download for %s" % params['labels']] }, form._errors)
def test_to_dict_with_selections_returns_bootloaders(self): keyring_data = b'Some Keyring Data' boot_source = factory.make_BootSource(keyring_data=keyring_data, keyring_filename='') boot_source_selection = factory.make_BootSourceSelection( boot_source=boot_source) bootloaders = [] for arch in boot_source_selection.arches: bootloader_type = factory.make_name('bootloader-type') factory.make_BootSourceCache(boot_source=boot_source, bootloader_type=bootloader_type, release=bootloader_type, arch=arch) bootloaders.append(bootloader_type) self.assertItemsEqual([ selection['release'] for selection in boot_source.to_dict()['selections'] if 'bootloader-type' in selection['release'] ], bootloaders)
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)
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)
def test__adds_release_to_cache(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, }) bootsources._update_cache(source.to_dict_without_selections(), image_mapping) 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_rejects_if_boot_source_cache_does_not_have_arch(self): boot_source = factory.make_BootSource() os = factory.make_name("os") release = factory.make_name("release") factory.make_BootSourceCache(boot_source, os=os, release=release) params = { "os": os, "release": release, "arches": [factory.make_name("arch")], } form = BootSourceSelectionForm(boot_source=boot_source, data=params) self.assertFalse(form.is_valid()) self.assertEqual( { "arches": ["No available images to download for %s" % params["arches"]] }, form._errors, )
def test__consistent_query_count(self): source = factory.make_BootSource(keyring_data=b'1234') image_mapping = BootImageMapping() for _ in range(random.randint(20, 50)): self.make_release(image_mapping) # Add all the items to the cache, always 5. queries, _ = count_queries(bootsources._update_cache, source.to_dict_without_selections(), image_mapping) self.assertEquals(5, queries) # Now that they all already exist, it should only be 4 queries. queries, _ = count_queries(bootsources._update_cache, source.to_dict_without_selections(), image_mapping) self.assertEquals(4, queries) # Do it again just to be sure. queries, _ = count_queries(bootsources._update_cache, source.to_dict_without_selections(), image_mapping) self.assertEquals(4, queries)
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 boot source {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__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_POST_creates_boot_source_selection(self): self.become_admin() boot_source = factory.make_BootSource() new_release = factory.make_name('release') boot_source_caches = factory.make_many_BootSourceCaches( 2, boot_source=boot_source, release=new_release) params = { 'release': new_release, 'arches': [boot_source_caches[0].arch, boot_source_caches[1].arch], 'subarches': [boot_source_caches[0].subarch, boot_source_caches[1].subarch], 'labels': [boot_source_caches[0].label], } response = self.client.post( reverse('boot_source_selections_handler', args=[boot_source.id]), params) self.assertEqual(http.client.OK, response.status_code) parsed_result = json_load_bytes(response.content) boot_source_selection = BootSourceSelection.objects.get( id=parsed_result['id']) self.assertAttributes(boot_source_selection, params)