def test__update_no_fields_mandatory(self): script = factory.make_Script() form = ScriptForm(data={}, instance=script) self.assertTrue(form.is_valid(), form.errors)
def test__create_requires_name(self): form = ScriptForm(data={'script': factory.make_script_content()}) self.assertFalse(form.is_valid()) self.assertItemsEqual([], VersionedTextFile.objects.all())
def create(self, request): """@description-title Create a new script @description Create a new script. @param (string) "name" [required=true] The name of the script. @param (string) "title" [required=false] The title of the script. @param (string) "description" [required=false] A description of what the script does. @param (string) "tags" [required=false] A comma seperated list of tags for this script. @param (string) "type" [required=false] The script_type defines when the script should be used: ``testing`` or ``commissioning``. Defaults to ``testing``. @param (string) "hardware_type" [required=false] The hardware_type defines what type of hardware the script is assoicated with. May be CPU, memory, storage, network, or node. @param (int) "parallel" [required=false] Whether the script may be run in parallel with other scripts. May be disabled to run by itself, instance to run along scripts with the same name, or any to run along any script. 1 == True, 0 == False. @param (int) "timeout" [required=false] How long the script is allowed to run before failing. 0 gives unlimited time, defaults to 0. @param (boolean) "destructive" [required=false] Whether or not the script overwrites data on any drive on the running system. Destructive scripts can not be run on deployed systems. Defaults to false. @param (string) "script" [required=false] The content of the script to be uploaded in binary form. Note: this is not a normal parameter, but a file upload. Its filename is ignored; MAAS will know it by the name you pass to the request. Optionally you can ignore the name and script parameter in favor of uploading a single file as part of the request. @param (string) "comment" [required=false] A comment about what this change does. @param (string) "for_hardware" [required=false] A list of modalias, PCI IDs, and/or USB IDs the script will automatically run on. Must start with ``modalias:``, ``pci:``, or ``usb:``. @param (boolean) "may_reboot" [required=false] Whether or not the script may reboot the system while running. @param (string) "recommission" [required=false] Whether builtin commissioning scripts should be rerun after successfully running this scripts. @success (http-status-code) "server-success" 200 @success (json) "success-json" A JSON object containing information about the new script. @success-example "success-json" [exkey=scripts-create] placeholder text """ data = request.data.copy() if "script" in request.FILES: data["script"] = request.FILES.get("script").read() elif len(request.FILES) == 1: for name, script in request.FILES.items(): data["name"] = name data["script"] = script.read() form = ScriptForm(data=data) if form.is_valid(): return form.save(request=request, endpoint=ENDPOINT.API) else: raise MAASAPIValidationError(form.errors)
def test__update(self): script = factory.make_Script() name = factory.make_name('name') title = factory.make_name('title') description = factory.make_name('description') tags = [factory.make_name('tag') for _ in range(3)] script_type = factory.pick_choice(SCRIPT_TYPE_CHOICES) hardware_type = factory.pick_choice(HARDWARE_TYPE_CHOICES) parallel = factory.pick_choice(SCRIPT_PARALLEL_CHOICES) packages = {'apt': [factory.make_name('package')]} timeout = random.randint(0, 1000) destructive = factory.pick_bool() script_content = factory.make_script_content() comment = factory.make_name('comment') orig_script_content = script.script.data may_reboot = factory.pick_bool() apply_configured_networking = factory.pick_bool() if script_type == SCRIPT_TYPE.COMMISSIONING: for_hardware = [ 'modalias:%s' % factory.make_name('mod_alias'), 'pci:%04x:%04X' % (random.randint(0, 9999), random.randint(0, 9999)), 'usb:%04x:%04X' % (random.randint(0, 9999), random.randint(0, 9999)), 'system_vendor:%s' % factory.make_name('system_name'), 'system_product:%s' % factory.make_name('system_product'), 'system_version:%s' % factory.make_name('system_version'), 'mainboard_vendor:%s' % factory.make_name('mobo_vendor'), 'mainboard_product:%s' % factory.make_name('mobo_product'), ] recommission = factory.pick_bool() else: for_hardware = [] recommission = False form = ScriptForm(data={ 'name': name, 'title': title, 'description': description, 'tags': ','.join(tags), 'type': script_type, 'hardware_type': hardware_type, 'parallel': parallel, 'packages': json.dumps(packages), 'timeout': str(timeout), 'destructive': destructive, 'script': script_content, 'comment': comment, 'may_reboot': may_reboot, 'for_hardware': ','.join(for_hardware), 'recommission': recommission, 'apply_configured_networking': apply_configured_networking, }, instance=script) self.assertTrue(form.is_valid(), form.errors) script = form.save() self.assertEquals(name, script.name) self.assertEquals(title, script.title) self.assertEquals(description, script.description) self.assertThat(script.tags, ContainsAll(tags)) self.assertEquals(script_type, script.script_type) self.assertEquals(hardware_type, script.hardware_type) self.assertEquals(parallel, script.parallel) self.assertDictEqual({}, script.results) self.assertDictEqual({}, script.parameters) self.assertDictEqual(packages, script.packages) self.assertEquals(timedelta(0, timeout), script.timeout) self.assertEquals(destructive, script.destructive) self.assertEquals(script_content, script.script.data) self.assertEquals(comment, script.script.comment) self.assertEquals(orig_script_content, script.script.previous_version.data) self.assertEquals(None, script.script.previous_version.comment) self.assertEquals(apply_configured_networking, script.apply_configured_networking) self.assertFalse(script.default)
def test_create_with_defined_values(self): name = factory.make_name("name") title = factory.make_name("title") description = factory.make_name("description") tags = [factory.make_name("tag") for _ in range(3)] script_type = factory.pick_choice(SCRIPT_TYPE_CHOICES) hardware_type = factory.pick_choice(HARDWARE_TYPE_CHOICES) parallel = factory.pick_choice(SCRIPT_PARALLEL_CHOICES) packages = {"apt": [factory.make_name("package")]} timeout = random.randint(0, 1000) destructive = factory.pick_bool() script_content = factory.make_script_content() comment = factory.make_name("comment") may_reboot = factory.pick_bool() if script_type == SCRIPT_TYPE.COMMISSIONING: for_hardware = [ "modalias:%s" % factory.make_name("mod_alias"), "pci:%04X:%04x" % (random.randint(0, 9999), random.randint(0, 9999)), "usb:%04x:%04X" % (random.randint(0, 9999), random.randint(0, 9999)), "system_vendor:%s" % factory.make_name("system_name"), "system_product:%s" % factory.make_name("system_product"), "system_version:%s" % factory.make_name("system_version"), "mainboard_vendor:%s" % factory.make_name("mobo_vendor"), "mainboard_product:%s" % factory.make_name("mobo_product"), ] recommission = factory.pick_bool() else: for_hardware = [] recommission = False form = ScriptForm( data={ "name": name, "title": title, "description": description, "tags": ",".join(tags), "type": script_type, "hardware_type": hardware_type, "parallel": parallel, "packages": json.dumps(packages), "timeout": str(timeout), "destructive": destructive, "script": script_content, "comment": comment, "may_reboot": may_reboot, "for_hardware": ",".join(for_hardware), "recommission": recommission, }) self.assertTrue(form.is_valid(), form.errors) script = form.save() self.assertEquals(name, script.name) self.assertEquals(title, script.title) self.assertEquals(description, script.description) self.assertThat(script.tags, ContainsAll(tags)) self.assertEquals(script_type, script.script_type) self.assertEquals(hardware_type, script.hardware_type) self.assertEquals(parallel, script.parallel) self.assertDictEqual(packages, script.packages) self.assertDictEqual({}, script.results) self.assertDictEqual({}, script.parameters) self.assertDictEqual(packages, script.packages) self.assertEquals(timedelta(0, timeout), script.timeout) self.assertEquals(destructive, script.destructive) self.assertEquals(script_content, script.script.data) self.assertEquals(comment, script.script.comment) self.assertEquals(may_reboot, script.may_reboot) self.assertItemsEqual(for_hardware, script.for_hardware) self.assertEquals(recommission, script.recommission) self.assertFalse(script.default)
def create(self, request): """Create a new script. :param name: The name of the script. :type name: unicode :param title: The title of the script. :type title: unicode :param description: A description of what the script does. :type description: unicode :param tags: A comma seperated list of tags for this script. :type tags: unicode :param type: The script_type defines when the script should be used. Can be testing or commissioning, defaults to testing. :type script_type: unicode :param hardware_type: The hardware_type defines what type of hardware the script is assoicated with. May be CPU, memory, storage, or node. :type hardware_type: unicode :param parallel: Whether the script may be run in parallel with other scripts. May be disabled to run by itself, instance to run along scripts with the same name, or any to run along any script. :type parallel: unicode :param timeout: How long the script is allowed to run before failing. 0 gives unlimited time, defaults to 0. :type timeout: unicode :param destructive: Whether or not the script overwrites data on any drive on the running system. Destructive scripts can not be run on deployed systems. Defaults to false. :type destructive: boolean :param script: The content of the script to be uploaded in binary form. note: this is not a normal parameter, but a file upload. Its filename is ignored; MAAS will know it by the name you pass to the request. Optionally you can ignore the name and script parameter in favor of uploading a single file as part of the request. :type script: unicode :param comment: A comment about what this change does. :type comment: unicode :param for_hardware: A list of modalias, PCI IDs, and/or USB IDs the script will automatically run on. Must start with modalias:, pci:, or usb:. :type for_hardware: unicode :param may_reboot: Whether or not the script may reboot the system while running. :type may_reboot: boolean :param recommission: Whether builtin commissioning scripts should be rerun after successfully running this scripts. :type recommission: boolean """ data = request.data.copy() if 'script' in request.FILES: data['script'] = request.FILES.get('script').read() elif len(request.FILES) == 1: for name, script in request.FILES.items(): data['name'] = name data['script'] = script.read() form = ScriptForm(data=data) if form.is_valid(): return form.save(request=request, endpoint=ENDPOINT.API) else: raise MAASAPIValidationError(form.errors)
def test_loads_yaml_embedded_attributes(self): embedded_yaml = { "name": factory.make_name("name"), "title": factory.make_name("title"), "description": factory.make_name("description"), "tags": [factory.make_name("tag") for _ in range(3)], "script_type": factory.pick_choice(SCRIPT_TYPE_CHOICES), "hardware_type": factory.pick_choice(HARDWARE_TYPE_CHOICES), "parallel": factory.pick_choice(SCRIPT_PARALLEL_CHOICES), "results": ["write_speed"], "parameters": [{ "type": "storage" }, { "type": "runtime" }], "packages": { "apt": [factory.make_name("package")] }, "timeout": random.randint(0, 1000), "destructive": factory.pick_bool(), "may_reboot": factory.pick_bool(), } if embedded_yaml["script_type"] == SCRIPT_TYPE.COMMISSIONING: embedded_yaml["for_hardware"] = [ "modalias:%s" % factory.make_name("mod_alias"), "pci:%04X:%04x" % (random.randint(0, 9999), random.randint(0, 9999)), "usb:%04X:%04x" % (random.randint(0, 9999), random.randint(0, 9999)), "system_vendor:%s" % factory.make_name("system_name"), "system_product:%s" % factory.make_name("system_product"), "system_version:%s" % factory.make_name("system_version"), "mainboard_vendor:%s" % factory.make_name("mobo_vendor"), "mainboard_product:%s" % factory.make_name("mobo_product"), ] embedded_yaml["recommission"] = factory.pick_bool() script_content = factory.make_script_content(embedded_yaml) form = ScriptForm(data={"script": script_content}) self.assertTrue(form.is_valid(), form.errors) script = form.save() self.assertEquals(embedded_yaml["name"], script.name) self.assertEquals(embedded_yaml["title"], script.title) self.assertEquals(embedded_yaml["description"], script.description) self.assertThat(script.tags, ContainsAll(embedded_yaml["tags"])) self.assertEquals(embedded_yaml["script_type"], script.script_type) self.assertEquals(embedded_yaml["hardware_type"], script.hardware_type) self.assertEquals(embedded_yaml["parallel"], script.parallel) self.assertItemsEqual(embedded_yaml["results"], script.results) self.assertItemsEqual(embedded_yaml["parameters"], script.parameters) self.assertDictEqual(embedded_yaml["packages"], script.packages) self.assertEquals(timedelta(0, embedded_yaml["timeout"]), script.timeout) self.assertEquals(embedded_yaml["destructive"], script.destructive) self.assertEquals(embedded_yaml["may_reboot"], script.may_reboot) if embedded_yaml["script_type"] == SCRIPT_TYPE.COMMISSIONING: self.assertItemsEqual(embedded_yaml["for_hardware"], script.for_hardware) self.assertEquals(embedded_yaml["recommission"], script.recommission) else: self.assertItemsEqual([], script.for_hardware) self.assertFalse(script.recommission) self.assertFalse(script.default) self.assertEquals(script_content, script.script.data)
def test_create_requires_script(self): form = ScriptForm(data={"name": factory.make_string()}) self.assertFalse(form.is_valid()) self.assertItemsEqual([], VersionedTextFile.objects.all())
def test_update_setting_default_has_no_effect(self): script = factory.make_Script(default=True) form = ScriptForm(data={"default": False}, instance=script) self.assertTrue(form.is_valid(), form.errors) script = form.save() self.assertTrue(script.default)
def test__loads_yaml_embedded_attributes(self): embedded_yaml = { 'name': factory.make_name('name'), 'title': factory.make_name('title'), 'description': factory.make_name('description'), 'tags': [factory.make_name('tag') for _ in range(3)], 'script_type': factory.pick_choice(SCRIPT_TYPE_CHOICES), 'hardware_type': factory.pick_choice(HARDWARE_TYPE_CHOICES), 'parallel': factory.pick_choice(SCRIPT_PARALLEL_CHOICES), 'results': ['write_speed'], 'parameters': [{ 'type': 'storage' }, { 'type': 'runtime' }], 'packages': { 'apt': [factory.make_name('package')] }, 'timeout': random.randint(0, 1000), 'destructive': factory.pick_bool(), 'may_reboot': factory.pick_bool(), } if embedded_yaml['script_type'] == SCRIPT_TYPE.COMMISSIONING: embedded_yaml['for_hardware'] = [ 'modalias:%s' % factory.make_name('mod_alias'), 'pci:%04X:%04x' % (random.randint(0, 9999), random.randint(0, 9999)), 'usb:%04X:%04x' % (random.randint(0, 9999), random.randint(0, 9999)), 'system_vendor:%s' % factory.make_name('system_name'), 'system_product:%s' % factory.make_name('system_product'), 'system_version:%s' % factory.make_name('system_version'), 'mainboard_vendor:%s' % factory.make_name('mobo_vendor'), 'mainboard_product:%s' % factory.make_name('mobo_product'), ] embedded_yaml['recommission'] = factory.pick_bool() script_content = factory.make_script_content(embedded_yaml) form = ScriptForm(data={'script': script_content}) self.assertTrue(form.is_valid(), form.errors) script = form.save() self.assertEquals(embedded_yaml['name'], script.name) self.assertEquals(embedded_yaml['title'], script.title) self.assertEquals(embedded_yaml['description'], script.description) self.assertThat(script.tags, ContainsAll(embedded_yaml['tags'])) self.assertEquals(embedded_yaml['script_type'], script.script_type) self.assertEquals(embedded_yaml['hardware_type'], script.hardware_type) self.assertEquals(embedded_yaml['parallel'], script.parallel) self.assertItemsEqual(embedded_yaml['results'], script.results) self.assertItemsEqual(embedded_yaml['parameters'], script.parameters) self.assertDictEqual(embedded_yaml['packages'], script.packages) self.assertEquals(timedelta(0, embedded_yaml['timeout']), script.timeout) self.assertEquals(embedded_yaml['destructive'], script.destructive) self.assertEquals(embedded_yaml['may_reboot'], script.may_reboot) if embedded_yaml['script_type'] == SCRIPT_TYPE.COMMISSIONING: self.assertItemsEqual(embedded_yaml['for_hardware'], script.for_hardware) self.assertEquals(embedded_yaml['recommission'], script.recommission) else: self.assertItemsEqual([], script.for_hardware) self.assertFalse(script.recommission) self.assertFalse(script.default) self.assertEquals(script_content, script.script.data)
def test__errors_on_bad_yaml(self): form = ScriptForm(data={ 'name': factory.make_name('name'), 'script': factory.make_script_content('# {'), }) self.assertFalse(form.is_valid())