def test_plain_source_string(self): snapcraft_yaml = fixture_setup.SnapcraftYaml(self.path) snapcraft_yaml.update_part( "my-part", {"plugin": "nil", "source": self.test_source} ) self.useFixture(snapcraft_yaml) self.run_snapcraft(["pull"])
def test_origin_runpath_migrated_to_rpath(self): # We stage libc6 for this to work on non xenial snapcraft_yaml = fixture_setup.SnapcraftYaml(self.path, confinement="classic") snapcraft_yaml.update_part( "test-part", { "plugin": "nil", "build-packages": ["patchelf"], "stage-packages": ["hello"], # Without --force-rpath this is essentially a RUNPATH "override-build": ("patchelf --set-rpath '$ORIGIN/foobar' " "$SNAPCRAFT_PART_INSTALL/usr/bin/hello"), }, ) self.useFixture(snapcraft_yaml) self.run_snapcraft("prime") bin_path = os.path.join(self.prime_dir, "usr", "bin", "hello") self.assertThat(bin_path, FileExists()) # Verify there aren't any duplicate rpath entries readelf_d = (subprocess.check_output(["readelf", "-d", bin_path]).decode().strip()) lines_with_runpath = [ l for l in readelf_d.splitlines() if "RUNPATH" in l ] lines_with_rpath = [l for l in readelf_d.splitlines() if "RPATH" in l] self.assertThat(len(lines_with_runpath), Annotate("Expected no RUNPATH entries", Equals(0))) self.assertThat(len(lines_with_rpath), Annotate("Expected one RPATH entry", Equals(1)))
def test_classic_confinement_patchelf_disabled(self): if os.environ.get("ADT_TEST") and self.deb_arch == "armhf": self.skipTest("The autopkgtest armhf runners can't install snaps") project_dir = "classic-build" # Now we set the required environment variable self.useFixture( fixtures.EnvironmentVariable("SNAPCRAFT_SETUP_CORE", "1")) self.copy_project_to_cwd(project_dir) # Create a new snapcraft.yaml snapcraft_yaml = fixture_setup.SnapcraftYaml(self.path, confinement="classic") snapcraft_yaml.update_part( "hello", { "source": ".", "plugin": "make", "build-attributes": ["no-patchelf"] }, ) self.useFixture(snapcraft_yaml) self.run_snapcraft("prime") bin_path = os.path.join(self.prime_dir, "bin", "hello-classic") self.assertThat(bin_path, FileExists()) interpreter = subprocess.check_output( [self.patchelf_command, "--print-interpreter", bin_path]).decode() self.assertThat(interpreter, StartsWith("/lib"))
def test_desktop_extracted_from_appstream(self): xdg_data_dir = os.path.join("usr", "local", "share", "applications") os.makedirs(os.path.join(xdg_data_dir, "com.example.test")) shutil.copyfile( os.path.join(os.path.dirname(tests.__file__), "data", "test.desktop"), os.path.join(self.path, xdg_data_dir, "com.example.test", "app.desktop"), ) snapcraft_yaml = fixture_setup.SnapcraftYaml( self.path, summary=None, description=None ) snapcraft_yaml.data["adopt-info"] = "test-part" snapcraft_yaml.update_part( "test-part", {"plugin": "dump", "parse-info": ["test.metainfo.xml"]} ) snapcraft_yaml.update_app( "test-app", {"command": "echo", "common-id": "com.example.testid"} ) self.useFixture(snapcraft_yaml) self.run_snapcraft("prime") self.assertThat( os.path.join("prime", "meta", "gui", "test-app.desktop"), FileExists() )
def test_source_on_other_arch(self): snapcraft_yaml = fixture_setup.SnapcraftYaml(self.path) snapcraft_yaml.update_part( "my-part", {"plugin": "nil", "source": [{"on other-arch": "invalid"}]} ) self.useFixture(snapcraft_yaml) self.run_snapcraft(["pull"])
def test_classic_confinement_patchelf_disabled(self): if os.environ.get('ADT_TEST') and self.deb_arch == 'armhf': self.skipTest("The autopkgtest armhf runners can't install snaps") project_dir = 'classic-build' # Now we set the required environment variable self.useFixture( fixtures.EnvironmentVariable('SNAPCRAFT_SETUP_CORE', '1')) self.copy_project_to_cwd(project_dir) # Create a new snapcraft.yaml snapcraft_yaml = fixture_setup.SnapcraftYaml(self.path, confinement='classic') snapcraft_yaml.update_part('hello', { 'source': '.', 'plugin': 'make', 'build-attributes': ['no-patchelf'] }) self.useFixture(snapcraft_yaml) self.run_snapcraft('prime') bin_path = os.path.join(self.prime_dir, 'bin', 'hello-classic') self.assertThat(bin_path, FileExists()) interpreter = subprocess.check_output( [self.patchelf_command, '--print-interpreter', bin_path]).decode() self.assertThat(interpreter, StartsWith('/lib'))
def setUp(self): super().setUp() # Use "clean" step for testing provider handling. self.step = "clean" # Don't actually run clean - we only want to test the command # line interface flag parsing. self.useFixture( fixtures.MockPatch("snapcraft.internal.lifecycle.clean")) # tests.unit.TestCase sets SNAPCRAFT_BUILD_ENVIRONMENT to host. # These build provider tests will want to set this explicitly. self.useFixture( fixtures.EnvironmentVariable("SNAPCRAFT_BUILD_ENVIRONMENT", None)) self.mock_get_provider_for = self.useFixture( fixtures.MockPatch( "snapcraft.internal.build_providers.get_provider_for", return_value=ProviderImpl, )).mock # Tests need to dictate this (or not). self.useFixture( fixtures.MockPatch( "snapcraft.internal.common.is_process_container", return_value=False)) self.useFixture(fixture_setup.FakeMultipass()) snapcraft_yaml = fixture_setup.SnapcraftYaml(self.path, base="core") snapcraft_yaml.update_part("part1", dict(plugin="nil")) self.useFixture(snapcraft_yaml)
def test_icon_extracted_from_appstream(self): os.mkdir("assets") shutil.copyfile( os.path.join(os.path.dirname(tests.__file__), "data", "icon.png"), os.path.join(self.path, "assets", "test-icon.png"), ) snapcraft_yaml = fixture_setup.SnapcraftYaml(self.path, summary=None, description=None) snapcraft_yaml.data["adopt-info"] = "test-part" snapcraft_yaml.update_part( "test-part", { "plugin": "dump", "source": ".", "parse-info": ["test.metainfo.xml"] }, ) self.useFixture(snapcraft_yaml) self.run_snapcraft("prime") self.assertThat(os.path.join("prime", "meta", "gui", "icon.png"), FileExists())
def test_origin_runpath_migrated_to_rpath(self): # We stage libc6 for this to work on non xenial snapcraft_yaml = fixture_setup.SnapcraftYaml(self.path, confinement='classic') snapcraft_yaml.update_part( 'test-part', { 'plugin': 'nil', 'build-packages': ['patchelf'], 'stage-packages': ['hello'], # Without --force-rpath this is essentially a RUNPATH 'override-build': ("patchelf --set-rpath '$ORIGIN/foobar' " "$SNAPCRAFT_PART_INSTALL/usr/bin/hello") }) self.useFixture(snapcraft_yaml) self.run_snapcraft('prime') bin_path = os.path.join(self.prime_dir, 'usr', 'bin', 'hello') self.assertThat(bin_path, FileExists()) # Verify there aren't any duplicate rpath entries readelf_d = subprocess.check_output(['readelf', '-d', bin_path]).decode().strip() lines_with_runpath = [ l for l in readelf_d.splitlines() if 'RUNPATH' in l ] lines_with_rpath = [l for l in readelf_d.splitlines() if 'RPATH' in l] self.assertThat(len(lines_with_runpath), Annotate('Expected no RUNPATH entries', Equals(0))) self.assertThat(len(lines_with_rpath), Annotate('Expected one RPATH entry', Equals(1)))
def test_desktop_extracted_from_appstream(self): xdg_data_dir = os.path.join('usr', 'local', 'share', 'applications') os.makedirs(os.path.join(xdg_data_dir, 'com.example.test')) shutil.copyfile( os.path.join(os.path.dirname(tests.__file__), 'data', 'test.desktop'), os.path.join(self.path, xdg_data_dir, 'com.example.test', 'app.desktop')) snapcraft_yaml = fixture_setup.SnapcraftYaml(self.path, summary=None, description=None) snapcraft_yaml.data['adopt-info'] = 'test-part' snapcraft_yaml.update_part('test-part', { 'plugin': 'dump', 'parse-info': ['test.metainfo.xml'] }) snapcraft_yaml.update_app('test-app', { 'command': 'echo', 'common-id': 'com.example.testid' }) self.useFixture(snapcraft_yaml) self.run_snapcraft('prime') self.assertThat( os.path.join('prime', 'meta', 'gui', 'test-app.desktop'), FileExists())
def test_static_base_with_libc6_stage_packaged(self): # The "bare" base is a static base. snapcraft_yaml = fixture_setup.SnapcraftYaml( self.path, base="bare", confinement=self.confinement) snapcraft_yaml.update_part("part1", dict(plugin="nil")) self.useFixture(snapcraft_yaml) project = Project( snapcraft_yaml_file_path=snapcraft_yaml.snapcraft_yaml_file_path) patcher = PartPatcher( elf_files=frozenset(["foo"]), project=project, confinement="strict", core_base="bare", snap_base_path="/snap/test-snap/current", stage_packages=["libc6"], stagedir="stage", primedir="prime", ) patcher.patch() self.fake_patchelf.mock.assert_called_once_with( dynamic_linker= "/snap/test-snap/current/lib/x86_64-linux-gnu/ld-2.27.so", preferred_patchelf_path=None, root_path="prime", )
def test_validation_passes(self): snapcraft_yaml = fixture_setup.SnapcraftYaml(self.path, base="core") snapcraft_yaml.update_part("part1", dict(plugin="nil")) self.useFixture(snapcraft_yaml) result = self.run_command(["pull"]) self.assertThat(result.exit_code, Equals(0))
def test_validation_fails(self): snapcraft_yaml = fixture_setup.SnapcraftYaml(self.path, name="name with spaces") snapcraft_yaml.update_part("part1", dict(plugin="nil")) self.useFixture(snapcraft_yaml) self.assertRaises(project.errors.YamlValidationError, self.run_command, ["pull"])
def test_plain_source_string(self): snapcraft_yaml = fixture_setup.SnapcraftYaml(self.path) snapcraft_yaml.update_part('my-part', { 'plugin': 'nil', 'source': self.test_source, }) self.useFixture(snapcraft_yaml) self.run_snapcraft(['pull'])
def test_cleanbuild_raises_when_yaml_is_invalid(self): snapcraft_yaml = fixture_setup.SnapcraftYaml(self.path, name="cleanbuild") snapcraft_yaml.update_part("part1", dict(sources="boo")) self.useFixture(snapcraft_yaml) self.assertRaises( project_errors.YamlValidationError, self.run_command, ["cleanbuild"] )
def test_cleanbuild_raises_when_yaml_is_valid(self): snapcraft_yaml = fixture_setup.SnapcraftYaml(self.path, name="cleanbuild") snapcraft_yaml.update_part("part1", dict(plugin="nil")) self.useFixture(snapcraft_yaml) self.assertRaises( errors.SnapcraftEnvironmentError, self.run_command, ["cleanbuild"] )
def make_snapcraft_project(self, *, confinement: str): snapcraft_yaml = fixture_setup.SnapcraftYaml(self.path) snapcraft_yaml.data["confinement"] = confinement snapcraft_yaml.update_part("test-part", dict(plugin="nil")) self.useFixture(snapcraft_yaml) project = Project( snapcraft_yaml_file_path=snapcraft_yaml.snapcraft_yaml_file_path) return project_loader.load_config(project)
def test_use_staged_pip(self): snapcraft_yaml = fixture_setup.SnapcraftYaml(self.path) snapcraft_yaml.update_part('test-part', { 'plugin': 'python', 'source': '.', 'stage-packages': ['python3-pip'] }) self.useFixture(snapcraft_yaml) self.run_snapcraft('pull')
def setUp(self): super().setUp() self.useFixture(fixture_setup.SnapcraftYaml(self.path)) self.mock_lc = self.useFixture( fixtures.MockPatch("snapcraft.cli.remote.LaunchpadClient", autospec=True)).mock.return_value self.mock_lc_architectures = mock.PropertyMock(return_value=["i386"]) type(self.mock_lc).architectures = self.mock_lc_architectures self.mock_lc.has_outstanding_build.return_value = False
def make_snapcraft_project(self, parts): snapcraft_yaml = fixture_setup.SnapcraftYaml(self.path) snapcraft_yaml.update_part("part1", dict(plugin="nil")) for part_name, part in parts: snapcraft_yaml.update_part(part_name, part) self.useFixture(snapcraft_yaml) project = Project( snapcraft_yaml_file_path=snapcraft_yaml.snapcraft_yaml_file_path) return project_loader.load_config(project)
def run_check(self): snapcraft_yaml = fixture_setup.SnapcraftYaml( os.path.dirname(self.snap_asset_dir)) snapcraft_yaml.update_part("fake", dict(plugin="nil")) self.useFixture(snapcraft_yaml) project = snapcraft.project.Project( is_managed_host=self.is_managed_host, snapcraft_yaml_file_path=snapcraft_yaml.snapcraft_yaml_file_path, ) conduct_project_sanity_check(project)
def make_snapcraft_project(self, apps): snapcraft_yaml = fixture_setup.SnapcraftYaml(self.path) snapcraft_yaml.update_part("part1", dict(plugin="nil")) for app_name, app in apps: snapcraft_yaml.update_app(app_name, app) self.useFixture(snapcraft_yaml) p = project.Project( snapcraft_yaml_file_path=snapcraft_yaml.snapcraft_yaml_file_path) return load_config(p)
def test_invalid_unicode_workaround(self): snapcraft_yaml = fixture_setup.SnapcraftYaml( self.path, name=self.name, summary=self.summary, description=self.description, ) snapcraft_yaml.update_part("my-part", {"plugin": "nil"}) self.useFixture(snapcraft_yaml) self.run_snapcraft("pull")
def test_source_try(self): snapcraft_yaml = fixture_setup.SnapcraftYaml(self.path) snapcraft_yaml.update_part( "my-part", { "plugin": "nil", "source": [{"try": self.test_source}, {"else": "invalid"}], }, ) self.useFixture(snapcraft_yaml) self.run_snapcraft(["pull"])
def test_build_snap(self): if os.environ.get('ADT_TEST') and self.deb_arch == 'armhf': self.skipTest("The autopkgtest armhf runners can't install snaps") self.useFixture(fixture_setup.WithoutSnapInstalled(self.snap)) snapcraft_yaml = fixture_setup.SnapcraftYaml(self.path) snapcraft_yaml.update_part('test-part-with-build-snap', { 'plugin': 'nil', 'build-snaps': [self.snap] }) self.useFixture(snapcraft_yaml) self.run_snapcraft('build') self.assertTrue(repo.is_snap_installed(self.snap))
def test_source_on_other_arch(self): snapcraft_yaml = fixture_setup.SnapcraftYaml(self.path) snapcraft_yaml.update_part('my-part', { 'plugin': 'nil', 'source': [ { 'on other-arch': 'invalid' }, ] }) self.useFixture(snapcraft_yaml) self.run_snapcraft(['pull'])
def test_build_snap(self): if os.environ.get("ADT_TEST") and self.deb_arch == "armhf": self.skipTest("The autopkgtest armhf runners can't install snaps") self.useFixture(fixture_setup.WithoutSnapInstalled(self.snap)) snapcraft_yaml = fixture_setup.SnapcraftYaml(self.path) snapcraft_yaml.update_part("test-part-with-build-snap", { "plugin": "nil", "build-snaps": [self.snap] }) self.useFixture(snapcraft_yaml) self.run_snapcraft("build") self.assertTrue(repo.is_snap_installed(self.snap))
def _get_snap_packaging(self, **yaml_args): if "parts" not in yaml_args: yaml_args["parts"] = dict(part1=dict(plugin="nil")) snapcraft_yaml = fixture_setup.SnapcraftYaml(self.path, **yaml_args) self.useFixture(snapcraft_yaml) project = Project( snapcraft_yaml_file_path=snapcraft_yaml.snapcraft_yaml_file_path) config = load_config(project) return _SnapPackaging(project_config=config, extracted_metadata=None)
def test_specified_metadata_not_overwritten(self): snapcraft_yaml = fixture_setup.SnapcraftYaml(self.path, description=None) snapcraft_yaml.data["adopt-info"] = "test-part" snapcraft_yaml.update_part( "test-part", {"plugin": "dump", "parse-info": ["test.metainfo.xml"]} ) self.useFixture(snapcraft_yaml) self.run_snapcraft("prime") with open(os.path.join("prime", "meta", "snap.yaml")) as snap_yaml_file: snap_yaml = yaml_utils.load(snap_yaml_file) self.assertThat(snap_yaml["description"], Equals("test-appstream-description")) self.assertThat(snap_yaml["summary"], Equals("test-summary"))
def test_use_staged_pip(self): snapcraft_yaml = fixture_setup.SnapcraftYaml(self.path, base=None) snapcraft_yaml.update_part( "test-part", { "plugin": "python", "source": ".", "stage-packages": ["python3-pip"] }, ) self.useFixture(snapcraft_yaml) self.run_snapcraft("pull")