def test_does_not_contain(self): tempdir = self.mkdtemp() filename = os.path.join(tempdir, 'foo') self.create_file(filename, 'Goodbye Cruel World!') mismatch = FileContains('Hello World!').match(filename) self.assertThat( Equals('Hello World!').match('Goodbye Cruel World!').describe(), Equals(mismatch.describe()))
def test_exe_is_in_path(self, run_mock): app_path = os.path.join(self.prime_dir, 'bin', 'app1') _create_file(app_path) relative_wrapper_path = self.packager._wrap_exe('app1') wrapper_path = os.path.join(self.prime_dir, relative_wrapper_path) expected = dedent("""\ #!/bin/sh # Workaround for LP: #1656340 [ -n "$XDG_RUNTIME_DIR" ] && mkdir -p $XDG_RUNTIME_DIR -m 700 exec "app1" "$@" """) self.assertThat(wrapper_path, FileContains(expected))
def test_configure_admin_custom_password(self): """ If a password is provided, it's used to configure the admin user. """ self.apt._set_jenkins_version('2.120.1') config = hookenv.config() orig_password = config["password"] try: config["password"] = "******" script = UPDATE_PASSWORD_SCRIPT.format(username="******", password="******") self.fakes.jenkins.scripts[script] = "" self.users.configure_admin() self.assertThat(paths.ADMIN_PASSWORD, FileContains("x")) self.assertThat(paths.ADMIN_PASSWORD, HasOwnership(0, 0)) self.assertThat(paths.ADMIN_PASSWORD, HasPermissions("0600")) self.assertThat(paths.LAST_EXEC, FileContains("2.0.0\n")) self.assertThat(paths.LAST_EXEC, HasOwnership(123, 456)) finally: config["password"] = orig_password
def test_command_relative_command_found_in_slash(self): cmd = command.Command( app_name="foo", command_name="command", command="sh", prime_dir=self.path, can_use_wrapper=True, ) app_command = cmd.get_command() wrapper_path = cmd.generate_wrapper() self.expectThat(app_command, Equals("command-foo.wrapper")) self.assertThat(wrapper_path, FileExists()) self.assertThat(wrapper_path, FileContains('#!/bin/sh\nexec /bin/sh "$@"\n'))
def test_signRepository_honours_pubconf(self): pubconf = getPubConfig(self.archive) pubconf.distsroot = self.makeTemporaryDirectory() suite_dir = os.path.join(pubconf.distsroot, self.suite) release_path = os.path.join(suite_dir, "Release") write_file(release_path, "Release contents") signer = ISignableArchive(self.archive) self.assertTrue(signer.can_sign) self.assertRaises(AssertionError, signer.signRepository, self.suite) self.assertContentEqual(["Release.gpg", "InRelease"], signer.signRepository(self.suite, pubconf=pubconf)) self.assertThat( os.path.join(suite_dir, "Release.gpg"), FileContains("detached signature of %s (%s, %s/%s)\n" % (release_path, self.archive_root, self.distro.name, self.suite))) self.assertThat( os.path.join(suite_dir, "InRelease"), FileContains("clear signature of %s (%s, %s/%s)\n" % (release_path, self.archive_root, self.distro.name, self.suite)))
def test_file_modified(self): source = "source" destination = "destination" os.mkdir(source) os.mkdir(destination) with open(os.path.join(source, "file"), "w") as f: f.write("1") # Now make a reference file with a timestamp later than the file was # created. We'll ensure this by setting it ourselves shutil.copy2(os.path.join(source, "file"), "reference") access_time = os.stat("reference").st_atime modify_time = os.stat("reference").st_mtime os.utime("reference", (access_time, modify_time + 1)) local = sources.Local(source, destination) local.pull() self.assertFalse( local.check("reference"), "Expected no updates to be available" ) self.assertThat(os.path.join(destination, "file"), FileContains("1")) # Now update the file in source, and make sure it has a timestamp # later than our reference (this whole test happens too fast) with open(os.path.join(source, "file"), "w") as f: f.write("2") access_time = os.stat("reference").st_atime modify_time = os.stat("reference").st_mtime os.utime(os.path.join(source, "file"), (access_time, modify_time + 1)) self.assertTrue(local.check("reference"), "Expected update to be available") local.update() self.assertThat(os.path.join(destination, "file"), FileContains("2"))
def test_save_encoded_to_file(self): conf = config.Config() conf.set('bar', 'baz') with open('test-config', 'w') as f: conf.save(config_fd=f, encode=True) f.flush() self.assertThat( 'test-config', FileContains('W2xvZ2luLnVidW50dS5jb21dCmJhciA9IGJhegoK')) new_conf = config.Config() with open('test-config', 'r') as f: new_conf.load(config_fd=f) self.assertThat(new_conf.get('bar'), Equals('baz'))
def test_building_multiple_main_packages_without_go_packages(self): self.copy_project_to_cwd('go-with-multiple-main-packages') snapcraft_yaml_file = 'snapcraft.yaml' with open(snapcraft_yaml_file) as f: snapcraft_yaml = yaml.load(f) del snapcraft_yaml['parts']['multiple-mains']['go-packages'] with open(snapcraft_yaml_file, 'w') as f: yaml.dump(snapcraft_yaml, f) self.assertThat(snapcraft_yaml_file, Not(FileContains('go-packages'))) self.run_snapcraft('stage') for bin in ['main1', 'main2', 'main3']: self.assertThat(os.path.join('stage', 'bin', bin), FileExists())
def test_copies_boot_image_files_from_tarball(self): prefix = factory.make_name() kernel_content = factory.getRandomString() initrd_content = factory.getRandomString() img_content = factory.getRandomString() tarball = factory.make_tarball( self.make_dir(), { '%s-vmlinuz.gz' % prefix: kernel_content, '%s-initrd.gz' % prefix: initrd_content, '%s.img' % prefix: img_content, }) target_dir = self.make_dir() self.patch(ephemerals_script, 'call_uec2roottar') extract_image_tarball(tarball, target_dir) self.assertItemsEqual(['linux', 'initrd.gz', 'disk.img'], listdir(target_dir)) self.assertThat(os.path.join(target_dir, 'linux'), FileContains(kernel_content)) self.assertThat(os.path.join(target_dir, 'initrd.gz'), FileContains(initrd_content)) self.assertThat(os.path.join(target_dir, 'disk.img'), FileContains(img_content))
def test_save_encoded_to_file(self): conf = config.Config() conf.set("bar", "baz") with open("test-config", "w") as f: conf.save(config_fd=f, encode=True) f.flush() self.assertThat( "test-config", FileContains("W2xvZ2luLnVidW50dS5jb21dCmJhciA9IGJhegoK") ) new_conf = config.Config() with open("test-config", "r") as f: new_conf.load(config_fd=f) self.assertThat(new_conf.get("bar"), Equals("baz"))
def test_prime_with_installed_snaps(self): self.useFixture( fixtures.EnvironmentVariable('SNAPCRAFT_BUILD_INFO', '1')) self.fake_snapd.snaps_result = [ { 'name': 'test-snap-1', 'revision': 'test-snap-1-revision' }, { 'name': 'test-snap-2', 'revision': 'test-snap-2-revision' }, ] self.make_snapcraft_yaml( textwrap.dedent("""\ parts: test-part: plugin: nil """)) lifecycle.execute('prime', self.project_options) expected = textwrap.dedent("""\ name: test version: 0 summary: test description: test confinement: strict grade: stable parts: test-part: build-packages: [] installed-packages: [] installed-snaps: {} plugin: nil prime: [] stage: [] stage-packages: [] uname: Linux test uname 4.10 x86_64 architectures: [{}] build-packages: [] build-snaps: [] """.format( '[test-snap-1=test-snap-1-revision, ' 'test-snap-2=test-snap-2-revision]', self.project_options.deb_arch)) self.assertThat(os.path.join('prime', 'snap', 'manifest.yaml'), FileContains(expected))
def test_snapcraft_installed_on_host_from_store_but_injection_disabled( self): self.useFixture(fixture_setup.FakeStore()) snap_injector = SnapInjector( snap_dir=self.provider._SNAPS_MOUNTPOINT, registry_filepath=self.registry_filepath, snap_arch="amd64", runner=self.provider._run, snap_dir_mounter=self.provider._mount_snaps_directory, snap_dir_unmounter=self.provider._unmount_snaps_directory, file_pusher=self.provider._push_file, inject_from_host=False, ) snap_injector.add("core") snap_injector.add("snapcraft") snap_injector.apply() self.get_assertion_mock.assert_not_called() self.provider.run_mock.assert_has_calls([ call(["sudo", "snap", "set", "core", ANY]), call(["sudo", "snap", "watch", "--last=auto-refresh"]), call([ "sudo", "snap", "install", "--channel", "latest/stable", "core" ]), call([ "sudo", "snap", "install", "--classic", "--channel", "latest/stable", "snapcraft", ]), ]) self.provider.mount_mock.assert_not_called() self.provider.unmount_mock.assert_not_called() self.provider.push_file_mock.assert_not_called() self.assertThat( self.registry_filepath, FileContains( dedent("""\ core: - revision: '10000' snapcraft: - revision: '25' """)), )
def test_send_and_set_to_always(self): self.prompt_mock.return_value = self.answer self.mock_isatty.return_value = self.tty try: self.call_handler(RuntimeError("not a SnapcraftError"), True) except Exception: self.fail("Exception unexpectedly raised") self.raven_client_mock.assert_called_once_with( mock.ANY, transport=self.raven_request_mock, name="snapcraft", processors=mock.ANY, release=mock.ANY, auto_log_stacks=False, ) config_path = os.path.join( xdg.BaseDirectory.save_config_path("snapcraft"), "cli.cfg" ) self.assertThat( config_path, FileContains( dedent( """\ [Sentry] always_send = true """ ) ), ) # It we have a tty, then the trace should be saved to a file and sent to sentry. # If we don't have a tty, then the same should happen, but the trace should # also be printed. self.error_mock.assert_not_called self.exit_mock.assert_called_once_with(1) expected_calls = [ mock.call(RuntimeError, mock.ANY, mock.ANY, file=_Tracefile(self)) ] if not self.tty: expected_calls.append( mock.call(RuntimeError, mock.ANY, mock.ANY, file=sys.stdout) ) self.print_exception_mock.assert_has_calls(expected_calls, any_order=True)
def test_prime_step_records_prime_keyword(self): self.useFixture(fixtures.EnvironmentVariable("SNAPCRAFT_BUILD_INFO", "1")) parts = textwrap.dedent( """\ parts: test-part: plugin: nil {}: [-*] """ ) project_config = self.make_snapcraft_project(parts.format(self.keyword)) lifecycle.execute(steps.PRIME, project_config) expected = textwrap.dedent( """\ snapcraft-version: '3.0' snapcraft-os-release-id: ubuntu snapcraft-os-release-version-id: '16.04' name: test version: 0 summary: test description: test confinement: strict grade: stable parts: test-part: build-packages: [] installed-packages: - patchelf=0.9 installed-snaps: [] plugin: nil prime: - -* stage: [] stage-packages: [] uname: Linux test uname 4.10 x86_64 architectures: - {} build-packages: [] build-snaps: [] """.format( project_config.project.deb_arch ) ) self.assertThat( os.path.join(steps.PRIME.name, "snap", "manifest.yaml"), FileContains(expected), )
def test_prime_with_build_package_with_any_architecture(self, _): self.useFixture( fixtures.EnvironmentVariable("SNAPCRAFT_BUILD_INFO", "1")) self.fake_apt_cache.add_package( fixture_setup.FakeAptCachePackage("test-package", "test-version")) project_config = self.make_snapcraft_project( textwrap.dedent("""\ parts: test-part: plugin: nil build-packages: ['test-package:any'] """)) lifecycle.execute(steps.PRIME, project_config) expected = textwrap.dedent("""\ snapcraft-version: '3.0' snapcraft-os-release-id: ubuntu snapcraft-os-release-version-id: '16.04' name: test version: 0 summary: test description: test confinement: strict grade: stable parts: test-part: build-packages: - test-package:any installed-packages: - patchelf=0.9 installed-snaps: [] plugin: nil prime: [] stage: [] stage-packages: [] uname: Linux test uname 4.10 x86_64 architectures: - {} build-packages: - test-package=test-version build-snaps: [] """.format(project_config.project.deb_arch)) self.assertThat( os.path.join(steps.PRIME.name, "snap", "manifest.yaml"), FileContains(expected), )
def test_handles_slash_32_dynamic_range(self): target_dir = patch_dns_config_path(self) domain = factory.make_string() network = factory.make_ipv4_network() ipv4_hostname = factory.make_name("host") ipv4_ip = factory.pick_ip_in_network(network) range_ip = factory.pick_ip_in_network(network, but_not={ipv4_ip}) ipv6_hostname = factory.make_name("host") ipv6_ip = factory.make_ipv6_address() ttl = random.randint(10, 300) mapping = { ipv4_hostname: HostnameIPMapping(None, ttl, {ipv4_ip}), ipv6_hostname: HostnameIPMapping(None, ttl, {ipv6_ip}), } dynamic_range = IPRange(IPAddress(range_ip), IPAddress(range_ip)) expected_generate_directives = ( DNSForwardZoneConfig.get_GENERATE_directives(dynamic_range) ) other_mapping = { ipv4_hostname: HostnameRRsetMapping(None, {(ttl, "MX", "10 bar")}) } dns_zone_config = DNSForwardZoneConfig( domain, serial=random.randint(1, 100), other_mapping=other_mapping, default_ttl=ttl, mapping=mapping, dynamic_ranges=[dynamic_range], ) dns_zone_config.write_config() self.assertThat( os.path.join(target_dir, "zone.%s" % domain), FileContains( matcher=ContainsAll( [ "$TTL %d" % ttl, "%s %d IN A %s" % (ipv4_hostname, ttl, ipv4_ip), "%s %d IN AAAA %s" % (ipv6_hostname, ttl, ipv6_ip), "%s %d IN MX 10 bar" % (ipv4_hostname, ttl), ] + [ "$GENERATE %s %s IN A %s" % (iterator_values, reverse_dns, hostname) for iterator_values, reverse_dns, hostname in expected_generate_directives ] ) ), )
def test_prime_with_plugin_manifest(self, fake_plugin_manifest): fake_plugin_manifest.return_value = { "test-plugin-manifest": "test-value" } self.useFixture( fixtures.EnvironmentVariable("SNAPCRAFT_BUILD_INFO", "1")) project_config = self.make_snapcraft_project( textwrap.dedent("""\ parts: test-part: plugin: nil """)) lifecycle.execute(steps.PRIME, project_config) expected = textwrap.dedent("""\ snapcraft-version: '3.0' snapcraft-started-at: '2019-05-07T19:25:53.939041Z' snapcraft-os-release-id: ubuntu snapcraft-os-release-version-id: '16.04' name: test base: core18 version: '1.0' summary: test description: test confinement: strict grade: stable parts: test-part: build-packages: [] installed-packages: - patchelf=0.9 installed-snaps: - core18=10 plugin: nil prime: [] stage: [] stage-packages: [] test-plugin-manifest: test-value uname: Linux test uname 4.10 x86_64 architectures: - {} build-packages: [] build-snaps: [] """.format(project_config.project.deb_arch)) self.assertThat( os.path.join(steps.PRIME.name, "snap", "manifest.yaml"), FileContains(expected), )
def test_prime_with_source_details(self, _): self.useFixture(fixtures.EnvironmentVariable( 'SNAPCRAFT_BUILD_INFO', '1')) self.fake_apt_cache.add_package( fixture_setup.FakeAptCachePackage('git', 'testversion')) self.make_snapcraft_yaml( textwrap.dedent("""\ parts: test-part: plugin: nil source: test-source source-type: git source-commit: test-commit """)) lifecycle.execute('prime', self.project_options) expected = textwrap.dedent("""\ name: test version: 0 summary: test description: test confinement: strict grade: stable parts: test-part: build-packages: [] installed-packages: [] installed-snaps: [] plugin: nil prime: [] source: test-source source-branch: '' source-checksum: '' source-commit: test-commit source-tag: '' source-type: git stage: [] stage-packages: [] uname: Linux test uname 4.10 x86_64 architectures: [{}] build-packages: [git=testversion] build-snaps: [] """.format(self.project_options.deb_arch)) self.assertThat( os.path.join('prime', 'snap', 'manifest.yaml'), FileContains(expected))
def test_init_must_write_gitignore_if_git_dir(self): expected_gitignore = dedent("""\ *.snap parts/* !parts/plugins/ prime/ stage/ snap/.snapcraft/ """) os.mkdir('.git') self.run_command(['init']) # Verify the .gitignore was created self.assertThat('.gitignore', FileContains(expected_gitignore))
def test_assets_in_meta(self): self.run_snapcraft("prime", self.project_dir) gui_dir = os.path.join(self.prime_dir, "meta", "gui") expected_desktop = dedent( """\ [Desktop Entry] Name=My App Exec=my-app Type=Application """ ) self.expectThat(os.path.join(gui_dir, "icon.png"), FileExists()) self.expectThat( os.path.join(gui_dir, "my-app.desktop"), FileContains(expected_desktop) )
def test_following_docstring_no_rewrite(self): file_path = _create_file('file', textwrap.dedent("""\ #!/usr/bin/env python3.5 ''' This is a test ======================= """)) mangling.rewrite_python_shebangs(os.path.dirname(file_path)) self.assertThat(file_path, FileContains(textwrap.dedent("""\ #!/usr/bin/env python3.5 ''' This is a test ======================= """)))
def test_writes_dns_zone_config_with_NS_record(self): target_dir = patch_dns_config_path(self) addr_ttl = random.randint(10, 100) ns_host_name = factory.make_name("ns") dns_zone_config = DNSForwardZoneConfig( factory.make_string(), serial=random.randint(1, 100), ns_host_name=ns_host_name, ipv4_ttl=addr_ttl, ipv6_ttl=addr_ttl, ) dns_zone_config.write_config() self.assertThat( os.path.join(target_dir, "zone.%s" % dns_zone_config.domain), FileContains(matcher=ContainsAll(["30 IN NS %s." % ns_host_name])), )
def test_write_config_skips_writing_if_overwrite_false(self): # If DNSConfig is created with overwrite=False, it won't # overwrite an existing config file. target_dir = patch_dns_config_path(self) random_content = factory.make_string() factory.make_file( location=target_dir, name=MAAS_NAMED_CONF_NAME, contents=random_content, ) dnsconfig = DNSConfig() dnsconfig.write_config(overwrite=False) self.assertThat( os.path.join(target_dir, MAAS_NAMED_CONF_NAME), FileContains(random_content), )
def test_writes_dns_zone_config_with_NS_record(self): target_dir = patch_dns_config_path(self) network = factory.make_ipv4_network() ns_host_name = factory.make_name("ns") dns_zone_config = DNSReverseZoneConfig( factory.make_string(), serial=random.randint(1, 100), ns_host_name=ns_host_name, network=network, ) dns_zone_config.write_config() for zone_name in [zi.zone_name for zi in dns_zone_config.zone_info]: self.assertThat( os.path.join(target_dir, "zone.%s" % zone_name), FileContains(matcher=Contains("30 IN NS %s." % ns_host_name)), )
def test_mktemp_not_deleted(self, base_test_case): """ ``mktemp`` returns a path that's not deleted after the test is run. """ created_files = [] class SomeTest(base_test_case): def test_create_file(self): path = self.mktemp() created_files.append(path) open(path, 'w').write('hello') run_test(SomeTest('test_create_file')) [path] = created_files self.addCleanup(os.unlink, path) self.assertThat(path, FileContains('hello'))
def test_get(self): provider = ProviderImpl(project=self.project, echoer=self.echoer_mock) os.makedirs(provider.provider_project_dir) cloud_data_filepath = provider._get_cloud_user_data( timezone="America/Argentina/Cordoba" ) self.assertThat( cloud_data_filepath, FileContains( dedent( """\ #cloud-config manage_etc_hosts: true package_update: false growpart: mode: growpart devices: ["/"] ignore_growroot_disabled: false runcmd: - ["ln", "-s", "../usr/share/zoneinfo/America/Argentina/Cordoba", "/etc/localtime"] write_files: - path: /root/.bashrc permissions: 0644 content: | export SNAPCRAFT_BUILD_ENVIRONMENT=managed-host export PS1="\h \$(/bin/_snapcraft_prompt)# " export PATH=/snap/bin:$PATH - path: /bin/_snapcraft_prompt permissions: 0755 content: | #!/bin/bash if [[ "$PWD" =~ ^$HOME.* ]]; then path="${PWD/#$HOME/\ ..}" if [[ "$path" == " .." ]]; then ps1="" else ps1="$path" fi else ps1="$PWD" fi echo -n $ps1 """ # noqa: W605 ) ), )
def test_download_file_from_ftp_source(self): """Download a file from a FTP source, LP: #1602323""" ftp_dir = os.path.join(self.path, 'ftp') os.mkdir(ftp_dir) ftp_server = FtpServerRunning(ftp_dir) self.useFixture(ftp_server) test_file_path = os.path.join(self.path, 'test') with open(test_file_path, 'w') as test_file: test_file.write('Hello ftp') with tarfile.open(os.path.join(ftp_dir, 'test.tar.gz'), 'w:gz') as tar: tar.add(test_file_path) self.run_snapcraft('pull', 'ftp-source') self.assertThat( os.path.join(self.parts_dir, 'ftp-part', 'src', 'test'), FileContains('Hello ftp'))
def test_python(self): file_path = _create_file( 'file', textwrap.dedent("""\ #! /usr/bin/python2.7 # Larger file """)) mangling.rewrite_python_shebangs(os.path.dirname(file_path)) self.assertThat( file_path, FileContains( textwrap.dedent("""\ #!/usr/bin/env python2.7 # Larger file """)))
def test_write_config_writes_config(self): target_dir = patch_dns_config_path(self) domain = factory.make_string() network = IPNetwork('192.168.0.3/24') ip = factory.pick_ip_in_network(network) forward_zone = DNSForwardZoneConfig( domain, mapping={factory.make_string(): ip}) reverse_zone = DNSReverseZoneConfig(domain, 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 test_make_temporary_path_not_deleted(self, base_test_case): """ ``make_temporary_path`` returns a path that's not deleted after the test is run. """ created_files = [] class SomeTest(base_test_case): def test_create_file(self): path = self.make_temporary_path() created_files.append(path) path.setContent('hello') run_test(SomeTest('test_create_file')) [path] = created_files self.addCleanup(path.remove) self.assertThat(path.path, FileContains('hello'))
def test_metadata_doesnt_overwrite_icon_file(self): os.makedirs(self.directory) icon_content = 'setup icon' _create_file(os.path.join(self.directory, self.file_name), content=icon_content) def _fake_extractor(file_path): return extractors.ExtractedMetadata( icon='test/extracted/unexistent/icon/path') self.useFixture( fixture_setup.FakeMetadataExtractor('fake', _fake_extractor)) self.generate_meta_yaml(build=True) expected_icon = os.path.join(self.meta_dir, 'gui', self.file_name) self.assertThat(expected_icon, FileContains(icon_content))
def test_not_exists(self): doesntexist = os.path.join(self.mkdtemp(), 'doesntexist') mismatch = FileContains('').match(doesntexist) self.assertThat( PathExists().match(doesntexist).describe(), Equals(mismatch.describe()))