<commit_before># Copyright 2014 Red Hat, Inc.
#
# 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.

import datetime

from nova import exception
from nova import objects
from nova import test


class TestImageMeta(test.NoDBTestCase):
    def test_basic_attrs(self):
        image = {'status': 'active',
                 'container_format': 'bare',
                 'min_ram': 0,
                 'updated_at': '2014-12-12T11:16:36.000000',
                 # Testing string -> int conversion
                 'min_disk': '0',
                 'owner': '2d8b9502858c406ebee60f0849486222',
                 # Testing string -> bool conversion
                 'protected': 'yes',
                 'properties': {
                     'os_type': 'Linux',
                     'hw_video_model': 'vga',
                     'hw_video_ram': '512',
                     'hw_qemu_guest_agent': 'yes',
                     'hw_scsi_model': 'virtio-scsi',
                 },
                 'size': 213581824,
                 'name': 'f16-x86_64-openstack-sda',
                 'checksum': '755122332caeb9f661d5c978adb8b45f',
                 'created_at': '2014-12-10T16:23:14.000000',
                 'disk_format': 'qcow2',
                 'id': 'c8b1790e-a07d-4971-b137-44f2432936cd'
        }
        image_meta = objects.ImageMeta.from_dict(image)

        self.assertEqual('active', image_meta.status)
        self.assertEqual('bare', image_meta.container_format)
        self.assertEqual(0, image_meta.min_ram)
        self.assertIsInstance(image_meta.updated_at, datetime.datetime)
        self.assertEqual(0, image_meta.min_disk)
        self.assertEqual('2d8b9502858c406ebee60f0849486222', image_meta.owner)
        self.assertTrue(image_meta.protected)
        self.assertEqual(213581824, image_meta.size)
        self.assertEqual('f16-x86_64-openstack-sda', image_meta.name)
        self.assertEqual('755122332caeb9f661d5c978adb8b45f',
                         image_meta.checksum)
        self.assertIsInstance(image_meta.created_at, datetime.datetime)
        self.assertEqual('qcow2', image_meta.disk_format)
        self.assertEqual('c8b1790e-a07d-4971-b137-44f2432936cd', image_meta.id)

        self.assertIsInstance(image_meta.properties, objects.ImageMetaProps)

    def test_no_props(self):
        image_meta = objects.ImageMeta.from_dict({})

        self.assertIsInstance(image_meta.properties, objects.ImageMetaProps)

    def test_volume_backed_image(self):
        image = {'container_format': None,
                 'size': 0,
                 'checksum': None,
                 'disk_format': None,
        }
        image_meta = objects.ImageMeta.from_dict(image)

        self.assertEqual('', image_meta.container_format)
        self.assertEqual(0, image_meta.size)
        self.assertEqual('', image_meta.checksum)
        self.assertEqual('', image_meta.disk_format)

    def test_null_substitution(self):
        image = {'name': None,
                 'checksum': None,
                 'owner': None,
                 'size': None,
                 'virtual_size': None,
                 'container_format': None,
                 'disk_format': None,
        }
        image_meta = objects.ImageMeta.from_dict(image)

        self.assertEqual('', image_meta.name)
        self.assertEqual('', image_meta.checksum)
        self.assertEqual('', image_meta.owner)
        self.assertEqual(0, image_meta.size)
        self.assertEqual(0, image_meta.virtual_size)
        self.assertEqual('', image_meta.container_format)
        self.assertEqual('', image_meta.disk_format)


