def test_get_next_version_name_returns_first_revision(self): expected_version = "%s.1" % self.configure_now() resource = factory.make_BootResource() factory.make_BootResourceSet( resource, version=resource.get_next_version_name() ) self.assertEqual(expected_version, resource.get_next_version_name())
def test_get_latest_complete_set(self): resource = factory.make_BootResource() factory.make_BootResourceSet(resource) self.make_complete_boot_resource_set(resource) latest_complete = self.make_complete_boot_resource_set(resource) factory.make_BootResourceSet(resource) self.assertEqual(latest_complete, resource.get_latest_complete_set())
def test_get_next_version_name_returns_later_revision(self): expected_version = self.configure_now() set_count = random.randint(2, 4) resource = factory.make_BootResource() for _ in range(set_count): factory.make_BootResourceSet( resource, version=resource.get_next_version_name()) self.assertEqual('%s.%d' % (expected_version, set_count), resource.get_next_version_name())
def test_boot_resource_to_dict_without_sets(self): resource = factory.make_BootResource() factory.make_BootResourceSet(resource) dict_representation = boot_resource_to_dict(resource, with_sets=False) self.assertEqual(resource.id, dict_representation['id']) self.assertEqual(BOOT_RESOURCE_TYPE_CHOICES_DICT[resource.rtype], dict_representation['type']) self.assertEqual(resource.name, dict_representation['name']) self.assertEqual(resource.architecture, dict_representation['architecture']) self.assertEqual(get_boot_resource_uri(resource), dict_representation['resource_uri']) self.assertFalse('sets' in dict_representation)
def test_get_commissionable_resource_returns_only_commissionable(self): os = factory.make_name('os') series = factory.make_name('series') name = '%s/%s' % (os, series) resource = factory.make_usable_boot_resource( rtype=BOOT_RESOURCE_TYPE.SYNCED, name=name) factory.make_incomplete_boot_resource(rtype=BOOT_RESOURCE_TYPE.SYNCED, name=name) not_commissionable = factory.make_BootResource( rtype=BOOT_RESOURCE_TYPE.SYNCED, name=name) factory.make_BootResourceSet(not_commissionable) commissionables = BootResource.objects.get_commissionable_resource( os, series) self.assertItemsEqual([resource], commissionables)
def test_returns_kpackage(self): resource = factory.make_BootResource( name="ubuntu/trusty", architecture="amd64/hwe-t", rtype=BOOT_RESOURCE_TYPE.SYNCED, ) resource_set = factory.make_BootResourceSet(resource) factory.make_boot_resource_file_with_content( resource_set, filename="boot-kernel", filetype="boot-kernel", extra={ "kpackage": "linux-image-generic-lts-trusty", "kparams": "a=b", }, ) node = factory.make_Node( interface=True, power_type="manual", osystem="ubuntu", distro_series="trusty", architecture="amd64/generic", hwe_kernel="hwe-t", ) self.assertEqual( "a=b", BootResource.objects.get_kparams_for_node(node) )
def test_xinstallable_returns_true_when_filetype_present(self): resource = factory.make_BootResource() resource_set = factory.make_BootResourceSet(resource) filetype = random.choice(XINSTALL_TYPES) factory.make_boot_resource_file_with_content( resource_set, filename=filetype, filetype=filetype) self.assertTrue(resource_set.xinstallable)
def test_progress_accumulates_all_files(self): resource = factory.make_BootResource() resource_set = factory.make_BootResourceSet(resource) final_size = 0 final_total_size = 0 sizes = [random.randint(512, 1024) for _ in range(3)] total_sizes = [random.randint(1025, 2048) for _ in range(3)] types = [ BOOT_RESOURCE_FILE_TYPE.ROOT_IMAGE, BOOT_RESOURCE_FILE_TYPE.BOOT_KERNEL, BOOT_RESOURCE_FILE_TYPE.BOOT_INITRD, ] for size in sizes: final_size += size total_size = total_sizes.pop() final_total_size += total_size filetype = types.pop() content = factory.make_bytes(size=size) largefile = factory.make_LargeFile(content=content, size=total_size) factory.make_BootResourceFile(resource_set, largefile, filename=filetype, filetype=filetype) progress = 100.0 * final_size / float(final_total_size) self.assertAlmostEqual(progress, resource_set.progress)
def make_empty_resource_file(self, rtype=None, content=None): # Create a largefile to use the generated content, # sha256, and total_size. if content is None: content = factory.make_bytes(1024) total_size = len(content) largefile = factory.make_LargeFile(content=content, size=total_size) sha256 = largefile.sha256 with largefile.content.open('rb') as stream: content = stream.read() with post_commit_hooks: largefile.delete() # Empty largefile largeobject = LargeObjectFile() largeobject.open().close() largefile = LargeFile.objects.create(sha256=sha256, total_size=total_size, content=largeobject) if rtype is None: rtype = BOOT_RESOURCE_TYPE.UPLOADED resource = factory.make_BootResource(rtype=rtype) resource_set = factory.make_BootResourceSet(resource) rfile = factory.make_BootResourceFile(resource_set, largefile) return rfile, content
def test_delete_does_nothing_if_linked(self): largefile = factory.make_LargeFile() resource = factory.make_BootResource() resource_set = factory.make_BootResourceSet(resource) factory.make_BootResourceFile(resource_set, largefile) largefile.delete() self.assertTrue(LargeFile.objects.filter(id=largefile.id).exists())
def make_complete_boot_resource_set(self, resource): resource_set = factory.make_BootResourceSet(resource) filename = factory.make_name('name') filetype = factory.pick_enum(BOOT_RESOURCE_FILE_TYPE) largefile = factory.make_LargeFile() factory.make_BootResourceFile( resource_set, largefile, filename=filename, filetype=filetype) return resource_set
def test_boot_resource_to_dict_without_sets(self): resource = factory.make_BootResource() factory.make_BootResourceSet(resource) dict_representation = boot_resource_to_dict(resource, with_sets=False) self.assertEqual(resource.id, dict_representation["id"]) self.assertEqual( BOOT_RESOURCE_TYPE_CHOICES_DICT[resource.rtype], dict_representation["type"], ) self.assertEqual(resource.name, dict_representation["name"]) self.assertEqual(resource.architecture, dict_representation["architecture"]) self.assertEqual( get_boot_resource_uri(resource), dict_representation["resource_uri"], ) self.assertFalse("sets" in dict_representation)
def test_boot_resource_to_dict_with_sets(self): resource = factory.make_BootResource() resource_set = factory.make_BootResourceSet(resource) dict_representation = boot_resource_to_dict(resource, with_sets=True) self.assertEqual( boot_resource_set_to_dict(resource_set), dict_representation["sets"][resource_set.version], )
def test_commissionable_returns_false_when_missing_filetypes(self): resource = factory.make_BootResource() resource_set = factory.make_BootResourceSet(resource) commissionable_set = { BOOT_RESOURCE_FILE_TYPE.BOOT_KERNEL, BOOT_RESOURCE_FILE_TYPE.BOOT_INITRD, } self.make_all_boot_resource_files(resource_set, commissionable_set) self.assertFalse(resource_set.commissionable)
def test_progress_handles_zero_division(self): resource = factory.make_BootResource() resource_set = factory.make_BootResourceSet(resource) filetype = BOOT_RESOURCE_FILE_TYPE.ROOT_IMAGE total_size = random.randint(1025, 2048) largefile = factory.make_LargeFile(content=b"", size=total_size) factory.make_BootResourceFile( resource_set, largefile, filename=filetype, filetype=filetype) self.assertEqual(0, resource_set.progress)
def test_xinstallable_returns_false_when_missing_filetypes(self): resource = factory.make_BootResource() resource_set = factory.make_BootResourceSet(resource) filetype = random.choice([ BOOT_RESOURCE_FILE_TYPE.BOOT_KERNEL, BOOT_RESOURCE_FILE_TYPE.BOOT_INITRD, BOOT_RESOURCE_FILE_TYPE.SQUASHFS_IMAGE, BOOT_RESOURCE_FILE_TYPE.ROOT_IMAGE, ]) factory.make_boot_resource_file_with_content( resource_set, filename=filetype, filetype=filetype) self.assertFalse(resource_set.xinstallable)
def test_complete_returns_true_for_complete_files(self): resource = factory.make_BootResource() resource_set = factory.make_BootResourceSet(resource) types = [ BOOT_RESOURCE_FILE_TYPE.ROOT_IMAGE, BOOT_RESOURCE_FILE_TYPE.BOOT_KERNEL, BOOT_RESOURCE_FILE_TYPE.BOOT_INITRD, ] for _ in range(3): filetype = types.pop() factory.make_boot_resource_file_with_content( resource_set, filename=filetype, filetype=filetype) self.assertTrue(resource_set.complete)
def test_commissionable_returns_true_when_all_filetypes_present(self): resource = factory.make_BootResource() resource_set = factory.make_BootResourceSet(resource) commissionable_set = { BOOT_RESOURCE_FILE_TYPE.BOOT_KERNEL, BOOT_RESOURCE_FILE_TYPE.BOOT_INITRD, random.choice([ BOOT_RESOURCE_FILE_TYPE.SQUASHFS_IMAGE, BOOT_RESOURCE_FILE_TYPE.ROOT_IMAGE, ]), } self.make_all_boot_resource_files(resource_set, commissionable_set) self.assertTrue(resource_set.commissionable)
def test_boot_resource_set_to_dict(self): resource = factory.make_BootResource() resource_set = factory.make_BootResourceSet(resource) total_size = random.randint(1024, 2048) content = factory.make_bytes(random.randint(512, 1023)) largefile = factory.make_LargeFile(content=content, size=total_size) rfile = factory.make_BootResourceFile(resource_set, largefile) dict_representation = boot_resource_set_to_dict(resource_set) self.assertEqual(resource_set.version, dict_representation['version']) self.assertEqual(resource_set.label, dict_representation['label']) self.assertEqual(resource_set.total_size, dict_representation['size']) self.assertEqual(False, dict_representation['complete']) self.assertEqual(resource_set.progress, dict_representation['progress']) self.assertEqual(boot_resource_file_to_dict(rfile), dict_representation['files'][rfile.filename])
def test_total_size(self): resource = factory.make_BootResource() resource_set = factory.make_BootResourceSet(resource) total_size = 0 sizes = [random.randint(512, 1024) for _ in range(3)] types = [ BOOT_RESOURCE_FILE_TYPE.ROOT_IMAGE, BOOT_RESOURCE_FILE_TYPE.BOOT_KERNEL, BOOT_RESOURCE_FILE_TYPE.BOOT_INITRD, ] for size in sizes: total_size += size filetype = types.pop() largefile = factory.make_LargeFile(size=size) factory.make_BootResourceFile( resource_set, largefile, filename=filetype, filetype=filetype) self.assertEqual(total_size, resource_set.total_size)
def test_boot_resource_file_to_dict(self): size = random.randint(512, 1023) total_size = random.randint(1024, 2048) content = factory.make_bytes(size) largefile = factory.make_LargeFile(content=content, size=total_size) resource = factory.make_BootResource(rtype=BOOT_RESOURCE_TYPE.UPLOADED) resource_set = factory.make_BootResourceSet(resource) rfile = factory.make_BootResourceFile(resource_set, largefile) dict_representation = boot_resource_file_to_dict(rfile) self.assertEqual(rfile.filename, dict_representation['filename']) self.assertEqual(rfile.filetype, dict_representation['filetype']) self.assertEqual(rfile.largefile.sha256, dict_representation['sha256']) self.assertEqual(total_size, dict_representation['size']) self.assertEqual(False, dict_representation['complete']) self.assertEqual(rfile.largefile.progress, dict_representation['progress']) self.assertEqual( reverse('boot_resource_file_upload_handler', args=[resource.id, rfile.id]), dict_representation['upload_uri'])
def test_complete_returns_false_for_one_incomplete_file(self): resource = factory.make_BootResource() resource_set = factory.make_BootResourceSet(resource) types = [ BOOT_RESOURCE_FILE_TYPE.ROOT_IMAGE, BOOT_RESOURCE_FILE_TYPE.BOOT_KERNEL, BOOT_RESOURCE_FILE_TYPE.BOOT_INITRD, ] for _ in range(2): filetype = types.pop() factory.make_boot_resource_file_with_content( resource_set, filename=filetype, filetype=filetype) size = random.randint(512, 1024) total_size = random.randint(1025, 2048) filetype = types.pop() content = factory.make_bytes(size=size) largefile = factory.make_LargeFile(content=content, size=total_size) factory.make_BootResourceFile( resource_set, largefile, filename=filetype, filetype=filetype) self.assertFalse(resource_set.complete)
def test_progress_increases_from_0_to_100(self): resource = factory.make_BootResource() resource_set = factory.make_BootResourceSet(resource) filetype = BOOT_RESOURCE_FILE_TYPE.ROOT_IMAGE total_size = 100 current_size = 0 largefile = factory.make_LargeFile(content=b"", size=total_size) factory.make_BootResourceFile( resource_set, largefile, filename=filetype, filetype=filetype) stream = largefile.content.open() self.addCleanup(stream.close) self.assertEqual(0, resource_set.progress) for _ in range(total_size): stream.write(b"a") largefile.size += 1 largefile.save() current_size += 1 self.assertAlmostEqual( 100.0 * current_size / float(total_size), resource_set.progress)
def test_complete_returns_false_for_no_files(self): resource = factory.make_BootResource() resource_set = factory.make_BootResourceSet(resource) self.assertFalse(resource_set.complete)
def test_get_latest_set(self): resource = factory.make_BootResource() factory.make_BootResourceSet(resource) latest_two = factory.make_BootResourceSet(resource) self.assertEqual(latest_two, resource.get_latest_set())