<commit_before># Copyright (c) 2012 OpenStack Foundation
# All Rights Reserved.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

"""Tests for compute node stats."""

from nova.compute import stats
from nova.compute import task_states
from nova.compute import vm_states
from nova import test
from nova.tests.unit import fake_instance


class StatsTestCase(test.NoDBTestCase):
    def setUp(self):
        super(StatsTestCase, self).setUp()
        self.stats = stats.Stats()

    def _fake_object(self, updates):
        return fake_instance.fake_instance_obj(None, **updates)

    def _create_instance(self, values=None):
        instance = {
            "os_type": "Linux",
            "project_id": "1234",
            "task_state": None,
            "vm_state": vm_states.BUILDING,
            "vcpus": 1,
            "uuid": "12-34-56-78-90",
        }
        if values:
            instance.update(values)
        return self._fake_object(instance)

    def test_os_type_count(self):
        os_type = "Linux"
        self.assertEqual(0, self.stats.num_os_type(os_type))
        self.stats._increment("num_os_type_" + os_type)
        self.stats._increment("num_os_type_" + os_type)
        self.stats._increment("num_os_type_Vax")
        self.assertEqual(2, self.stats.num_os_type(os_type))
        self.stats["num_os_type_" + os_type] -= 1
        self.assertEqual(1, self.stats.num_os_type(os_type))

    def test_update_project_count(self):
        proj_id = "1234"

        def _get():
            return self.stats.num_instances_for_project(proj_id)

        self.assertEqual(0, _get())
        self.stats._increment("num_proj_" + proj_id)
        self.assertEqual(1, _get())
        self.stats["num_proj_" + proj_id] -= 1
        self.assertEqual(0, _get())

    def test_instance_count(self):
        self.assertEqual(0, self.stats.num_instances)
        for i in range(5):
            self.stats._increment("num_instances")
        self.stats["num_instances"] -= 1
        self.assertEqual(4, self.stats.num_instances)

    def test_add_stats_for_instance(self):
        instance = {
            "os_type": "Linux",
            "project_id": "1234",
            "task_state": None,
            "vm_state": vm_states.BUILDING,
            "vcpus": 3,
            "uuid": "12-34-56-78-90",
        }
        self.stats.update_stats_for_instance(self._fake_object(instance))

        instance = {
            "os_type": "FreeBSD",
            "project_id": "1234",
            "task_state": task_states.SCHEDULING,
            "vm_state": None,
            "vcpus": 1,
            "uuid": "23-45-67-89-01",
        }
        self.stats.update_stats_for_instance(self._fake_object(instance))

        instance = {
            "os_type": "Linux",
            "project_id": "2345",
            "task_state": task_states.SCHEDULING,
            "vm_state": vm_states.BUILDING,
            "vcpus": 2,
            "uuid": "34-56-78-90-12",
        }

        self.stats.update_stats_for_instance(self._fake_object(instance))

        instance = {
            "os_type": "Linux",
            "project_id": "2345",
            "task_state": task_states.RESCUING,
            "vm_state": vm_states.ACTIVE,
            "vcpus": 2,
            "uuid": "34-56-78-90-13",
        }

        self.stats.update_stats_for_instance(self._fake_object(instance))

        instance = {
            "os_type": "Linux",
            "project_id": "2345",
            "task_state": task_states.UNSHELVING,
            "vm_state": vm_states.ACTIVE,
            "vcpus": 2,
            "uuid": "34-56-78-90-14",
        }

        self.stats.update_stats_for_instance(self._fake_object(instance))

        self.assertEqual(4, self.stats.num_os_type("Linux"))
        self.assertEqual(1, self.stats.num_os_type("FreeBSD"))
<commit_msg>Implements proper UUID format for compute/test_stats*<commit_after># Copyright (c) 2012 OpenStack Foundation
# All Rights Reserved.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

"""Tests for compute node stats."""

