<commit_before># vim: tabstop=4 shiftwidth=4 softtabstop=4

# Copyright 2010 OpenStack LLC.
# 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 of the new image services, both as a service layer,
and as a WSGI layer
"""

import json
import datetime
import logging
import unittest

import stubout
import webob

from nova import exception
from nova import flags
from nova import utils
import nova.api.openstack
from nova.api.openstack import images
from nova.tests.api.openstack import fakes


FLAGS = flags.FLAGS


class BaseImageServiceTests(object):

    """Tasks to test for all image services"""

    def test_create(self):

        fixture = {'name': 'test image',
                   'updated': None,
                   'created': None,
                   'status': None,
                   'serverId': None,
                   'progress': None}

        num_images = len(self.service.index())

        id = self.service.create(fixture)

        self.assertNotEquals(None, id)
        self.assertEquals(num_images + 1, len(self.service.index()))

    def test_create_and_show_non_existing_image(self):

        fixture = {'name': 'test image',
                   'updated': None,
                   'created': None,
                   'status': None,
                   'serverId': None,
                   'progress': None}

        num_images = len(self.service.index())

        id = self.service.create(fixture)

        self.assertNotEquals(None, id)

        self.assertRaises(exception.NotFound,
                          self.service.show,
                          'bad image id')

    def test_update(self):

        fixture = {'name': 'test image',
                   'updated': None,
                   'created': None,
                   'status': None,
                   'serverId': None,
                   'progress': None}

        id = self.service.create(fixture)

        fixture['status'] = 'in progress'
        
        self.service.update(id, fixture)
        new_image_data = self.service.show(id)
        self.assertEquals('in progress', new_image_data['status'])

    def test_delete(self):

        fixtures = [
                    {'name': 'test image 1',
                     'updated': None,
                     'created': None,
                     'status': None,
                     'serverId': None,
                     'progress': None},
                    {'name': 'test image 2',
                     'updated': None,
                     'created': None,
                     'status': None,
                     'serverId': None,
                     'progress': None}]

        num_images = len(self.service.index())
        self.assertEquals(0, num_images, str(self.service.index()))

        ids = []
        for fixture in fixtures:
            new_id = self.service.create(fixture)
            ids.append(new_id)

        num_images = len(self.service.index())
        self.assertEquals(2, num_images, str(self.service.index()))
        
        self.service.delete(ids[0])

        num_images = len(self.service.index())
        self.assertEquals(1, num_images)


class LocalImageServiceTest(unittest.TestCase,
                            BaseImageServiceTests):

    """Tests the local image service"""

    def setUp(self):
        self.stubs = stubout.StubOutForTesting()
        self.service = utils.import_object('nova.image.service.LocalImageService')

    def tearDown(self):
        self.service.delete_all()
        self.stubs.UnsetAll()


class GlanceImageServiceTest(unittest.TestCase,
                             BaseImageServiceTests):

    """Tests the local image service"""

    def setUp(self):
        self.stubs = stubout.StubOutForTesting()
        fakes.stub_out_glance(self.stubs)
        self.service = utils.import_object('nova.image.services.glance.GlanceImageService')
        self.service.delete_all()

    def tearDown(self):
        self.stubs.UnsetAll()


class ImageControllerWithGlanceServiceTest(unittest.TestCase):

    """Test of the OpenStack API /images application controller"""

    # Registered images at start of each test.

    IMAGE_FIXTURES = [
        {'id': '23g2ogk23k4hhkk4k42l',
         'name': 'public image #1',
         'created_at': str(datetime.datetime.utcnow()),
         'modified_at': str(datetime.datetime.utcnow()),
         'deleted_at': None,
         'deleted': False,
         'is_public': True,
         'status': 'available',
         'image_type': 'kernel'
        },
        {'id': 'slkduhfas73kkaskgdas',
         'name': 'public image #2',
         'created_at': str(datetime.datetime.utcnow()),
         'modified_at': str(datetime.datetime.utcnow()),
         'deleted_at': None,
         'deleted': False,
         'is_public': True,
         'status': 'available',
         'image_type': 'ramdisk'
        },
    ]

    def setUp(self):<commit_msg>Fix typo in test case<commit_after># vim: tabstop=4 shiftwidth=4 softtabstop=4

# Copyright 2010 OpenStack LLC.
# 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 of the new image services, both as a service layer,
and as a WSGI layer
"""

