Example #1
0
 def test_get_token_for_node_prefetches(self):
     # token.consumer.key is almost always used when retrieving the token.
     # Prefetch it so it can be called in one deferToDatabase.
     node = factory.make_Node()
     consumer_key = NodeKey.objects.get_token_for_node(node).consumer.key
     with CountQueries() as prefetch_count:
         token = NodeKey.objects.get_token_for_node(node)
     self.assertEquals(3, prefetch_count.num_queries)
     # Verify consumer was prefetched
     with CountQueries() as access_count:
         self.assertEquals(consumer_key, token.consumer.key)
     self.assertEquals(0, access_count.num_queries)
Example #2
0
 def test_get_history(self):
     user = factory.make_User()
     handler = NodeResultHandler(user, {}, None)
     node = factory.make_Node(owner=user)
     script = factory.make_Script()
     script_results = []
     for _ in range(10):
         script_set = factory.make_ScriptSet(node=node)
         script_results.append(factory.make_ScriptResult(
             script=script, script_set=script_set,
             status=SCRIPT_STATUS.PASSED))
     latest_script_result = script_results[-1]
     script_results = sorted(
         script_results, key=lambda i: i.id, reverse=True)
     queries = CountQueries()
     with queries:
         ret = handler.get_history({'id': latest_script_result.id})
     self.assertEqual(4, queries.num_queries)
     for script_result, out in zip(script_results, ret):
         self.assertDictEqual({
             'id': script_result.id,
             'updated': dehydrate_datetime(script_result.updated),
             'status': script_result.status,
             'status_name': script_result.status_name,
             'runtime': script_result.runtime,
             'starttime': script_result.starttime,
             'endtime': script_result.endtime,
             'estimated_runtime': script_result.estimated_runtime,
             'suppressed': script_result.suppressed,
         }, out)
Example #3
0
 def test_history_query_count(self):
     script_name = factory.make_name("script_name")
     script_set = factory.make_ScriptSet()
     script_results = [
         factory.make_ScriptResult(script_name=script_name,
                                   script_set=script_set) for _ in range(10)
     ]
     queries_one = CountQueries()
     script_result_one = ScriptResult.objects.get(id=script_results[0].id)
     with queries_one:
         script_result_one.history
     queries_many = CountQueries()
     script_result_many = ScriptResult.objects.get(id=script_results[-1].id)
     with queries_many:
         script_result_many.history
     self.assertEqual(1, queries_one.num_queries)
     self.assertEqual(1, queries_many.num_queries)
Example #4
0
    def test_mark_nodes_failed_after_missing_timeout_prefetches(self):
        self.patch(Node, "mark_failed")
        current_time = now()
        node, script_set = self.make_node()
        script_set.last_ping = current_time
        script_set.save()
        script = factory.make_Script(timeout=timedelta(seconds=60))
        factory.make_ScriptResult(
            script_set=script_set,
            status=SCRIPT_STATUS.RUNNING,
            script=script,
            started=current_time - timedelta(minutes=3),
        )

        counter_one = CountQueries()
        with counter_one:
            mark_nodes_failed_after_missing_script_timeout(current_time, 20)

        nodes = []
        for _ in range(6):
            node, script_set = self.make_node()
            script_set.last_ping = current_time
            script_set.save()
            script = factory.make_Script(timeout=timedelta(seconds=60))
            factory.make_ScriptResult(
                script_set=script_set,
                status=SCRIPT_STATUS.RUNNING,
                script=script,
                started=current_time - timedelta(minutes=3),
            )
            nodes.append(node)

        counter_many = CountQueries()
        with counter_many:
            mark_nodes_failed_after_missing_script_timeout(current_time, 20)

        # Lookup takes 7 queries no matter the amount of Nodes
        # 1. Get all Nodes in commissioning or testing
        # 2. Get all commissioning ScriptSets
        # 3. Get all testing ScriptSets
        # 4. Get all commissioning ScriptResults
        # 5. Get all testing ScriptResults
        # 6. Get all commissioning Scripts
        # 7. Get all testing Scripts
        self.assertEquals(7, counter_one.num_queries)
        self.assertEquals(7, counter_many.num_queries)
    def test_mark_nodes_failed_after_missing_timeout_prefetches(self):
        self.patch(Node, 'mark_failed')
        now = datetime.now()
        nodes = []
        for _ in range(3):
            node, script_set = self.make_node()
            script_set.last_ping = now
            script_set.save()
            script = factory.make_Script(timeout=timedelta(seconds=60))
            factory.make_ScriptResult(
                script_set=script_set, status=SCRIPT_STATUS.RUNNING,
                script=script, started=now - timedelta(minutes=3))
            nodes.append(node)

        counter = CountQueries()
        with counter:
            mark_nodes_failed_after_missing_script_timeout()
        # Initial lookup and prefetch take three queries. This is done once to
        # find the nodes which nodes are being tests and on each node which
        # scripts are currently running.
        self.assertEquals(3 + len(nodes) * 2, counter.num_queries)