from nova.compute import stats
from nova.compute import task_states
from nova.compute import vm_states
from nova import test
from nova.tests.unit import fake_instance
from nova.tests import uuidsentinel as uuids


class StatsTestCase(test.NoDBTestCase):
    def setUp(self):
        super(StatsTestCase, self).setUp()
        self.stats = stats.Stats()

    def _fake_object(self, updates):
        return fake_instance.fake_instance_obj(None, **updates)

    def _create_instance(self, values=None):
        instance = {
            "os_type": "Linux",
            "project_id": "1234",
            "task_state": None,
            "vm_state": vm_states.BUILDING,
            "vcpus": 1,
            "uuid": uuids.stats_linux_instance_1,
        }
        if values:
            instance.update(values)
        return self._fake_object(instance)

    def test_os_type_count(self):
        os_type = "Linux"
        self.assertEqual(0, self.stats.num_os_type(os_type))
        self.stats._increment("num_os_type_" + os_type)
        self.stats._increment("num_os_type_" + os_type)
        self.stats._increment("num_os_type_Vax")
        self.assertEqual(2, self.stats.num_os_type(os_type))
        self.stats["num_os_type_" + os_type] -= 1
        self.assertEqual(1, self.stats.num_os_type(os_type))

    def test_update_project_count(self):
        proj_id = "1234"

        def _get():
            return self.stats.num_instances_for_project(proj_id)

        self.assertEqual(0, _get())
        self.stats._increment("num_proj_" + proj_id)
        self.assertEqual(1, _get())
        self.stats["num_proj_" + proj_id] -= 1
        self.assertEqual(0, _get())

    def test_instance_count(self):
        self.assertEqual(0, self.stats.num_instances)
        for i in range(5):
            self.stats._increment("num_instances")
        self.stats["num_instances"] -= 1
        self.assertEqual(4, self.stats.num_instances)

    def test_add_stats_for_instance(self):
        instance = {
            "os_type": "Linux",
            "project_id": "1234",
            "task_state": None,
            "vm_state": vm_states.BUILDING,
            "vcpus": 3,
            "uuid": uuids.stats_linux_instance_1,
        }
        self.stats.update_stats_for_instance(self._fake_object(instance))

        instance = {
            "os_type": "FreeBSD",
            "project_id": "1234",
            "task_state": task_states.SCHEDULING,
            "vm_state": None,
            "vcpus": 1,
            "uuid": uuids.stats_freebsd_instance,
        }
        self.stats.update_stats_for_instance(self._fake_object(instance))

        instance = {
            "os_type": "Linux",
            "project_id": "2345",
            "task_state": task_states.SCHEDULING,
            "vm_state": vm_states.BUILDING,
            "vcpus": 2,
            "uuid": uuids.stats_linux_instance_2,
        }

        self.stats.update_stats_for_instance(self._fake_object(instance))

        instance = {
            "os_type": "Linux",
            "project_id": "2345",
            "task_state": task_states.RESCUING,
            "vm_state": vm_states.ACTIVE,
            "vcpus": 2,
            "uuid": uuids.stats_linux_instance_3,
        }

        self.stats.update_stats_for_instance(self._fake_object(instance))

        instance = {
            "os_type": "Linux",
            "project_id": "2345",
            "task_state": task_states.UNSHELVING,
            "vm_state": vm_states.ACTIVE,
            "vcpus": 2,
            "uuid": uuids.stats_linux_instance_4,
        }

        self.stats.update_stats_for_instance(self._fake_object(instance))

        self.assertEqual(4, self.stats.num_os_type("Linux"))
        self.assertEqual(1, self.stats.num_os_type("FreeBSD"))

{'fraction_non_alphanumeric': 0.11442448887382808, 'fraction_numerical': 0.019993222636394442, 'mean_word_length': 2.351249053747161, 'pattern_counts': {'":': 72, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7134141', 'n_tokens_mistral': 2887, 'n_tokens_neox': 2725, 'n_words': 557}