示例#1
0
 def test__set_compute_images(self, mock_glance):
     mock_glanceclient = mock.MagicMock()
     mock_glanceclient.images.list.return_value = [
         fakes.FakeImage(id="id1", name="cirros1"),
         fakes.FakeImage(id="id2", name="cirros2")
     ]
     mock_glance.Client.return_value = mock_glanceclient
     self.conf_generator._set_compute_images()
     expected = ("id1", "id2")
     results = (self.conf_generator.conf.get("compute", "image_ref"),
                self.conf_generator.conf.get("compute", "image_ref_alt"))
     self.assertEqual(sorted(expected), sorted(results))
示例#2
0
 def test__set_compute_images_create(self, mock_open, mock_glance):
     mock_glanceclient = mock.MagicMock()
     mock_glanceclient.images.list.return_value = []
     mock_glanceclient.images.create.side_effect = [
         fakes.FakeImage(id="id1"),
         fakes.FakeImage(id="id2")
     ]
     mock_glance.Client.return_value = mock_glanceclient
     self.conf_generator._set_compute_images()
     self.assertEqual(mock_glanceclient.images.create.call_count, 2)
     expected = ("id1", "id2")
     results = (self.conf_generator.conf.get("compute", "image_ref"),
                self.conf_generator.conf.get("compute", "image_ref_alt"))
     self.assertEqual(sorted(expected), sorted(results))
    def test_image_valid_on_flavor_fail(self, mock_osclients):
        fakegclient = fakes.FakeGlanceClient()
        image = fakes.FakeImage()
        image.min_ram = 1
        image.size = 1
        image.min_disk = 1
        fakegclient.images.get = mock.MagicMock(return_value=image)
        mock_osclients.glance.return_value = fakegclient

        fakenclient = fakes.FakeNovaClient()
        flavor = fakes.FakeFlavor()
        flavor.ram = 0
        flavor.disk = 0
        fakenclient.flavors.get = mock.MagicMock(return_value=flavor)
        mock_osclients.nova.return_value = fakenclient

        validator = validation.image_valid_on_flavor("flavor", "image")

        result = validator(clients=mock_osclients,
                           flavor={"id": flavor.id},
                           image={"id": image.id})

        fakenclient.flavors.get.assert_called_once_with(flavor=flavor.id)
        fakegclient.images.get.assert_called_once_with(image=image.id)

        self.assertFalse(result.is_valid)
        self.assertIsNotNone(result.msg)
示例#4
0
 def test_create_image_and_boot_instances(self, mock_osclients,
                                          mock_create_image,
                                          mock_boot_servers,
                                          mock_random_name):
     glance_scenario = images.GlanceImages()
     nova_scenario = servers.NovaServers()
     fc = fakes.FakeClients()
     mock_osclients.Clients.return_value = fc
     fake_glance = fakes.FakeGlanceClient()
     fc.glance = lambda: fake_glance
     fake_nova = fakes.FakeNovaClient()
     fc.nova = lambda: fake_nova
     user_endpoint = endpoint.Endpoint("url", "user", "password", "tenant")
     nova_scenario._clients = osclients.Clients(user_endpoint)
     fake_image = fakes.FakeImage()
     fake_servers = [object() for i in range(5)]
     mock_create_image.return_value = fake_image
     mock_boot_servers.return_value = fake_servers
     mock_random_name.return_value = "random_name"
     kwargs = {'fakearg': 'f'}
     with mock.patch("rally.benchmark.scenarios.glance.utils.time.sleep"):
         glance_scenario.\
             create_image_and_boot_instances("cf", "url", "df",
                                             "fid", 5, **kwargs)
         mock_create_image.assert_called_once_with("random_name", "cf",
                                                   "url", "df", **kwargs)
         mock_boot_servers.assert_called_once_with("random_name",
                                                   "image-id-0", "fid", 5,
                                                   **kwargs)
示例#5
0
    def test_image_valid_on_missing_flavor_disk(self, mock_osclients):
        fakegclient = fakes.FakeGlanceClient()
        image = fakes.FakeImage()
        image.min_ram = 0
        image.size = 0
        image.min_disk = 100
        fakegclient.images.get = mock.MagicMock(return_value=image)
        mock_osclients.glance.return_value = fakegclient

        fakenclient = fakes.FakeNovaClient()
        flavor = fakes.FakeFlavor()
        flavor.ram = 1
        fakenclient.flavors.get = mock.MagicMock(return_value=flavor)
        mock_osclients.nova.return_value = fakenclient

        validator = validation.image_valid_on_flavor("flavor", "image")

        config = {
            "args": {
                "image": {
                    "id": image.id
                },
                "flavor": {
                    "id": flavor.id
                }
            }
        }
        result = validator(config, clients=mock_osclients, task=None)

        fakenclient.flavors.get.assert_called_once_with(flavor=flavor.id)
        fakegclient.images.get.assert_called_once_with(image=image.id)

        self.assertTrue(result.is_valid)
