Revision fac83f8a

b/lib/backend.py
1112 1112
    _Fail("Missing bridges %s", utils.CommaJoin(missing))
1113 1113

  
1114 1114

  
1115
def GetInstanceList(hypervisor_list):
1115
def GetInstanceList(hypervisor_list, all_hvparams=None,
1116
                    get_hv_fn=hypervisor.GetHypervisor):
1116 1117
  """Provides a list of instances.
1117 1118

  
1118 1119
  @type hypervisor_list: list
1119 1120
  @param hypervisor_list: the list of hypervisors to query information
1121
  @type all_hvparams: dict of dict of strings
1122
  @param all_hvparams: a dictionary mapping hypervisor types to respective
1123
    cluster-wide hypervisor parameters
1124
  @type get_hv_fn: function
1125
  @param get_hv_fn: function that returns a hypervisor for the given hypervisor
1126
    name; optional parameter to increase testability
1120 1127

  
1121 1128
  @rtype: list
1122 1129
  @return: a list of all running instances on the current node
......
1127 1134
  results = []
1128 1135
  for hname in hypervisor_list:
1129 1136
    try:
1130
      names = hypervisor.GetHypervisor(hname).ListInstances()
1137
      hvparams = None
1138
      if all_hvparams is not None:
1139
        hvparams = all_hvparams[hname]
1140
      hv = get_hv_fn(hname)
1141
      names = hv.ListInstances(hvparams)
1131 1142
      results.extend(names)
1132 1143
    except errors.HypervisorError, err:
1133 1144
      _Fail("Error enumerating instances (hypervisor %s): %s",
b/test/py/ganeti.backend_unittest.py
26 26
import shutil
27 27
import tempfile
28 28
import unittest
29
import mock
29 30

  
30 31
from ganeti import utils
31 32
from ganeti import constants
......
33 34
from ganeti import netutils
34 35
from ganeti import errors
35 36
from ganeti import serializer
37
from ganeti import hypervisor
36 38

  
37 39
import testutils
38 40
import mocks
......
538 540
      self._Test("inst1.example.com", idx)
539 541

  
540 542

  
543
class TestGetInstanceList(unittest.TestCase):
544

  
545
  def setUp(self):
546
    self._test_hv = self._TestHypervisor()
547
    self._test_hv.ListInstances = mock.Mock(
548
      return_value=["instance1", "instance2", "instance3"] )
549

  
550
  class _TestHypervisor(hypervisor.hv_base.BaseHypervisor):
551
    def __init__(self):
552
      hypervisor.hv_base.BaseHypervisor.__init__(self)
553

  
554
  def _GetHypervisor(self, name):
555
    return self._test_hv
556

  
557
  def testHvparams(self):
558
    fake_hvparams = {constants.HV_XEN_CMD: constants.XEN_CMD_XL}
559
    hvparams = {constants.HT_FAKE: fake_hvparams}
560
    backend.GetInstanceList([constants.HT_FAKE], all_hvparams=hvparams,
561
                            get_hv_fn=self._GetHypervisor)
562
    self._test_hv.ListInstances.assert_called_with(hvparams=fake_hvparams)
563

  
564

  
541 565
if __name__ == "__main__":
542 566
  testutils.GanetiTestProgram()

Also available in: Unified diff