Пример #1
0
    def test_input_string_int(self):
        # String and password fields are identical from the forms POV.
        param_type = random.choice(["string", "password"])
        script = factory.make_Script(
            parameters={param_type: {
                "type": param_type
            }})
        input = random.randint(0, 100)

        form = ParametersForm(data={param_type: input},
                              script=script,
                              node=factory.make_Node())

        self.assertTrue(form.is_valid(), form.errors)
        self.assertEqual(input,
                         form.cleaned_data["input"][0][param_type]["value"])
Пример #2
0
 def test_input_errors_on_unknown_paramater(self):
     script = factory.make_Script()
     bad_param = factory.make_name("bad_param")
     form = ParametersForm(
         data={bad_param: factory.make_name("bad_input")},
         script=script,
         node=factory.make_Node(),
     )
     self.assertFalse(form.is_valid())
     self.assertDictEqual(
         {
             "input":
             ["Unknown parameter '%s' for %s" % (bad_param, script.name)]
         },
         form.errors,
     )
Пример #3
0
 def test__starts_testing(self):
     node = factory.make_Node(status=NODE_STATUS.DEPLOYED)
     script = factory.make_Script(script_type=SCRIPT_TYPE.TESTING)
     enable_ssh = factory.pick_bool()
     self.patch(node, '_power_cycle').return_value = None
     admin = factory.make_admin()
     action = Test(node, admin)
     with post_commit_hooks:
         action.execute(
             enable_ssh=enable_ssh, testing_scripts=[script.name])
     node = reload_object(node)
     self.assertEqual(NODE_STATUS.TESTING, node.status)
     self.assertEqual(enable_ssh, node.enable_ssh)
     self.assertEqual(
         script.name,
         node.current_testing_script_set.scriptresult_set.first().name)
Пример #4
0
    def test_store_result_stores_script_version(self):
        script = factory.make_Script()
        old_version = script.script
        script.script = script.script.update(factory.make_string())
        script.save()
        script_result = factory.make_ScriptResult(
            status=SCRIPT_STATUS.RUNNING, script=script
        )
        exit_status = random.randint(0, 255)

        script_result.store_result(
            exit_status, script_version_id=old_version.id
        )

        self.assertEquals(exit_status, script_result.exit_status)
        self.assertEquals(old_version, script_result.script_version)
Пример #5
0
    def test_create_testing_script_set(self):
        node = factory.make_Node()
        expected_scripts = [
            factory.make_Script(
                script_type=SCRIPT_TYPE.TESTING, tags=['commissioning']).name
            for _ in range(3)
        ]

        script_set = ScriptSet.objects.create_testing_script_set(node)

        self.assertItemsEqual(
            expected_scripts,
            [script_result.name for script_result in script_set])
        self.assertEquals(RESULT_TYPE.TESTING, script_set.result_type)
        self.assertEquals(
            node.power_state, script_set.power_state_before_transition)
Пример #6
0
    def test_create_testing_script_set_cleans_up_current(self):
        Config.objects.set_config('max_node_testing_results', 1)
        node = factory.make_Node()
        script_set = factory.make_ScriptSet(node=node,
                                            result_type=RESULT_TYPE.TESTING)
        node.current_testing_script_set = script_set
        node.save()

        script = factory.make_Script(script_type=SCRIPT_TYPE.TESTING)
        ScriptSet.objects.create_testing_script_set(node,
                                                    scripts=[script.name])

        self.assertEquals(
            1,
            ScriptSet.objects.filter(node=node,
                                     result_type=RESULT_TYPE.TESTING).count())
Пример #7
0
 def test_estimated_runtime_sets_start_time_when_unavailable(self):
     # Regression test for LP:1730799
     script = factory.make_Script()
     script_set = factory.make_ScriptSet()
     no_started_result = factory.make_ScriptResult(script=script,
                                                   script_set=script_set)
     no_started_result.status = SCRIPT_STATUS.PASSED
     no_started_result.started = None
     no_started_result.save()
     script_result = factory.make_ScriptResult(
         status=SCRIPT_STATUS.RUNNING,
         started=datetime.now(),
         script=script,
         script_set=script_set,
     )
     self.assertEquals("0:00:00", script_result.estimated_runtime)
Пример #8
0
 def test_POST_test_deletes_scriptset_on_failure(self):
     node = factory.make_Node(
         status=NODE_STATUS.READY, owner=factory.make_User())
     script = factory.make_Script(
         script_type=SCRIPT_TYPE.TESTING, parameters={
             'interface': {'type': 'interface'}})
     self.become_admin()
     response = self.client.post(self.get_node_uri(node), {
         'op': 'test',
         'testing_scripts': script.name,
     })
     self.assertEqual(http.client.BAD_REQUEST, response.status_code)
     self.assertEqual(
         b'An interface must be configured to run network testing!',
         response.content)
     self.assertFalse(node.scriptset_set.exists())
