<commit_before># -*- encoding: utf-8 -*-
#
# Copyright © 2012 New Dream Network, LLC (DreamHost)
#
# Author: <NAME> <<EMAIL>>
#         <NAME> <<EMAIL>>
#
# 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 ceilometer.compute.nova_notifier
"""

import contextlib
import datetime

import mock

from stevedore import extension

## NOTE(dhellmann): These imports are not in the generally approved
## alphabetical order, but they are in the order that actually
## works. Please don't change them.
from nova.tests import fake_network
from nova.compute import vm_states
try:
    from nova.compute import flavors
except ImportError:
    from nova.compute import instance_types as flavors

from nova.objects import instance as nova_instance
from nova import config
from nova import context
from nova import db
from nova.openstack.common import importutils
from nova.openstack.common import log as logging

# This option is used in the nova_notifier module, so make
# sure it is defined.
config.cfg.CONF.import_opt('compute_manager', 'nova.service')

# HACK(jd) Import this first because of the second HACK below, and because
# of Nova not having these module yet as of this writing
from ceilometer.openstack.common import test
from ceilometer.openstack.common.fixture import config
from ceilometer.openstack.common.fixture import moxstubout

# HACK(dhellmann): Import this before any other ceilometer code
# because the notifier module messes with the import path to force
# nova's version of oslo to be used instead of ceilometer's.
from ceilometer.compute import nova_notifier

from ceilometer import sample
from ceilometer.compute.pollsters import util

LOG = logging.getLogger(__name__)
nova_CONF = config.cfg.CONF


class TestNovaNotifier(test.BaseTestCase):

    class Pollster(object):
        instances = []
        test_data_1 = sample.Sample(
            name='test1',
            type=sample.TYPE_CUMULATIVE,
            unit='units-go-here',
            volume=1,
            user_id='test',
            project_id='test',
            resource_id='test_run_tasks',
            timestamp=datetime.datetime.utcnow().isoformat(),
            resource_metadata={'name': 'Pollster',
                               },
        )

        def get_samples(self, manager, cache, instance):
            self.instances.append((manager, instance))
            test_data_2 = util.make_sample_from_instance(
                instance,
                name='test2',
                type=sample.TYPE_CUMULATIVE,
                unit='units-go-here',
                volume=1,
            )
            return [self.test_data_1, test_data_2]

    @mock.patch('ceilometer.pipeline.setup_pipeline', mock.MagicMock())
    def setUp(self):
        super(TestNovaNotifier, self).setUp()
        nova_CONF.compute_driver = 'nova.virt.fake.FakeDriver'
        nova_CONF.notification_driver = [
            nova_notifier.__name__,
            'nova.openstack.common.notifier.rpc_notifier',
        ]
        nova_CONF.rpc_backend = 'nova.openstack.common.rpc.impl_fake'
        nova_CONF.vnc_enabled = False
        nova_CONF.spice.enabled = False
        self.compute = importutils.import_object(nova_CONF.compute_manager)
        self.context = context.get_admin_context()
        self.stubs = self.useFixture(moxstubout.MoxStubout()).stubs
        fake_network.set_stub_network_methods(self.stubs)

        self.instance_data = {"display_name": "instance-1",
                              "id": 1,
                              "image_ref": "FAKE",
                              "user_id": "FAKE",
                              "project_id": "FAKE",
                              "display_name": "<NAME>",
                              "hostname": "abcdef",
                              "reservation_id": "FAKE RID",
                              "instance_type_id": 1,
                              "architecture": "x86",
                              "memory_mb": "1024",
                              "root_gb": "20",
                              "ephemeral_gb": "0",
                              "vcpus": 1,
                              'node': "fakenode",
                              "host": "fakehost",
                              "availability_zone":
                              "1e3ce043029547f1a61c1996d1a531a4",
                              "created_at": '2012-05-08 20:23:41',
                              "launched_at": '2012-05-08 20:25:45',
                              "terminated_at": '2012-05-09 20:23:41',
                              "os_type": "linux",
                              "kernel_id": "kernelid",
                              "ramdisk_id": "ramdiskid",
                              "vm_state": vm_states.ACTIVE,
                              "task_state": None,
                              "access_ip_v4": "192.168.5.4",
                              "access_ip_v6": "2001:DB8::0",
                              "metadata": {},
                              "uuid": "144e08f4-00cb-11e2-888e-5453ed1bbb5f",
                              "system_metadata": {},
                              "user_data": None,
                              "cleaned": 0,
                              "deleted": None,
                              "vm_mode": None,
                              "deleted_at": None,
                              "disable_terminate": False,
                              "root_device_name": None,
                              "default_swap_device": None,
                              "launched_on": None,
                              "display_description": None,
                              "key_data": None,
                              "key_name": None,
                              "config_drive": None,
                              "power_state": None,
                              "default_ephemeral_device": None,
                              "progress": 0,
                              "scheduled_at": None,
                              "updated_at": None,
                              "shutdown_terminate": False,
                              "cell_name": 'cell',
                              "locked": False,
                              "locked_by": None,
                              "launch_index": 0,
                              "auto_disk_config": False,
                              }<commit_msg>Fix the py27 failure because of "ephemeral_key_uuid" error<commit_after># -*- encoding: utf-8 -*-
#
# Copyright © 2012 New Dream Network, LLC (DreamHost)
#
# Author: <NAME> <<EMAIL>>
#         <NAME> <<EMAIL>>
#
# 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 ceilometer.compute.nova_notifier
"""

