def test_validates_if_boot_source_cache_has_same_os_and_release(self): boot_source = factory.make_BootSource() boot_cache = factory.make_BootSourceCache(boot_source) params = {"os": boot_cache.os, "release": boot_cache.release} form = BootSourceSelectionForm(boot_source=boot_source, data=params) self.assertTrue(form.is_valid(), form._errors)
def test_creates_boot_source_selection_object(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) boot_source_selection = form.save() self.assertAttributes(boot_source_selection, params)
def test_edits_boot_source_selection_object(self): boot_source_selection = factory.make_BootSourceSelection() boot_source = boot_source_selection.boot_source params = self.make_valid_source_selection_params(boot_source) form = BootSourceSelectionForm(instance=boot_source_selection, data=params) self.assertTrue(form.is_valid(), form._errors) form.save() boot_source_selection = reload_object(boot_source_selection) self.assertAttributes(boot_source_selection, params)
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_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_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_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 create(self, request, boot_source_id): """Create a new boot source selection. :param os: The OS (e.g. ubuntu, centos) for which to import resources. :param release: The release for which to import resources. :param arches: The architecture list for which to import resources. :param subarches: The subarchitecture list for which to import resources. :param labels: The label lists for which to import resources. """ boot_source = get_object_or_404(BootSource, id=boot_source_id) form = BootSourceSelectionForm(data=request.data, boot_source=boot_source) if form.is_valid(): return form.save() else: raise MAASAPIValidationError(form.errors)
def update(self, request, boot_source_id, id): """@description-title Update a boot-source selection @description Update a boot source selection with the given id. @param (string) "{boot_source_id}" [required=true] A boot-source id. @param (string) "{id}" [required=true] A boot-source selection id. @param (string) "os" [required=false] The OS (e.g. ubuntu, centos) for which to import resources. @param (string) "release" [required=false] The release for which to import resources. @param (string) "arches" [required=false] The list of architectures for which to import resources. @param (string) "subarches" [required=false] The list of sub-architectures for which to import resources. @param (string) "labels" [required=false] The list of labels for which to import resources. @success (http-status-code) "server-success" 200 @success (json) "success-json" A JSON object containing the requested boot-source selection object. @success-example "success-json" [exkey=boot-source-sel-update] placeholder text @error (http-status-code) "404" 404 @error (content) "not-found" The requested boot-source or boot-source selection is not found. @error-example "not-found" Not Found """ boot_source = get_object_or_404(BootSource, id=boot_source_id) boot_source_selection = get_object_or_404( BootSourceSelection, boot_source=boot_source, id=id ) form = BootSourceSelectionForm( data=request.data, instance=boot_source_selection ) if form.is_valid(): return form.save() else: raise MAASAPIValidationError(form.errors)
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 update(self, request, boot_source_id, id): """Update a specific boot source selection. :param os: The OS (e.g. ubuntu, centos) for which to import resources. :param release: The release for which to import resources. :param arches: The list of architectures for which to import resources. :param subarches: The list of subarchitectures for which to import resources. :param labels: The list of labels for which to import resources. """ boot_source = get_object_or_404(BootSource, id=boot_source_id) boot_source_selection = get_object_or_404(BootSourceSelection, boot_source=boot_source, id=id) form = BootSourceSelectionForm(data=request.data, instance=boot_source_selection) if form.is_valid(): return form.save() else: raise MAASAPIValidationError(form.errors)
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_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)