26
28
from maasserver.testing.factory import factory
27
29
from maasserver.testing.testcase import TestCase
28
30
from maasserver.utils import map_enum
31
from maasserver.utils.orm import get_one
29
32
from maasserver.worker_user import get_worker_user
30
33
from maastesting.celery import CeleryFixture
31
34
from maastesting.fakemethod import FakeMethod
61
65
class TestNodeGroupManager(TestCase):
63
def test_new_creates_nodegroup(self):
64
name = factory.make_name('nodegroup')
65
uuid = factory.getRandomUUID()
66
ip = factory.getRandomIPAddress()
68
NodeGroup.objects.new(name, uuid, ip),
69
MatchesStructure.fromExample(
70
{'name': name, 'uuid': uuid, 'worker_ip': ip}))
72
def test_new_does_not_require_dhcp_settings(self):
67
def test_new_creates_nodegroup_with_interface(self):
73
68
name = factory.make_name('nodegroup')
74
69
uuid = factory.getRandomUUID()
75
70
ip = factory.getRandomIPAddress()
76
71
nodegroup = NodeGroup.objects.new(name, uuid, ip)
77
dhcp_network, dhcp_settings = make_dhcp_settings()
79
nodegroup, MatchesStructure.fromExample(
80
dict.fromkeys(dhcp_settings)))
72
interface = get_one(nodegroup.nodegroupinterface_set.all())
75
(nodegroup.name, nodegroup.uuid, interface.ip))
82
77
def test_new_requires_all_dhcp_settings_or_none(self):
83
78
name = factory.make_name('nodegroup')
94
89
ip = factory.getRandomIPInNetwork(dhcp_network)
95
90
nodegroup = NodeGroup.objects.new(name, uuid, ip, **dhcp_settings)
96
91
nodegroup = reload_object(nodegroup)
92
interface = get_one(nodegroup.nodegroupinterface_set.all())
97
93
self.assertEqual(name, nodegroup.name)
99
nodegroup, MatchesStructure.fromExample(dhcp_settings))
95
interface, MatchesStructure.byEquality(**dhcp_settings))
101
97
def test_new_assigns_token_and_key_for_worker_user(self):
102
98
nodegroup = NodeGroup.objects.new(
122
118
self.assertEqual(key, nodegroup.dhcp_key)
124
def test_ensure_master_creates_minimal_master_nodegroup(self):
120
def test_ensure_master_creates_minimal_interface(self):
121
master = NodeGroup.objects.ensure_master()
122
interface = get_one(master.nodegroupinterface_set.all())
126
NodeGroup.objects.ensure_master(),
127
MatchesStructure.fromExample({
129
'worker_id': 'master',
130
'worker_ip': '127.0.0.1',
132
'broadcast_ip': None,
134
'ip_range_low': None,
135
'ip_range_high': None,
125
MatchesStructure.byEquality(
138
134
def test_ensure_master_writes_master_nodegroup_to_database(self):
139
135
master = NodeGroup.objects.ensure_master()
251
247
(unaffected_status, 0),
252
248
(reload_object(nodegroup).status, changed_count))
250
def test_import_boot_images_accepted_clusters_calls_tasks(self):
251
recorder = self.patch(nodegroup_module, 'import_boot_images')
252
proxy = factory.make_name('proxy')
253
Config.objects.set_config('http_proxy', proxy)
254
accepted_nodegroups = [
255
factory.make_node_group(status=NODEGROUP_STATUS.ACCEPTED),
256
factory.make_node_group(status=NODEGROUP_STATUS.ACCEPTED),
258
factory.make_node_group(status=NODEGROUP_STATUS.REJECTED)
259
factory.make_node_group(status=NODEGROUP_STATUS.PENDING)
260
NodeGroup.objects.import_boot_images_accepted_clusters()
263
for nodegroup in accepted_nodegroups]
266
for args, kwargs in recorder.apply_async.call_args_list]
267
self.assertItemsEqual(expected_queues, actual_queues)
270
def make_archive_url(name):
271
"""Create a fake archive URL."""
272
return "http://%s.example.com/%s/" % (
273
factory.make_name(name),
274
factory.make_name('path'),
254
277
def test_refresh_workers_refreshes_accepted_cluster_controllers(self):
255
278
self.patch(nodegroup_module, 'refresh_worker')
256
279
nodegroup = factory.make_node_group(status=NODEGROUP_STATUS.ACCEPTED)
363
386
nodegroup1.ensure_dhcp_key()
364
387
nodegroup2.ensure_dhcp_key()
365
388
self.assertNotEqual(nodegroup1.dhcp_key, nodegroup2.dhcp_key)
390
def test_import_boot_images_calls_script_with_proxy(self):
391
recorder = self.patch(tasks, 'check_call', Mock())
392
proxy = factory.make_name('proxy')
393
Config.objects.set_config('http_proxy', proxy)
394
nodegroup = factory.make_node_group()
395
nodegroup.import_boot_images()
396
args, kwargs = recorder.call_args
400
(env.get('http_proxy'), env.get('https_proxy')))
402
def test_import_boot_images_selects_archive_locations_from_config(self):
403
recorder = self.patch(nodegroup_module, 'import_boot_images')
404
nodegroup = factory.make_node_group(status=NODEGROUP_STATUS.ACCEPTED)
407
'main_archive': make_archive_url('main'),
408
'ports_archive': make_archive_url('ports'),
409
'cloud_images_archive': make_archive_url('cloud_images'),
411
for key, value in archives.items():
412
Config.objects.set_config(key, value)
414
nodegroup.import_boot_images()
416
kwargs = recorder.apply_async.call_args[1]['kwargs']
417
archive_options = {arg: kwargs.get(arg) for arg in archives}
418
self.assertEqual(archives, archive_options)
420
def test_import_boot_images_sent_to_nodegroup_queue(self):
421
recorder = self.patch(nodegroup_module, 'import_boot_images', Mock())
422
nodegroup = factory.make_node_group()
423
proxy = factory.make_name('proxy')
424
Config.objects.set_config('http_proxy', proxy)
425
nodegroup.import_boot_images()
426
args, kwargs = recorder.apply_async.call_args
427
self.assertEqual(nodegroup.uuid, kwargs['queue'])