Exemplo n.º 1
0
 def test__results_must_be_list_or_dict(self):
     form = ScriptForm(
         data={
             "script":
             factory.make_script_content({
                 "name": factory.make_name("name"),
                 "results": None
             })
         })
     self.assertFalse(form.is_valid())
     self.assertDictEqual(
         {
             "results": [
                 "results must be a list of strings or a dictionary of "
                 "dictionaries."
             ]
         },
         form.errors,
     )
     self.assertItemsEqual([], VersionedTextFile.objects.all())
Exemplo n.º 2
0
 def test_snap_package_mode_must_be_valid(self):
     form = ScriptForm(
         data={
             "script":
             factory.make_script_content({
                 "name": factory.make_name("name"),
                 "packages": {
                     "snap": {
                         "name": factory.make_name("script_name"),
                         "mode": factory.make_name("mode"),
                     }
                 },
             })
         })
     self.assertFalse(form.is_valid())
     self.assertDictEqual(
         {"packages": ["Snap mode must be classic, dev, or jail."]},
         form.errors,
     )
     self.assertItemsEqual([], VersionedTextFile.objects.all())
Exemplo n.º 3
0
 def test__snap_package_channel_must_be_valid(self):
     form = ScriptForm(
         data={
             'script':
             factory.make_script_content({
                 'name': factory.make_name('name'),
                 'packages': {
                     'snap': {
                         'name': factory.make_name('script_name'),
                         'channel': factory.make_name('channel'),
                     }
                 },
             })
         })
     self.assertFalse(form.is_valid())
     self.assertDictEqual(
         {
             'packages':
             ['Snap channel must be stable, edge, beta, or candidate.']
         }, form.errors)
     self.assertItemsEqual([], VersionedTextFile.objects.all())
Exemplo n.º 4
0
    def test__update_script_doesnt_effect_other_fields(self):
        script = factory.make_Script()
        script_content = factory.make_script_content()
        name = script.name
        title = script.title
        description = script.description
        tags = script.tags
        script_type = script.script_type
        hardware_type = script.hardware_type
        parallel = script.parallel
        results = script.results
        parameters = script.parameters
        packages = script.packages
        timeout = script.timeout
        destructive = script.destructive
        may_reboot = script.may_reboot
        for_hardware = script.for_hardware
        recommission = script.recommission

        form = ScriptForm(data={"script": script_content}, 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.assertEquals(tags, script.tags)
        self.assertEquals(script_type, script.script_type)
        self.assertEquals(hardware_type, script.hardware_type)
        self.assertDictEqual(packages, script.packages)
        self.assertEquals(parallel, script.parallel)
        self.assertDictEqual(results, script.results)
        self.assertDictEqual(parameters, script.parameters)
        self.assertEquals(timeout, script.timeout)
        self.assertEquals(destructive, script.destructive)
        self.assertFalse(script.default)
        self.assertEquals(script_content, script.script.data)
        self.assertEquals(may_reboot, script.may_reboot)
        self.assertItemsEqual(for_hardware, script.for_hardware)
        self.assertEquals(recommission, script.recommission)
Exemplo n.º 5
0
 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(),
     }
     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.assertFalse(script.default)
     self.assertEquals(script_content, script.script.data)
Exemplo n.º 6
0
 def test__errors_when_hardware_invalid(self):
     hw_id = factory.make_name("hw_id")
     form = ScriptForm(
         data={
             "script":
             factory.make_script_content({
                 "name": factory.make_name("name"),
                 "for_hardware": hw_id
             })
         })
     self.assertFalse(form.is_valid())
     self.assertDictEqual(
         {
             "for_hardware": [
                 "Hardware identifier '%s' must be a modalias, PCI ID, "
                 "USB ID, system vendor, system product, system version, "
                 "mainboard vendor, or mainboard product." % hw_id
             ]
         },
         form.errors,
     )
     self.assertItemsEqual([], VersionedTextFile.objects.all())
Exemplo n.º 7
0
    def test__create_with_default_values(self):
        name = factory.make_name('name')
        script_content = factory.make_script_content()

        form = ScriptForm(data={'name': name, 'script': script_content})
        self.assertTrue(form.is_valid(), form.errors)
        script = form.save()

        self.assertEquals(name, script.name)
        self.assertEquals('', script.title)
        self.assertEquals('', script.description)
        self.assertEquals(1, len(script.tags))
        self.assertEquals(SCRIPT_TYPE.TESTING, script.script_type)
        self.assertEquals(HARDWARE_TYPE.NODE, script.hardware_type)
        self.assertEquals(SCRIPT_PARALLEL.DISABLED, script.parallel)
        self.assertDictEqual({}, script.packages)
        self.assertDictEqual({}, script.results)
        self.assertDictEqual({}, script.parameters)
        self.assertEquals(timedelta(0), script.timeout)
        self.assertFalse(script.destructive)
        self.assertEquals(script_content, script.script.data)
        self.assertFalse(script.default)
Exemplo n.º 8
0
    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)
        timeout = random.randint(0, 1000)
        destructive = factory.pick_bool()
        script_content = factory.make_string()
        comment = factory.make_name('comment')

        form = ScriptForm(
            data={
                'name': name,
                'title': title,
                'description': description,
                'tags': ','.join(tags),
                'type': script_type,
                'timeout': str(timeout),
                'destructive': destructive,
                'script': script_content,
                'comment': comment,
            })
        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)
        if script.destructive:
            tags.append('destructive')
        self.assertEquals(tags, script.tags)
        self.assertEquals(script_type, script.script_type)
        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.assertFalse(script.default)