Пример #9
0
 def test_add_tag(self):
     self.become_admin()
     script = factory.make_Script()
     new_tag = factory.make_name("tag")
     response = self.client.post(self.get_script_uri(script), {
         "op": "add_tag",
         "tag": new_tag
     })
     self.assertThat(response, HasStatusCode(http.client.OK))
     self.assertIn(new_tag, reload_object(script).tags)
     event = Event.objects.get(type__level=AUDIT)
     self.assertIsNotNone(event)
     self.assertEqual(
         event.description,
         "Added tag '%s' to script '%s'." % (new_tag, script.name),
     )
Пример #10
0
    def test_GET_include_script(self):
        scripts = {}
        for _ in range(3):
            script = factory.make_Script()
            scripts[script.name] = script

        response = self.client.get(self.get_scripts_uri(),
                                   {"include_script": True})
        self.assertThat(response, HasStatusCode(http.client.OK))
        parsed_results = json_load_bytes(response.content)

        for result in parsed_results:
            self.assertEquals(
                scripts[result["name"]].script.data.encode(),
                b64decode(result["history"][0]["data"].encode()),
            )
Пример #11
0
 def test_input_interface_id_errors(self):
     node = factory.make_Node()
     for _ in range(3):
         factory.make_Interface(node=node)
     script = factory.make_Script(
         parameters={"interface": {
             "type": "interface"
         }})
     form = ParametersForm(
         data={"interface": random.randint(1000, 2000)},
         script=script,
         node=node,
     )
     self.assertFalse(form.is_valid())
     self.assertDictEqual({"interface": ["Interface id does not exist"]},
                          form.errors)
Пример #12
0
 def test_remove_tag(self):
     self.become_admin()
     script = factory.make_Script(destructive=False)
     removed_tag = random.choice(
         [tag for tag in script.tags if 'tag' in tag])
     response = self.client.post(self.get_script_uri(script), {
         'op': 'remove_tag',
         'tag': removed_tag,
     })
     self.assertThat(response, HasStatusCode(http.client.OK))
     self.assertNotIn(removed_tag, reload_object(script).tags)
     event = Event.objects.get(type__level=AUDIT)
     self.assertIsNotNone(event)
     self.assertEqual(
         event.description,
         "Removed tag '%s' from script '%s'." % (removed_tag, script.name))
Пример #13
0
    def test_GET_filters(self):
        node = factory.make_Node()
        scripts = [factory.make_Script() for _ in range(3)]
        name_filter_script = random.choice(scripts)
        tag_filter_script = random.choice(scripts)
        script_set_ids = []
        for _ in range(3):
            script_set = factory.make_ScriptSet(node=node)
            script_set_ids.append(script_set.id)
            for script in scripts:
                factory.make_ScriptResult(script_set=script_set, script=script)

        # Script sets for different nodes.
        for _ in range(3):
            factory.make_ScriptSet()

        response = self.client.get(
            self.get_script_results_uri(node),
            {
                "filters": ",".join(
                    [
                        name_filter_script.name,
                        random.choice(
                            [
                                tag
                                for tag in tag_filter_script.tags
                                if "tag" in tag
                            ]
                        ),
                    ]
                )
            },
        )
        self.assertThat(response, HasStatusCode(http.client.OK))
        parsed_results = json_load_bytes(response.content)

        self.assertItemsEqual(
            script_set_ids, [result["id"] for result in parsed_results]
        )
        for script_set in parsed_results:
            for result in script_set["results"]:
                self.assertIn(
                    result["name"],
                    {name_filter_script.name, tag_filter_script.name},
                )
                for key in ["output", "stdout", "stderr", "result"]:
                    self.assertNotIn(key, result)
Пример #14
0
    def test_select_for_hardware_scripts_doesnt_remove_if_selected(self):
        script = factory.make_Script(
            script_type=SCRIPT_TYPE.COMMISSIONING,
            for_hardware=[random.choice([
                "usb:174c:07d1",
                "pci:8086:1918",
                "modalias:pci:v00001A03d00001150sv000015D9*"
            ])])
        node = factory.make_Node()
        script_set = ScriptSet.objects.create_commissioning_script_set(
            node, [script.name])

        script_set.select_for_hardware_scripts()

        self.assertItemsEqual(
            list(NODE_INFO_SCRIPTS) + [script.name],
            [script_result.name for script_result in script_set])
Пример #15
0
    def test_PUT_updates_contents(self):
        old_content = factory.make_script_content().encode("ascii")
        script = factory.make_Script(
            script_type=SCRIPT_TYPE.COMMISSIONING, script=old_content
        )
        new_content = factory.make_script_content().encode("ascii")

        response = self.client.put(
            self.get_url(script.name),
            {"content": factory.make_file_upload(content=new_content)},
        )
        self.assertThat(response, HasStatusCode(http.client.OK))
        self.assertEquals(rc.ALL_OK.content, response.content)

        self.assertEqual(
            new_content.decode("utf-8"), reload_object(script).script.data
        )