import contextlib
import datetime

import mock

from stevedore import extension

## NOTE(dhellmann): These imports are not in the generally approved
## alphabetical order, but they are in the order that actually
## works. Please don't change them.
from nova.tests import fake_network
from nova.compute import vm_states
try:
    from nova.compute import flavors
except ImportError:
    from nova.compute import instance_types as flavors

from nova.objects import instance as nova_instance
from nova import config
from nova import context
from nova import db
from nova.openstack.common import importutils
from nova.openstack.common import log as logging

# This option is used in the nova_notifier module, so make
# sure it is defined.
config.cfg.CONF.import_opt('compute_manager', 'nova.service')

# HACK(jd) Import this first because of the second HACK below, and because
# of Nova not having these module yet as of this writing
from ceilometer.openstack.common import test
from ceilometer.openstack.common.fixture import config
from ceilometer.openstack.common.fixture import moxstubout

# HACK(dhellmann): Import this before any other ceilometer code
# because the notifier module messes with the import path to force
# nova's version of oslo to be used instead of ceilometer's.
from ceilometer.compute import nova_notifier

from ceilometer import sample
from ceilometer.compute.pollsters import util

LOG = logging.getLogger(__name__)
nova_CONF = config.cfg.CONF


class TestNovaNotifier(test.BaseTestCase):

    class Pollster(object):
        instances = []
        test_data_1 = sample.Sample(
            name='test1',
            type=sample.TYPE_CUMULATIVE,
            unit='units-go-here',
            volume=1,
            user_id='test',
            project_id='test',
            resource_id='test_run_tasks',
            timestamp=datetime.datetime.utcnow().isoformat(),
            resource_metadata={'name': 'Pollster',
                               },
        )

        def get_samples(self, manager, cache, instance):
            self.instances.append((manager, instance))
            test_data_2 = util.make_sample_from_instance(
                instance,
                name='test2',
                type=sample.TYPE_CUMULATIVE,
                unit='units-go-here',
                volume=1,
            )
            return [self.test_data_1, test_data_2]

    @mock.patch('ceilometer.pipeline.setup_pipeline', mock.MagicMock())
    def setUp(self):
        super(TestNovaNotifier, self).setUp()
        nova_CONF.compute_driver = 'nova.virt.fake.FakeDriver'
        nova_CONF.notification_driver = [
            nova_notifier.__name__,
            'nova.openstack.common.notifier.rpc_notifier',
        ]
        nova_CONF.rpc_backend = 'nova.openstack.common.rpc.impl_fake'
        nova_CONF.vnc_enabled = False
        nova_CONF.spice.enabled = False
        self.compute = importutils.import_object(nova_CONF.compute_manager)
        self.context = context.get_admin_context()
        self.stubs = self.useFixture(moxstubout.MoxStubout()).stubs
        fake_network.set_stub_network_methods(self.stubs)

        self.instance_data = {"display_name": "instance-1",
                              "id": 1,
                              "image_ref": "FAKE",
                              "user_id": "FAKE",
                              "project_id": "FAKE",
                              "display_name": "<NAME>",
                              "hostname": "abcdef",
                              "reservation_id": "FAKE RID",
                              "instance_type_id": 1,
                              "architecture": "x86",
                              "memory_mb": "1024",
                              "root_gb": "20",
                              "ephemeral_gb": "0",
                              "vcpus": 1,
                              'node': "fakenode",
                              "host": "fakehost",
                              "availability_zone":
                              "1e3ce043029547f1a61c1996d1a531a4",
                              "created_at": '2012-05-08 20:23:41',
                              "launched_at": '2012-05-08 20:25:45',
                              "terminated_at": '2012-05-09 20:23:41',
                              "os_type": "linux",
                              "kernel_id": "kernelid",
                              "ramdisk_id": "ramdiskid",
                              "vm_state": vm_states.ACTIVE,
                              "task_state": None,
                              "access_ip_v4": "192.168.5.4",
                              "access_ip_v6": "2001:DB8::0",
                              "metadata": {},
                              "uuid": "144e08f4-00cb-11e2-888e-5453ed1bbb5f",
                              "system_metadata": {},
                              "user_data": None,
                              "cleaned": 0,
                              "deleted": None,
                              "vm_mode": None,
                              "deleted_at": None,
                              "disable_terminate": False,
                              "root_device_name": None,
                              "default_swap_device": None,
                              "launched_on": None,
                              "display_description": None,
                              "key_data": None,
                              "key_name": None,
                              "config_drive": None,
                              "power_state": None,
                              "default_ephemeral_device": None,
                              "progress": 0,
                              "scheduled_at": None,
                              "updated_at": None,
                              "shutdown_terminate": False,
                              "cell_name": 'cell',
                              "locked": False,
                              "locked_by": None,
                              "launch_index": 0,
                              "auto_disk_config": False,
                              "ephemeral_key_uuid": None
                              }
{'fraction_non_alphanumeric': 0.09011544011544012, 'fraction_numerical': 0.01948051948051948, 'mean_word_length': 1.520181818181818, 'pattern_counts': {'":': 107, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 2, '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': '1178746', 'n_tokens_mistral': 3773, 'n_tokens_neox': 3592, 'n_words': 899}