def test_mksquashfs_from_snap_used_if_using_snap(self, mock_run_mksquashfs, mock_check_command): self.useFixture(fixture_setup.FakeSnapcraftIsASnap()) self.make_snapcraft_yaml() original_exists = os.path.exists def _fake_exists(path): if path == '/snap/snapcraft/current/usr/bin/mksquashfs': return True else: return original_exists(path) with mock.patch('os.path.exists', side_effect=_fake_exists): self.run_command(['snap']) mksquashfs_path = os.path.join('/snap', 'snapcraft', 'current', 'usr', 'bin', 'mksquashfs') mock_run_mksquashfs.assert_called_once_with( mksquashfs_path, directory=self.prime_dir, snap_name='snap-test', snap_type='app', output_snap_name='snap-test_1.0_amd64.snap')
def test_tools_from_snap_used_if_using_snap(self): self.useFixture(fixture_setup.FakeSnapcraftIsASnap()) real_exists = os.path.exists def _fake_exists(path): if path == '/snap/snapcraft/current/bin/patchelf': return True elif path == '/snap/snapcraft/current/usr/bin/strip': return True else: return real_exists(path) with mock.patch('os.path.exists', side_effect=_fake_exists): # The base_path does not matter here as there are not files to # be crawled for. elf_patcher = elf.Patcher(dynamic_linker='/lib/fake-ld', root_path='/fake') expected_patchelf = os.path.join('/snap', 'snapcraft', 'current', 'bin', 'patchelf') self.assertThat(elf_patcher._patchelf_cmd, Equals(expected_patchelf)) expected_strip = os.path.join('/snap', 'snapcraft', 'current', 'usr', 'bin', 'strip') self.assertThat(elf_patcher._strip_cmd, Equals(expected_strip))
def test_snapcraft_utils_in_path_if_snap(self): self.useFixture(fixture_setup.FakeSnapcraftIsASnap()) os.mkdir("builddir") runner = _runner.Runner( part_properties={"override-build": "echo $PATH > path"}, partdir=self.partdir, sourcedir="sourcedir", builddir="builddir", stagedir="stagedir", primedir="primedir", builtin_functions={}, build_env_generator=lambda: "export FOO=BAR", ) runner.build() expected_path_segment = "/snap/snapcraft/current/bin/scriptlet-bin" self.assertThat(os.path.join("builddir", "path"), FileExists()) self.assertThat( os.path.join("builddir", "path"), FileContains(matcher=Contains(expected_path_segment)), )
def test_tools_from_snap_used_if_using_snap(self): self.useFixture(fixture_setup.FakeSnapcraftIsASnap()) real_exists = os.path.exists def _fake_exists(path): if path == "/snap/snapcraft/current/bin/patchelf": return True elif path == "/snap/snapcraft/current/usr/bin/strip": return True else: return real_exists(path) with mock.patch("os.path.exists", side_effect=_fake_exists): # The base_path does not matter here as there are not files to # be crawled for. elf_patcher = elf.Patcher(dynamic_linker="/lib/fake-ld", root_path="/fake") expected_patchelf = os.path.join( "/snap", "snapcraft", "current", "bin", "patchelf" ) self.assertThat(elf_patcher._patchelf_cmd, Equals(expected_patchelf)) expected_strip = os.path.join( "/snap", "snapcraft", "current", "usr", "bin", "strip" ) self.assertThat(elf_patcher._strip_cmd, Equals(expected_strip))
def test_get_tool_path_in_container_fails_root(self): self.useFixture(fixture_setup.FakeSnapcraftIsASnap()) self.assertRaises( file_utils.ToolMissingError, file_utils.get_tool_path, "non-existent-tool-command", )
def test_push_a_snap_running_from_snap(self): self.useFixture(fixture_setup.FakeSnapcraftIsASnap()) mock_tracker = mock.Mock(storeapi._status_tracker.StatusTracker) mock_tracker.track.return_value = { 'code': 'ready_to_release', 'processed': True, 'can_release': True, 'url': '/fake/url', 'revision': 9, } patcher = mock.patch.object(storeapi.StoreClient, 'upload') mock_upload = patcher.start() self.addCleanup(patcher.stop) mock_upload.return_value = mock_tracker original_exists = os.path.exists def _fake_exists(path): if path == '/snap/snapcraft/current/usr/bin/unsquashfs': return True else: return original_exists(path) actual_unsquashfs_path = None original_check_output = subprocess.check_output # Push requires unsquashfs to work, but we're faking it out to use one # that doesn't exist. So we record what path it WOULD use, and then # pass it through to the real one so the rest of the function works. def _fake_check_output(*args, **kwargs): nonlocal actual_unsquashfs_path if 'unsquashfs' in args[0][0]: actual_unsquashfs_path = args[0][0] args[0][0] = 'unsquashfs' return original_check_output(*args, **kwargs) # Upload with mock.patch('subprocess.check_output', side_effect=_fake_check_output): with mock.patch('snapcraft.storeapi.' '_status_tracker.StatusTracker'): with mock.patch('os.path.exists', side_effect=_fake_exists): result = self.run_command(['push', self.snap_file]) self.assertThat(result.exit_code, Equals(0)) self.assertRegexpMatches( self.fake_logger.output, ".*push '.*test-snap.snap' to the store\.\n" "Revision 9 of 'basic' created\.", ) mock_upload.assert_called_once_with('basic', self.snap_file) unsquashfs_path = os.path.join('/snap', 'snapcraft', 'current', 'usr', 'bin', 'unsquashfs') self.assertThat(actual_unsquashfs_path, Equals(unsquashfs_path))
def test_get_tool_from_snapcraft_snap_path(self): self.useFixture(fixture_setup.FakeSnapcraftIsASnap()) snap_root = os.path.join(os.path.sep, "snap", "snapcraft", "current") self._patch(snap_root) self.assertThat( file_utils.get_tool_path("tool-command"), Equals(os.path.join(snap_root, self.tool_path)), )
def test_get_tool_from_docker_snap_path(self): self.useFixture(fixture_setup.FakeSnapcraftIsASnap()) snap_root = os.path.join(os.path.sep, "snap", "snapcraft", "current") self._patch(snap_root) with mock.patch("snapcraft.internal.common.is_docker_instance", return_value=True): self.assertThat( file_utils.get_tool_path("tool-command"), Equals(os.path.join(snap_root, self.tool_path)), )
def setUp(self): super().setUp() self.useFixture(fixture_setup.FakeSnapcraftIsASnap()) self.instance_name = "snapcraft-project-name" patcher = mock.patch( "snapcraft.internal.build_providers._base_provider.SnapInjector") self.snap_injector_mock = patcher.start() self.addCleanup(patcher.stop) self.echoer_mock = mock.Mock()
def test_snapcraftctl_alias_if_snap(self): self.useFixture(fixture_setup.FakeSnapcraftIsASnap()) os.mkdir('builddir') runner = _runner.Runner( part_properties={'prepare': 'alias snapcraftctl > definition'}, sourcedir='sourcedir', builddir='builddir', stagedir='stagedir', primedir='primedir', builtin_functions={}) with mock.patch('os.path.exists', return_value=True): runner.prepare() expected_snapcrafctl = '/snap/snapcraft/current/bin/snapcraftctl' self.assertThat(os.path.join('builddir', 'definition'), FileExists()) self.assertThat( os.path.join('builddir', 'definition'), FileContains('snapcraftctl={!r}\n'.format(expected_snapcrafctl)))
def setUp(self): super().setUp() self.useFixture(fixture_setup.FakeSnapcraftIsASnap()) patcher = mock.patch("sys.exit") self.exit_mock = patcher.start() self.addCleanup(patcher.stop) patcher = mock.patch("builtins.print") self.print_mock = patcher.start() self.addCleanup(patcher.stop) patcher = mock.patch("snapcraft.cli._errors.echo.error") self.error_mock = patcher.start() self.addCleanup(patcher.stop) patcher = mock.patch("traceback.print_exception") self.print_exception_mock = patcher.start() self.addCleanup(patcher.stop) patcher = mock.patch("sys.stdout.isatty", return_value=True) self.mock_isatty = patcher.start() self.addCleanup(patcher.stop)
def test_snapcraftctl_alias_if_snap(self): self.useFixture(fixture_setup.FakeSnapcraftIsASnap()) os.mkdir("builddir") runner = _runner.Runner( part_properties={"prepare": "alias snapcraftctl > definition"}, sourcedir="sourcedir", builddir="builddir", stagedir="stagedir", primedir="primedir", builtin_functions={}, ) with mock.patch("os.path.exists", return_value=True): runner.prepare() expected_snapcrafctl = "/snap/snapcraft/current/bin/snapcraftctl" self.assertThat(os.path.join("builddir", "definition"), FileExists()) self.assertThat( os.path.join("builddir", "definition"), FileContains("snapcraftctl={!r}\n".format(expected_snapcrafctl)), )
def test_patch_uses_snapped_strip(self): self.useFixture(fixture_setup.FakeSnapcraftIsASnap()) self.fake_elf = fixture_setup.FakeElf(root_path=self.path, patchelf_version='0.8') self.useFixture(self.fake_elf) elf_file = self.fake_elf['fake_elf-bad-patchelf'] real_check_call = subprocess.check_call real_check_output = subprocess.check_output real_exists = os.path.exists def _fake_check_call(*args, **kwargs): if 'patchelf' in args[0][0]: self.assertThat(args[0][0], Equals('/snap/snapcraft/current/bin/patchelf')) args[0][0] = 'patchelf' elif 'strip' in args[0][0]: self.assertThat( args[0][0], Equals('/snap/snapcraft/current/usr/bin/strip')) args[0][0] = 'strip' real_check_call(*args, **kwargs) def _fake_check_output(*args, **kwargs): if 'patchelf' in args[0][0]: self.assertThat(args[0][0], Equals('/snap/snapcraft/current/bin/patchelf')) args[0][0] = 'patchelf' elif 'strip' in args[0][0]: self.assertThat( args[0][0], Equals('/snap/snapcraft/current/usr/bin/strip')) args[0][0] = 'strip' return real_check_output(*args, **kwargs) def _fake_exists(path): if path == '/snap/snapcraft/current/bin/patchelf': return True elif path == '/snap/snapcraft/current/usr/bin/strip': return True else: return real_exists(path) with mock.patch('subprocess.check_call') as mock_check_call: with mock.patch('subprocess.check_output') as mock_check_output: with mock.patch('os.path.exists', side_effect=_fake_exists): mock_check_call.side_effect = _fake_check_call mock_check_output.side_effect = _fake_check_output # The base_path does not matter here as there are not files # for which to crawl. elf_patcher = elf.Patcher(dynamic_linker='/lib/fake-ld', root_path='/fake') self.assertRaises(errors.PatcherNewerPatchelfError, elf_patcher.patch, elf_file=elf_file) # Test that .note.go.buildid is stripped off mock_check_call.assert_has_calls([ mock.call([ 'patchelf', '--set-interpreter', '/lib/fake-ld', mock.ANY ]), mock.call([ 'strip', '--remove-section', '.note.go.buildid', mock.ANY ]), mock.call([ 'patchelf', '--set-interpreter', '/lib/fake-ld', mock.ANY ]), ])
def test_patch_uses_snapped_strip(self): self.useFixture(fixture_setup.FakeSnapcraftIsASnap()) self.fake_elf = fixture_setup.FakeElf( root_path=self.path, patchelf_version="0.8" ) self.useFixture(self.fake_elf) elf_file = self.fake_elf["fake_elf-bad-patchelf"] real_check_call = subprocess.check_call real_check_output = subprocess.check_output real_exists = os.path.exists def _fake_check_call(*args, **kwargs): if "patchelf" in args[0][0]: self.assertThat( args[0][0], Equals("/snap/snapcraft/current/bin/patchelf") ) args[0][0] = "patchelf" elif "strip" in args[0][0]: self.assertThat( args[0][0], Equals("/snap/snapcraft/current/usr/bin/strip") ) args[0][0] = "strip" real_check_call(*args, **kwargs) def _fake_check_output(*args, **kwargs): if "patchelf" in args[0][0]: self.assertThat( args[0][0], Equals("/snap/snapcraft/current/bin/patchelf") ) args[0][0] = "patchelf" elif "strip" in args[0][0]: self.assertThat( args[0][0], Equals("/snap/snapcraft/current/usr/bin/strip") ) args[0][0] = "strip" return real_check_output(*args, **kwargs) def _fake_exists(path): if path == "/snap/snapcraft/current/bin/patchelf": return True elif path == "/snap/snapcraft/current/usr/bin/strip": return True else: return real_exists(path) with mock.patch("subprocess.check_call") as mock_check_call: with mock.patch("subprocess.check_output") as mock_check_output: with mock.patch("os.path.exists", side_effect=_fake_exists): mock_check_call.side_effect = _fake_check_call mock_check_output.side_effect = _fake_check_output # The base_path does not matter here as there are not files # for which to crawl. elf_patcher = elf.Patcher( dynamic_linker="/lib/fake-ld", root_path="/fake" ) self.assertRaises( errors.PatcherNewerPatchelfError, elf_patcher.patch, elf_file=elf_file, ) # Test that .note.go.buildid is stripped off mock_check_call.assert_has_calls( [ mock.call( [ "patchelf", "--set-interpreter", "/lib/fake-ld", mock.ANY, ] ), mock.call( [ "strip", "--remove-section", ".note.go.buildid", mock.ANY, ] ), mock.call( [ "patchelf", "--set-interpreter", "/lib/fake-ld", mock.ANY, ] ), ] )