class TestImageMetaProps(test.NoDBTestCase):
    def test_normal_props(self):
        props = {'os_type': 'windows',
                 'hw_video_model': 'vga',
                 'hw_video_ram': '512',
                 'hw_qemu_guest_agent': 'yes',
                 # Fill sane values for the rest here
                 }
        virtprops = objects.ImageMetaProps.from_dict(props)
        self.assertEqual('windows', virtprops.os_type)
        self.assertEqual('vga', virtprops.hw_video_model)
        self.assertEqual(512, virtprops.hw_video_ram)
        self.assertEqual(True, virtprops.hw_qemu_guest_agent)

    def test_default_props(self):
        props = {}
        virtprops = objects.ImageMetaProps.from_dict(props)

        for prop in virtprops.fields:
            self.assertIsNone(virtprops.get(prop))

    def test_default_prop_value(self):
        props = {}
        virtprops = objects.ImageMetaProps.from_dict(props)

        self.assertEqual("hvm", virtprops.get("hw_vm_mode", "hvm"))

    def test_non_existent_prop(self):
        props = {}
        virtprops = objects.ImageMetaProps.from_dict(props)

        self.assertRaises(AttributeError,
                          virtprops.get,
                          "doesnotexist")

    def test_legacy_compat(self):
        legacy_props = {
            'architecture': 'x86_64',
            'owner_id': '123',
            'vmware_adaptertype': 'lsiLogic',
            'vmware_disktype': 'preallocated',
            'vmware_image_version': '2',
            'vmware_ostype': 'rhel3_64Guest',
            'auto_disk_config': 'yes',
            'ipxe_boot': 'yes',
            'xenapi_device_id': '3',
            'xenapi_image_compression_level': '2',
            'vmware_linked_clone': 'false',
            'xenapi_use_agent': 'yes',
            'xenapi_skip_agent_inject_ssh': 'no',
            'xenapi_skip_agent_inject_files_at_boot': 'no',
            'cache_in_nova': 'yes',
            'vm_mode': 'hvm',
            'bittorrent': 'yes',
            'mappings': [],
            'block_device_mapping': [],
            'bdm_v2': 'yes',
            'root_device_name': '/dev/vda',
        }

        image_meta = objects.ImageMetaProps.from_dict(legacy_props)

        self.assertEqual('x86_64', image_meta.hw_architecture)
        self.assertEqual('123', image_meta.img_owner_id)
        self.assertEqual('lsilogic', image_meta.hw_scsi_model)
        self.assertEqual('preallocated', image_meta.hw_disk_type)
        self.assertEqual(2, image_meta.img_version)
        self.assertEqual('rhel3_64Guest', image_meta.os_distro)
        self.assertTrue(image_meta.hw_auto_disk_config)
        self.assertTrue(image_meta.hw_ipxe_boot)
        self.assertEqual(3, image_meta.hw_device_id)
        self.assertEqual(2, image_meta.img_compression_level)
        self.assertFalse(image_meta.img_linked_clone)
        self.assertTrue(image_meta.img_use_agent)
        self.assertFalse(image_meta.os_skip_agent_inject_ssh)
        self.assertFalse(image_meta.os_skip_agent_inject_files_at_boot)
        self.assertTrue(image_meta.img_cache_in_nova)
        self.assertTrue(image_meta.img_bittorrent)
        self.assertEqual([], image_meta.img_mappings)
        self.assertEqual([], image_meta.img_block_device_mapping)
        self.assertTrue(image_meta.img_bdm_v2)
        self.assertEqual("/dev/vda", image_meta.img_root_device_name)

    def test_legacy_compat_vmware_adapter_types(self):
        legacy_types = ['lsiLogic', 'busLogic', 'ide', 'lsiLogicsas',
                        'paraVirtual', None, '']

        for legacy_type in legacy_types:
            legacy_props = {
                'vmware_adaptertype': legacy_type,
            }

            image_meta = objects.ImageMetaProps.from_dict(legacy_props)
            if legacy_type == 'ide':
                self.assertEqual('ide', image_meta.hw_disk_bus)
            elif not legacy_type:
                self.assertFalse(image_meta.obj_attr_is_set('hw_disk_bus'))
                self.assertFalse(image_meta.obj_attr_is_set('hw_scsi_model'))
            else:
                self.assertEqual('scsi', image_meta.hw_disk_bus)
                if legacy_type == 'lsiLogicsas':
                    expected = 'lsisas1068'
                elif legacy_type == 'paraVirtual':
                    expected = 'vmpvscsi'
                else:
                    expected = legacy_type.lower()
                self.assertEqual(expected, image_meta.hw_scsi_model)

    def test_duplicate_legacy_and_normal_props(self):<commit_msg>Add hv testing for ImageMetaProps._legacy_property_map<commit_after># Copyright 2014 Red Hat, Inc.
#
# 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.

import datetime

from nova import exception
from nova import objects
from nova import test


