summaryrefslogtreecommitdiffstats
path: root/nova/tests/api/openstack/compute/contrib/test_admin_actions_with_cells.py
blob: 0c5dea2c8c46a8a0ef645d3950672c207e2e4aec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# vim: tabstop=4 shiftwidth=4 softtabstop=4

# Copyright (c) 2012 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.
"""
Tests For Compute admin api w/ Cells
"""

from nova.api.openstack.compute.contrib import admin_actions
from nova.compute import cells_api as compute_cells_api
from nova.compute import vm_states
from nova.openstack.common import uuidutils
from nova import test
from nova.tests.api.openstack import fakes


INSTANCE_IDS = {'inst_id': 1}


class CellsAdminAPITestCase(test.TestCase):

    def setUp(self):
        super(CellsAdminAPITestCase, self).setUp()

        def _fake_cell_read_only(*args, **kwargs):
            return False

        def _fake_validate_cell(*args, **kwargs):
            return

        def _fake_compute_api_get(context, instance_id):
            return {'id': 1, 'uuid': instance_id, 'vm_state': vm_states.ACTIVE,
                    'task_state': None, 'cell_name': None}

        def _fake_instance_update_and_get_original(context, instance_uuid,
                                                   values):
            inst = fakes.stub_instance(INSTANCE_IDS.get(instance_uuid),
                                       name=values.get('display_name'))
            return (inst, inst)

        def fake_cast_to_cells(context, instance, method, *args, **kwargs):
            """
            Makes sure that the cells receive the cast to update
            the cell state
            """
            self.cells_received_kwargs.update(kwargs)

        self.admin_api = admin_actions.AdminActionsController()
        self.admin_api.compute_api = compute_cells_api.ComputeCellsAPI()
        self.stubs.Set(self.admin_api.compute_api, '_cell_read_only',
                       _fake_cell_read_only)
        self.stubs.Set(self.admin_api.compute_api, '_validate_cell',
                       _fake_validate_cell)
        self.stubs.Set(self.admin_api.compute_api, 'get',
                       _fake_compute_api_get)
        self.stubs.Set(self.admin_api.compute_api.db,
                       'instance_update_and_get_original',
                       _fake_instance_update_and_get_original)
        self.stubs.Set(self.admin_api.compute_api, '_cast_to_cells',
                       fake_cast_to_cells)

        self.uuid = uuidutils.generate_uuid()
        url = '/fake/servers/%s/action' % self.uuid
        self.request = fakes.HTTPRequest.blank(url)
        self.cells_received_kwargs = {}

    def test_reset_active(self):
        body = {"os-resetState": {"state": "error"}}
        result = self.admin_api._reset_state(self.request, 'inst_id', body)

        self.assertEqual(result.status_int, 202)
        # Make sure the cells received the update
        self.assertEqual(self.cells_received_kwargs,
                         dict(vm_state=vm_states.ERROR,
                              task_state=None))