示例#6
0
    def test_image_invalid_on_size(self, mock_osclients):
        fakegclient = fakes.FakeGlanceClient()
        image = fakes.FakeImage()
        image.min_ram = 0
        image.size = 0
        image.min_disk = 100
        fakegclient.images.get = mock.MagicMock(return_value=image)
        mock_osclients.glance.return_value = fakegclient

        fakenclient = fakes.FakeNovaClient()
        flavor = fakes.FakeFlavor()
        flavor.ram = 1
        flavor.disk = 99
        fakenclient.flavors.get = mock.MagicMock(return_value=flavor)
        mock_osclients.nova.return_value = fakenclient

        validator = validation.image_valid_on_flavor("flavor", "image")

        config = {
            "args": {
                "image": {
                    "id": image.id
                },
                "flavor": {
                    "id": flavor.id
                }
            }
        }
        result = validator(config, clients=mock_osclients, task=None)

        fakenclient.flavors.get.assert_called_once_with(flavor=flavor.id)
        fakegclient.images.get.assert_called_once_with(image=image.id)

        self.assertFalse(result.is_valid)
        self.assertEqual(
            result.msg,
            _("The disk size for flavor 'flavor-id-0'"
              " is too small for requested image 'image-id-0'"))
示例#7
0
class ImageGeneratorTestCase(test.TestCase):
    def test_init(self):
        context = {}
        context["task"] = mock.MagicMock()
        context["config"] = {
            "images": {
                "image_url": "mock_url",
                "image_type": "qcow2",
                "image_container": "bare",
                "images_per_tenant": 4,
            }
        }

        new_context = copy.deepcopy(context)
        new_context["images"] = []
        images.ImageGenerator(context)
        self.assertEqual(new_context, context)

    def test_init_validation(self):
        context = {}
        context["task"] = mock.MagicMock()
        context["config"] = {"images": {"image_url": "mock_url"}}

        self.assertRaises(jsonschema.ValidationError,
                          images.ImageGenerator.validate, context)

    @mock.patch("%s.glance.utils.GlanceScenario._create_image" % SCN,
                return_value=fakes.FakeImage(id="uuid"))
    @mock.patch("%s.images.osclients" % CTX)
    def test_setup(self, mock_osclients, mock_image_create):

        fc = fakes.FakeClients()
        mock_osclients.Clients.return_value = fc

        image_list = ["uuid"] * 5
        image_key = [{
            'image_id': image_list,
            'endpoint': 'endpoint',
            'tenant_id': i
        } for i in range(2)]
        user_key = [{
            'id': i,
            'tenant_id': j,
            'endpoint': 'endpoint'
        } for j in range(2) for i in range(5)]

        real_context = {
            "config": {
                "users": {
                    "tenants": 2,
                    "users_per_tenant": 5,
                    "concurrent": 10,
                },
                "images": {
                    "image_url": "mock_url",
                    "image_type": "qcow2",
                    "image_container": "bare",
                    "images_per_tenant": 5,
                }
            },
            "admin": {
                "endpoint": mock.MagicMock()
            },
            "task": mock.MagicMock(),
            "users": user_key,
        }

        new_context = copy.deepcopy(real_context)
        new_context["images"] = image_key

        images_ctx = images.ImageGenerator(real_context)
        images_ctx.setup()
        self.assertEqual(new_context, real_context)

    @mock.patch("%s.images.osclients" % CTX)
    @mock.patch("%s.cleanup.utils.delete_glance_resources" % CTX)
    def test_cleanup(self, mock_image_remover, mock_osclients):
        image_list = ["uuid"] * 5
        image_key = [{
            'image_id': image_list,
            'endpoint': 'endpoint',
            'tenant_id': i
        } for i in range(2)]
        user_key = [{
            'id': i,
            'tenant_id': j,
            'endpoint': 'endpoint'
        } for j in range(2) for i in range(5)]

        context = {
            "config": {
                "users": {
                    "tenants": 2,
                    "users_per_tenant": 5,
                    "concurrent": 10,
                },
                "images": {
                    "image_url": "mock_url",
                    "image_type": "qcow2",
                    "image_container": "bare",
                    "images_per_tenant": 5,
                }
            },
            "admin": {
                "endpoint": mock.MagicMock()
            },
            "task": mock.MagicMock(),
            "users": user_key,
            "images": image_key,
        }

        images_ctx = images.ImageGenerator(context)
        images_ctx.cleanup()

        self.assertEqual(2, len(mock_image_remover.mock_calls))

        mock_image_remover.side_effect = Exception('failed_deletion')
        self.assertRaises(exceptions.ImageCleanUpException, images_ctx.cleanup)

    def test_validate_semantic(self):
        users = [fakes.FakeClients()]
        images.ImageGenerator.validate_semantic(None, None, users, None)

    @mock.patch("%s.images.osclients.Clients.glance" % CTX)
    def test_validate_semantic_unavailabe(self, mock_glance):
        mock_glance.side_effect = Exception("list error")
        self.assertRaises(exceptions.InvalidScenarioArgument,
                          images.ImageGenerator.validate_semantic, None, None,
                          None, None)