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

import datetime

import mock
from oslo_serialization import jsonutils
import six

from nova.api.openstack import compute
from nova.compute import api as compute_api
from nova.compute import flavors
from nova import test
from nova.tests.unit.api.openstack import fakes
from nova.tests.unit import fake_instance
import nova.tests.unit.image.fake


MANUAL_INSTANCE_UUID = fakes.FAKE_UUID
AUTO_INSTANCE_UUID = fakes.FAKE_UUID.replace('a', 'b')

stub_instance = fakes.stub_instance

API_DISK_CONFIG = 'OS-DCF:diskConfig'


def instance_addresses(context, instance_id):
    return None


class DiskConfigTestCaseV21(test.TestCase):

    def setUp(self):
        super(DiskConfigTestCaseV21, self).setUp()
        fakes.stub_out_nw_api(self)
        fakes.stub_out_secgroup_api(self)
        self._set_up_app()
        self._setup_fake_image_service()

        FAKE_INSTANCES = [
            fakes.stub_instance(1,
                                uuid=MANUAL_INSTANCE_UUID,
                                auto_disk_config=False),
            fakes.stub_instance(2,
                                uuid=AUTO_INSTANCE_UUID,
                                auto_disk_config=True)
        ]

        def fake_instance_get(context, id_):
            for instance in FAKE_INSTANCES:
                if id_ == instance['id']:
                    return instance

        self.stub_out('nova.db.api.instance_get', fake_instance_get)

        def fake_instance_get_by_uuid(context, uuid,
                                      columns_to_join=None, use_slave=False):
            for instance in FAKE_INSTANCES:
                if uuid == instance['uuid']:
                    return instance

        self.stub_out('nova.db.api.instance_get_by_uuid',
                      fake_instance_get_by_uuid)

        def fake_instance_get_all(context, *args, **kwargs):
            return FAKE_INSTANCES

        self.stub_out('nova.db.api.instance_get_all', fake_instance_get_all)
        self.stub_out('nova.db.api.instance_get_all_by_filters',
                      fake_instance_get_all)

        self.stub_out('nova.objects.Instance.save',
                      lambda *args, **kwargs: None)

        def fake_rebuild(*args, **kwargs):
            pass

        self.stub_out('nova.compute.api.API.rebuild', fake_rebuild)

        def fake_instance_create(context, inst_, session=None):
            inst = fake_instance.fake_db_instance(**{
                    'id': 1,
                    'uuid': AUTO_INSTANCE_UUID,
                    'created_at': datetime.datetime(2010, 10, 10, 12, 0, 0),
                    'updated_at': datetime.datetime(2010, 10, 10, 12, 0, 0),
                    'progress': 0,
                    'name': 'instance-1',  # this is a property
                    'task_state': '',
                    'vm_state': '',
                    'auto_disk_config': inst_['auto_disk_config'],
                    'security_groups': inst_['security_groups'],
                    'instance_type': flavors.get_default_flavor(),
                    })

            def fake_instance_get_for_create(context, id_, *args, **kwargs):
                return (inst, inst)

            self.stub_out('nova.db.api.instance_update_and_get_original',
                          fake_instance_get_for_create)

            def fake_instance_get_all_for_create(context, *args, **kwargs):
                return [inst]
            self.stub_out('nova.db.api.instance_get_all',
                           fake_instance_get_all_for_create)
            self.stub_out('nova.db.api.instance_get_all_by_filters',
                           fake_instance_get_all_for_create)

            def fake_instance_add_security_group(context, instance_id,
                                                 security_group_id):
                pass

            self.stub_out('nova.db.api.instance_add_security_group',
                          fake_instance_add_security_group)

            return inst

        self.stub_out('nova.db.api.instance_create', fake_instance_create)

    def _set_up_app(self):
        self.app = compute.APIRouterV21()

    def _get_expected_msg_for_invalid_disk_config(self):
        if six.PY3:
            return ('{{"badRequest": {{"message": "Invalid input for'
                    ' field/attribute {0}. Value: {1}. \'{1}\' is'
                    ' not one of [\'AUTO\', \'MANUAL\']", "code": 400}}}}')
        else:<commit_msg>Update compute API.get() stubs for test_disk_config<commit_after># Copyright 2011 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.

import datetime

import mock
from oslo_serialization import jsonutils
import six

from nova.api.openstack import compute
from nova.compute import api as compute_api
from nova.compute import flavors
from nova import context as nova_context
from nova import exception
from nova import test
from nova.tests.unit.api.openstack import fakes
from nova.tests.unit import fake_instance
import nova.tests.unit.image.fake


MANUAL_INSTANCE_UUID = fakes.FAKE_UUID
AUTO_INSTANCE_UUID = fakes.FAKE_UUID.replace('a', 'b')

API_DISK_CONFIG = 'OS-DCF:diskConfig'


class DiskConfigTestCaseV21(test.TestCase):

    def setUp(self):
        super(DiskConfigTestCaseV21, self).setUp()
        fakes.stub_out_nw_api(self)
        fakes.stub_out_secgroup_api(self)
        self._set_up_app()
        self._setup_fake_image_service()
        ctxt = nova_context.RequestContext(
            # These values match what is used in fakes.HTTPRequest.blank.
            user_id='fake_user', project_id='fake')
        FAKE_INSTANCES = [
            fakes.stub_instance_obj(ctxt,
                                    uuid=MANUAL_INSTANCE_UUID,
                                    auto_disk_config=False),
            fakes.stub_instance_obj(ctxt,
                                    uuid=AUTO_INSTANCE_UUID,
                                    auto_disk_config=True)
        ]

        def fake_instance_get(_self, context, server_id, *args, **kwargs):
            for instance in FAKE_INSTANCES:
                if server_id == instance.uuid:
                    return instance
            raise exception.InstanceNotFound(instance_id=server_id)

        self.stub_out('nova.compute.api.API.get', fake_instance_get)
        self.stub_out('nova.objects.Instance.save',
                      lambda *args, **kwargs: None)

        def fake_rebuild(*args, **kwargs):
            pass

        self.stub_out('nova.compute.api.API.rebuild', fake_rebuild)

        def fake_instance_create(context, inst_, session=None):
            inst = fake_instance.fake_db_instance(**{
                    'id': 1,
                    'uuid': AUTO_INSTANCE_UUID,
                    'created_at': datetime.datetime(2010, 10, 10, 12, 0, 0),
                    'updated_at': datetime.datetime(2010, 10, 10, 12, 0, 0),
                    'progress': 0,
                    'name': 'instance-1',  # this is a property
                    'task_state': '',
                    'vm_state': '',
                    'auto_disk_config': inst_['auto_disk_config'],
                    'security_groups': inst_['security_groups'],
                    'instance_type': flavors.get_default_flavor(),
                    })

            return inst

        self.stub_out('nova.db.api.instance_create', fake_instance_create)

    def _set_up_app(self):
        self.app = compute.APIRouterV21()

    def _get_expected_msg_for_invalid_disk_config(self):
        if six.PY3:
            return ('{{"badRequest": {{"message": "Invalid input for'
                    ' field/attribute {0}. Value: {1}. \'{1}\' is'
                    ' not one of [\'AUTO\', \'MANUAL\']", "code": 400}}}}')
        else:
{'fraction_non_alphanumeric': 0.09190764402600314, 'fraction_numerical': 0.01098408428603452, 'mean_word_length': 2.1101429069362148, 'pattern_counts': {'":': 6, '<': 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': 7, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1214748', 'n_tokens_mistral': 2649, 'n_tokens_neox': 2535, 'n_words': 581}