class TestImageMeta(test.NoDBTestCase):
    def test_basic_attrs(self):
        image = {'status': 'active',
                 'container_format': 'bare',
                 'min_ram': 0,
                 'updated_at': '2014-12-12T11:16:36.000000',
                 # Testing string -> int conversion
                 'min_disk': '0',
                 'owner': '2d8b9502858c406ebee60f0849486222',
                 # Testing string -> bool conversion
                 'protected': 'yes',
                 'properties': {
                     'os_type': 'Linux',
                     'hw_video_model': 'vga',
                     'hw_video_ram': '512',
                     'hw_qemu_guest_agent': 'yes',
                     'hw_scsi_model': 'virtio-scsi',
                 },
                 'size': 213581824,
                 'name': 'f16-x86_64-openstack-sda',
                 'checksum': '755122332caeb9f661d5c978adb8b45f',
                 'created_at': '2014-12-10T16:23:14.000000',
                 'disk_format': 'qcow2',
                 'id': 'c8b1790e-a07d-4971-b137-44f2432936cd'
        }
        image_meta = objects.ImageMeta.from_dict(image)

        self.assertEqual('active', image_meta.status)
        self.assertEqual('bare', image_meta.container_format)
        self.assertEqual(0, image_meta.min_ram)
        self.assertIsInstance(image_meta.updated_at, datetime.datetime)
        self.assertEqual(0, image_meta.min_disk)
        self.assertEqual('2d8b9502858c406ebee60f0849486222', image_meta.owner)
        self.assertTrue(image_meta.protected)
        self.assertEqual(213581824, image_meta.size)
        self.assertEqual('f16-x86_64-openstack-sda', image_meta.name)
        self.assertEqual('755122332caeb9f661d5c978adb8b45f',
                         image_meta.checksum)
        self.assertIsInstance(image_meta.created_at, datetime.datetime)
        self.assertEqual('qcow2', image_meta.disk_format)
        self.assertEqual('c8b1790e-a07d-4971-b137-44f2432936cd', image_meta.id)

        self.assertIsInstance(image_meta.properties, objects.ImageMetaProps)

    def test_no_props(self):
        image_meta = objects.ImageMeta.from_dict({})

        self.assertIsInstance(image_meta.properties, objects.ImageMetaProps)

    def test_volume_backed_image(self):
        image = {'container_format': None,
                 'size': 0,
                 'checksum': None,
                 'disk_format': None,
        }
        image_meta = objects.ImageMeta.from_dict(image)

        self.assertEqual('', image_meta.container_format)
        self.assertEqual(0, image_meta.size)
        self.assertEqual('', image_meta.checksum)
        self.assertEqual('', image_meta.disk_format)

    def test_null_substitution(self):
        image = {'name': None,
                 'checksum': None,
                 'owner': None,
                 'size': None,
                 'virtual_size': None,
                 'container_format': None,
                 'disk_format': None,
        }
        image_meta = objects.ImageMeta.from_dict(image)

        self.assertEqual('', image_meta.name)
        self.assertEqual('', image_meta.checksum)
        self.assertEqual('', image_meta.owner)
        self.assertEqual(0, image_meta.size)
        self.assertEqual(0, image_meta.virtual_size)
        self.assertEqual('', image_meta.container_format)
        self.assertEqual('', image_meta.disk_format)