Exemplo n.º 9
0
 def test__snap_package_channel_must_be_valid(self):
     form = ScriptForm(
         data={
             "script":
             factory.make_script_content({
                 "name": factory.make_name("name"),
                 "packages": {
                     "snap": {
                         "name": factory.make_name("script_name"),
                         "channel": factory.make_name("channel"),
                     }
                 },
             })
         })
     self.assertFalse(form.is_valid())
     self.assertDictEqual(
         {
             "packages":
             ["Snap channel must be stable, edge, beta, or candidate."]
         },
         form.errors,
     )
     self.assertItemsEqual([], VersionedTextFile.objects.all())
Exemplo n.º 10
0
    def test__update_script_doesnt_effect_other_fields(self):
        script = factory.make_Script()
        script_content = factory.make_string()
        name = script.name
        title = script.title
        description = script.description
        tags = script.tags
        script_type = script.script_type
        timeout = script.timeout
        destructive = script.destructive

        form = ScriptForm(data={'script': script_content}, 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.assertEquals(tags, script.tags)
        self.assertEquals(script_type, script.script_type)
        self.assertEquals(timeout, script.timeout)
        self.assertEquals(destructive, script.destructive)
        self.assertFalse(script.default)
Exemplo n.º 11
0
 def test__title_description_of_result_must_be_strings(self):
     results = {
         factory.make_name('result'): {
             'title': None,
             'description': None,
         }
     }
     form = ScriptForm(
         data={
             'script':
             factory.make_script_content({
                 'name': factory.make_name('name'),
                 'results': results,
             })
         })
     self.assertFalse(form.is_valid())
     self.assertDictEqual(
         {
             'results': [
                 'title must be a string.',
                 'description must be a string.',
             ]
         }, form.errors)
     self.assertItemsEqual([], VersionedTextFile.objects.all())
Exemplo n.º 12
0
 def test__converts_single_package_to_list(self):
     apt_pkg = factory.make_name('apt_pkg')
     snap_pkg = factory.make_name('snap_pkg')
     url = factory.make_url()
     form = ScriptForm(
         data={
             'script':
             factory.make_script_content({
                 'name': factory.make_name('name'),
                 'packages': {
                     'apt': apt_pkg,
                     'snap': snap_pkg,
                     'url': url
                 },
             })
         })
     self.assertTrue(form.is_valid(), form.errors)
     script = form.save()
     self.assertDictEqual(
         {
             'apt': [apt_pkg],
             'snap': [snap_pkg],
             'url': [url]
         }, script.packages)
Exemplo n.º 13
0
 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())
Exemplo n.º 14
0
    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)
Exemplo n.º 15
0
 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)
Exemplo n.º 16
0
 def test__create_requires_script(self):
     form = ScriptForm(data={"name": factory.make_string()})
     self.assertFalse(form.is_valid())
     self.assertItemsEqual([], VersionedTextFile.objects.all())
Exemplo n.º 17
0
    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)
Exemplo n.º 18
0
 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)
Exemplo n.º 19
0
 def test__update_no_fields_mandatory(self):
     script = factory.make_Script()
     form = ScriptForm(data={}, instance=script)
     self.assertTrue(form.is_valid(), form.errors)
Exemplo n.º 20
0
    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, 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)
Exemplo n.º 21
0
    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)
Exemplo n.º 22
0
    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)),
                ]
            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)
Exemplo n.º 23
0
 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)
Exemplo n.º 24
0
 def test__create_requires_name(self):
     form = ScriptForm(data={'script': factory.make_script_content()})
     self.assertFalse(form.is_valid())
     self.assertItemsEqual([], VersionedTextFile.objects.all())
Exemplo n.º 25
0
    def update(self, request, name):
        """@description-title Update a script
        @description Update a script with the given name.

        @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 type defines when the
        script should be used. Can be ``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 (boolean) "recommission" [required=false] Whether built-in
        commissioning scripts should be rerun after successfully running this
        scripts.

        @param (boolean) "apply_configured_networking" [required=false] Whether
        to apply the provided network configuration before the script runs.

        @success (http-status-code) "server-success" 200
        @success (json) "success-json" A JSON object containing information
        about the updated script.
        @success-example "success-json" [exkey=scripts-update] placeholder text

        @error (http-status-code) "404" 404
        @error (content) "not-found" The requested script is not found.
        @error-example "not-found"
            Not Found
        """
        if name.isdigit():
            script = get_object_or_404(Script, id=int(name))
        else:
            script = get_object_or_404(Script, name=name)

        data = request.data.copy()
        if "script" in request.FILES:
            data["script"] = request.FILES.get("script").read()
        elif len(request.FILES) == 1:
            for name, script_content in request.FILES.items():
                data["name"] = name
                data["script"] = script_content.read()

        form = ScriptForm(instance=script, data=data)
        if form.is_valid():
            return form.save(request=request, endpoint=ENDPOINT.API)
        else:
            raise MAASAPIValidationError(form.errors)