summaryrefslogtreecommitdiff
path: root/ironic/tests/drivers/test_snmp.py
blob: 3f3992224f78bf669cf7dd6d6b88d8b8bd748557 (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
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
# Copyright 2013,2014 Cray Inc
#
# Authors: David Hewson <dhewson@cray.com>
#          Stig Telfer <stelfer@cray.com>
#          Mark Goddard <mgoddard@cray.com>
#
# 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.

"""Test class for SNMP power driver module."""

import mock
from oslo_config import cfg
from pysnmp.entity.rfc3413.oneliner import cmdgen
from pysnmp import error as snmp_error

from ironic.common import exception
from ironic.common import states
from ironic.conductor import task_manager
from ironic.drivers.modules import snmp as snmp
from ironic.tests import base
from ironic.tests.conductor import utils as mgr_utils
from ironic.tests.db import base as db_base
from ironic.tests.db import utils as db_utils
from ironic.tests.objects import utils as obj_utils

CONF = cfg.CONF
INFO_DICT = db_utils.get_test_snmp_info()


@mock.patch.object(cmdgen, 'CommandGenerator')
class SNMPClientTestCase(base.TestCase):
    def setUp(self):
        super(SNMPClientTestCase, self).setUp()
        self.address = '1.2.3.4'
        self.port = '6700'
        self.oid = 'oid'
        self.value = 'value'

    def test___init__(self, mock_cmdgen):
        client = snmp.SNMPClient(self.address, self.port, snmp.SNMP_V1)
        mock_cmdgen.assert_called_once_with()
        self.assertEqual(self.address, client.address)
        self.assertEqual(self.port, client.port)
        self.assertEqual(snmp.SNMP_V1, client.version)
        self.assertIsNone(client.community)
        self.assertFalse('security' in client.__dict__)
        self.assertEqual(mock_cmdgen.return_value, client.cmd_gen)

    @mock.patch.object(cmdgen, 'CommunityData')
    def test__get_auth_v1(self, mock_community, mock_cmdgen):
        client = snmp.SNMPClient(self.address, self.port, snmp.SNMP_V1)
        client._get_auth()
        mock_cmdgen.assert_called_once_with()
        mock_community.assert_called_once_with(client.community, mpModel=0)

    @mock.patch.object(cmdgen, 'UsmUserData')
    def test__get_auth_v3(self, mock_user, mock_cmdgen):
        client = snmp.SNMPClient(self.address, self.port, snmp.SNMP_V3)
        client._get_auth()
        mock_cmdgen.assert_called_once_with()
        mock_user.assert_called_once_with(client.security)

    @mock.patch.object(cmdgen, 'UdpTransportTarget')
    def test__get_transport(self, mock_transport, mock_cmdgen):
        client = snmp.SNMPClient(self.address, self.port, snmp.SNMP_V3)
        client._get_transport()
        mock_cmdgen.assert_called_once_with()
        mock_transport.assert_called_once_with((client.address, client.port))

    @mock.patch.object(cmdgen, 'UdpTransportTarget')
    def test__get_transport_err(self, mock_transport, mock_cmdgen):
        mock_transport.side_effect = snmp_error.PySnmpError
        client = snmp.SNMPClient(self.address, self.port, snmp.SNMP_V3)
        self.assertRaises(snmp_error.PySnmpError, client._get_transport)
        mock_cmdgen.assert_called_once_with()
        mock_transport.assert_called_once_with((client.address, client.port))

    @mock.patch.object(snmp.SNMPClient, '_get_transport')
    @mock.patch.object(snmp.SNMPClient, '_get_auth')
    def test_get(self, mock_auth, mock_transport, mock_cmdgen):
        var_bind = (self.oid, self.value)
        mock_cmdgenerator = mock_cmdgen.return_value
        mock_cmdgenerator.getCmd.return_value = ("", None, 0, [var_bind])
        client = snmp.SNMPClient(self.address, self.port, snmp.SNMP_V3)
        val = client.get(self.oid)
        self.assertEqual(var_bind[1], val)
        mock_cmdgenerator.getCmd.assert_called_once_with(mock.ANY, mock.ANY,
                                                         self.oid)

    @mock.patch.object(snmp.SNMPClient, '_get_transport')
    @mock.patch.object(snmp.SNMPClient, '_get_auth')
    def test_get_err_transport(self, mock_auth, mock_transport, mock_cmdgen):
        mock_transport.side_effect = snmp_error.PySnmpError
        var_bind = (self.oid, self.value)
        mock_cmdgenerator = mock_cmdgen.return_value
        mock_cmdgenerator.getCmd.return_value = ("engine error", None, 0,
                                                 [var_bind])
        client = snmp.SNMPClient(self.address, self.port, snmp.SNMP_V3)
        self.assertRaises(exception.SNMPFailure, client.get, self.oid)
        self.assertFalse(mock_cmdgenerator.getCmd.called)

    @mock.patch.object(snmp.SNMPClient, '_get_transport')
    @mock.patch.object(snmp.SNMPClient, '_get_auth')
    def test_get_err_engine(self, mock_auth, mock_transport, mock_cmdgen):
        var_bind = (self.oid, self.value)
        mock_cmdgenerator = mock_cmdgen.return_value
        mock_cmdgenerator.getCmd.return_value = ("engine error", None, 0,
                                                 [var_bind])
        client = snmp.SNMPClient(self.address, self.port, snmp.SNMP_V3)
        self.assertRaises(exception.SNMPFailure, client.get, self.oid)
        mock_cmdgenerator.getCmd.assert_called_once_with(mock.ANY, mock.ANY,
                                                         self.oid)

    @mock.patch.object(snmp.SNMPClient, '_get_transport')
    @mock.patch.object(snmp.SNMPClient, '_get_auth')
    def test_set(self, mock_auth, mock_transport, mock_cmdgen):
        var_bind = (self.oid, self.value)
        mock_cmdgenerator = mock_cmdgen.return_value
        mock_cmdgenerator.setCmd.return_value = ("", None, 0, [var_bind])
        client = snmp.SNMPClient(self.address, self.port, snmp.SNMP_V3)
        client.set(self.oid, self.value)
        mock_cmdgenerator.setCmd.assert_called_once_with(mock.ANY, mock.ANY,
                                                         var_bind)

    @mock.patch.object(snmp.SNMPClient, '_get_transport')
    @mock.patch.object(snmp.SNMPClient, '_get_auth')
    def test_set_err_transport(self, mock_auth, mock_transport, mock_cmdgen):
        mock_transport.side_effect = snmp_error.PySnmpError
        var_bind = (self.oid, self.value)
        mock_cmdgenerator = mock_cmdgen.return_value
        mock_cmdgenerator.setCmd.return_value = ("engine error", None, 0,
                                                 [var_bind])
        client = snmp.SNMPClient(self.address, self.port, snmp.SNMP_V3)
        self.assertRaises(exception.SNMPFailure,
                          client.set, self.oid, self.value)
        self.assertFalse(mock_cmdgenerator.setCmd.called)

    @mock.patch.object(snmp.SNMPClient, '_get_transport')
    @mock.patch.object(snmp.SNMPClient, '_get_auth')
    def test_set_err_engine(self, mock_auth, mock_transport, mock_cmdgen):
        var_bind = (self.oid, self.value)
        mock_cmdgenerator = mock_cmdgen.return_value
        mock_cmdgenerator.setCmd.return_value = ("engine error", None, 0,
                                                 [var_bind])
        client = snmp.SNMPClient(self.address, self.port, snmp.SNMP_V3)
        self.assertRaises(exception.SNMPFailure,
                          client.set, self.oid, self.value)
        mock_cmdgenerator.setCmd.assert_called_once_with(mock.ANY, mock.ANY,
                                                         var_bind)


class SNMPValidateParametersTestCase(db_base.DbTestCase):

    def _get_test_node(self, driver_info):
        return obj_utils.get_test_node(
                self.context,
                driver_info=driver_info)

    def test__parse_driver_info_default(self):
        # Make sure we get back the expected things.
        node = self._get_test_node(INFO_DICT)
        info = snmp._parse_driver_info(node)
        self.assertEqual(INFO_DICT['snmp_driver'], info.get('driver'))
        self.assertEqual(INFO_DICT['snmp_address'], info.get('address'))
        self.assertEqual(INFO_DICT['snmp_port'], str(info.get('port')))
        self.assertEqual(INFO_DICT['snmp_outlet'], info.get('outlet'))
        self.assertEqual(INFO_DICT['snmp_version'], info.get('version'))
        self.assertEqual(INFO_DICT.get('snmp_community'),
                         info.get('community'))
        self.assertEqual(INFO_DICT.get('snmp_security'),
                         info.get('security'))

    def test__parse_driver_info_apc(self):
        # Make sure the APC driver type is parsed.
        info = db_utils.get_test_snmp_info(snmp_driver='apc')
        node = self._get_test_node(info)
        info = snmp._parse_driver_info(node)
        self.assertEqual('apc', info.get('driver'))

    def test__parse_driver_info_aten(self):
        # Make sure the Aten driver type is parsed.
        info = db_utils.get_test_snmp_info(snmp_driver='aten')
        node = self._get_test_node(info)
        info = snmp._parse_driver_info(node)
        self.assertEqual('aten', info.get('driver'))

    def test__parse_driver_info_cyberpower(self):
        # Make sure the CyberPower driver type is parsed.
        info = db_utils.get_test_snmp_info(snmp_driver='cyberpower')
        node = self._get_test_node(info)
        info = snmp._parse_driver_info(node)
        self.assertEqual('cyberpower', info.get('driver'))

    def test__parse_driver_info_eatonpower(self):
        # Make sure the Eaton Power driver type is parsed.
        info = db_utils.get_test_snmp_info(snmp_driver='eatonpower')
        node = self._get_test_node(info)
        info = snmp._parse_driver_info(node)
        self.assertEqual('eatonpower', info.get('driver'))

    def test__parse_driver_info_teltronix(self):
        # Make sure the Teltronix driver type is parsed.
        info = db_utils.get_test_snmp_info(snmp_driver='teltronix')
        node = self._get_test_node(info)
        info = snmp._parse_driver_info(node)
        self.assertEqual('teltronix', info.get('driver'))

    def test__parse_driver_info_snmp_v1(self):
        # Make sure SNMPv1 is parsed with a community string.
        info = db_utils.get_test_snmp_info(snmp_version='1',
                                           snmp_community='public')
        node = self._get_test_node(info)
        info = snmp._parse_driver_info(node)
        self.assertEqual('1', info.get('version'))
        self.assertEqual('public', info.get('community'))

    def test__parse_driver_info_snmp_v2c(self):
        # Make sure SNMPv2c is parsed with a community string.
        info = db_utils.get_test_snmp_info(snmp_version='2c',
                                           snmp_community='private')
        node = self._get_test_node(info)
        info = snmp._parse_driver_info(node)
        self.assertEqual('2c', info.get('version'))
        self.assertEqual('private', info.get('community'))

    def test__parse_driver_info_snmp_v3(self):
        # Make sure SNMPv3 is parsed with a security string.
        info = db_utils.get_test_snmp_info(snmp_version='3',
                                           snmp_security='pass')
        node = self._get_test_node(info)
        info = snmp._parse_driver_info(node)
        self.assertEqual('3', info.get('version'))
        self.assertEqual('pass', info.get('security'))

    def test__parse_driver_info_snmp_port_default(self):
        # Make sure default SNMP UDP port numbers are correct
        info = dict(INFO_DICT)
        del info['snmp_port']
        node = self._get_test_node(info)
        info = snmp._parse_driver_info(node)
        self.assertEqual(161, info.get('port'))

    def test__parse_driver_info_snmp_port(self):
        # Make sure non-default SNMP UDP port numbers can be configured
        info = db_utils.get_test_snmp_info(snmp_port='10161')
        node = self._get_test_node(info)
        info = snmp._parse_driver_info(node)
        self.assertEqual(10161, info.get('port'))

    def test__parse_driver_info_missing_driver(self):
        # Make sure exception is raised when the driver type is missing.
        info = dict(INFO_DICT)
        del info['snmp_driver']
        node = self._get_test_node(info)
        self.assertRaises(exception.MissingParameterValue,
                          snmp._parse_driver_info,
                          node)

    def test__parse_driver_info_invalid_driver(self):
        # Make sure exception is raised when the driver type is invalid.
        info = db_utils.get_test_snmp_info(snmp_driver='invalidpower')
        node = self._get_test_node(info)
        self.assertRaises(exception.InvalidParameterValue,
                          snmp._parse_driver_info,
                          node)

    def test__parse_driver_info_missing_address(self):
        # Make sure exception is raised when the address is missing.
        info = dict(INFO_DICT)
        del info['snmp_address']
        node = self._get_test_node(info)
        self.assertRaises(exception.MissingParameterValue,
                          snmp._parse_driver_info,
                          node)

    def test__parse_driver_info_missing_outlet(self):
        # Make sure exception is raised when the outlet is missing.
        info = dict(INFO_DICT)
        del info['snmp_outlet']
        node = self._get_test_node(info)
        self.assertRaises(exception.MissingParameterValue,
                          snmp._parse_driver_info,
                          node)

    def test__parse_driver_info_default_version(self):
        # Make sure version defaults to 1 when it is missing.
        info = dict(INFO_DICT)
        del info['snmp_version']
        node = self._get_test_node(info)
        info = snmp._parse_driver_info(node)
        self.assertEqual('1', info.get('version'))
        self.assertEqual(INFO_DICT['snmp_community'], info.get('community'))

    def test__parse_driver_info_invalid_version(self):
        # Make sure exception is raised when version is invalid.
        info = db_utils.get_test_snmp_info(snmp_version='42',
                                           snmp_community='public',
                                           snmp_security='pass')
        node = self._get_test_node(info)
        self.assertRaises(exception.InvalidParameterValue,
                          snmp._parse_driver_info,
                          node)

    def test__parse_driver_info_default_version_and_missing_community(self):
        # Make sure exception is raised when version and community are missing.
        info = dict(INFO_DICT)
        del info['snmp_version']
        del info['snmp_community']
        node = self._get_test_node(info)
        self.assertRaises(exception.MissingParameterValue,
                          snmp._parse_driver_info,
                          node)

    def test__parse_driver_info_missing_community_snmp_v1(self):
        # Make sure exception is raised when community is missing with SNMPv1.
        info = dict(INFO_DICT)
        del info['snmp_community']
        node = self._get_test_node(info)
        self.assertRaises(exception.MissingParameterValue,
                          snmp._parse_driver_info,
                          node)

    def test__parse_driver_info_missing_community_snmp_v2c(self):
        # Make sure exception is raised when community is missing with SNMPv2c.
        info = db_utils.get_test_snmp_info(snmp_version='2c')
        del info['snmp_community']
        node = self._get_test_node(info)
        self.assertRaises(exception.MissingParameterValue,
                          snmp._parse_driver_info,
                          node)

    def test__parse_driver_info_missing_security(self):
        # Make sure exception is raised when security is missing with SNMPv3.
        info = db_utils.get_test_snmp_info(snmp_version='3')
        del info['snmp_security']
        node = self._get_test_node(info)
        self.assertRaises(exception.MissingParameterValue,
                          snmp._parse_driver_info,
                          node)


@mock.patch.object(snmp, '_get_client')
class SNMPDeviceDriverTestCase(db_base.DbTestCase):
    """Tests for the SNMP device-specific driver classes.

    The SNMP client object is mocked to allow various error cases to be tested.
    """

    def setUp(self):
        super(SNMPDeviceDriverTestCase, self).setUp()
        self.node = obj_utils.get_test_node(
            self.context,
            driver='fake_snmp',
            driver_info=INFO_DICT)

    def _update_driver_info(self, **kwargs):
        self.node["driver_info"].update(**kwargs)

    def _set_snmp_driver(self, snmp_driver):
        self._update_driver_info(snmp_driver=snmp_driver)

    def _get_snmp_failure(self):
        return exception.SNMPFailure(operation='test-operation',
                                     error='test-error')

    def test_power_state_on(self, mock_get_client):
        # Ensure the power on state is queried correctly
        mock_client = mock_get_client.return_value
        driver = snmp._get_driver(self.node)
        mock_client.get.return_value = driver.value_power_on
        pstate = driver.power_state()
        mock_client.get.assert_called_once_with(driver._snmp_oid())
        self.assertEqual(states.POWER_ON, pstate)

    def test_power_state_off(self, mock_get_client):
        # Ensure the power off state is queried correctly
        mock_client = mock_get_client.return_value
        driver = snmp._get_driver(self.node)
        mock_client.get.return_value = driver.value_power_off
        pstate = driver.power_state()
        mock_client.get.assert_called_once_with(driver._snmp_oid())
        self.assertEqual(states.POWER_OFF, pstate)

    def test_power_state_error(self, mock_get_client):
        # Ensure an unexpected power state returns an error
        mock_client = mock_get_client.return_value
        driver = snmp._get_driver(self.node)
        mock_client.get.return_value = 42
        pstate = driver.power_state()
        mock_client.get.assert_called_once_with(driver._snmp_oid())
        self.assertEqual(states.ERROR, pstate)

    def test_power_state_snmp_failure(self, mock_get_client):
        # Ensure SNMP failure exceptions raised during a query are propagated
        mock_client = mock_get_client.return_value
        driver = snmp._get_driver(self.node)
        mock_client.get.side_effect = self._get_snmp_failure()
        self.assertRaises(exception.SNMPFailure,
                          driver.power_state)
        mock_client.get.assert_called_once_with(driver._snmp_oid())

    def test_power_on(self, mock_get_client):
        # Ensure the device is powered on correctly
        mock_client = mock_get_client.return_value
        driver = snmp._get_driver(self.node)
        mock_client.get.return_value = driver.value_power_on
        pstate = driver.power_on()
        mock_client.set.assert_called_once_with(driver._snmp_oid(),
                                                driver.value_power_on)
        mock_client.get.assert_called_once_with(driver._snmp_oid())
        self.assertEqual(states.POWER_ON, pstate)

    def test_power_off(self, mock_get_client):
        # Ensure the device is powered off correctly
        mock_client = mock_get_client.return_value
        driver = snmp._get_driver(self.node)
        mock_client.get.return_value = driver.value_power_off
        pstate = driver.power_off()
        mock_client.set.assert_called_once_with(driver._snmp_oid(),
                                                driver.value_power_off)
        mock_client.get.assert_called_once_with(driver._snmp_oid())
        self.assertEqual(states.POWER_OFF, pstate)

    @mock.patch("eventlet.greenthread.sleep")
    def test_power_on_delay(self, mock_sleep, mock_get_client):
        # Ensure driver waits for the state to change following a power on
        mock_client = mock_get_client.return_value
        driver = snmp._get_driver(self.node)
        mock_client.get.side_effect = [driver.value_power_off,
                                       driver.value_power_on]
        pstate = driver.power_on()
        mock_client.set.assert_called_once_with(driver._snmp_oid(),
                                                driver.value_power_on)
        calls = [mock.call(driver._snmp_oid())] * 2
        mock_client.get.assert_has_calls(calls)
        self.assertEqual(states.POWER_ON, pstate)

    @mock.patch("eventlet.greenthread.sleep")
    def test_power_off_delay(self, mock_sleep, mock_get_client):
        # Ensure driver waits for the state to change following a power off
        mock_client = mock_get_client.return_value
        driver = snmp._get_driver(self.node)
        mock_client.get.side_effect = [driver.value_power_on,
                                       driver.value_power_off]
        pstate = driver.power_off()
        mock_client.set.assert_called_once_with(driver._snmp_oid(),
                                                driver.value_power_off)
        calls = [mock.call(driver._snmp_oid())] * 2
        mock_client.get.assert_has_calls(calls)
        self.assertEqual(states.POWER_OFF, pstate)

    @mock.patch("eventlet.greenthread.sleep")
    def test_power_on_invalid_state(self, mock_sleep, mock_get_client):
        # Ensure driver retries when querying unexpected states following a
        # power on
        mock_client = mock_get_client.return_value
        driver = snmp._get_driver(self.node)
        mock_client.get.return_value = 42
        pstate = driver.power_on()
        mock_client.set.assert_called_once_with(driver._snmp_oid(),
                                                driver.value_power_on)
        attempts = CONF.snmp.power_timeout // driver.retry_interval
        calls = [mock.call(driver._snmp_oid())] * attempts
        mock_client.get.assert_has_calls(calls)
        self.assertEqual(states.ERROR, pstate)

    @mock.patch("eventlet.greenthread.sleep")
    def test_power_off_invalid_state(self, mock_sleep, mock_get_client):
        # Ensure driver retries when querying unexpected states following a
        # power off
        mock_client = mock_get_client.return_value
        driver = snmp._get_driver(self.node)
        mock_client.get.return_value = 42
        pstate = driver.power_off()
        mock_client.set.assert_called_once_with(driver._snmp_oid(),
                                                driver.value_power_off)
        attempts = CONF.snmp.power_timeout // driver.retry_interval
        calls = [mock.call(driver._snmp_oid())] * attempts
        mock_client.get.assert_has_calls(calls)
        self.assertEqual(states.ERROR, pstate)

    def test_power_on_snmp_set_failure(self, mock_get_client):
        # Ensure SNMP failure exceptions raised during a power on set operation
        # are propagated
        mock_client = mock_get_client.return_value
        driver = snmp._get_driver(self.node)
        mock_client.set.side_effect = self._get_snmp_failure()
        self.assertRaises(exception.SNMPFailure,
                          driver.power_on)
        mock_client.set.assert_called_once_with(driver._snmp_oid(),
                                                driver.value_power_on)

    def test_power_off_snmp_set_failure(self, mock_get_client):
        # Ensure SNMP failure exceptions raised during a power off set
        # operation are propagated
        mock_client = mock_get_client.return_value
        driver = snmp._get_driver(self.node)
        mock_client.set.side_effect = self._get_snmp_failure()
        self.assertRaises(exception.SNMPFailure,
                          driver.power_off)
        mock_client.set.assert_called_once_with(driver._snmp_oid(),
                                                driver.value_power_off)

    def test_power_on_snmp_get_failure(self, mock_get_client):
        # Ensure SNMP failure exceptions raised during a power on get operation
        # are propagated
        mock_client = mock_get_client.return_value
        driver = snmp._get_driver(self.node)
        mock_client.get.side_effect = self._get_snmp_failure()
        self.assertRaises(exception.SNMPFailure,
                          driver.power_on)
        mock_client.set.assert_called_once_with(driver._snmp_oid(),
                                                driver.value_power_on)
        mock_client.get.assert_called_once_with(driver._snmp_oid())

    def test_power_off_snmp_get_failure(self, mock_get_client):
        # Ensure SNMP failure exceptions raised during a power off get
        # operation are propagated
        mock_client = mock_get_client.return_value
        driver = snmp._get_driver(self.node)
        mock_client.get.side_effect = self._get_snmp_failure()
        self.assertRaises(exception.SNMPFailure,
                          driver.power_off)
        mock_client.set.assert_called_once_with(driver._snmp_oid(),
                                                driver.value_power_off)
        mock_client.get.assert_called_once_with(driver._snmp_oid())

    @mock.patch("eventlet.greenthread.sleep")
    def test_power_on_timeout(self, mock_sleep, mock_get_client):
        # Ensure that a power on consistency poll timeout causes an error
        mock_client = mock_get_client.return_value
        driver = snmp._get_driver(self.node)
        mock_client.get.return_value = driver.value_power_off
        pstate = driver.power_on()
        mock_client.set.assert_called_once_with(driver._snmp_oid(),
                                                driver.value_power_on)
        attempts = CONF.snmp.power_timeout // driver.retry_interval
        calls = [mock.call(driver._snmp_oid())] * attempts
        mock_client.get.assert_has_calls(calls)
        self.assertEqual(states.ERROR, pstate)

    @mock.patch("eventlet.greenthread.sleep")
    def test_power_off_timeout(self, mock_sleep, mock_get_client):
        # Ensure that a power off consistency poll timeout causes an error
        mock_client = mock_get_client.return_value
        CONF.snmp.power_timeout = 5
        driver = snmp._get_driver(self.node)
        mock_client.get.return_value = driver.value_power_on
        pstate = driver.power_off()
        mock_client.set.assert_called_once_with(driver._snmp_oid(),
                                                driver.value_power_off)
        attempts = CONF.snmp.power_timeout // driver.retry_interval
        calls = [mock.call(driver._snmp_oid())] * attempts
        mock_client.get.assert_has_calls(calls)
        self.assertEqual(states.ERROR, pstate)

    def test_power_reset(self, mock_get_client):
        # Ensure the device is reset correctly
        mock_client = mock_get_client.return_value
        driver = snmp._get_driver(self.node)
        mock_client.get.side_effect = [driver.value_power_off,
                                       driver.value_power_on]
        pstate = driver.power_reset()
        calls = [mock.call(driver._snmp_oid(), driver.value_power_off),
                 mock.call(driver._snmp_oid(), driver.value_power_on)]
        mock_client.set.assert_has_calls(calls)
        calls = [mock.call(driver._snmp_oid())] * 2
        mock_client.get.assert_has_calls(calls)
        self.assertEqual(states.POWER_ON, pstate)

    @mock.patch("eventlet.greenthread.sleep")
    def test_power_reset_off_delay(self, mock_sleep, mock_get_client):
        # Ensure driver waits for the power off state change following a power
        # reset
        mock_client = mock_get_client.return_value
        driver = snmp._get_driver(self.node)
        mock_client.get.side_effect = [driver.value_power_on,
                                       driver.value_power_off,
                                       driver.value_power_on]
        pstate = driver.power_reset()
        calls = [mock.call(driver._snmp_oid(), driver.value_power_off),
                 mock.call(driver._snmp_oid(), driver.value_power_on)]
        mock_client.set.assert_has_calls(calls)
        calls = [mock.call(driver._snmp_oid())] * 3
        mock_client.get.assert_has_calls(calls)
        self.assertEqual(states.POWER_ON, pstate)

    @mock.patch("eventlet.greenthread.sleep")
    def test_power_reset_on_delay(self, mock_sleep, mock_get_client):
        # Ensure driver waits for the power on state change following a power
        # reset
        mock_client = mock_get_client.return_value
        driver = snmp._get_driver(self.node)
        mock_client.get.side_effect = [driver.value_power_off,
                                       driver.value_power_off,
                                       driver.value_power_on]
        pstate = driver.power_reset()
        calls = [mock.call(driver._snmp_oid(), driver.value_power_off),
                 mock.call(driver._snmp_oid(), driver.value_power_on)]
        mock_client.set.assert_has_calls(calls)
        calls = [mock.call(driver._snmp_oid())] * 3
        mock_client.get.assert_has_calls(calls)
        self.assertEqual(states.POWER_ON, pstate)

    @mock.patch("eventlet.greenthread.sleep")
    def test_power_reset_off_delay_on_delay(self, mock_sleep, mock_get_client):
        # Ensure driver waits for both state changes following a power reset
        mock_client = mock_get_client.return_value
        driver = snmp._get_driver(self.node)
        mock_client.get.side_effect = [driver.value_power_on,
                                       driver.value_power_off,
                                       driver.value_power_off,
                                       driver.value_power_on]
        pstate = driver.power_reset()
        calls = [mock.call(driver._snmp_oid(), driver.value_power_off),
                 mock.call(driver._snmp_oid(), driver.value_power_on)]
        mock_client.set.assert_has_calls(calls)
        calls = [mock.call(driver._snmp_oid())] * 4
        mock_client.get.assert_has_calls(calls)
        self.assertEqual(states.POWER_ON, pstate)

    @mock.patch("eventlet.greenthread.sleep")
    def test_power_reset_off_invalid_state(self, mock_sleep, mock_get_client):
        # Ensure driver retries when querying unexpected states following a
        # power off during a reset
        mock_client = mock_get_client.return_value
        driver = snmp._get_driver(self.node)
        mock_client.get.return_value = 42
        pstate = driver.power_reset()
        mock_client.set.assert_called_once_with(driver._snmp_oid(),
                                                driver.value_power_off)
        attempts = CONF.snmp.power_timeout // driver.retry_interval
        calls = [mock.call(driver._snmp_oid())] * attempts
        mock_client.get.assert_has_calls(calls)
        self.assertEqual(states.ERROR, pstate)

    @mock.patch("eventlet.greenthread.sleep")
    def test_power_reset_on_invalid_state(self, mock_sleep, mock_get_client):
        # Ensure driver retries when querying unexpected states following a
        # power on during a reset
        mock_client = mock_get_client.return_value
        driver = snmp._get_driver(self.node)
        attempts = CONF.snmp.power_timeout // driver.retry_interval
        mock_client.get.side_effect = ([driver.value_power_off] +
                                       [42] * attempts)
        pstate = driver.power_reset()
        calls = [mock.call(driver._snmp_oid(), driver.value_power_off),
                 mock.call(driver._snmp_oid(), driver.value_power_on)]
        mock_client.set.assert_has_calls(calls)
        calls = [mock.call(driver._snmp_oid())] * (1 + attempts)
        mock_client.get.assert_has_calls(calls)
        self.assertEqual(states.ERROR, pstate)

    @mock.patch("eventlet.greenthread.sleep")
    def test_power_reset_off_timeout(self, mock_sleep, mock_get_client):
        # Ensure that a power off consistency poll timeout during a reset
        # causes an error
        mock_client = mock_get_client.return_value
        driver = snmp._get_driver(self.node)
        mock_client.get.return_value = driver.value_power_on
        pstate = driver.power_reset()
        mock_client.set.assert_called_once_with(driver._snmp_oid(),
                                                driver.value_power_off)
        attempts = CONF.snmp.power_timeout // driver.retry_interval
        calls = [mock.call(driver._snmp_oid())] * attempts
        mock_client.get.assert_has_calls(calls)
        self.assertEqual(states.ERROR, pstate)

    @mock.patch("eventlet.greenthread.sleep")
    def test_power_reset_on_timeout(self, mock_sleep, mock_get_client):
        # Ensure that a power on consistency poll timeout during a reset
        # causes an error
        mock_client = mock_get_client.return_value
        driver = snmp._get_driver(self.node)
        attempts = CONF.snmp.power_timeout // driver.retry_interval
        mock_client.get.side_effect = ([driver.value_power_off] *
                                       (1 + attempts))
        pstate = driver.power_reset()
        calls = [mock.call(driver._snmp_oid(), driver.value_power_off),
                 mock.call(driver._snmp_oid(), driver.value_power_on)]
        mock_client.set.assert_has_calls(calls)
        calls = [mock.call(driver._snmp_oid())] * (1 + attempts)
        mock_client.get.assert_has_calls(calls)
        self.assertEqual(states.ERROR, pstate)

    def test_power_reset_off_snmp_set_failure(self, mock_get_client):
        # Ensure SNMP failure exceptions raised during a reset power off set
        # operation are propagated
        mock_client = mock_get_client.return_value
        driver = snmp._get_driver(self.node)
        mock_client.set.side_effect = self._get_snmp_failure()
        self.assertRaises(exception.SNMPFailure,
                          driver.power_reset)
        mock_client.set.assert_called_once_with(driver._snmp_oid(),
                                                driver.value_power_off)
        self.assertFalse(mock_client.get.called)

    def test_power_reset_off_snmp_get_failure(self, mock_get_client):
        # Ensure SNMP failure exceptions raised during a reset power off get
        # operation are propagated
        mock_client = mock_get_client.return_value
        driver = snmp._get_driver(self.node)
        mock_client.get.side_effect = self._get_snmp_failure()
        self.assertRaises(exception.SNMPFailure,
                          driver.power_reset)
        mock_client.set.assert_called_once_with(driver._snmp_oid(),
                                                driver.value_power_off)
        mock_client.get.assert_called_once_with(driver._snmp_oid())

    def test_power_reset_on_snmp_set_failure(self, mock_get_client):
        # Ensure SNMP failure exceptions raised during a reset power on set
        # operation are propagated
        mock_client = mock_get_client.return_value
        driver = snmp._get_driver(self.node)
        mock_client.set.side_effect = [None, self._get_snmp_failure()]
        mock_client.get.return_value = driver.value_power_off
        self.assertRaises(exception.SNMPFailure,
                          driver.power_reset)
        calls = [mock.call(driver._snmp_oid(), driver.value_power_off),
                 mock.call(driver._snmp_oid(), driver.value_power_on)]
        mock_client.set.assert_has_calls(calls)
        mock_client.get.assert_called_once_with(driver._snmp_oid())

    def test_power_reset_on_snmp_get_failure(self, mock_get_client):
        # Ensure SNMP failure exceptions raised during a reset power on get
        # operation are propagated
        mock_client = mock_get_client.return_value
        driver = snmp._get_driver(self.node)
        mock_client.get.side_effect = [driver.value_power_off,
                                       self._get_snmp_failure()]
        self.assertRaises(exception.SNMPFailure,
                          driver.power_reset)
        calls = [mock.call(driver._snmp_oid(), driver.value_power_off),
                 mock.call(driver._snmp_oid(), driver.value_power_on)]
        mock_client.set.assert_has_calls(calls)
        calls = [mock.call(driver._snmp_oid()), mock.call(driver._snmp_oid())]
        mock_client.get.assert_has_calls(calls)

    def _test_simple_device_power_state_on(self, snmp_driver, mock_get_client):
        # Ensure a simple device driver queries power on correctly
        mock_client = mock_get_client.return_value
        self._set_snmp_driver(snmp_driver)
        driver = snmp._get_driver(self.node)
        mock_client.get.return_value = driver.value_power_on
        pstate = driver.power_state()
        mock_client.get.assert_called_once_with(driver._snmp_oid())
        self.assertEqual(states.POWER_ON, pstate)

    def _test_simple_device_power_state_off(self, snmp_driver,
                                            mock_get_client):
        # Ensure a simple device driver queries power off correctly
        mock_client = mock_get_client.return_value
        self._set_snmp_driver(snmp_driver)
        driver = snmp._get_driver(self.node)
        mock_client.get.return_value = driver.value_power_off
        pstate = driver.power_state()
        mock_client.get.assert_called_once_with(driver._snmp_oid())
        self.assertEqual(states.POWER_OFF, pstate)

    def _test_simple_device_power_on(self, snmp_driver, mock_get_client):
        # Ensure a simple device driver powers on correctly
        mock_client = mock_get_client.return_value
        self._set_snmp_driver(snmp_driver)
        driver = snmp._get_driver(self.node)
        mock_client.get.return_value = driver.value_power_on
        pstate = driver.power_on()
        mock_client.set.assert_called_once_with(driver._snmp_oid(),
                                                driver.value_power_on)
        mock_client.get.assert_called_once_with(driver._snmp_oid())
        self.assertEqual(states.POWER_ON, pstate)

    def _test_simple_device_power_off(self, snmp_driver, mock_get_client):
        # Ensure a simple device driver powers off correctly
        mock_client = mock_get_client.return_value
        self._set_snmp_driver(snmp_driver)
        driver = snmp._get_driver(self.node)
        mock_client.get.return_value = driver.value_power_off
        pstate = driver.power_off()
        mock_client.set.assert_called_once_with(driver._snmp_oid(),
                                                driver.value_power_off)
        mock_client.get.assert_called_once_with(driver._snmp_oid())
        self.assertEqual(states.POWER_OFF, pstate)

    def _test_simple_device_power_reset(self, snmp_driver, mock_get_client):
        # Ensure a simple device driver resets correctly
        mock_client = mock_get_client.return_value
        self._set_snmp_driver(snmp_driver)
        driver = snmp._get_driver(self.node)
        mock_client.get.side_effect = [driver.value_power_off,
                                       driver.value_power_on]
        pstate = driver.power_reset()
        calls = [mock.call(driver._snmp_oid(), driver.value_power_off),
                 mock.call(driver._snmp_oid(), driver.value_power_on)]
        mock_client.set.assert_has_calls(calls)
        calls = [mock.call(driver._snmp_oid())] * 2
        mock_client.get.assert_has_calls(calls)
        self.assertEqual(states.POWER_ON, pstate)

    def test_apc_snmp_objects(self, mock_get_client):
        # Ensure the correct SNMP object OIDs and values are used by the APC
        # driver
        self._update_driver_info(snmp_driver="apc",
                                 snmp_outlet="3")
        driver = snmp._get_driver(self.node)
        oid = (1, 3, 6, 1, 4, 1, 318, 1, 1, 4, 4, 2, 1, 3, 3)
        self.assertEqual(oid, driver._snmp_oid())
        self.assertEqual(1, driver.value_power_on)
        self.assertEqual(2, driver.value_power_off)

    def test_apc_power_state_on(self, mock_get_client):
        self._test_simple_device_power_state_on('apc', mock_get_client)

    def test_apc_power_state_off(self, mock_get_client):
        self._test_simple_device_power_state_off('apc', mock_get_client)

    def test_apc_power_on(self, mock_get_client):
        self._test_simple_device_power_on('apc', mock_get_client)

    def test_apc_power_off(self, mock_get_client):
        self._test_simple_device_power_off('apc', mock_get_client)

    def test_apc_power_reset(self, mock_get_client):
        self._test_simple_device_power_reset('apc', mock_get_client)

    def test_aten_snmp_objects(self, mock_get_client):
        # Ensure the correct SNMP object OIDs and values are used by the
        # Aten driver
        self._update_driver_info(snmp_driver="aten",
                                 snmp_outlet="3")
        driver = snmp._get_driver(self.node)
        oid = (1, 3, 6, 1, 4, 1, 21317, 1, 3, 2, 2, 2, 2, 3, 0)
        self.assertEqual(oid, driver._snmp_oid())
        self.assertEqual(2, driver.value_power_on)
        self.assertEqual(1, driver.value_power_off)

    def test_aten_power_state_on(self, mock_get_client):
        self._test_simple_device_power_state_on('aten', mock_get_client)

    def test_aten_power_state_off(self, mock_get_client):
        self._test_simple_device_power_state_off('aten', mock_get_client)

    def test_aten_power_on(self, mock_get_client):
        self._test_simple_device_power_on('aten', mock_get_client)

    def test_aten_power_off(self, mock_get_client):
        self._test_simple_device_power_off('aten', mock_get_client)

    def test_aten_power_reset(self, mock_get_client):
        self._test_simple_device_power_reset('aten', mock_get_client)

    def test_cyberpower_snmp_objects(self, mock_get_client):
        # Ensure the correct SNMP object OIDs and values are used by the
        # CyberPower driver
        self._update_driver_info(snmp_driver="cyberpower",
                                 snmp_outlet="3")
        driver = snmp._get_driver(self.node)
        oid = (1, 3, 6, 1, 4, 1, 3808, 1, 1, 3, 3, 3, 1, 1, 4, 3)
        self.assertEqual(oid, driver._snmp_oid())
        self.assertEqual(1, driver.value_power_on)
        self.assertEqual(2, driver.value_power_off)

    def test_cyberpower_power_state_on(self, mock_get_client):
        self._test_simple_device_power_state_on('cyberpower', mock_get_client)

    def test_cyberpower_power_state_off(self, mock_get_client):
        self._test_simple_device_power_state_off('cyberpower', mock_get_client)

    def test_cyberpower_power_on(self, mock_get_client):
        self._test_simple_device_power_on('cyberpower', mock_get_client)

    def test_cyberpower_power_off(self, mock_get_client):
        self._test_simple_device_power_off('cyberpower', mock_get_client)

    def test_cyberpower_power_reset(self, mock_get_client):
        self._test_simple_device_power_reset('cyberpower', mock_get_client)

    def test_teltronix_snmp_objects(self, mock_get_client):
        # Ensure the correct SNMP object OIDs and values are used by the
        # Teltronix driver
        self._update_driver_info(snmp_driver="teltronix",
                                 snmp_outlet="3")
        driver = snmp._get_driver(self.node)
        oid = (1, 3, 6, 1, 4, 1, 23620, 1, 2, 2, 1, 4, 3)
        self.assertEqual(oid, driver._snmp_oid())
        self.assertEqual(2, driver.value_power_on)
        self.assertEqual(1, driver.value_power_off)

    def test_teltronix_power_state_on(self, mock_get_client):
        self._test_simple_device_power_state_on('teltronix', mock_get_client)

    def test_teltronix_power_state_off(self, mock_get_client):
        self._test_simple_device_power_state_off('teltronix', mock_get_client)

    def test_teltronix_power_on(self, mock_get_client):
        self._test_simple_device_power_on('teltronix', mock_get_client)

    def test_teltronix_power_off(self, mock_get_client):
        self._test_simple_device_power_off('teltronix', mock_get_client)

    def test_teltronix_power_reset(self, mock_get_client):
        self._test_simple_device_power_reset('teltronix', mock_get_client)

    def test_eaton_power_snmp_objects(self, mock_get_client):
        # Ensure the correct SNMP object OIDs and values are used by the Eaton
        # Power driver
        self._update_driver_info(snmp_driver="eatonpower",
                                 snmp_outlet="3")
        driver = snmp._get_driver(self.node)
        status_oid = (1, 3, 6, 1, 4, 1, 534, 6, 6, 7, 6, 6, 1, 2, 3)
        poweron_oid = (1, 3, 6, 1, 4, 1, 534, 6, 6, 7, 6, 6, 1, 3, 3)
        poweroff_oid = (1, 3, 6, 1, 4, 1, 534, 6, 6, 7, 6, 6, 1, 4, 3)
        self.assertEqual(status_oid, driver._snmp_oid(driver.oid_status))
        self.assertEqual(poweron_oid, driver._snmp_oid(driver.oid_poweron))
        self.assertEqual(poweroff_oid, driver._snmp_oid(driver.oid_poweroff))
        self.assertEqual(0, driver.status_off)
        self.assertEqual(1, driver.status_on)
        self.assertEqual(2, driver.status_pending_off)
        self.assertEqual(3, driver.status_pending_on)

    def test_eaton_power_power_state_on(self, mock_get_client):
        # Ensure the Eaton Power driver queries on correctly
        mock_client = mock_get_client.return_value
        self._set_snmp_driver("eatonpower")
        driver = snmp._get_driver(self.node)
        mock_client.get.return_value = driver.status_on
        pstate = driver.power_state()
        mock_client.get.assert_called_once_with(
                driver._snmp_oid(driver.oid_status))
        self.assertEqual(states.POWER_ON, pstate)

    def test_eaton_power_power_state_off(self, mock_get_client):
        # Ensure the Eaton Power driver queries off correctly
        mock_client = mock_get_client.return_value
        self._set_snmp_driver("eatonpower")
        driver = snmp._get_driver(self.node)
        mock_client.get.return_value = driver.status_off
        pstate = driver.power_state()
        mock_client.get.assert_called_once_with(
                driver._snmp_oid(driver.oid_status))
        self.assertEqual(states.POWER_OFF, pstate)

    def test_eaton_power_power_state_pending_off(self, mock_get_client):
        # Ensure the Eaton Power driver queries pending off correctly
        mock_client = mock_get_client.return_value
        self._set_snmp_driver("eatonpower")
        driver = snmp._get_driver(self.node)
        mock_client.get.return_value = driver.status_pending_off
        pstate = driver.power_state()
        mock_client.get.assert_called_once_with(
                driver._snmp_oid(driver.oid_status))
        self.assertEqual(states.POWER_ON, pstate)

    def test_eaton_power_power_state_pending_on(self, mock_get_client):
        # Ensure the Eaton Power driver queries pending on correctly
        mock_client = mock_get_client.return_value
        self._set_snmp_driver("eatonpower")
        driver = snmp._get_driver(self.node)
        mock_client.get.return_value = driver.status_pending_on
        pstate = driver.power_state()
        mock_client.get.assert_called_once_with(
                driver._snmp_oid(driver.oid_status))
        self.assertEqual(states.POWER_OFF, pstate)

    def test_eaton_power_power_on(self, mock_get_client):
        # Ensure the Eaton Power driver powers on correctly
        mock_client = mock_get_client.return_value
        self._set_snmp_driver("eatonpower")
        driver = snmp._get_driver(self.node)
        mock_client.get.return_value = driver.status_on
        pstate = driver.power_on()
        mock_client.set.assert_called_once_with(
                driver._snmp_oid(driver.oid_poweron), driver.value_power_on)
        mock_client.get.assert_called_once_with(
                driver._snmp_oid(driver.oid_status))
        self.assertEqual(states.POWER_ON, pstate)

    def test_eaton_power_power_off(self, mock_get_client):
        # Ensure the Eaton Power driver powers off correctly
        mock_client = mock_get_client.return_value
        self._set_snmp_driver("eatonpower")
        driver = snmp._get_driver(self.node)
        mock_client.get.return_value = driver.status_off
        pstate = driver.power_off()
        mock_client.set.assert_called_once_with(
                driver._snmp_oid(driver.oid_poweroff), driver.value_power_off)
        mock_client.get.assert_called_once_with(
                driver._snmp_oid(driver.oid_status))
        self.assertEqual(states.POWER_OFF, pstate)

    def test_eaton_power_power_reset(self, mock_get_client):
        # Ensure the Eaton Power driver resets correctly
        mock_client = mock_get_client.return_value
        self._set_snmp_driver("eatonpower")
        driver = snmp._get_driver(self.node)
        mock_client.get.side_effect = [driver.status_off, driver.status_on]
        pstate = driver.power_reset()
        calls = [mock.call(driver._snmp_oid(driver.oid_poweroff),
                           driver.value_power_off),
                 mock.call(driver._snmp_oid(driver.oid_poweron),
                           driver.value_power_on)]
        mock_client.set.assert_has_calls(calls)
        calls = [mock.call(driver._snmp_oid(driver.oid_status))] * 2
        mock_client.get.assert_has_calls(calls)
        self.assertEqual(states.POWER_ON, pstate)


@mock.patch.object(snmp, '_get_driver')
class SNMPDriverTestCase(db_base.DbTestCase):
    """SNMP power driver interface tests.

    In this test case, the SNMP power driver interface is exercised. The
    device-specific SNMP driver is mocked to allow various error cases to be
    tested.
    """

    def setUp(self):
        super(SNMPDriverTestCase, self).setUp()
        mgr_utils.mock_the_extension_manager(driver='fake_snmp')

        self.node = obj_utils.create_test_node(self.context,
                                               driver='fake_snmp',
                                               driver_info=INFO_DICT)

    def _get_snmp_failure(self):
        return exception.SNMPFailure(operation='test-operation',
                                     error='test-error')

    def test_get_properties(self, mock_get_driver):
        expected = snmp.COMMON_PROPERTIES
        with task_manager.acquire(self.context, self.node.uuid) as task:
            self.assertEqual(expected, task.driver.get_properties())

    def test_get_power_state_on(self, mock_get_driver):
        mock_driver = mock_get_driver.return_value
        mock_driver.power_state.return_value = states.POWER_ON
        with task_manager.acquire(self.context, self.node.uuid) as task:
            pstate = task.driver.power.get_power_state(task)
        mock_driver.power_state.assert_called_once_with()
        self.assertEqual(states.POWER_ON, pstate)

    def test_get_power_state_off(self, mock_get_driver):
        mock_driver = mock_get_driver.return_value
        mock_driver.power_state.return_value = states.POWER_OFF
        with task_manager.acquire(self.context, self.node.uuid) as task:
            pstate = task.driver.power.get_power_state(task)
        mock_driver.power_state.assert_called_once_with()
        self.assertEqual(states.POWER_OFF, pstate)

    def test_get_power_state_error(self, mock_get_driver):
        mock_driver = mock_get_driver.return_value
        mock_driver.power_state.return_value = states.ERROR
        with task_manager.acquire(self.context, self.node.uuid) as task:
            pstate = task.driver.power.get_power_state(task)
        mock_driver.power_state.assert_called_once_with()
        self.assertEqual(states.ERROR, pstate)

    def test_get_power_state_snmp_failure(self, mock_get_driver):
        mock_driver = mock_get_driver.return_value
        mock_driver.power_state.side_effect = self._get_snmp_failure()
        with task_manager.acquire(self.context, self.node.uuid) as task:
            self.assertRaises(exception.SNMPFailure,
                              task.driver.power.get_power_state, task)
        mock_driver.power_state.assert_called_once_with()

    def test_set_power_state_on(self, mock_get_driver):
        mock_driver = mock_get_driver.return_value
        mock_driver.power_on.return_value = states.POWER_ON
        with task_manager.acquire(self.context, self.node.uuid) as task:
            task.driver.power.set_power_state(task, states.POWER_ON)
        mock_driver.power_on.assert_called_once_with()

    def test_set_power_state_off(self, mock_get_driver):
        mock_driver = mock_get_driver.return_value
        mock_driver.power_off.return_value = states.POWER_OFF
        with task_manager.acquire(self.context, self.node.uuid) as task:
            task.driver.power.set_power_state(task, states.POWER_OFF)
        mock_driver.power_off.assert_called_once_with()

    def test_set_power_state_error(self, mock_get_driver):
        with task_manager.acquire(self.context, self.node.uuid) as task:
            self.assertRaises(exception.InvalidParameterValue,
                              task.driver.power.set_power_state,
                              task, states.ERROR)

    def test_set_power_state_on_snmp_failure(self, mock_get_driver):
        mock_driver = mock_get_driver.return_value
        mock_driver.power_on.side_effect = self._get_snmp_failure()
        with task_manager.acquire(self.context, self.node.uuid) as task:
            self.assertRaises(exception.SNMPFailure,
                              task.driver.power.set_power_state,
                              task, states.POWER_ON)
        mock_driver.power_on.assert_called_once_with()

    def test_set_power_state_off_snmp_failure(self, mock_get_driver):
        mock_driver = mock_get_driver.return_value
        mock_driver.power_off.side_effect = self._get_snmp_failure()
        with task_manager.acquire(self.context, self.node.uuid) as task:
            self.assertRaises(exception.SNMPFailure,
                              task.driver.power.set_power_state,
                              task, states.POWER_OFF)
        mock_driver.power_off.assert_called_once_with()

    def test_set_power_state_on_timeout(self, mock_get_driver):
        mock_driver = mock_get_driver.return_value
        mock_driver.power_on.return_value = states.ERROR
        with task_manager.acquire(self.context, self.node.uuid) as task:
            self.assertRaises(exception.PowerStateFailure,
                              task.driver.power.set_power_state,
                              task, states.POWER_ON)
        mock_driver.power_on.assert_called_once_with()

    def test_set_power_state_off_timeout(self, mock_get_driver):
        mock_driver = mock_get_driver.return_value
        mock_driver.power_off.return_value = states.ERROR
        with task_manager.acquire(self.context, self.node.uuid) as task:
            self.assertRaises(exception.PowerStateFailure,
                              task.driver.power.set_power_state,
                              task, states.POWER_OFF)
        mock_driver.power_off.assert_called_once_with()

    def test_reboot(self, mock_get_driver):
        mock_driver = mock_get_driver.return_value
        mock_driver.power_reset.return_value = states.POWER_ON
        with task_manager.acquire(self.context, self.node.uuid) as task:
            task.driver.power.reboot(task)
        mock_driver.power_reset.assert_called_once_with()

    def test_reboot_snmp_failure(self, mock_get_driver):
        mock_driver = mock_get_driver.return_value
        mock_driver.power_reset.side_effect = self._get_snmp_failure()
        with task_manager.acquire(self.context, self.node.uuid) as task:
            self.assertRaises(exception.SNMPFailure,
                              task.driver.power.reboot, task)
        mock_driver.power_reset.assert_called_once_with()

    def test_reboot_timeout(self, mock_get_driver):
        mock_driver = mock_get_driver.return_value
        mock_driver.power_reset.return_value = states.ERROR
        with task_manager.acquire(self.context, self.node.uuid) as task:
            self.assertRaises(exception.PowerStateFailure,
                              task.driver.power.reboot, task)
        mock_driver.power_reset.assert_called_once_with()