summaryrefslogtreecommitdiff
path: root/ironic/tests/api/test_nodes.py
blob: c2809887cf4b0ad48fc49fda0453015bdc06a171 (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
# vim: tabstop=4 shiftwidth=4 softtabstop=4
# -*- encoding: utf-8 -*-
#
#    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 the API /nodes/ methods.
"""

import datetime

import mock
from testtools.matchers import HasLength
import webtest.app

from ironic.common import exception
from ironic.common import states
from ironic.common import utils
from ironic.conductor import rpcapi
from ironic import objects
from ironic.openstack.common import timeutils
from ironic.tests.api import base
from ironic.tests.db import utils as dbutils


class TestListNodes(base.FunctionalTest):

    def setUp(self):
        super(TestListNodes, self).setUp()
        cdict = dbutils.get_test_chassis()
        self.chassis = self.dbapi.create_chassis(cdict)

    def _create_association_test_nodes(self):
        #create some unassociated nodes
        unassociated_nodes = []
        for id in range(3):
            ndict = dbutils.get_test_node(id=id,
                                          uuid=utils.generate_uuid())
            node = self.dbapi.create_node(ndict)
            unassociated_nodes.append(node['uuid'])

        #created some associated nodes
        associated_nodes = []
        for id in range(3, 7):
            ndict = dbutils.get_test_node(
                        id=id,
                        uuid=utils.generate_uuid(),
                        instance_uuid=utils.generate_uuid())
            node = self.dbapi.create_node(ndict)
            associated_nodes.append(node['uuid'])
        return {'associated': associated_nodes,
                'unassociated': unassociated_nodes}

    def test_empty(self):
        data = self.get_json('/nodes')
        self.assertEqual([], data['nodes'])

    def test_one(self):
        ndict = dbutils.get_test_node()
        node = self.dbapi.create_node(ndict)
        data = self.get_json('/nodes')
        self.assertEqual(node['uuid'], data['nodes'][0]["uuid"])
        self.assertNotIn('driver', data['nodes'][0])
        self.assertNotIn('driver_info', data['nodes'][0])
        self.assertNotIn('extra', data['nodes'][0])
        self.assertNotIn('properties', data['nodes'][0])
        self.assertNotIn('chassis_id', data['nodes'][0])

    def test_detail(self):
        ndict = dbutils.get_test_node()
        node = self.dbapi.create_node(ndict)
        data = self.get_json('/nodes/detail')
        self.assertEqual(node['uuid'], data['nodes'][0]["uuid"])
        self.assertIn('driver', data['nodes'][0])
        self.assertIn('driver_info', data['nodes'][0])
        self.assertIn('extra', data['nodes'][0])
        self.assertIn('properties', data['nodes'][0])
        self.assertIn('chassis_id', data['nodes'][0])

    def test_detail_against_single(self):
        ndict = dbutils.get_test_node()
        node = self.dbapi.create_node(ndict)
        response = self.get_json('/nodes/%s/detail' % node['uuid'],
                                 expect_errors=True)
        self.assertEqual(response.status_int, 404)

    def test_many(self):
        nodes = []
        for id in range(5):
            ndict = dbutils.get_test_node(id=id,
                                          uuid=utils.generate_uuid())
            node = self.dbapi.create_node(ndict)
            nodes.append(node['uuid'])
        data = self.get_json('/nodes')
        self.assertEqual(len(nodes), len(data['nodes']))

        uuids = [n['uuid'] for n in data['nodes']]
        self.assertEqual(sorted(nodes), sorted(uuids))

    def test_links(self):
        uuid = utils.generate_uuid()
        ndict = dbutils.get_test_node(id=1, uuid=uuid)
        self.dbapi.create_node(ndict)
        data = self.get_json('/nodes/1')
        self.assertIn('links', data.keys())
        self.assertEqual(len(data['links']), 2)
        self.assertIn(uuid, data['links'][0]['href'])
        self.assertTrue(self.validate_link(data['links'][0]['href']))
        self.assertTrue(self.validate_link(data['links'][1]['href']))

    def test_collection_links(self):
        nodes = []
        for id in range(5):
            ndict = dbutils.get_test_node(id=id,
                                          uuid=utils.generate_uuid())
            node = self.dbapi.create_node(ndict)
            nodes.append(node['uuid'])
        data = self.get_json('/nodes/?limit=3')
        self.assertEqual(len(data['nodes']), 3)

        next_marker = data['nodes'][-1]['uuid']
        self.assertIn(next_marker, data['next'])

    def test_ports_subresource_link(self):
        ndict = dbutils.get_test_node()
        self.dbapi.create_node(ndict)

        data = self.get_json('/nodes/%s' % ndict['uuid'])
        self.assertIn('ports', data.keys())

    def test_ports_subresource(self):
        ndict = dbutils.get_test_node()
        self.dbapi.create_node(ndict)

        for id in range(2):
            pdict = dbutils.get_test_port(id=id, node_id=ndict['id'],
                                          uuid=utils.generate_uuid(),
                                          address='52:54:00:cf:2d:3%s' % id)
            self.dbapi.create_port(pdict)

        data = self.get_json('/nodes/%s/ports' % ndict['uuid'])
        self.assertEqual(len(data['ports']), 2)
        self.assertNotIn('next', data.keys())

        # Test collection pagination
        data = self.get_json('/nodes/%s/ports?limit=1' % ndict['uuid'])
        self.assertEqual(len(data['ports']), 1)
        self.assertIn('next', data.keys())

    def test_ports_subresource_noid(self):
        ndict = dbutils.get_test_node()
        self.dbapi.create_node(ndict)
        pdict = dbutils.get_test_port(node_id=ndict['id'])
        self.dbapi.create_port(pdict)
        # No node id specified
        response = self.get_json('/nodes/ports', expect_errors=True)
        self.assertEqual(response.status_int, 400)

    def test_ports_subresource_node_not_found(self):
        non_existent_uuid = 'eeeeeeee-cccc-aaaa-bbbb-cccccccccccc'
        response = self.get_json('/nodes/%s/ports' % non_existent_uuid,
                                 expect_errors=True)
        self.assertEqual(response.status_int, 404)

    def test_state(self):
        ndict = dbutils.get_test_node()
        self.dbapi.create_node(ndict)
        data = self.get_json('/nodes/%s/state' % ndict['uuid'])
        [self.assertIn(key, data) for key in ['power', 'provision']]

        # Check if it only returns a sub-set of the attributes
        [self.assertIn(key, ['current', 'links'])
                       for key in data['power'].keys()]
        [self.assertIn(key, ['current', 'links'])
                       for key in data['provision'].keys()]

    def test_power_state(self):
        ndict = dbutils.get_test_node()
        self.dbapi.create_node(ndict)
        data = self.get_json('/nodes/%s/state/power' % ndict['uuid'])
        [self.assertIn(key, data) for key in
                       ['available', 'current', 'target', 'links']]
        #TODO(lucasagomes): Add more tests to check to which states it can
        # transition to from the current one, and check if they are present
        # in the available list.

    def test_provision_state(self):
        ndict = dbutils.get_test_node()
        self.dbapi.create_node(ndict)
        data = self.get_json('/nodes/%s/state/provision' % ndict['uuid'])
        [self.assertIn(key, data) for key in
                       ['available', 'current', 'target', 'links']]
        #TODO(lucasagomes): Add more tests to check to which states it can
        # transition to from the current one, and check if they are present
        # in the available list.

    def test_node_by_instance_uuid(self):
        ndict = dbutils.get_test_node(uuid=utils.generate_uuid(),
                                      instance_uuid=utils.generate_uuid())
        node = self.dbapi.create_node(ndict)
        instance_uuid = node['instance_uuid']

        data = self.get_json('/nodes?instance_uuid=%s' % instance_uuid)

        self.assertThat(data['nodes'], HasLength(1))
        self.assertEqual(node['instance_uuid'],
                         data['nodes'][0]["instance_uuid"])

    def test_node_by_instance_uuid_wrong_uuid(self):
        ndict = dbutils.get_test_node(uuid=utils.generate_uuid(),
                                      instance_uuid=utils.generate_uuid())
        self.dbapi.create_node(ndict)
        wrong_uuid = utils.generate_uuid()

        data = self.get_json('/nodes?instance_uuid=%s' % wrong_uuid)

        self.assertThat(data['nodes'], HasLength(0))

    def test_node_by_instance_uuid_invalid_uuid(self):
        response = self.get_json('/nodes?instance_uuid=fake',
                                 expect_errors=True)

        self.assertEqual('application/json', response.content_type)
        self.assertEqual(400, response.status_code)

    def test_associated_nodes_insensitive(self):
        associated_nodes = self._create_association_test_nodes().\
                get('associated')

        data = self.get_json('/nodes?associated=true')
        data1 = self.get_json('/nodes?associated=True')

        uuids = [n['uuid'] for n in data['nodes']]
        uuids1 = [n['uuid'] for n in data1['nodes']]
        self.assertEqual(sorted(associated_nodes), sorted(uuids1))
        self.assertEqual(sorted(associated_nodes), sorted(uuids))

    def test_associated_nodes_error(self):
        self._create_association_test_nodes()

        self.assertRaises(webtest.app.AppError, self.get_json,
                          '/nodes?associated=blah')

    def test_unassociated_nodes_insensitive(self):
        unassociated_nodes = self._create_association_test_nodes().\
                get('unassociated')

        data = self.get_json('/nodes?associated=false')
        data1 = self.get_json('/nodes?associated=FALSE')

        uuids = [n['uuid'] for n in data['nodes']]
        uuids1 = [n['uuid'] for n in data1['nodes']]
        self.assertEqual(sorted(unassociated_nodes), sorted(uuids1))
        self.assertEqual(sorted(unassociated_nodes), sorted(uuids))

    def test_unassociated_nodes_with_limit(self):
        unassociated_nodes = self._create_association_test_nodes().\
                get('unassociated')

        data = self.get_json('/nodes?associated=False&limit=2')

        self.assertThat(data['nodes'], HasLength(2))
        self.assertTrue(data['nodes'][0]['uuid'] in unassociated_nodes)

    def test_next_link_with_association(self):
        self._create_association_test_nodes()
        data = self.get_json('/nodes/?limit=3&associated=True')
        self.assertThat(data['nodes'], HasLength(3))
        self.assertIn('associated=true', data['next'])

    def test_detail_with_association_filter(self):
        associated_nodes = self._create_association_test_nodes().\
                get('associated')
        data = self.get_json('/nodes/detail?associated=true')
        self.assertIn('driver', data['nodes'][0])
        self.assertEqual(len(associated_nodes), len(data['nodes']))

    def test_next_link_with_association_with_detail(self):
        self._create_association_test_nodes()
        data = self.get_json('/nodes/detail?limit=3&associated=true')
        self.assertThat(data['nodes'], HasLength(3))
        self.assertIn('driver', data['nodes'][0])
        self.assertIn('associated=true', data['next'])

    def test_detail_with_instance_uuid(self):
        ndict = dbutils.get_test_node(uuid=utils.generate_uuid(),
                                      instance_uuid=utils.generate_uuid())
        node = self.dbapi.create_node(ndict)
        instance_uuid = node['instance_uuid']

        data = self.get_json('/nodes/detail?instance_uuid=%s' % instance_uuid)

        self.assertEqual(node['instance_uuid'],
                         data['nodes'][0]["instance_uuid"])
        self.assertIn('driver', data['nodes'][0])
        self.assertIn('driver_info', data['nodes'][0])
        self.assertIn('extra', data['nodes'][0])
        self.assertIn('properties', data['nodes'][0])
        self.assertIn('chassis_id', data['nodes'][0])


class TestPatch(base.FunctionalTest):

    def setUp(self):
        super(TestPatch, self).setUp()
        cdict = dbutils.get_test_chassis()
        self.chassis = self.dbapi.create_chassis(cdict)
        ndict = dbutils.get_test_node()
        self.node = self.dbapi.create_node(ndict)
        p = mock.patch.object(rpcapi.ConductorAPI, 'update_node')
        self.mock_update_node = p.start()
        self.addCleanup(p.stop)
        p = mock.patch.object(rpcapi.ConductorAPI, 'change_node_power_state')
        self.mock_cnps = p.start()
        self.addCleanup(p.stop)

    def test_update_ok(self):
        self.mock_update_node.return_value = self.node
        self.mock_update_node.return_value.updated_at = \
                                   "2013-12-03T06:20:41.184720+00:00"
        response = self.patch_json('/nodes/%s' % self.node['uuid'],
                                   [{'path': '/instance_uuid',
                                     'value': 'fake instance uuid',
                                     'op': 'replace'}])
        self.assertEqual(response.content_type, 'application/json')
        self.assertEqual(response.status_code, 200)
        self.assertEqual(self.mock_update_node.return_value.updated_at,
                         timeutils.parse_isotime(response.json['updated_at']))
        self.mock_update_node.assert_called_once_with(mock.ANY, mock.ANY)

    def test_update_state(self):
        self.assertRaises(webtest.app.AppError, self.patch_json,
                          '/nodes/%s' % self.node['uuid'],
                          {'power_state': 'new state'})

    def test_update_fails_bad_driver_info(self):
        fake_err = 'Fake Error Message'
        self.mock_update_node.side_effect = exception.InvalidParameterValue(
                                                fake_err)

        response = self.patch_json('/nodes/%s' % self.node['uuid'],
                                   [{'path': '/driver_info/this',
                                     'value': 'foo',
                                     'op': 'add'},
                                    {'path': '/driver_info/that',
                                     'value': 'bar',
                                     'op': 'add'}],
                                   expect_errors=True)
        self.assertEqual(response.content_type, 'application/json')
        self.assertEqual(response.status_code, 400)

        self.mock_update_node.assert_called_once_with(mock.ANY, mock.ANY)

    def test_update_fails_bad_state(self):
        fake_err = 'Fake Power State'
        self.mock_update_node.side_effect = exception.NodeInWrongPowerState(
                    node=self.node['uuid'], pstate=fake_err)

        response = self.patch_json('/nodes/%s' % self.node['uuid'],
                                   [{'path': '/instance_uuid',
                                     'value': 'fake instance uuid',
                                     'op': 'replace'}],
                                   expect_errors=True)
        self.assertEqual(response.content_type, 'application/json')
        self.assertEqual(response.status_code, 409)

        self.mock_update_node.assert_called_once_with(mock.ANY, mock.ANY)

    def test_add_ok(self):
        self.mock_update_node.return_value = self.node

        response = self.patch_json('/nodes/%s' % self.node['uuid'],
                                   [{'path': '/extra/foo',
                                     'value': 'bar',
                                     'op': 'add'}])
        self.assertEqual(response.content_type, 'application/json')
        self.assertEqual(response.status_code, 200)

        self.mock_update_node.assert_called_once_with(mock.ANY, mock.ANY)

    def test_add_fail(self):
        self.assertRaises(webtest.app.AppError, self.patch_json,
                          '/nodes/%s' % self.node['uuid'],
                          [{'path': '/foo', 'value': 'bar', 'op': 'add'}])

    def test_remove_ok(self):
        self.mock_update_node.return_value = self.node

        response = self.patch_json('/nodes/%s' % self.node['uuid'],
                                   [{'path': '/extra',
                                     'op': 'remove'}])
        self.assertEqual(response.content_type, 'application/json')
        self.assertEqual(response.status_code, 200)

        self.mock_update_node.assert_called_once_with(mock.ANY, mock.ANY)

    def test_remove_fail(self):
        self.assertRaises(webtest.app.AppError, self.patch_json,
                          '/nodes/%s' % self.node['uuid'],
                          [{'path': '/extra/non-existent', 'op': 'remove'}])

    def test_update_state_in_progress(self):
        ndict = dbutils.get_test_node(id=99, uuid=utils.generate_uuid(),
                                      target_power_state=states.POWER_OFF)
        node = self.dbapi.create_node(ndict)
        self.assertRaises(webtest.app.AppError, self.patch_json,
                          '/nodes/%s' % node['uuid'],
                          [{'path': '/extra/foo', 'value': 'bar',
                            'op': 'add'}])
        response = self.patch_json('/nodes/%s' % node['uuid'],
                                   [{'path': '/extra/foo', 'value': 'bar',
                                     'op': 'add'}], expect_errors=True)
        self.assertEqual(response.status_code, 409)

    def test_patch_ports_subresource(self):
        response = self.patch_json('/nodes/%s/ports' % self.node['uuid'],
                                   [{'path': '/extra/foo', 'value': 'bar',
                                     'op': 'add'}], expect_errors=True)
        self.assertEqual(response.status_int, 403)

    def test_remove_uuid(self):
        ndict = dbutils.get_test_node()
        self.assertRaises(webtest.app.AppError, self.patch_json,
                          '/nodes/%s' % ndict['uuid'],
                          [{'path': '/uuid', 'op': 'remove'}])


class TestPost(base.FunctionalTest):

    def setUp(self):
        super(TestPost, self).setUp()
        cdict = dbutils.get_test_chassis()
        self.chassis = self.dbapi.create_chassis(cdict)
        self.addCleanup(timeutils.clear_time_override)

    def test_create_node(self):
        ndict = dbutils.get_test_node()
        t1 = datetime.datetime(2000, 1, 1, 0, 0)
        timeutils.set_time_override(t1)
        self.post_json('/nodes', ndict)
        result = self.get_json('/nodes/%s' % ndict['uuid'])
        self.assertEqual(ndict['uuid'], result['uuid'])
        self.assertFalse(result['updated_at'])
        return_created_at = timeutils.parse_isotime(
                result['created_at']).replace(tzinfo=None)
        self.assertEqual(t1, return_created_at)

    def test_create_node_valid_extra(self):
        ndict = dbutils.get_test_node(extra={'foo': 123})
        self.post_json('/nodes', ndict)
        result = self.get_json('/nodes/%s' % ndict['uuid'])
        self.assertEqual(ndict['extra'], result['extra'])

    def test_create_node_invalid_extra(self):
        ndict = dbutils.get_test_node(extra={'foo': 0.123})
        self.assertRaises(webtest.app.AppError, self.post_json, '/nodes',
                          ndict)

    def test_vendor_passthru_ok(self):
        ndict = dbutils.get_test_node()
        self.post_json('/nodes', ndict)
        uuid = ndict['uuid']
        info = {'foo': 'bar'}

        with mock.patch.object(
                rpcapi.ConductorAPI, 'vendor_passthru') as mock_vendor:
            mock_vendor.return_value = 'OK'
            response = self.post_json('/nodes/%s/vendor_passthru/test' % uuid,
                                      info, expect_errors=False)
            mock_vendor.assert_called_once_with(mock.ANY, uuid, 'test', info)
            self.assertEqual(response.body, '"OK"')
            self.assertEqual(response.status_code, 202)

    def test_vendor_passthru_no_such_method(self):
        ndict = dbutils.get_test_node()
        self.post_json('/nodes', ndict)
        uuid = ndict['uuid']
        info = {'foo': 'bar'}

        with mock.patch.object(
                rpcapi.ConductorAPI, 'vendor_passthru') as mock_vendor:
            mock_vendor.side_effect = exception.UnsupportedDriverExtension(
                                        {'driver': ndict['driver'],
                                         'node': uuid,
                                         'extension': 'test'})
            response = self.post_json('/nodes/%s/vendor_passthru/test' % uuid,
                                      info, expect_errors=True)
            mock_vendor.assert_called_once_with(mock.ANY, uuid, 'test', info)
            self.assertEqual(response.status_code, 400)

    def test_vendor_passthru_without_method(self):
        ndict = dbutils.get_test_node()
        self.post_json('/nodes', ndict)
        self.assertRaises(webtest.app.AppError, self.post_json,
                          '/nodes/%s/vendor_passthru' % ndict['uuid'],
                          {'foo': 'bar'})

    def test_post_ports_subresource(self):
        ndict = dbutils.get_test_node()
        self.post_json('/nodes', ndict)
        pdict = dbutils.get_test_port(node_id=None)
        pdict['node_uuid'] = ndict['uuid']
        response = self.post_json('/nodes/ports', pdict,
                                  expect_errors=True)
        self.assertEqual(response.status_int, 403)


class TestDelete(base.FunctionalTest):

    def setUp(self):
        super(TestDelete, self).setUp()
        cdict = dbutils.get_test_chassis()
        self.chassis = self.dbapi.create_chassis(cdict)

    def test_delete_node(self):
        ndict = dbutils.get_test_node()
        self.post_json('/nodes', ndict)
        self.delete('/nodes/%s' % ndict['uuid'])
        response = self.get_json('/nodes/%s' % ndict['uuid'],
                                 expect_errors=True)
        self.assertEqual(response.status_int, 404)
        self.assertEqual(response.content_type, 'application/json')
        self.assertTrue(response.json['error_message'])

    def test_delete_ports_subresource(self):
        ndict = dbutils.get_test_node()
        self.post_json('/nodes', ndict)
        response = self.delete('/nodes/%s/ports' % ndict['uuid'],
                               expect_errors=True)
        self.assertEqual(response.status_int, 403)

    def test_delete_associated(self):
        ndict = dbutils.get_test_node(instance_uuid='fake-uuid-1234')
        self.post_json('/nodes', ndict)
        response = self.delete('/nodes/%s' % ndict['uuid'], expect_errors=True)
        self.assertEqual(response.status_int, 409)


class TestPut(base.FunctionalTest):

    def setUp(self):
        super(TestPut, self).setUp()
        cdict = dbutils.get_test_chassis()
        self.chassis = self.dbapi.create_chassis(cdict)
        ndict = dbutils.get_test_node()
        self.node = self.dbapi.create_node(ndict)
        p = mock.patch.object(rpcapi.ConductorAPI, 'change_node_power_state')
        self.mock_cnps = p.start()
        self.addCleanup(p.stop)

    def test_power_state(self):
        response = self.put_json('/nodes/%s/state/power' % self.node['uuid'],
                                 {'target': states.POWER_ON})
        self.assertEqual(response.content_type, 'application/json')
        self.assertEqual(response.status_code, 202)

        self.mock_cnps.assert_called_once_with(mock.ANY, mock.ANY, mock.ANY)

    def test_power_state_in_progress(self):
        manager = mock.MagicMock()
        with mock.patch.object(objects.Node, 'get_by_uuid') as mock_get_node:
            mock_get_node.return_value = self.node
            manager.attach_mock(mock_get_node, 'get_by_uuid')
            manager.attach_mock(self.mock_cnps, 'change_node_power_state')
            expected = [mock.call.get_by_uuid(mock.ANY, self.node['uuid']),
                        mock.call.change_node_power_state(mock.ANY, mock.ANY,
                                                          mock.ANY)]

            self.put_json('/nodes/%s/state/power' % self.node['uuid'],
                          {'target': states.POWER_ON})
            self.assertEqual(manager.mock_calls, expected)

        self.dbapi.update_node(self.node['uuid'],
                               {'target_power_state': 'fake'})
        self.assertRaises(webtest.app.AppError, self.put_json,
                          '/nodes/%s/state/power' % self.node['uuid'],
                          {'target': states.POWER_ON})
        response = self.put_json('/nodes/%s/state/power' % self.node['uuid'],
                                 {'target': states.POWER_ON},
                                 expect_errors=True)
        self.assertEqual(response.status_code, 409)