def _setUp(self): super()._setUp() snapcraft_path = get_snapcraft_path() tempdir = self.useFixture(fixtures.TempDir()).path altered_path = "{}:{}".format(tempdir, os.environ.get("PATH")) self.useFixture(fixtures.EnvironmentVariable("PATH", altered_path)) snapcraftctl_path = os.path.join(tempdir, "snapcraftctl") with open(snapcraftctl_path, "w") as f: f.write( textwrap.dedent("""\ #!/usr/bin/env python3 # Make sure we can find snapcraft, even if it's not installed # (like in CI). import sys sys.path.append('{snapcraft_path!s}') import snapcraft.cli.__main__ if __name__ == '__main__': snapcraft.cli.__main__.run_snapcraftctl( prog_name='snapcraftctl') """.format(snapcraft_path=snapcraft_path))) f.flush() os.chmod(snapcraftctl_path, 0o755)
def setUp(self): super().setUp() temp_cwd_fixture = fixture_setup.TempCWD() self.useFixture(temp_cwd_fixture) self.path = temp_cwd_fixture.path # Use a separate path for XDG dirs, or changes there may be detected as # source changes. self.xdg_path = self.useFixture(fixtures.TempDir()).path self.useFixture(fixture_setup.TempXDG(self.xdg_path)) self.fake_terminal = fixture_setup.FakeTerminal() self.useFixture(self.fake_terminal) self.useFixture(fixture_setup.SilentSnapProgress()) # Some tests will directly or indirectly change the plugindir, which # is a module variable. Make sure that it is returned to the original # value when a test ends. self.addCleanup(common.set_plugindir, common.get_plugindir()) self.addCleanup(common.set_schemadir, common.get_schemadir()) self.addCleanup(common.set_librariesdir, common.get_librariesdir()) self.addCleanup(common.reset_env) common.set_schemadir(os.path.join(get_snapcraft_path(), "schema")) self.fake_logger = fixtures.FakeLogger(level=logging.ERROR) self.useFixture(self.fake_logger) patcher = mock.patch("multiprocessing.cpu_count") self.cpu_count = patcher.start() self.cpu_count.return_value = 2 self.addCleanup(patcher.stop) patcher = mock.patch( "snapcraft.internal.indicators.ProgressBar", new=SilentProgressBar ) patcher.start() self.addCleanup(patcher.stop) # These are what we expect by default self.snap_dir = os.path.join(os.getcwd(), "snap") self.prime_dir = os.path.join(os.getcwd(), "prime") self.stage_dir = os.path.join(os.getcwd(), "stage") self.parts_dir = os.path.join(os.getcwd(), "parts") self.local_plugins_dir = os.path.join(self.snap_dir, "plugins") # Avoid installing patchelf in the tests self.useFixture(fixtures.EnvironmentVariable("SNAPCRAFT_NO_PATCHELF", "1")) # Disable Sentry reporting for tests, otherwise they'll hang waiting # for input self.useFixture( fixtures.EnvironmentVariable("SNAPCRAFT_ENABLE_SENTRY", "false") ) machine = os.environ.get("SNAPCRAFT_TEST_MOCK_MACHINE", None) self.base_environment = fixture_setup.FakeBaseEnvironment(machine=machine) self.useFixture(self.base_environment) # Make sure "SNAPCRAFT_ENABLE_DEVELOPER_DEBUG" is reset between tests self.useFixture( fixtures.EnvironmentVariable("SNAPCRAFT_ENABLE_DEVELOPER_DEBUG") ) self.useFixture(fixture_setup.FakeSnapcraftctl())
def setUp(self): super().setUp() temp_dir_fixture = fixtures.TempDir() self.useFixture(temp_dir_fixture) self.path = temp_dir_fixture.path patcher = mock.patch("snapcraft.repo._deb.apt.Cache") self.mock_apt_cache = patcher.start() self.addCleanup(patcher.stop) self.cache = self.Cache() self.mock_apt_cache.return_value = self.cache for package, version in self.packages: self.add_package(FakeAptCachePackage(package, version)) def fetch_binary(package_candidate, destination): path = os.path.join(self.path, "{}.deb".format(package_candidate.name)) open(path, "w").close() return path patcher = mock.patch("snapcraft.repo._deb._AptCache.fetch_binary") mock_fetch_binary = patcher.start() mock_fetch_binary.side_effect = fetch_binary self.addCleanup(patcher.stop) # Add all the packages in the manifest. with open( os.path.join(get_snapcraft_path(), "snapcraft", "internal", "repo", "manifest.txt")) as manifest_file: self.add_packages([line.strip() for line in manifest_file])
def setUp(self): super().setUp() temp_dir_fixture = fixtures.TempDir() self.useFixture(temp_dir_fixture) self.path = temp_dir_fixture.path patcher = mock.patch("snapcraft.repo._deb.apt.Cache") self.mock_apt_cache = patcher.start() self.addCleanup(patcher.stop) self.cache = self.Cache() self.mock_apt_cache.return_value = self.cache for package, version in self.packages: self.add_package(FakeAptCachePackage(package, version)) def fetch_binary(package_candidate, destination): path = os.path.join(self.path, "{}.deb".format(package_candidate.name)) open(path, "w").close() return path patcher = mock.patch("snapcraft.repo._deb._AptCache.fetch_binary") mock_fetch_binary = patcher.start() mock_fetch_binary.side_effect = fetch_binary self.addCleanup(patcher.stop) # Add all the packages in the manifest. with open( os.path.join( get_snapcraft_path(), "snapcraft", "internal", "repo", "manifest.txt" ) ) as manifest_file: self.add_packages([line.strip() for line in manifest_file])
def _setUp(self): super()._setUp() snapcraft_path = get_snapcraft_path() tempdir = self.useFixture(fixtures.TempDir()).path altered_path = "{}:{}".format(tempdir, os.environ.get("PATH")) self.useFixture(fixtures.EnvironmentVariable("PATH", altered_path)) snapcraftctl_path = os.path.join(tempdir, "snapcraftctl") with open(snapcraftctl_path, "w") as f: f.write( textwrap.dedent( """\ #!/usr/bin/env python3 # Make sure we can find snapcraft, even if it's not installed # (like in CI). import sys sys.path.append('{snapcraft_path!s}') import snapcraft.cli.__main__ if __name__ == '__main__': snapcraft.cli.__main__.run_snapcraftctl( prog_name='snapcraftctl') """.format( snapcraft_path=snapcraft_path ) ) ) f.flush() os.chmod(snapcraftctl_path, 0o755)
def setUp(self): super().setUp() self.rpm_file_path = (os.path.join(get_snapcraft_path(), 'tests', 'integration', 'snaps', 'rpm-hello', 'small-0.1-1.noarch.rpm')) self.dest_dir = 'dst' os.makedirs(self.dest_dir)
def setUp(self): super().setUp() temp_cwd_fixture = fixture_setup.TempCWD() self.useFixture(temp_cwd_fixture) self.path = temp_cwd_fixture.path self.useFixture(fixture_setup.TempXDG(self.path)) self.fake_terminal = fixture_setup.FakeTerminal() self.useFixture(self.fake_terminal) self.useFixture(fixture_setup.SilentSnapProgress()) # Some tests will directly or indirectly change the plugindir, which # is a module variable. Make sure that it is returned to the original # value when a test ends. self.addCleanup(common.set_plugindir, common.get_plugindir()) self.addCleanup(common.set_schemadir, common.get_schemadir()) self.addCleanup(common.set_librariesdir, common.get_librariesdir()) self.addCleanup(common.reset_env) common.set_schemadir(os.path.join(get_snapcraft_path(), 'schema')) self.fake_logger = fixtures.FakeLogger(level=logging.ERROR) self.useFixture(self.fake_logger) patcher = mock.patch('multiprocessing.cpu_count') self.cpu_count = patcher.start() self.cpu_count.return_value = 2 self.addCleanup(patcher.stop) patcher = mock.patch('snapcraft.internal.indicators.ProgressBar', new=SilentProgressBar) patcher.start() self.addCleanup(patcher.stop) # These are what we expect by default self.snap_dir = os.path.join(os.getcwd(), 'snap') self.prime_dir = os.path.join(os.getcwd(), 'prime') self.stage_dir = os.path.join(os.getcwd(), 'stage') self.parts_dir = os.path.join(os.getcwd(), 'parts') self.local_plugins_dir = os.path.join(self.snap_dir, 'plugins') # Avoid installing patchelf in the tests self.useFixture( fixtures.EnvironmentVariable('SNAPCRAFT_NO_PATCHELF', '1')) # Disable Sentry reporting for tests, otherwise they'll hang waiting # for input self.useFixture( fixtures.EnvironmentVariable('SNAPCRAFT_ENABLE_SENTRY', 'false')) machine = os.environ.get('SNAPCRAFT_TEST_MOCK_MACHINE', None) self.base_environment = fixture_setup.FakeBaseEnvironment( machine=machine) self.useFixture(self.base_environment) # Make sure SNAPCRAFT_DEBUG is reset between tests self.useFixture(fixtures.EnvironmentVariable('SNAPCRAFT_DEBUG')) self.useFixture(fixture_setup.FakeSnapcraftctl())
def setUp(self): super().setUp() self.rpm_file_path = os.path.join( get_snapcraft_path(), "tests", "integration", "snaps", "rpm-hello", "small-0.1-1.noarch.rpm", ) self.dest_dir = "dst" os.makedirs(self.dest_dir)
def setUp(self): super().setUp() self.rpm_file_path = os.path.join( get_snapcraft_path(), "tests", "integration", "snaps", "rpm-hello", "small-0.1-1.noarch.rpm", ) self.dest_dir = "dst" os.makedirs(self.dest_dir)
def setUp(self): super().setUp() temp_cwd_fixture = fixture_setup.TempCWD() self.useFixture(temp_cwd_fixture) self.path = temp_cwd_fixture.path # Use a separate path for XDG dirs, or changes there may be detected as # source changes. self.xdg_path = self.useFixture(fixtures.TempDir()).path self.useFixture(fixture_setup.TempXDG(self.xdg_path)) self.fake_terminal = fixture_setup.FakeTerminal() self.useFixture(self.fake_terminal) # Some tests will directly or indirectly change the plugindir, which # is a module variable. Make sure that it is returned to the original # value when a test ends. self.addCleanup(common.set_plugindir, common.get_plugindir()) self.addCleanup(common.set_schemadir, common.get_schemadir()) self.addCleanup(common.set_extensionsdir, common.get_extensionsdir()) self.addCleanup(common.set_keyringsdir, common.get_keyringsdir()) self.addCleanup(common.reset_env) common.set_schemadir(os.path.join(get_snapcraft_path(), "schema")) self.fake_logger = fixtures.FakeLogger(level=logging.ERROR) self.useFixture(self.fake_logger) # Some tests will change the apt Dir::Etc::Trusted and # Dir::Etc::TrustedParts directories. Make sure they're properly reset. self.addCleanup( apt.apt_pkg.config.set, "Dir::Etc::Trusted", apt.apt_pkg.config.find_file("Dir::Etc::Trusted"), ) self.addCleanup( apt.apt_pkg.config.set, "Dir::Etc::TrustedParts", apt.apt_pkg.config.find_file("Dir::Etc::TrustedParts"), ) patcher = mock.patch("os.sched_getaffinity") self.cpu_count = patcher.start() self.cpu_count.return_value = {1, 2} self.addCleanup(patcher.stop) # We do not want the paths to affect every test we have. patcher = mock.patch( "snapcraft.file_utils.get_snap_tool_path", side_effect=lambda x: x ) patcher.start() self.addCleanup(patcher.stop) patcher = mock.patch( "snapcraft.internal.indicators.ProgressBar", new=SilentProgressBar ) patcher.start() self.addCleanup(patcher.stop) # These are what we expect by default self.snap_dir = os.path.join(os.getcwd(), "snap") self.prime_dir = os.path.join(os.getcwd(), "prime") self.stage_dir = os.path.join(os.getcwd(), "stage") self.parts_dir = os.path.join(os.getcwd(), "parts") self.local_plugins_dir = os.path.join(self.snap_dir, "plugins") # Use this host to run through the lifecycle tests self.useFixture( fixtures.EnvironmentVariable("SNAPCRAFT_BUILD_ENVIRONMENT", "host") ) # Make sure snap installation does the right thing self.fake_snapd.installed_snaps = [ dict(name="core16", channel="stable", revision="10"), dict(name="core18", channel="stable", revision="10"), ] self.fake_snapd.snaps_result = [ dict(name="core16", channel="stable", revision="10"), dict(name="core18", channel="stable", revision="10"), ] self.fake_snapd.find_result = [ dict( core16=dict( channel="stable", channels={"latest/stable": dict(confinement="strict")}, ) ), dict( core18=dict( channel="stable", channels={"latest/stable": dict(confinement="strict")}, ) ), ] self.fake_snapd.snap_details_func = None self.fake_snap_command = fixture_setup.FakeSnapCommand() self.useFixture(self.fake_snap_command) # Avoid installing patchelf in the tests self.useFixture(fixtures.EnvironmentVariable("SNAPCRAFT_NO_PATCHELF", "1")) # Disable Sentry reporting for tests, otherwise they'll hang waiting # for input self.useFixture( fixtures.EnvironmentVariable("SNAPCRAFT_ENABLE_ERROR_REPORTING", "false") ) # Don't let the managed host variable leak into tests self.useFixture(fixtures.EnvironmentVariable("SNAPCRAFT_MANAGED_HOST")) machine = os.environ.get("SNAPCRAFT_TEST_MOCK_MACHINE", None) self.base_environment = fixture_setup.FakeBaseEnvironment(machine=machine) self.useFixture(self.base_environment) # Make sure "SNAPCRAFT_ENABLE_DEVELOPER_DEBUG" is reset between tests self.useFixture( fixtures.EnvironmentVariable("SNAPCRAFT_ENABLE_DEVELOPER_DEBUG") ) self.useFixture(fixture_setup.FakeSnapcraftctl()) # Don't let host SNAPCRAFT_BUILD_INFO variable leak into tests self.useFixture(fixtures.EnvironmentVariable("SNAPCRAFT_BUILD_INFO"))
def _setUp(self): super()._setUp() self.core_base_path = self.useFixture(fixtures.TempDir()).path binaries_path = os.path.join(get_snapcraft_path(), "tests", "bin", "elf") new_binaries_path = self.useFixture(fixtures.TempDir()).path current_path = os.environ.get("PATH") new_path = "{}:{}".format(new_binaries_path, current_path) self.useFixture(fixtures.EnvironmentVariable("PATH", new_path)) # Copy strip for f in ["strip", "execstack"]: shutil.copy(os.path.join(binaries_path, f), os.path.join(new_binaries_path, f)) os.chmod(os.path.join(new_binaries_path, f), 0o755) # Some values in ldd need to be set with core_path with open(os.path.join(binaries_path, "ldd")) as rf: with open(os.path.join(new_binaries_path, "ldd"), "w") as wf: for line in rf.readlines(): wf.write(line.replace("{CORE_PATH}", self.core_base_path)) os.chmod(os.path.join(new_binaries_path, "ldd"), 0o755) # Some values in ldd need to be set with core_path self.patchelf_path = os.path.join(new_binaries_path, "patchelf") with open(os.path.join(binaries_path, "patchelf")) as rf: with open(self.patchelf_path, "w") as wf: for line in rf.readlines(): wf.write(line.replace("{VERSION}", self._patchelf_version)) os.chmod(os.path.join(new_binaries_path, "patchelf"), 0o755) patcher = mock.patch.object( elf.ElfFile, "_extract_attributes", new_callable=lambda: _fake_elffile_extract_attributes, ) patcher.start() self.addCleanup(patcher.stop) self._elf_files = { "fake_elf-2.26": elf.ElfFile(path=os.path.join(self.root_path, "fake_elf-2.26")), "fake_elf-2.23": elf.ElfFile(path=os.path.join(self.root_path, "fake_elf-2.23")), "fake_elf-1.1": elf.ElfFile(path=os.path.join(self.root_path, "fake_elf-1.1")), "fake_elf-static": elf.ElfFile(path=os.path.join(self.root_path, "fake_elf-static")), "fake_elf-shared-object": elf.ElfFile( path=os.path.join(self.root_path, "fake_elf-shared-object")), "fake_elf-bad-ldd": elf.ElfFile(path=os.path.join(self.root_path, "fake_elf-bad-ldd")), "fake_elf-bad-patchelf": elf.ElfFile( path=os.path.join(self.root_path, "fake_elf-bad-patchelf")), "fake_elf-with-core-libs": elf.ElfFile( path=os.path.join(self.root_path, "fake_elf-with-core-libs")), "fake_elf-with-missing-libs": elf.ElfFile(path=os.path.join(self.root_path, "fake_elf-with-missing-libs")), "fake_elf-with-execstack": elf.ElfFile( path=os.path.join(self.root_path, "fake_elf-with-execstack")), "fake_elf-with-bad-execstack": elf.ElfFile(path=os.path.join(self.root_path, "fake_elf-with-bad-execstack")), "libc.so.6": elf.ElfFile(path=os.path.join(self.root_path, "libc.so.6")), "libssl.so.1.0.0": elf.ElfFile(path=os.path.join(self.root_path, "libssl.so.1.0.0")), } for elf_file in self._elf_files.values(): with open(elf_file.path, "wb") as f: f.write(b"\x7fELF") if elf_file.path.endswith("fake_elf-bad-patchelf"): f.write(b"nointerpreter") self.root_libraries = { "foo.so.1": os.path.join(self.root_path, "foo.so.1") } for root_library in self.root_libraries.values(): with open(root_library, "wb") as f: f.write(b"\x7fELF")
def _setUp(self): super()._setUp() self.core_base_path = self.useFixture(fixtures.TempDir()).path binaries_path = os.path.join(get_snapcraft_path(), 'tests', 'bin', 'elf') new_binaries_path = self.useFixture(fixtures.TempDir()).path current_path = os.environ.get('PATH') new_path = '{}:{}'.format(new_binaries_path, current_path) self.useFixture(fixtures.EnvironmentVariable('PATH', new_path)) # Copy strip for f in ['strip', 'execstack']: shutil.copy(os.path.join(binaries_path, f), os.path.join(new_binaries_path, f)) os.chmod(os.path.join(new_binaries_path, f), 0o755) # Some values in ldd need to be set with core_path with open(os.path.join(binaries_path, 'ldd')) as rf: with open(os.path.join(new_binaries_path, 'ldd'), 'w') as wf: for line in rf.readlines(): wf.write(line.replace('{CORE_PATH}', self.core_base_path)) os.chmod(os.path.join(new_binaries_path, 'ldd'), 0o755) # Some values in ldd need to be set with core_path self.patchelf_path = os.path.join(new_binaries_path, 'patchelf') with open(os.path.join(binaries_path, 'patchelf')) as rf: with open(self.patchelf_path, 'w') as wf: for line in rf.readlines(): wf.write(line.replace( '{VERSION}', self._patchelf_version)) os.chmod(os.path.join(new_binaries_path, 'patchelf'), 0o755) patcher = mock.patch.object(elf.ElfFile, '_extract', new_callable=lambda: _fake_elffile_extract) patcher.start() self.addCleanup(patcher.stop) self._elf_files = { 'fake_elf-2.26': elf.ElfFile( path=os.path.join(self.root_path, 'fake_elf-2.26')), 'fake_elf-2.23': elf.ElfFile( path=os.path.join(self.root_path, 'fake_elf-2.23')), 'fake_elf-1.1': elf.ElfFile( path=os.path.join(self.root_path, 'fake_elf-1.1')), 'fake_elf-static': elf.ElfFile( path=os.path.join(self.root_path, 'fake_elf-static')), 'fake_elf-shared-object': elf.ElfFile( path=os.path.join(self.root_path, 'fake_elf-shared-object')), 'fake_elf-bad-ldd': elf.ElfFile( path=os.path.join(self.root_path, 'fake_elf-bad-ldd')), 'fake_elf-bad-patchelf': elf.ElfFile( path=os.path.join(self.root_path, 'fake_elf-bad-patchelf')), 'fake_elf-with-core-libs': elf.ElfFile( path=os.path.join(self.root_path, 'fake_elf-with-core-libs')), 'fake_elf-with-execstack': elf.ElfFile( path=os.path.join(self.root_path, 'fake_elf-with-execstack')), 'fake_elf-with-bad-execstack': elf.ElfFile( path=os.path.join(self.root_path, 'fake_elf-with-bad-execstack')), 'libc.so.6': elf.ElfFile( path=os.path.join(self.root_path, 'libc.so.6')), 'libssl.so.1.0.0': elf.ElfFile( path=os.path.join(self.root_path, 'libssl.so.1.0.0')), } for elf_file in self._elf_files.values(): with open(elf_file.path, 'wb') as f: f.write(b'\x7fELF') if elf_file.path.endswith('fake_elf-bad-patchelf'): f.write(b'nointerpreter') self.root_libraries = { 'foo.so.1': os.path.join(self.root_path, 'foo.so.1'), } for root_library in self.root_libraries.values(): with open(root_library, 'wb') as f: f.write(b'\x7fELF')
def _setUp(self): super()._setUp() self.core_base_path = self.useFixture(fixtures.TempDir()).path binaries_path = os.path.join(get_snapcraft_path(), "tests", "bin", "elf") new_binaries_path = self.useFixture(fixtures.TempDir()).path current_path = os.environ.get("PATH") new_path = "{}:{}".format(new_binaries_path, current_path) self.useFixture(fixtures.EnvironmentVariable("PATH", new_path)) # Copy strip for f in ["strip", "execstack"]: shutil.copy( os.path.join(binaries_path, f), os.path.join(new_binaries_path, f) ) os.chmod(os.path.join(new_binaries_path, f), 0o755) # Some values in ldd need to be set with core_path with open(os.path.join(binaries_path, "ldd")) as rf: with open(os.path.join(new_binaries_path, "ldd"), "w") as wf: for line in rf.readlines(): wf.write(line.replace("{CORE_PATH}", self.core_base_path)) os.chmod(os.path.join(new_binaries_path, "ldd"), 0o755) # Some values in ldd need to be set with core_path self.patchelf_path = os.path.join(new_binaries_path, "patchelf") with open(os.path.join(binaries_path, "patchelf")) as rf: with open(self.patchelf_path, "w") as wf: for line in rf.readlines(): wf.write(line.replace("{VERSION}", self._patchelf_version)) os.chmod(os.path.join(new_binaries_path, "patchelf"), 0o755) patcher = mock.patch.object( elf.ElfFile, "_extract", new_callable=lambda: _fake_elffile_extract ) patcher.start() self.addCleanup(patcher.stop) self._elf_files = { "fake_elf-2.26": elf.ElfFile( path=os.path.join(self.root_path, "fake_elf-2.26") ), "fake_elf-2.23": elf.ElfFile( path=os.path.join(self.root_path, "fake_elf-2.23") ), "fake_elf-1.1": elf.ElfFile( path=os.path.join(self.root_path, "fake_elf-1.1") ), "fake_elf-static": elf.ElfFile( path=os.path.join(self.root_path, "fake_elf-static") ), "fake_elf-shared-object": elf.ElfFile( path=os.path.join(self.root_path, "fake_elf-shared-object") ), "fake_elf-bad-ldd": elf.ElfFile( path=os.path.join(self.root_path, "fake_elf-bad-ldd") ), "fake_elf-bad-patchelf": elf.ElfFile( path=os.path.join(self.root_path, "fake_elf-bad-patchelf") ), "fake_elf-with-core-libs": elf.ElfFile( path=os.path.join(self.root_path, "fake_elf-with-core-libs") ), "fake_elf-with-execstack": elf.ElfFile( path=os.path.join(self.root_path, "fake_elf-with-execstack") ), "fake_elf-with-bad-execstack": elf.ElfFile( path=os.path.join(self.root_path, "fake_elf-with-bad-execstack") ), "libc.so.6": elf.ElfFile(path=os.path.join(self.root_path, "libc.so.6")), "libssl.so.1.0.0": elf.ElfFile( path=os.path.join(self.root_path, "libssl.so.1.0.0") ), } for elf_file in self._elf_files.values(): with open(elf_file.path, "wb") as f: f.write(b"\x7fELF") if elf_file.path.endswith("fake_elf-bad-patchelf"): f.write(b"nointerpreter") self.root_libraries = {"foo.so.1": os.path.join(self.root_path, "foo.so.1")} for root_library in self.root_libraries.values(): with open(root_library, "wb") as f: f.write(b"\x7fELF")