def test_render_pxe_config_scenarios(self): # The commissioning config uses an extra PXELINUX module to auto # select between i386 and amd64. get_ephemeral_name = self.patch(kernel_opts, "get_ephemeral_name") get_ephemeral_name.return_value = factory.make_name("ephemeral") options = { "kernel_params": make_kernel_parameters(purpose=self.purpose), } output = render_pxe_config(**options) config = parse_pxe_config(output) # The default section is defined. default_section_label = config.header["DEFAULT"] self.assertThat(config, Contains(default_section_label)) default_section = config[default_section_label] # The default section uses the ifcpu64 module, branching to the "i386" # or "amd64" labels accordingly. self.assertEqual("ifcpu64.c32", default_section["KERNEL"]) self.assertEqual( ["amd64", "--", "i386"], default_section["APPEND"].split()) # Both "i386" and "amd64" sections exist. self.assertThat(config, ContainsAll(("i386", "amd64"))) # Each section defines KERNEL, INITRD, and APPEND settings. The # KERNEL and INITRD ones contain paths referring to their # architectures. for section_label in ("i386", "amd64"): section = config[section_label] self.assertThat( section, ContainsAll(("KERNEL", "INITRD", "APPEND"))) contains_arch_path = StartsWith("%s/" % section_label) self.assertThat(section["KERNEL"], contains_arch_path) self.assertThat(section["INITRD"], contains_arch_path) self.assertIn("APPEND", section)
def test_zone_list_displays_zone_details(self): # Zone listing displays the zone name and the zone description. zones = [factory.make_zone() for i in range(3)] response = self.client.get(reverse('zone-list')) zone_names = [zone.name for zone in zones] truncated_zone_descriptions = [zone.description[:20] for zone in zones] self.assertThat(response.content, ContainsAll(zone_names)) self.assertThat(response.content, ContainsAll(truncated_zone_descriptions))
def test_zone_list_contains_edit_links(self): zones = [factory.make_zone() for i in range(3)] response = self.client.get(reverse('zone-list')) zone_edit_links = [ reverse('zone-edit', args=[zone.name]) for zone in zones ] zone_delete_links = [ reverse('zone-del', args=[zone.name]) for zone in zones ] all_links = get_content_links(response) self.assertThat(all_links, ContainsAll(zone_edit_links)) self.assertThat(all_links, ContainsAll(zone_delete_links))
def test_install_compose_kernel_command_line_includes_name_domain(self): params = make_kernel_parameters(purpose="install") self.assertThat( compose_kernel_command_line(params), ContainsAll([ "hostname=%s" % params.hostname, "domain=%s" % params.domain, ]))
def test_render_preseed_uses_default_archives_arm(self): node = factory.make_node(architecture=ARCHITECTURE.armhf_highbank) default_snippets = [ "d-i mirror/http/hostname string ports.ubuntu.com", "d-i mirror/http/directory string /ubuntu-ports", ] preseed = render_preseed(node, PRESEED_TYPE.DEFAULT, "precise") self.assertThat(preseed, ContainsAll(default_snippets))
def test_settings_contains_names_and_content_of_scripts(self): scripts = { factory.make_commissioning_script(), factory.make_commissioning_script(), } response = self.client.get(reverse('settings')) names = [script.name for script in scripts] contents = [script.content for script in scripts] self.assertThat(response.content, ContainsAll(names + contents))
def test_compose_kernel_command_line_inc_common_opts(self): # Test that some kernel arguments appear on commissioning, install # and xinstall command lines. get_ephemeral_name = self.patch(kernel_opts, "get_ephemeral_name") get_ephemeral_name.return_value = "RELEASE-ARCH" expected = ["nomodeset"] params = make_kernel_parameters(purpose="commissioning", arch="i386") cmdline = compose_kernel_command_line(params) self.assertThat(cmdline, ContainsAll(expected)) params = make_kernel_parameters(purpose="xinstall", arch="i386") cmdline = compose_kernel_command_line(params) self.assertThat(cmdline, ContainsAll(expected)) params = make_kernel_parameters(purpose="install", arch="i386") cmdline = compose_kernel_command_line(params) self.assertThat(cmdline, ContainsAll(expected))
def test_GET_lists_users(self): users = [factory.make_user() for counter in range(2)] response = self.client.get(reverse('users_handler')) self.assertEqual(httplib.OK, response.status_code, response.content) listing = json.loads(response.content) self.assertThat([user['username'] for user in listing], ContainsAll([user.username for user in users]))
def test_get_archive_wraps_all_scripts(self): scripts = {factory.make_commissioning_script() for counter in range(3)} archive = open_tarfile(CommissioningScript.objects.get_archive()) self.assertThat( archive.getnames(), ContainsAll({ os.path.join(ARCHIVE_PREFIX, script.name) for script in scripts }))
def test_get_curtin_config(self): node = factory.make_node() node.use_fastpath_installer() config = get_curtin_config(node) self.assertThat(config, ContainsAll([ 'mode: reboot', "debconf_selections:", ]))
def test_version_index_shows_unconditional_entries(self): client = make_node_client() view_name = self.get_metadata_name('-version') url = reverse(view_name, args=['latest']) items = client.get(url).content.splitlines() self.assertThat( items, ContainsAll([ 'meta-data', 'maas-commissioning-scripts', ]))
def test_install_compose_kernel_command_line_includes_log_settings(self): params = make_kernel_parameters(purpose="install") # Port 514 (UDP) is syslog. log_port = "514" self.assertThat( compose_kernel_command_line(params), ContainsAll([ "log_host=%s" % params.log_host, "log_port=%s" % log_port, ]))
def test_view_tag_displays_tag_info(self): # The tag page features the basic information about the tag. tag = factory.make_tag(name='the-named-tag', comment='Human description of the tag', definition='//xpath') tag_link = reverse('tag-view', args=[tag.name]) response = self.client.get(tag_link) doc = fromstring(response.content) content_text = doc.cssselect('#content')[0].text_content() self.assertThat(content_text, ContainsAll([tag.comment, tag.definition]))
def test_settings_contains_links_to_delete_scripts(self): scripts = { factory.make_commissioning_script(), factory.make_commissioning_script(), } links = get_content_links(self.client.get(reverse('settings'))) script_delete_links = [ reverse('commissioning-script-delete', args=[script.id]) for script in scripts ] self.assertThat(links, ContainsAll(script_delete_links))
def test_render_preseed_uses_default_archives_intel(self): nodes = [ factory.make_node(architecture=ARCHITECTURE.i386), factory.make_node(architecture=ARCHITECTURE.amd64), ] default_snippets = [ "d-i mirror/http/hostname string archive.ubuntu.com", "d-i mirror/http/directory string /ubuntu", ] for node in nodes: preseed = render_preseed(node, PRESEED_TYPE.DEFAULT, "precise") self.assertThat(preseed, ContainsAll(default_snippets))
def test_script_executable(self): script = ["%s/bin/maas-provision" % root, "generate-dhcp-config"] script.extend(self.test_args) cmd = Popen(script, stdout=PIPE, env=dict(PYTHONPATH=":".join(sys.path))) output, err = cmd.communicate() contains_all_params = ContainsAll([ 'subnet', 'subnet-mask', 'broadcast-ip', 'omapi-key', 'dns-servers', 'ntp-server', 'domain-name', 'router-ip', 'ip-range-low', 'ip-range-high' ]) self.assertThat(output, contains_all_params)
def test_write_conf_writes_conf_file(self): conf = self.make_file() target = factory.make_name('target') image = factory.make_name('image') write_conf(conf, target, image) self.assertThat( read_text_file(conf), ContainsAll([ 'com.ubuntu:maas:%s' % target, 'backing-store "%s"' % image, '</target>', ]))
def test_compose_kernel_command_line_inc_purpose_opts_comm_node(self): # The result of compose_kernel_command_line includes the purpose # options for a "commissioning" node. ephemeral_name = factory.make_name("ephemeral") params = make_kernel_parameters(purpose="commissioning") self.create_ephemeral_info(ephemeral_name, params.arch, params.release) self.assertThat( compose_kernel_command_line(params), ContainsAll([ "iscsi_target_name=%s:%s" % (ISCSI_TARGET_NAME_PREFIX, ephemeral_name), "iscsi_target_port=3260", "iscsi_target_ip=%s" % params.fs_host, ]))
def test_commissioning_compose_kernel_command_line_inc_purpose_opts(self): # The result of compose_kernel_command_line includes the purpose # options for a non "commissioning" node. get_ephemeral_name = self.patch(kernel_opts, "get_ephemeral_name") get_ephemeral_name.return_value = "RELEASE-ARCH" params = make_kernel_parameters(purpose="commissioning") cmdline = compose_kernel_command_line(params) self.assertThat( cmdline, ContainsAll([ "root=/dev/disk/by-path/ip-", "iscsi_initiator=", "overlayroot=tmpfs", "ip=::::%s:BOOTIF" % params.hostname ]))
def test_view_tag_includes_node_links(self): tag = factory.make_tag() node = factory.make_node() node.tags.add(tag) mac = factory.make_mac_address(node=node).mac_address tag_link = reverse('tag-view', args=[tag.name]) node_link = reverse('node-view', args=[node.system_id]) response = self.client.get(tag_link) doc = fromstring(response.content) content_text = doc.cssselect('#content')[0].text_content() self.assertThat(content_text, ContainsAll([unicode(mac), '%s' % node.hostname])) self.assertNotIn(node.system_id, content_text) self.assertIn(node_link, get_content_links(response))
def test_writes_dns_zone_config_with_NS_record(self): target_dir = self.make_dir() self.patch(DNSForwardZoneConfig, 'target_dir', target_dir) network = factory.getRandomNetwork() dns_ip = factory.getRandomIPAddress() dns_zone_config = DNSForwardZoneConfig( factory.getRandomString(), serial=random.randint(1, 100), dns_ip=dns_ip, networks=[network]) dns_zone_config.write_config() self.assertThat( os.path.join(target_dir, 'zone.%s' % dns_zone_config.domain), FileContains( matcher=ContainsAll( [ 'IN NS %s.' % dns_zone_config.domain, '%s. IN A %s' % (dns_zone_config.domain, dns_ip), ])))
def test_cleanup_old_nonces_cleans_up_old_nonces(self): now = time.time() # Patch the module's time module so that the nonces appear as if # they were created now - timestamp_threshold seconds ago. timemod = self.patch(module_time, "time") timemod.return_value = now - timestamp_threshold old_nonces = [Nonce.objects.create() for i in range(3)] self.assertEquals(0, cleanup_old_nonces()) # Patch the module's time module back. timemod.return_value = now new_nonces = [Nonce.objects.create() for i in range(3)] cleanup_count = cleanup_old_nonces() # The old nonces plus the checkpoint nonce are deleted. self.assertEquals(len(old_nonces) + 1, cleanup_count) self.assertThat(Nonce.objects.all(), ContainsAll(new_nonces)) self.assertEqual(len(new_nonces) + 1, Nonce.objects.all().count())
def test_writes_dns_zone_config(self): target_dir = self.make_dir() self.patch(DNSForwardZoneConfig, 'target_dir', target_dir) domain = factory.getRandomString() hostname = factory.getRandomString() network = factory.getRandomNetwork() ip = factory.getRandomIPInNetwork(network) dns_zone_config = DNSForwardZoneConfig( domain, serial=random.randint(1, 100), mapping={hostname: ip}, networks=[network]) dns_zone_config.write_config() self.assertThat( os.path.join(target_dir, 'zone.%s' % domain), FileContains( matcher=ContainsAll( [ '%s IN CNAME %s' % (hostname, generated_hostname(ip)), '%s IN A %s' % (generated_hostname(ip), ip), ])))
def test_run(self): self.patch(sys, "stdout", BytesIO()) parser = ArgumentParser() writer.add_arguments(parser) args = parser.parse_args(self.test_args) writer.run(args) output = sys.stdout.getvalue() contains_all_params = ContainsAll([ 'subnet', 'subnet-mask', 'broadcast-ip', 'omapi-key', 'dns-servers', 'ntp-server', 'domain-name', 'router-ip', 'ip-range-low', 'ip-range-high', ]) self.assertThat(output, contains_all_params)
def test_run_save_to_file(self): parser = ArgumentParser() writer.add_arguments(parser) outfile = os.path.join(self.make_dir(), "outfile.txt") args = parser.parse_args(self.test_args + ("--outfile", outfile)) writer.run(args) with open(outfile, "rb") as stream: output = stream.read() contains_all_params = ContainsAll([ 'subnet', 'subnet-mask', 'broadcast-ip', 'omapi-key', 'dns-servers', 'ntp-server', 'domain-name', 'router-ip', 'ip-range-low', 'ip-range-high', ]) self.assertThat(output, contains_all_params)
def test_write_config_writes_config(self): target_dir = self.make_dir() self.patch(DNSConfig, 'target_dir', target_dir) domain = factory.getRandomString() network = IPNetwork('192.168.0.3/24') ip = factory.getRandomIPInNetwork(network) forward_zone = DNSForwardZoneConfig( domain, mapping={factory.getRandomString(): ip}, networks=[network]) reverse_zone = DNSReverseZoneConfig( domain, mapping={factory.getRandomString(): ip}, network=network) dnsconfig = DNSConfig((forward_zone, reverse_zone)) dnsconfig.write_config() self.assertThat( os.path.join(target_dir, MAAS_NAMED_CONF_NAME), FileContains( matcher=ContainsAll( [ 'zone.%s' % domain, 'zone.0.168.192.in-addr.arpa', MAAS_NAMED_RNDC_CONF_NAME, ])))
def assertRecordedStdin(self, recorder, *args): # Helper to check that the function recorder "recorder" has all # of the items mentioned in "args" which are extracted from # stdin. We can just check that all the parameters that were # passed are being used. self.assertThat(recorder.extract_args()[0][0], ContainsAll(args))
def test_pxeconfig_returns_all_kernel_parameters(self): self.assertThat(self.get_pxeconfig(), ContainsAll(KernelParameters._fields))
def test_ContainsAll_passes_if_all_elements_are_present(self): items = [factory.getRandomString() for i in range(3)] self.assertThat(items, ContainsAll([items[0], items[2]]))
def test_ContainsAll_raises_if_one_element_is_missing(self): items = [factory.getRandomString() for i in range(3)] self.assertRaises(MismatchError, self.assertThat, items, ContainsAll([items[0], factory.getRandomString()]))