def test__does_not_match_when_comparing_text_to_binary(self): contents = factory.make_string() # text filename = self.make_file(contents=contents.encode("ascii")) matcher = FileContains(contents=contents) self.assertMismatch( matcher.match(filename), "%r != %r" % ( contents.encode("ascii"), contents))
def test__does_not_match_when_comparing_binary_to_text(self): contents = factory.make_string().encode("ascii") # bytes filename = self.make_file(contents=contents) matcher = FileContains(contents=contents, encoding="ascii") self.assertMismatch( matcher.match(filename), "%r != %r" % ( contents.decode("ascii"), contents))
def test_setUp_creates_config_files(self): with BINDServerResources() as resources: self.assertThat( resources.conf_file, FileContains(matcher=Contains(b'listen-on port %d' % resources.port))) self.assertThat( resources.rndcconf_file, FileContains(matcher=Contains(b'default-port %d' % (resources.rndc_port)))) # This should ideally be in its own test but it's here to cut # test run time. See test_setUp_honours_include_in_options() # as its counterpart. self.assertThat(resources.conf_file, Not(FileContains(matcher=Contains("forwarders"))))
def test_installs_new_bootloader(self): contents = factory.make_bytes() loader = self.make_file(contents=contents) install_dir = self.make_dir() dest = os.path.join(install_dir, factory.make_name("loader")) atomic_copy(loader, dest) self.assertThat(dest, FileContains(contents))
def test_link_bootloader_creates_ipxe_cfg(self): method = IPXEBootMethod() with tempdir() as tmp: method.link_bootloader(tmp) cfg_file_path = os.path.join(tmp, "ipxe.cfg") self.assertTrue(cfg_file_path, FileContains(CONFIG_FILE))
def test_can_write_file_in_development(self): filename = get_maas_data_path("dhcpd.conf") contents = factory.make_bytes() # Binary safe. mode = random.randint(0o000, 0o777) | 0o400 # Always u+r. sudo_write_file(filename, contents, mode) self.assertThat(filename, FileContains(contents)) self.assertThat(os.stat(filename).st_mode & 0o777, Equals(mode))
def test_setUp_creates_config_files(self): with NginxServerResources() as resources: self.assertThat( resources.conf_file, FileContains( matcher=Contains(b'pid %s;' % resources.pid_file.encode('ascii'))))
def test__is_idempotent(self): path = self.set_bootresources_yaml(factory.make_string()) upgrade_cluster.retire_bootresources_yaml() content_after_upgrade = read_text_file(path) upgrade_cluster.retire_bootresources_yaml() self.assertThat(path, FileContains(content_after_upgrade, encoding="utf-8"))
def test__compares_using_matcher_with_encoding(self): contents = factory.make_string() # text filename = self.make_file(contents=contents.encode("ascii")) self.assertThat( filename, FileContains(matcher=Contains(contents[:5]), encoding="ascii"), )
def test_setUp_honours_include_in_options(self): forwarders = "forwarders { 1.2.3.4; };" with BINDServerResources(include_in_options=forwarders) as resources: expected_in_file = (resources.homedir + '/' + forwarders).encode("ascii") self.assertThat(resources.conf_file, FileContains(matcher=Contains(expected_in_file)))
def test__string_representation_explains_binary_match_with_matcher(self): contents_binary = factory.make_bytes() contents_matcher = Contains(contents_binary) self.assertDocTestMatches( "File at path exists and its contents (unencoded; raw) " "match %s" % (contents_matcher, ), FileContains(matcher=contents_matcher))
def test__string_representation_explains_text_match(self): encoding = factory.make_name("encoding") contents_text = factory.make_string() self.assertDocTestMatches( "File at path exists and its contents (encoded as %s) " "match Equals(%r)" % (encoding, contents_text), FileContains(contents=contents_text, encoding=encoding))
def test_writes_keyring_to_file(self): keyring_data = "A keyring! My kingdom for a keyring!" keyring_path = os.path.join(self.make_dir(), "a-keyring-file") keyrings.write_keyring(keyring_path, keyring_data.encode("utf-8")) self.assertTrue(os.path.exists(keyring_path)) self.assertThat(keyring_path, FileContains(keyring_data, encoding="ascii"))
def test__string_representation_explains_binary_match(self): contents_binary = factory.make_bytes() self.assertDocTestMatches( "File at path exists and its contents (unencoded; raw) " "match Equals(%r)" % (contents_binary, ), FileContains(contents=contents_binary), )
def test__prefixes_header_to_file_if_present(self): content = factory.make_string() path = self.set_bootresources_yaml(content) upgrade_cluster.retire_bootresources_yaml() self.assertThat( path, FileContains(upgrade_cluster.BOOTRESOURCES_WARNING + content, encoding="utf-8"))
def test__string_representation_explains_text_match_with_matcher(self): encoding = factory.make_name("encoding") contents_text = factory.make_string() contents_matcher = Contains(contents_text) self.assertDocTestMatches( "File at path exists and its contents (encoded as %s) " "match %s" % (encoding, contents_matcher), FileContains(matcher=contents_matcher, encoding=encoding))
def test__uses_database_secret_when_none_on_fs(self): secret_before = security.get_shared_secret() unlink(security.get_shared_secret_filesystem_path()) secret_after = security.get_shared_secret() self.assertEqual(secret_before, secret_after) # The secret found in the database is written to the filesystem. self.assertThat(security.get_shared_secret_filesystem_path(), FileContains(b2a_hex(secret_after)))
def test_sweeps_aside_dot_new_if_any(self): contents = factory.make_bytes() loader = self.make_file(contents=contents) dest = self.make_file(contents="Old contents") temp_file = "%s.new" % dest factory.make_file(os.path.dirname(temp_file), name=os.path.basename(temp_file)) atomic_copy(loader, dest) self.assertThat(dest, FileContains(contents))
def test_skips_if_unchanged(self): contents = factory.make_bytes() dest = self.make_file(contents=contents) age_file(dest, 100) original_write_time = os.stat(dest).st_mtime loader = self.make_file(contents=contents) atomic_copy(loader, dest) self.assertThat(dest, FileContains(contents)) self.assertEqual(original_write_time, os.stat(dest).st_mtime)
def test_make_tarball_writes_tarball(self): filename = factory.make_name() contents = {filename: factory.make_string().encode("ascii")} tarball = factory.make_tarball(self.make_dir(), contents) dest = self.make_dir() subprocess.check_call(["tar", "-xzf", tarball, "-C", dest]) self.assertThat(os.path.join(dest, filename), FileContains(contents[filename]))
def test__survives_encoding_problems(self): path = os.path.join(self.make_dir(), 'bootresources.yaml') content = b'[[%s]]' % sample_binary_data with open(path, 'wb') as config: config.write(content) self.patch(upgrade_cluster, 'BOOTRESOURCES_FILE', path) upgrade_cluster.retire_bootresources_yaml() self.assertThat( path, FileContains( upgrade_cluster.BOOTRESOURCES_WARNING.encode('ascii') + content))
def test_survives_encoding_problems(self): path = os.path.join(self.make_dir(), "bootresources.yaml") content = b"[[%s]]" % sample_binary_data with open(path, "wb") as config: config.write(content) self.patch(upgrade_cluster, "BOOTRESOURCES_FILE", path) upgrade_cluster.retire_bootresources_yaml() self.assertThat( path, FileContains( upgrade_cluster.BOOTRESOURCES_WARNING.encode("ascii") + content ), )
def test_link_bootloader_creates_grub_cfg(self): method = UEFIAMD64BootMethod() with tempdir() as tmp: stream_path = os.path.join(tmp, 'bootloader', method.bios_boot_method, method.bootloader_arches[0]) os.makedirs(stream_path) for bootloader_file in method.bootloader_files: factory.make_file(stream_path, bootloader_file) method.link_bootloader(tmp) for bootloader_file in method.bootloader_files: bootloader_file_path = os.path.join(tmp, bootloader_file) self.assertTrue(os.path.islink(bootloader_file_path)) grub_file_path = os.path.join(tmp, 'grub', 'grub.cfg') self.assertTrue(grub_file_path, FileContains(CONFIG_FILE))
def test_renames_file_before_deleting(self): # Intercept calls to os.remove. os_remove = self.patch(fs_module.os, "remove") contents = factory.make_name("contents").encode("ascii") filepath = self.make_file(contents=contents) filedir = os.path.dirname(filepath) atomic_delete(filepath) listing = os.listdir(filedir) self.assertThat(listing, HasLength(1)) [deletedname] = listing self.assertThat(deletedname, MatchesRegex(r"^[.][^.]+[.]deleted$")) deletedpath = os.path.join(filedir, deletedname) self.assertThat(os_remove, MockCalledOnceWith(deletedpath)) self.assertThat(deletedpath, FileContains(contents))
def test__compares_in_text_mode_when_encoding_supplied(self): contents = factory.make_string() # text filename = self.make_file(contents=contents.encode("ascii")) self.assertThat(filename, FileContains(contents=contents, encoding="ascii"))
def test__compares_in_binary_mode_when_encoding_not_supplied(self): contents = factory.make_bytes() # bytes filename = self.make_file(contents=contents) self.assertThat(filename, FileContains(contents=contents))
def test__does_not_match_if_file_does_not_exist(self): self.assertMismatch( FileContains("").match("/does/not/exist"), "/does/not/exist does not exist", )
def test_overwrites_file(self): path = self.make_file(contents="original text") text = factory.make_string() write_text_file(path, text) self.assertThat(path, FileContains(text, encoding="ascii"))
def test_creates_file(self): path = os.path.join(self.make_dir(), factory.make_name("text")) text = factory.make_string() write_text_file(path, text) self.assertThat(path, FileContains(text, encoding="ascii"))
def test_replaces_file_if_changed(self): contents = factory.make_bytes() loader = self.make_file(contents=contents) dest = self.make_file(contents="Old contents") atomic_copy(loader, dest) self.assertThat(dest, FileContains(contents))