<commit_before># Copyright 2012 <NAME>
#
# 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.

from mocktest import *
from pyqcy import *

import shutil
import libvirt
from hashlib import sha1

import neat.locals.manager as manager
import neat.common as common
import neat.locals.collector as collector

import logging
logging.disable(logging.CRITICAL)


class LocalManager(TestCase):

    @qc(10)
    def start(
            iterations=int_(min=0, max=10),
            time_interval=int_(min=0)
    ):
        with MockTransaction:
            state = {'property': 'value'}
            config = {
                'log_directory': 'dir',
                'log_level': 2,
                'local_manager_interval': str(time_interval)}
            paths = [manager.DEFAILT_CONFIG_PATH, manager.CONFIG_PATH]
            fields = manager.REQUIRED_FIELDS
            expect(manager).read_and_validate_config(paths, fields). \
                and_return(config).once()
            expect(common).init_logging('dir', 'local-manager.log', 2).once()
            expect(common).start(manager.init_state,
                                 manager.execute,
                                 config,
                                 time_interval).and_return(state).once()
            assert manager.start() == state

    @qc(1)
    def init_state():
        with MockTransaction:
            vir_connection = mock('virConnect')
            db = mock('db')
            mhz = 3000
            expect(libvirt).openReadOnly(None). \
                and_return(vir_connection).once()
            expect(manager).init_db('db'). \
                and_return(db).once()
            expect(common).physical_cpu_mhz_total(vir_connection). \
                and_return(mhz)
            expect(vir_connection).getHostname().and_return('host').once()
            config = {'sql_connection': 'db',
                      'os_admin_user': 'user',
                      'os_admin_password': 'password',
                      'host_cpu_usable_by_vms': 0.75}
            state = manager.init_state(config)
            assert state['previous_time'] == 0
            assert state['vir_connection'] == vir_connection
            assert state['db'] == db
            assert state['physical_cpu_mhz_total'] == mhz * 0.75
            assert state['hostname'] == 'host'
            assert state['hashed_username'] == sha1('user').hexdigest()
            assert state['hashed_password'] == sha1('password').hexdigest()

    @qc(1)
    def get_local_data(
        data=dict_(
            keys=str_(of='abc123-', min_length=36, max_length=36),
            values=list_(of=int_(min=1, max=3000),
                         min_length=0, max_length=10),
            min_length=0, max_length=5
        )
    ):
        path = os.path.join(os.path.dirname(__file__),
                            '..', 'resources', 'vms', 'tmp')
        shutil.rmtree(path, True)
        os.mkdir(path)
        collector.write_data_locally(path, data, 10)

        assert manager.get_local_data(path) == data
        shutil.rmtree(path)

    @qc(10)
    def cleanup_vm_data(
        data=dict_(
            keys=str_(of='abc123-', min_length=36, max_length=36),
            values=list_(of=int_(min=1, max=3000),
                         min_length=0, max_length=10),
            min_length=0, max_length=5
        )
    ):
        original_data = dict(data)
        uuids = data.keys()
        if data:
            n = random.randrange(len(data))
            for _ in range(n):
                uuid = random.choice(uuids)
                del data[uuid]
                uuids.remove(uuid)

        assert manager.cleanup_vm_data(original_data, uuids) == data

    @qc(10)
    def get_ram(
        data=dict_(
            keys=str_(of='abc123-', min_length=36, max_length=36),
            values=int_(min=1, max=100),
            min_length=0, max_length=10<commit_msg>Fixed a call to collector.write_vm_data_locally<commit_after># Copyright 2012 <NAME>
#
# 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.

from mocktest import *
from pyqcy import *

import shutil
import libvirt
from hashlib import sha1

import neat.locals.manager as manager
import neat.common as common
import neat.locals.collector as collector

import logging
logging.disable(logging.CRITICAL)


class LocalManager(TestCase):

    @qc(10)
    def start(
            iterations=int_(min=0, max=10),
            time_interval=int_(min=0)
    ):
        with MockTransaction:
            state = {'property': 'value'}
            config = {
                'log_directory': 'dir',
                'log_level': 2,
                'local_manager_interval': str(time_interval)}
            paths = [manager.DEFAILT_CONFIG_PATH, manager.CONFIG_PATH]
            fields = manager.REQUIRED_FIELDS
            expect(manager).read_and_validate_config(paths, fields). \
                and_return(config).once()
            expect(common).init_logging('dir', 'local-manager.log', 2).once()
            expect(common).start(manager.init_state,
                                 manager.execute,
                                 config,
                                 time_interval).and_return(state).once()
            assert manager.start() == state

    @qc(1)
    def init_state():
        with MockTransaction:
            vir_connection = mock('virConnect')
            db = mock('db')
            mhz = 3000
            expect(libvirt).openReadOnly(None). \
                and_return(vir_connection).once()
            expect(manager).init_db('db'). \
                and_return(db).once()
            expect(common).physical_cpu_mhz_total(vir_connection). \
                and_return(mhz)
            expect(vir_connection).getHostname().and_return('host').once()
            config = {'sql_connection': 'db',
                      'os_admin_user': 'user',
                      'os_admin_password': 'password',
                      'host_cpu_usable_by_vms': 0.75}
            state = manager.init_state(config)
            assert state['previous_time'] == 0
            assert state['vir_connection'] == vir_connection
            assert state['db'] == db
            assert state['physical_cpu_mhz_total'] == mhz * 0.75
            assert state['hostname'] == 'host'
            assert state['hashed_username'] == sha1('user').hexdigest()
            assert state['hashed_password'] == sha1('password').hexdigest()

    @qc(1)
    def get_local_data(
        data=dict_(
            keys=str_(of='abc123-', min_length=36, max_length=36),
            values=list_(of=int_(min=1, max=3000),
                         min_length=0, max_length=10),
            min_length=0, max_length=5
        )
    ):
        path = os.path.join(os.path.dirname(__file__),
                            '..', 'resources', 'vms', 'tmp')
        shutil.rmtree(path, True)
        os.mkdir(path)
        collector.write_vm_data_locally(path, data, 10)

        assert manager.get_local_data(path) == data
        shutil.rmtree(path)

    @qc(10)
    def cleanup_vm_data(
        data=dict_(
            keys=str_(of='abc123-', min_length=36, max_length=36),
            values=list_(of=int_(min=1, max=3000),
                         min_length=0, max_length=10),
            min_length=0, max_length=5
        )
    ):
        original_data = dict(data)
        uuids = data.keys()
        if data:
            n = random.randrange(len(data))
            for _ in range(n):
                uuid = random.choice(uuids)
                del data[uuid]
                uuids.remove(uuid)

        assert manager.cleanup_vm_data(original_data, uuids) == data

    @qc(10)
    def get_ram(
        data=dict_(
            keys=str_(of='abc123-', min_length=36, max_length=36),
            values=int_(min=1, max=100),
            min_length=0, max_length=10
{'fraction_non_alphanumeric': 0.11174134997163924, 'fraction_numerical': 0.01951219512195122, 'mean_word_length': 2.037904893177119, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6317547', 'n_tokens_mistral': 2645, 'n_tokens_neox': 2477, 'n_words': 547}