class TestImageMetaProps(test.NoDBTestCase):
    def test_normal_props(self):
        props = {'os_type': 'windows',
                 'hw_video_model': 'vga',
                 'hw_video_ram': '512',
                 'hw_qemu_guest_agent': 'yes',
                 # Fill sane values for the rest here
                 }
        virtprops = objects.ImageMetaProps.from_dict(props)
        self.assertEqual('windows', virtprops.os_type)
        self.assertEqual('vga', virtprops.hw_video_model)
        self.assertEqual(512, virtprops.hw_video_ram)
        self.assertEqual(True, virtprops.hw_qemu_guest_agent)

    def test_default_props(self):
        props = {}
        virtprops = objects.ImageMetaProps.from_dict(props)

        for prop in virtprops.fields:
            self.assertIsNone(virtprops.get(prop))

    def test_default_prop_value(self):
        props = {}
        virtprops = objects.ImageMetaProps.from_dict(props)

        self.assertEqual("hvm", virtprops.get("hw_vm_mode", "hvm"))

    def test_non_existent_prop(self):
        props = {}
        virtprops = objects.ImageMetaProps.from_dict(props)

        self.assertRaises(AttributeError,
                          virtprops.get,
                          "doesnotexist")

    def test_legacy_compat(self):
        legacy_props = {
            'architecture': 'x86_64',
            'owner_id': '123',
            'vmware_adaptertype': 'lsiLogic',
            'vmware_disktype': 'preallocated',
            'vmware_image_version': '2',
            'vmware_ostype': 'rhel3_64Guest',
            'auto_disk_config': 'yes',
            'ipxe_boot': 'yes',
            'xenapi_device_id': '3',
            'xenapi_image_compression_level': '2',
            'vmware_linked_clone': 'false',
            'xenapi_use_agent': 'yes',
            'xenapi_skip_agent_inject_ssh': 'no',
            'xenapi_skip_agent_inject_files_at_boot': 'no',
            'cache_in_nova': 'yes',
            'vm_mode': 'hvm',
            'bittorrent': 'yes',
            'mappings': [],
            'block_device_mapping': [],
            'bdm_v2': 'yes',
            'root_device_name': '/dev/vda',
            'hypervisor_version_requires': '>=1.5.3',
            'hypervisor_type': 'qemu',
        }

        image_meta = objects.ImageMetaProps.from_dict(legacy_props)

        self.assertEqual('x86_64', image_meta.hw_architecture)
        self.assertEqual('123', image_meta.img_owner_id)
        self.assertEqual('lsilogic', image_meta.hw_scsi_model)
        self.assertEqual('preallocated', image_meta.hw_disk_type)
        self.assertEqual(2, image_meta.img_version)
        self.assertEqual('rhel3_64Guest', image_meta.os_distro)
        self.assertTrue(image_meta.hw_auto_disk_config)
        self.assertTrue(image_meta.hw_ipxe_boot)
        self.assertEqual(3, image_meta.hw_device_id)
        self.assertEqual(2, image_meta.img_compression_level)
        self.assertFalse(image_meta.img_linked_clone)
        self.assertTrue(image_meta.img_use_agent)
        self.assertFalse(image_meta.os_skip_agent_inject_ssh)
        self.assertFalse(image_meta.os_skip_agent_inject_files_at_boot)
        self.assertTrue(image_meta.img_cache_in_nova)
        self.assertTrue(image_meta.img_bittorrent)
        self.assertEqual([], image_meta.img_mappings)
        self.assertEqual([], image_meta.img_block_device_mapping)
        self.assertTrue(image_meta.img_bdm_v2)
        self.assertEqual("/dev/vda", image_meta.img_root_device_name)
        self.assertEqual('>=1.5.3', image_meta.img_hv_requested_version)
        self.assertEqual('qemu', image_meta.img_hv_type)

    def test_legacy_compat_vmware_adapter_types(self):
        legacy_types = ['lsiLogic', 'busLogic', 'ide', 'lsiLogicsas',
                        'paraVirtual', None, '']

        for legacy_type in legacy_types:
            legacy_props = {
                'vmware_adaptertype': legacy_type,
            }

            image_meta = objects.ImageMetaProps.from_dict(legacy_props)
            if legacy_type == 'ide':
                self.assertEqual('ide', image_meta.hw_disk_bus)
            elif not legacy_type:
                self.assertFalse(image_meta.obj_attr_is_set('hw_disk_bus'))
                self.assertFalse(image_meta.obj_attr_is_set('hw_scsi_model'))
            else:
                self.assertEqual('scsi', image_meta.hw_disk_bus)
                if legacy_type == 'lsiLogicsas':
                    expected = 'lsisas1068'
                elif legacy_type == 'paraVirtual':
                    expected = 'vmpvscsi'
                else:
                    expected = legacy_type.lower()
                self.assertEqual(expected, image_meta.hw_scsi_model)

    def test_duplicate_legacy_and_normal_props(self):
{'fraction_non_alphanumeric': 0.10622647964420116, 'fraction_numerical': 0.030334131599954385, 'mean_word_length': 2.3382185001903313, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '1875455', 'n_tokens_mistral': 5609, 'n_tokens_neox': 5218, 'n_words': 845}