def test_get_url(self): driver = lxd_module.LXDPodDriver() context = {"power_address": factory.make_hostname()} # Test ip adds protocol and port self.assertEqual( join("https://", "%s:%d" % (context["power_address"], 8443)), driver.get_url(context), ) # Test ip:port adds protocol context["power_address"] += ":1234" self.assertEqual( join("https://", "%s" % context["power_address"]), driver.get_url(context), ) # Test protocol:ip adds port context["power_address"] = join("https://", factory.make_hostname()) self.assertEqual( "%s:%d" % (context.get("power_address"), 8443), driver.get_url(context), ) # Test protocol:ip:port doesn't do anything context["power_address"] += ":1234" self.assertEqual(context.get("power_address"), driver.get_url(context))
def test_configure(self): servers = [ factory.make_ipv4_address(), factory.make_ipv6_address(), factory.make_hostname(), ] peers = [ factory.make_ipv4_address(), factory.make_ipv6_address(), factory.make_hostname(), ] offset = randrange(0, 5) config.configure(servers, peers, offset) ntp_conf_path = get_data_path("etc", config._ntp_conf_name) ntp_maas_conf_path = get_data_path("etc", config._ntp_maas_conf_name) ntp_conf = read_configuration(ntp_conf_path) self.assertThat(extract_servers_and_pools(ntp_conf), Equals([])) self.assertThat(extract_included_files(ntp_conf), Equals([ntp_maas_conf_path])) ntp_maas_conf = read_configuration(ntp_maas_conf_path) self.assertThat(extract_servers_and_pools(ntp_maas_conf), Equals(servers)) self.assertThat(extract_peers(ntp_maas_conf), Equals(peers)) self.assertThat(extract_tos_options(ntp_maas_conf), Equals([str(offset + 8), "orphan"]))
def test_make_parsed_url_asserts_with_conflicting_port_numbers(self): with ExpectedException(AssertionError): netloc = "%s:%d" % (factory.make_hostname(), factory.pick_port()) factory.make_parsed_url(netloc=netloc, port=factory.pick_port()) with ExpectedException(AssertionError): netloc = "%s:%d" % (factory.make_hostname(), factory.pick_port()) factory.make_parsed_url(netloc=netloc, port=True)
def test_clears_lock_if_change_power_state_fails(self): class TestException(Exception): pass pcs = self.patch_autospec(power, 'power_change_starting') pcs.return_value = fail(TestException('boom')) system_id = factory.make_name('system_id') hostname = factory.make_hostname() power_driver = random.choice( [driver for _, driver in PowerDriverRegistry if driver.queryable]) power_change = random.choice(['on', 'off', 'cycle']) context = sentinel.context logger = self.useFixture(TwistedLoggerFixture()) yield power.maybe_change_power_state(system_id, hostname, power_driver.name, power_change, context) reactor.runUntilCurrent() # Run all delayed calls. self.assertNotIn(system_id, power.power_action_registry) self.assertDocTestMatches( """\ %s: Power %s failed. Traceback (most recent call last): ... %s.TestException: boom """ % (hostname, power_change, __name__), logger.dump())
def test_clears_lock_if_change_power_state_is_cancelled(self): # Patch in an unfired Deferred here. This will pause the call so that # we can grab the delayed call from the registry in time to cancel it. self.patch_autospec(power, 'change_power_state') power.change_power_state.return_value = Deferred() self.patch_autospec(power, 'power_change_failure') system_id = factory.make_name('system_id') hostname = factory.make_hostname() power_driver = random.choice( [driver for _, driver in PowerDriverRegistry if driver.queryable]) power_change = random.choice(['on', 'off', 'cycle']) context = sentinel.context logger = self.useFixture(TwistedLoggerFixture()) yield power.maybe_change_power_state(system_id, hostname, power_driver.name, power_change, context) # Get the Deferred from the registry and cancel it. _, d = power.power_action_registry[system_id] d.cancel() yield d self.assertNotIn(system_id, power.power_action_registry) self.assertDocTestMatches( """\ %s: Power could not be set to %s; timed out. """ % (hostname, power_change), logger.dump()) self.assertThat( power.power_change_failure, MockCalledOnceWith(system_id, hostname, power_change, "Timed out"))
def test_probe_and_enlist(self): node_id = make_node_id() node_list = NODE_LIST % node_id node_info = NODE_INFO % (node_id, self.product_name) node_macaddr = NODE_MACADDR % (node_id, factory.make_mac_address(), factory.make_mac_address()) macs = re.findall(r':'.join(['[0-9a-f]{2}'] * 6), node_macaddr) user = factory.make_name('user') host = factory.make_hostname('mscm') username = factory.make_name('user') password = factory.make_name('password') domain = factory.make_name('domain') system_id = factory.make_name('system_id') mscm_driver = self.patch(mscm_module, "MSCMPowerDriver").return_value mscm_driver.run_mscm_command.side_effect = (node_list, None, node_info, node_macaddr) create_node = self.patch(mscm_module, 'create_node') create_node.side_effect = asynchronous(lambda *args: system_id) commission_node = self.patch(mscm_module, 'commission_node') params = { 'power_address': host, 'power_user': username, 'power_pass': password, 'node_id': node_id, } yield deferToThread(probe_and_enlist_mscm, user, host, username, password, True, domain) self.expectThat( create_node, MockCalledOnceWith(macs, self.arch, 'mscm', params, domain)) self.expectThat(commission_node, MockCalledOnceWith(system_id, user))
def test_geturl_sends_request(self): self.assertEquals( "mock response", maas_api_helper.geturl( "http://%s" % factory.make_hostname(), {} ).read(), )
def test_probe_and_enlist(self): num_servers = 100 self.configure_vmomi_api(servers=num_servers) mock_create_node = self.patch(vmware, "create_node") system_id = factory.make_name("system_id") mock_create_node.side_effect = asynchronous( lambda *args, **kwargs: system_id ) mock_commission_node = self.patch(vmware, "commission_node") host = factory.make_hostname() username = factory.make_username() password = factory.make_username() yield deferToThread( vmware.probe_vmware_and_enlist, factory.make_username(), host, username, password, accept_all=True, ) self.assertEqual(mock_create_node.call_count, num_servers) self.assertEqual(mock_commission_node.call_count, num_servers)
def test_calls_create_node_rpc(self): protocol, connecting = self.prepare_region_rpc() self.addCleanup((yield connecting)) protocol.CreateNode.return_value = defer.succeed( {"system_id": factory.make_name("system-id")}) uuid = 'node-' + factory.make_UUID() macs = sorted(factory.make_mac_address() for _ in range(3)) arch = factory.make_name('architecture') hostname = factory.make_hostname() domain = factory.make_name('domain') power_type = factory.make_name('power_type') power_parameters = { 'power_address': factory.make_ipv4_address(), 'power_user': factory.make_name('power_user'), 'power_pass': factory.make_name('power_pass'), 'power_control': None, 'system_id': uuid } yield create_node( macs, arch, power_type, power_parameters, domain=domain, hostname=hostname) self.assertThat( protocol.CreateNode, MockCalledOnceWith( protocol, architecture=arch, power_type=power_type, power_parameters=json.dumps(power_parameters), mac_addresses=macs, domain=domain, hostname=hostname))
def test_probe_and_enlist_skips_pxe_config_if_create_node_failed(self): num_servers = 1 self.configure_vmomi_api(servers=num_servers) mock_create_node = self.patch(vmware, "create_node") mock_create_node.side_effect = asynchronous( lambda *args, **kwargs: None) mock_reconfigure_vm = self.patch(FakeVmomiVM, "ReconfigVM_Task") # We need to not actually try to commission any nodes... self.patch(vmware, "commission_node") host = factory.make_hostname() username = factory.make_username() password = factory.make_username() yield deferToThread( vmware.probe_vmware_and_enlist, factory.make_username(), host, username, password, accept_all=True, ) self.assertEqual(mock_reconfigure_vm.call_count, 0)
def test_clears_lock_if_change_power_state_fails(self): class TestException(Exception): pass pcs = self.patch_autospec(power, "power_change_starting") pcs.return_value = fail(TestException("boom")) system_id = factory.make_name("system_id") hostname = factory.make_hostname() power_driver = random.choice( [driver for _, driver in PowerDriverRegistry if driver.queryable] ) power_change = random.choice(["on", "off", "cycle"]) context = sentinel.context logger = self.useFixture(TwistedLoggerFixture()) yield power.maybe_change_power_state( system_id, hostname, power_driver.name, power_change, context ) self.assertNotIn(system_id, power.power_action_registry) self.assertDocTestMatches( """\ %s: Power %s failed. Traceback (most recent call last): ... %s.TestException: boom """ % (hostname, power_change, __name__), logger.dump(), )
def test_query_nodes_calls_query_all_nodes(self): service = self.make_monitor_service() service.max_nodes_at_once = sentinel.max_nodes_at_once example_power_parameters = { "system_id": factory.make_UUID(), "hostname": factory.make_hostname(), "power_state": factory.make_name("power_state"), "power_type": factory.make_name("power_type"), "context": {}, } rpc_fixture = self.useFixture(MockClusterToRegionRPCFixture()) proto_region, io = rpc_fixture.makeEventLoop( region.ListNodePowerParameters) proto_region.ListNodePowerParameters.side_effect = [ succeed({"nodes": [example_power_parameters]}), succeed({"nodes": []}), ] query_all_nodes = self.patch(npms, "query_all_nodes") d = service.query_nodes(getRegionClient()) io.flush() self.assertEqual(None, extract_result(d)) self.assertThat( query_all_nodes, MockCalledOnceWith( [example_power_parameters], max_concurrency=sentinel.max_nodes_at_once, clock=service.clock, ), )
def test_geturl_raises_exception_on_failure(self): sleep = self.patch(maas_api_helper.time, 'sleep') warn = self.patch(maas_api_helper, 'warn') self.assertRaises(urllib.error.HTTPError, maas_api_helper.geturl, "http://%s-broken" % factory.make_hostname(), {}) self.assertEquals(7, sleep.call_count) self.assertThat(warn, MockAnyCall('date field not in 400 headers'))
def test_get_vmware_servers(self): self.configure_vmomi_api(servers=10) servers = vmware.get_vmware_servers( factory.make_hostname(), factory.make_username(), factory.make_username(), ) self.expectThat(servers, Not(Equals({})))
def test_get_vmware_servers_empty(self): self.configure_vmomi_api(servers=0) servers = vmware.get_vmware_servers(factory.make_hostname(), factory.make_username(), factory.make_username(), port=8443, protocol='https') self.expectThat(servers, Equals({}))
def test_power_query_missing_server_raises_VMwareVMNotFound(self): self.configure_vmomi_api(servers=1, has_instance_uuid=True, has_uuid=True) host = factory.make_hostname() username = factory.make_username() password = factory.make_username() with ExpectedException(VMwareVMNotFound): vmware.power_query_vmware(host, username, password, None, None)
def make_sources(): hosts = [factory.make_hostname().lower() for _ in range(2)] hosts.append(factory.make_ipv4_address()) hosts.append("[%s]" % factory.make_ipv6_address()) urls = [ "http://%s:%s/images-stream/streams/v1/index.json" % (host, randint(1, 1000)) for host in hosts ] sources = [{"url": url, "selections": []} for url in urls] return sources, hosts
def test_get_server_by_name(self): mock_vmomi_api = self.configure_vmomi_api(servers=1, has_instance_uuid=False, has_uuid=True) host = factory.make_hostname() username = factory.make_username() password = factory.make_username() servers = vmware.get_vmware_servers(host, username, password) for vm_name in servers.keys(): vm = vmware._find_vm_by_uuid_or_name(mock_vmomi_api, None, vm_name) self.assertIsNotNone(vm)
def test_get_domain(self): domain = factory.make_hostname() url = "%s://%s:%d/%s/%s/%s" % ( factory.make_name("proto"), domain, randint(1, 65535), factory.make_name(), factory.make_name(), factory.make_name(), ) self.assertEqual(domain, get_domain(url))
def test_get_domain_fqdn(self): domain = factory.make_hostname() url = "%s://%s.example.com:%d/%s/%s/%s" % ( factory.make_name("proto"), domain, randint(1, 65535), factory.make_name(), factory.make_name(), factory.make_name(), ) self.assertEquals("%s.example.com" % domain, get_domain(url))
def make_servers_and_peers(self): return ( frozenset({ factory.make_ipv4_address(), factory.make_ipv6_address(), factory.make_hostname(), }), frozenset( {factory.make_ipv4_address(), factory.make_ipv6_address()}), )
def test_geturl_increments_skew(self): sleep = self.patch(maas_api_helper.time, 'sleep') warn = self.patch(maas_api_helper, 'warn') self.assertRaises( urllib.error.HTTPError, maas_api_helper.geturl, "http://%s-broken_with_date" % factory.make_hostname(), {}) self.assertEquals(7, sleep.call_count) clock_shew_updates = [ call[0][0].startswith("updated clock shew to") for call in warn.call_args_list ] self.assertEquals(14, len(clock_shew_updates))
def test_renders_the_given_peers(self): peers = [ factory.make_ipv4_address(), factory.make_ipv6_address(), factory.make_hostname(), ] ntp_maas_conf = config._render_ntp_maas_conf([], peers, 0) self.assertThat(ntp_maas_conf, StartsWith('# MAAS NTP configuration.\n')) observed_peers = extract_peers_full(ntp_maas_conf) self.assertThat(observed_peers, Equals([("peer", peer, "") for peer in peers]))
def test_api_connection(self): mock_vmomi_api = self.configure_vmomi_api(servers=0) api = VMwarePyvmomiAPI(factory.make_hostname(), factory.make_username(), factory.make_username()) api.connect() self.expectThat(api.service_instance, IsInstance(FakeVmomiServiceInstance)) self.expectThat(api.is_connected(), Equals(True)) api.disconnect() self.expectThat(mock_vmomi_api.SmartConnect.called, Equals(True)) self.expectThat(mock_vmomi_api.Disconnect.called, Equals(True))
def test_geturl_posts_data(self): mock_urlopen = self.patch(maas_api_helper.urllib.request.urlopen) post_data = {factory.make_name("key"): factory.make_name("value")} maas_api_helper.geturl( "http://%s" % factory.make_hostname(), post_data=post_data ) self.assertThat( mock_urlopen, MockCalledOnceWith( ANY, data=urllib.parse.urlencode(post_data).encode("ascii") ), )
def test_power_control(self): mock_vmomi_api = self.configure_vmomi_api(servers=100) host = factory.make_hostname() username = factory.make_username() password = factory.make_username() servers = vmware.get_vmware_servers(host, username, password) # here we're grabbing indexes only available in the private mock object search_index = ( mock_vmomi_api.SmartConnect.return_value.content.searchIndex ) bios_uuids = list(search_index.vms_by_uuid) instance_uuids = list(search_index.vms_by_instance_uuid) # at least one should have a randomly-invalid state (just checking # for coverage, but since it's random, don't want to assert) vm_name = None for uuid in bios_uuids: vmware.power_query_vmware(host, username, password, vm_name, uuid) for uuid in instance_uuids: vmware.power_query_vmware(host, username, password, vm_name, uuid) for vm_name in servers: vmware.power_query_vmware(host, username, password, vm_name, None) # turn on a set of VMs, then verify they are on for uuid in bios_uuids: vmware.power_control_vmware( host, username, password, vm_name, uuid, "on" ) for uuid in bios_uuids: state = vmware.power_query_vmware( host, username, password, vm_name, uuid ) self.expectThat(state, Equals("on")) # turn off a set of VMs, then verify they are off for uuid in instance_uuids: vmware.power_control_vmware( host, username, password, vm_name, uuid, "off" ) for uuid in instance_uuids: state = vmware.power_query_vmware( host, username, password, vm_name, uuid ) self.expectThat(state, Equals("off")) self.expectThat(servers, Not(Equals({})))
def test_api_failed_connection(self): mock_vmomi_api = self.patch(vmware, 'vmomi_api') mock_vmomi_api.SmartConnect.return_value = None api = VMwarePyvmomiAPI(factory.make_hostname(), factory.make_username(), factory.make_username()) with ExpectedException(vmware.VMwareAPIConnectionFailed): api.connect() self.expectThat(api.service_instance, Is(None)) self.expectThat(api.is_connected(), Equals(False)) api.disconnect() self.expectThat(mock_vmomi_api.SmartConnect.called, Equals(True)) self.expectThat(mock_vmomi_api.Disconnect.called, Equals(True))
def test_probe_and_enlist_mscm_crashes_for_no_match(self): node_id = make_node_id() node_list = NODE_LIST % node_id user = factory.make_name("user") host = factory.make_hostname("mscm") username = factory.make_name("user") password = factory.make_name("password") Driver = self.patch(mscm_module, "MSCMPowerDriver") mscm_driver = Driver.return_value mscm_driver.run_mscm_command.side_effect = (node_list, None, "Error") with ExpectedException(PowerFatalError): yield deferToThread(probe_and_enlist_mscm, user, host, username, password)
def __init__( self, name=None, nics=None, has_instance_uuid=None, has_uuid=None ): if name is None: self._name = factory.make_hostname() else: self._name = name self.summary = FakeVmomiVMSummary( self._name, has_instance_uuid=has_instance_uuid, has_uuid=has_uuid ) self.runtime = FakeVmomiVMRuntime() self.config = FakeVmomiVMConfig(nics=nics)
def test_get_sys_info_empty(self): hostname = factory.make_hostname() architecture = factory.make_name('architecture') self.patch(refresh.socket, 'gethostname').return_value = hostname self.patch_autospec(refresh, 'get_os_release').return_value = {} self.patch_autospec( refresh, 'get_architecture').return_value = architecture self.assertThat({ 'hostname': hostname, 'architecture': architecture, 'osystem': '', 'distro_series': '', 'interfaces': {}, }, Equals(refresh.get_sys_info()))