import json
import datetime
import logging
import unittest

import stubout
import webob

from nova import exception
from nova import flags
from nova import utils
import nova.api.openstack
from nova.api.openstack import images
from nova.tests.api.openstack import fakes


FLAGS = flags.FLAGS


class BaseImageServiceTests(object):

    """Tasks to test for all image services"""

    def test_create(self):

        fixture = {'name': 'test image',
                   'updated': None,
                   'created': None,
                   'status': None,
                   'serverId': None,
                   'progress': None}

        num_images = len(self.service.index())

        id = self.service.create(fixture)

        self.assertNotEquals(None, id)
        self.assertEquals(num_images + 1, len(self.service.index()))

    def test_create_and_show_non_existing_image(self):

        fixture = {'name': 'test image',
                   'updated': None,
                   'created': None,
                   'status': None,
                   'serverId': None,
                   'progress': None}

        num_images = len(self.service.index())

        id = self.service.create(fixture)

        self.assertNotEquals(None, id)

        self.assertRaises(exception.NotFound,
                          self.service.show,
                          'bad image id')

    def test_update(self):

        fixture = {'name': 'test image',
                   'updated': None,
                   'created': None,
                   'status': None,
                   'serverId': None,
                   'progress': None}

        id = self.service.create(fixture)

        fixture['status'] = 'in progress'
        
        self.service.update(id, fixture)
        new_image_data = self.service.show(id)
        self.assertEquals('in progress', new_image_data['status'])

    def test_delete(self):

        fixtures = [
                    {'name': 'test image 1',
                     'updated': None,
                     'created': None,
                     'status': None,
                     'serverId': None,
                     'progress': None},
                    {'name': 'test image 2',
                     'updated': None,
                     'created': None,
                     'status': None,
                     'serverId': None,
                     'progress': None}]

        num_images = len(self.service.index())
        self.assertEquals(0, num_images, str(self.service.index()))

        ids = []
        for fixture in fixtures:
            new_id = self.service.create(fixture)
            ids.append(new_id)

        num_images = len(self.service.index())
        self.assertEquals(2, num_images, str(self.service.index()))
        
        self.service.delete(ids[0])

        num_images = len(self.service.index())
        self.assertEquals(1, num_images)


class LocalImageServiceTest(unittest.TestCase,
                            BaseImageServiceTests):

    """Tests the local image service"""

    def setUp(self):
        self.stubs = stubout.StubOutForTesting()
        self.service = utils.import_object('nova.image.service.LocalImageService')

    def tearDown(self):
        self.service.delete_all()
        self.stubs.UnsetAll()


class GlanceImageServiceTest(unittest.TestCase,
                             BaseImageServiceTests):

    """Tests the local image service"""

    def setUp(self):
        self.stubs = stubout.StubOutForTesting()
        fakes.stub_out_glance(self.stubs)
        self.service = utils.import_object('nova.image.services.glance.GlanceImageService')
        self.service.delete_all()

    def tearDown(self):
        self.stubs.UnsetAll()


class ImageControllerWithGlanceServiceTest(unittest.TestCase):

    """Test of the OpenStack API /images application controller"""

    # Registered images at start of each test.

    IMAGE_FIXTURES = [
        {'id': '23g2ogk23k4hhkk4k42l',
         'name': 'public image #1',
         'created_at': str(datetime.datetime.utcnow()),
         'updated_at': str(datetime.datetime.utcnow()),
         'deleted_at': None,
         'deleted': False,
         'is_public': True,
         'status': 'available',
         'image_type': 'kernel'
        },
        {'id': 'slkduhfas73kkaskgdas',
         'name': 'public image #2',
         'created_at': str(datetime.datetime.utcnow()),
         'updated_at': str(datetime.datetime.utcnow()),
         'deleted_at': None,
         'deleted': False,
         'is_public': True,
         'status': 'available',
         'image_type': 'ramdisk'
        },
    ]

    def setUp(self):
{'fraction_non_alphanumeric': 0.11033468186833395, 'fraction_numerical': 0.00570062522986392, 'mean_word_length': 1.993944398568676, 'pattern_counts': {'":': 0, '<': 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': '5817319', 'n_tokens_mistral': 3070, 'n_tokens_neox': 2959, 'n_words': 767}