本文整理匯總了Python中cloudinstall.placement.controller.PlacementController.assign方法的典型用法代碼示例。如果您正苦於以下問題:Python PlacementController.assign方法的具體用法?Python PlacementController.assign怎麽用?Python PlacementController.assign使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類cloudinstall.placement.controller.PlacementController
的用法示例。
在下文中一共展示了PlacementController.assign方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: PlacementControllerTestCase
# 需要導入模塊: from cloudinstall.placement.controller import PlacementController [as 別名]
# 或者: from cloudinstall.placement.controller.PlacementController import assign [as 別名]
class PlacementControllerTestCase(unittest.TestCase):
def setUp(self):
self.mock_maas_state = MagicMock()
with NamedTemporaryFile(mode='w+', encoding='utf-8') as tempf:
utils.spew(tempf.name, yaml.dump(dict()))
self.conf = Config({}, tempf.name)
self.pc = PlacementController(self.mock_maas_state,
self.conf)
self.mock_machine = MagicMock(name='machine1')
pmid = PropertyMock(return_value='fake-instance-id-1')
type(self.mock_machine).instance_id = pmid
self.mock_machine_2 = MagicMock(name='machine2')
pmid2 = PropertyMock(return_value='fake-instance-id-2')
type(self.mock_machine_2).instance_id = pmid2
self.mock_machines = [self.mock_machine, self.mock_machine_2]
self.mock_maas_state.machines.return_value = self.mock_machines
def test_get_assignments_atype(self):
self.assertEqual(0,
len(self.pc.get_assignments(CharmNovaCompute)))
self.pc.assign(self.mock_machine, CharmNovaCompute, AssignmentType.LXC)
self.pc.assign(self.mock_machine, CharmNovaCompute, AssignmentType.LXC)
md = self.pc.get_assignments(CharmNovaCompute)
self.assertEqual(1, len(md))
self.assertEqual(2, len(md[AssignmentType.LXC]))
def _do_test_simple_assign_type(self, assignment_type):
self.pc.assign(self.mock_machine, CharmNovaCompute, assignment_type)
print("assignments is {}".format(self.pc.assignments))
machines = self.pc.get_assignments(CharmNovaCompute)
print('machines for charm is {}'.format(machines))
self.assertEqual(machines,
{assignment_type: [self.mock_machine]})
ma = self.pc.assignments_for_machine(self.mock_machine)
self.assertEqual(ma[assignment_type], [CharmNovaCompute])
def test_simple_assign_bare(self):
self._do_test_simple_assign_type(AssignmentType.BareMetal)
def test_simple_assign_lxc(self):
self._do_test_simple_assign_type(AssignmentType.LXC)
def test_simple_assign_kvm(self):
self._do_test_simple_assign_type(AssignmentType.KVM)
def test_assign_nonmulti(self):
self.pc.assign(self.mock_machine, CharmKeystone, AssignmentType.LXC)
self.assertEqual(self.pc.get_assignments(CharmKeystone),
{AssignmentType.LXC: [self.mock_machine]})
self.pc.assign(self.mock_machine, CharmKeystone, AssignmentType.KVM)
self.assertEqual(self.pc.get_assignments(CharmKeystone),
{AssignmentType.KVM: [self.mock_machine]})
am = self.pc.assignments_for_machine(self.mock_machine)
self.assertEqual(am[AssignmentType.KVM], [CharmKeystone])
self.assertEqual(am[AssignmentType.LXC], [])
def test_assign_multi(self):
self.pc.assign(self.mock_machine, CharmNovaCompute, AssignmentType.LXC)
self.assertEqual(self.pc.get_assignments(CharmNovaCompute),
{AssignmentType.LXC: [self.mock_machine]})
self.pc.assign(self.mock_machine, CharmNovaCompute, AssignmentType.KVM)
self.assertEqual(self.pc.get_assignments(CharmNovaCompute),
{AssignmentType.LXC: [self.mock_machine],
AssignmentType.KVM: [self.mock_machine]})
ma = self.pc.assignments_for_machine(self.mock_machine)
self.assertEqual(ma[AssignmentType.LXC], [CharmNovaCompute])
self.assertEqual(ma[AssignmentType.KVM], [CharmNovaCompute])
def test_remove_assignment_multi(self):
self.pc.assign(self.mock_machine, CharmNovaCompute, AssignmentType.LXC)
self.pc.assign(self.mock_machine_2, CharmNovaCompute,
AssignmentType.LXC)
mfc = self.pc.get_assignments(CharmNovaCompute)
mfc_lxc = set(mfc[AssignmentType.LXC])
self.assertEqual(mfc_lxc, set(self.mock_machines))
self.pc.clear_assignments(self.mock_machine)
self.assertEqual(self.pc.get_assignments(CharmNovaCompute),
{AssignmentType.LXC: [self.mock_machine_2]})
def test_gen_defaults(self):
satisfies_importstring = 'cloudinstall.placement.controller.satisfies'
with patch(satisfies_importstring) as mock_satisfies:
mock_satisfies.return_value = (True, )
defs = self.pc.gen_defaults(charm_classes=[CharmNovaCompute,
CharmKeystone],
maas_machines=[self.mock_machine,
#.........這裏部分代碼省略.........
示例2: PlacementControllerTestCase
# 需要導入模塊: from cloudinstall.placement.controller import PlacementController [as 別名]
# 或者: from cloudinstall.placement.controller.PlacementController import assign [as 別名]
class PlacementControllerTestCase(unittest.TestCase):
def setUp(self):
self.mock_maas_state = MagicMock()
self.mock_opts = MagicMock()
swopt = PropertyMock(return_value=False)
type(self.mock_opts).enable_swift = swopt
self.pc = PlacementController(self.mock_maas_state,
self.mock_opts)
self.mock_machine = MagicMock(name='machine1')
pmid = PropertyMock(return_value='fake-instance-id-1')
type(self.mock_machine).instance_id = pmid
self.mock_machine_2 = MagicMock(name='machine2')
pmid2 = PropertyMock(return_value='fake-instance-id-2')
type(self.mock_machine_2).instance_id = pmid2
self.mock_machines = [self.mock_machine, self.mock_machine_2]
self.mock_maas_state.machines.return_value = self.mock_machines
def test_machines_for_charm_atype(self):
self.assertEqual(0, len(self.pc.machines_for_charm(CharmNovaCompute)))
self.pc.assign(self.mock_machine, CharmNovaCompute, AssignmentType.LXC)
self.pc.assign(self.mock_machine, CharmNovaCompute, AssignmentType.LXC)
md = self.pc.machines_for_charm(CharmNovaCompute)
self.assertEqual(1, len(md))
self.assertEqual(2, len(md[AssignmentType.LXC]))
def _do_test_simple_assign_type(self, assignment_type):
self.pc.assign(self.mock_machine, CharmNovaCompute, assignment_type)
print("assignments is {}".format(self.pc.assignments))
machines = self.pc.machines_for_charm(CharmNovaCompute)
print('machines for charm is {}'.format(machines))
self.assertEqual(machines,
{assignment_type: [self.mock_machine]})
ma = self.pc.assignments_for_machine(self.mock_machine)
self.assertEqual(ma[assignment_type], [CharmNovaCompute])
def test_simple_assign_bare(self):
self._do_test_simple_assign_type(AssignmentType.BareMetal)
def test_simple_assign_lxc(self):
self._do_test_simple_assign_type(AssignmentType.LXC)
def test_simple_assign_kvm(self):
self._do_test_simple_assign_type(AssignmentType.KVM)
def test_assign_nonmulti(self):
self.pc.assign(self.mock_machine, CharmKeystone, AssignmentType.LXC)
self.assertEqual(self.pc.machines_for_charm(CharmKeystone),
{AssignmentType.LXC: [self.mock_machine]})
self.pc.assign(self.mock_machine, CharmKeystone, AssignmentType.KVM)
self.assertEqual(self.pc.machines_for_charm(CharmKeystone),
{AssignmentType.KVM: [self.mock_machine]})
am = self.pc.assignments_for_machine(self.mock_machine)
self.assertEqual(am[AssignmentType.KVM], [CharmKeystone])
self.assertEqual(am[AssignmentType.LXC], [])
def test_assign_multi(self):
self.pc.assign(self.mock_machine, CharmNovaCompute, AssignmentType.LXC)
self.assertEqual(self.pc.machines_for_charm(CharmNovaCompute),
{AssignmentType.LXC: [self.mock_machine]})
self.pc.assign(self.mock_machine, CharmNovaCompute, AssignmentType.KVM)
self.assertEqual(self.pc.machines_for_charm(CharmNovaCompute),
{AssignmentType.LXC: [self.mock_machine],
AssignmentType.KVM: [self.mock_machine]})
ma = self.pc.assignments_for_machine(self.mock_machine)
self.assertEqual(ma[AssignmentType.LXC], [CharmNovaCompute])
self.assertEqual(ma[AssignmentType.KVM], [CharmNovaCompute])
def test_remove_assignment_multi(self):
self.pc.assign(self.mock_machine, CharmNovaCompute, AssignmentType.LXC)
self.pc.assign(self.mock_machine_2, CharmNovaCompute,
AssignmentType.LXC)
mfc = self.pc.machines_for_charm(CharmNovaCompute)
mfc_lxc = set(mfc[AssignmentType.LXC])
self.assertEqual(mfc_lxc, set(self.mock_machines))
self.pc.clear_assignments(self.mock_machine)
self.assertEqual(self.pc.machines_for_charm(CharmNovaCompute),
{AssignmentType.LXC: [self.mock_machine_2]})
def test_gen_defaults(self):
satisfies_importstring = 'cloudinstall.placement.controller.satisfies'
with patch(satisfies_importstring) as mock_satisfies:
mock_satisfies.return_value = (True, )
defs = self.pc.gen_defaults(charm_classes=[CharmNovaCompute,
CharmKeystone],
maas_machines=[self.mock_machine,
self.mock_machine_2])
#.........這裏部分代碼省略.........
示例3: ServicesListTestCase
# 需要導入模塊: from cloudinstall.placement.controller import PlacementController [as 別名]
# 或者: from cloudinstall.placement.controller.PlacementController import assign [as 別名]
class ServicesListTestCase(unittest.TestCase):
def setUp(self):
self.mock_maas_state = MagicMock()
with NamedTemporaryFile(mode='w+', encoding='utf-8') as tempf:
utils.spew(tempf.name, yaml.dump(dict()))
self.conf = Config({}, tempf.name)
self.pc = PlacementController(self.mock_maas_state,
self.conf)
self.mock_machine = make_fake_machine('machine1', {'cpu_count': 3})
self.mock_machine2 = make_fake_machine('machine2')
self.mock_machine3 = make_fake_machine('machine3')
self.mock_machines = [self.mock_machine]
self.mock_maas_state.machines.return_value = self.mock_machines
self.actions = []
self.sub_actions = []
def test_widgets_config(self, mock_servicewidgetclass):
for show_constraints in [False, True]:
sl = ServicesList(self.pc, self.actions, self.sub_actions,
show_constraints=show_constraints)
mock_servicewidgetclass.assert_any_call(
CharmNovaCompute,
self.pc,
self.actions,
show_constraints,
show_placements=sl.show_placements)
mock_servicewidgetclass.reset_mock()
def test_no_machine_no_constraints(self, mock_servicewidgetclass):
with patch.object(self.pc, 'charm_classes') as mock_classesfunc:
fc = MagicMock(name='fakeclass1')
fc.required_num_units.return_value = 1
fc.constraints = {'cpu_count': 1000}
mock_classesfunc.return_value = [fc]
sl = ServicesList(self.pc, self.actions, self.sub_actions)
self.assertEqual(len(sl.service_widgets), 1)
def test_machine_checks_constraints(self, mock_servicewidgetclass):
mock_machine = make_fake_machine('fm', {'cpu_count': 0,
'storage': 0,
'memory': 0})
sl = ServicesList(self.pc, self.actions, self.sub_actions,
machine=mock_machine)
self.assertEqual(len(sl.service_widgets), 0)
def test_do_not_show_assigned(self, mock_servicewidgetclass):
mock_machine = make_fake_machine('fm', {'cpu_count': 0,
'storage': 0,
'memory': 0})
self.pc.assign(mock_machine, CharmNovaCompute,
AssignmentType.LXC)
sl = ServicesList(self.pc, self.actions, self.sub_actions,
machine=mock_machine)
classes = [sw.charm_class for sw in sl.service_widgets]
self.assertTrue(CharmNovaCompute not in classes)
def test_show_type(self, mock_servicewidgetclass):
"""Test combinations of show_type values.
This tests three values of show_type with three return values
for is_required(): all required, no required, and 1/3
required. It's all lumped in one test to consolidate setup.
"""
mock_sw1 = MagicMock(name='sw1')
mock_sw1.charm_class.charm_name = 'cc1'
mock_sw2 = MagicMock(name='sw2')
mock_sw2.charm_class.charm_name = 'cc2'
mock_sw3 = MagicMock(name='sw3')
mock_sw3.charm_class.charm_name = 'cc3'
mock_servicewidgetclass.side_effect = [mock_sw1, mock_sw2,
mock_sw3]
with patch.object(self.pc, 'get_charm_state') as mock_get_state:
with patch.object(self.pc, 'charm_classes') as mock_classesfunc:
mock_classesfunc.return_value = [MagicMock(name='fake-class-1',
charm_name='cc1'),
MagicMock(name='fake-class-2',
charm_name='cc2'),
MagicMock(name='fake-class-3',
charm_name='cc3')]
# First, test when all charms are required
mock_get_state.return_value = (CharmState.REQUIRED, [], [])
# rsl shows required charms
rsl = ServicesList(self.pc, self.actions,
self.sub_actions, machine=None,
show_type='required')
self.assertEqual(len(mock_get_state.mock_calls), 3)
# should show all 3
self.assertEqual(len(rsl.service_widgets), 3)
mock_get_state.reset_mock()
mock_servicewidgetclass.reset_mock()
#.........這裏部分代碼省略.........
示例4: ServiceWidgetTestCase
# 需要導入模塊: from cloudinstall.placement.controller import PlacementController [as 別名]
# 或者: from cloudinstall.placement.controller.PlacementController import assign [as 別名]
class ServiceWidgetTestCase(unittest.TestCase):
def setUp(self):
self.mock_maas_state = MagicMock()
with NamedTemporaryFile(mode='w+', encoding='utf-8') as tempf:
utils.spew(tempf.name, yaml.dump(dict()))
self.conf = Config({}, tempf.name)
self.pc = PlacementController(self.mock_maas_state,
self.conf)
self.mock_machine = make_fake_machine('machine1')
self.mock_machine_2 = make_fake_machine('machine2')
self.mock_machines = [self.mock_machine, self.mock_machine_2]
self.mock_maas_state.machines.return_value = self.mock_machines
def test_required_label_shown(self):
"""Widget showing a required charm should have a label showing how
many units are required"""
w = ServiceWidget(CharmKeystone, self.pc)
self.assertTrue(search_in_widget("0 of 1 placed", w))
def test_required_label_not_shown(self):
"""Widget showing a non-required charm should NOT have a label showing
how many units are required.
"""
w = ServiceWidget(CharmJujuGui, self.pc)
self.assertFalse(search_in_widget(".* of .* placed", w))
def test_show_placements(self):
"""Widget with show_placements set should show placements"""
self.pc.assign(self.mock_machine, CharmNovaCompute, AssignmentType.LXC)
w = ServiceWidget(CharmNovaCompute, self.pc, show_placements=True)
self.assertTrue(search_in_widget("LXC.*machine1-hostname", w))
def test_dont_show_placements(self):
"""Widget with show_placements set to FALSE should NOT show
placements"""
self.pc.assign(self.mock_machine, CharmNovaCompute, AssignmentType.LXC)
w = ServiceWidget(CharmNovaCompute, self.pc, show_placements=False)
self.assertFalse(search_in_widget("LXC.*machine1-hostname", w))
def test_show_constraints(self):
"""Widget with show_constraints set should show constraints"""
self.pc.assign(self.mock_machine, CharmNovaCompute, AssignmentType.LXC)
w = ServiceWidget(CharmNovaCompute, self.pc, show_constraints=True)
conpat = ("constraints.*" +
".*".join(CharmNovaCompute.constraints.keys()))
self.assertTrue(search_in_widget(conpat, w))
def test_dont_show_constraints(self):
"""Widget with show_constraints set to FALSE should NOT show
constraints"""
self.pc.assign(self.mock_machine, CharmNovaCompute, AssignmentType.LXC)
w = ServiceWidget(CharmNovaCompute, self.pc, show_constraints=False)
self.assertFalse(search_in_widget("constraints", w))
def test_show_actions(self):
"""Actions should be shown as buttons"""
fake_action_func = MagicMock()
actions = [("fake-action", fake_action_func)]
w = ServiceWidget(CharmNovaCompute, self.pc, actions=actions)
self.assertTrue(search_in_widget("fake-action", w))
def test_actions_use_pred(self):
"""Action predicates control whether a button appears (disabled)"""
# NOTE: this test assumes that disabled buttons are just the
# button label with parentheses.
fake_action_func = MagicMock()
fake_pred = MagicMock()
fake_pred.return_value = False
actions = [(fake_pred, "fake-action", fake_action_func)]
w = ServiceWidget(CharmNovaCompute, self.pc, actions=actions)
self.assertTrue(search_in_widget("\(.*fake-action.*\)", w))
fake_pred.assert_called_with(CharmNovaCompute)
fake_pred.return_value = True
fake_pred.reset_mock()
w.update()
self.assertTrue(search_in_widget("<.*fake-action.*>", w))
fake_pred.assert_called_with(CharmNovaCompute)
示例5: MachineWidgetTestCase
# 需要導入模塊: from cloudinstall.placement.controller import PlacementController [as 別名]
# 或者: from cloudinstall.placement.controller.PlacementController import assign [as 別名]
class MachineWidgetTestCase(unittest.TestCase):
def setUp(self):
self.mock_maas_state = MagicMock()
with NamedTemporaryFile(mode='w+', encoding='utf-8') as tempf:
utils.spew(tempf.name, yaml.dump(dict()))
self.conf = Config({}, tempf.name)
self.pc = PlacementController(self.mock_maas_state,
self.conf)
self.mock_machine = make_fake_machine('machine1')
self.mock_machines = [self.mock_machine]
self.mock_maas_state.machines.return_value = self.mock_machines
def test_hardware_shown(self):
"""show_hardware=True should show hardware details"""
w = MachineWidget(self.mock_machine, self.pc, show_hardware=True)
self.assertTrue(search_in_widget("arch", w))
self.assertTrue(search_in_widget("cores", w))
self.assertTrue(search_in_widget("mem", w))
self.assertTrue(search_in_widget("storage", w))
def test_hardware_not_shown(self):
"""show_hardware=False should NOT show hardware details"""
w = MachineWidget(self.mock_machine, self.pc, show_hardware=False)
self.assertFalse(search_in_widget("arch", w))
self.assertFalse(search_in_widget("cores", w))
self.assertFalse(search_in_widget("mem", w))
self.assertFalse(search_in_widget("storage", w))
def test_show_assignments(self):
"""Widget with show_assignments set should show assignments"""
self.pc.assign(self.mock_machine, CharmNovaCompute, AssignmentType.LXC)
w = MachineWidget(self.mock_machine, self.pc, show_assignments=True)
self.assertTrue(search_in_widget("LXC.*Compute", w))
def test_dont_show_assignments(self):
"""Widget with show_assignments set to FALSE should NOT show
assignments"""
self.pc.assign(self.mock_machine, CharmNovaCompute, AssignmentType.LXC)
w = MachineWidget(self.mock_machine, self.pc, show_assignments=False)
self.assertFalse(search_in_widget("LXC.*Compute", w))
def test_show_actions(self):
"""Actions passed as 2-tuples should always be shown as buttons"""
fake_action_func = MagicMock()
actions = [("fake-action", fake_action_func)]
w = MachineWidget(self.mock_machine, self.pc, actions=actions)
self.assertTrue(search_in_widget("fake-action", w))
def test_actions_use_pred(self):
"""Action predicates control whether a button appears (disabled)"""
# NOTE: this test assumes that disabled buttons are just the
# button label with parentheses.
fake_action_func = MagicMock()
fake_pred = MagicMock()
fake_pred.return_value = False
actions = [(fake_pred, "fake-action", fake_action_func)]
w = MachineWidget(self.mock_machine, self.pc, actions=actions)
self.assertTrue(search_in_widget("\(.*fake-action.*\)", w))
fake_pred.assert_called_with(self.mock_machine)
fake_pred.return_value = True
fake_pred.reset_mock()
w.update()
self.assertTrue(search_in_widget("<.*fake-action.*>", w))
fake_pred.assert_called_with(self.mock_machine)