Esempio n. 1
0
    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())
Esempio n. 2
0
    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())
Esempio n. 3
0
    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"))
Esempio n. 4
0
    def setUp(self):
        super().setUp()

        self.patchelf_command = "patchelf"
        self.execstack_command = "execstack"

        package_type = os.getenv("SNAPCRAFT_PACKAGE_TYPE")
        if package_type == "snap":
            self.snapcraft_command = "/snap/bin/snapcraft"
            self.patchelf_command = "/snap/snapcraft/current/usr/bin/patchelf"
            self.execstack_command = "/snap/snapcraft/current/usr/sbin/execstack"
        elif package_type == "deb":
            self.snapcraft_command = "/usr/bin/snapcraft"
            self.snapcraft_parser_command = "/usr/bin/snapcraft-parser"
        elif os.getenv("VIRTUAL_ENV") and sys.platform == "win32":
            self.snapcraft_command = ["python", "-m", "snapcraft.cli.__main__"]
            self.snapcraft_parser_command = os.path.join(
                os.getenv("VIRTUAL_ENV"), "bin", "snapcraft-parser")
        elif os.getenv("VIRTUAL_ENV"):
            self.snapcraft_command = os.path.join(os.getenv("VIRTUAL_ENV"),
                                                  "bin", "snapcraft")
            self.snapcraft_parser_command = os.path.join(
                os.getenv("VIRTUAL_ENV"), "bin", "snapcraft-parser")
        elif package_type == "brew":
            self.snapcraft_command = "/usr/local/bin/snapcraft"
        else:
            raise EnvironmentError(
                "snapcraft is not setup correctly for testing. Either set "
                "SNAPCRAFT_PACKAGE_TYPE to 'snap', 'deb' or 'brew', to run from "
                "either the snap, deb or homebrew or make sure your venv is properly "
                "setup as described in HACKING.md.")

        self.snaps_dir = os.path.join(os.path.dirname(__file__), "snaps")
        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))

        # Use this host to run through the lifecycle tests
        self.useFixture(
            fixtures.EnvironmentVariable("SNAPCRAFT_BUILD_ENVIRONMENT",
                                         "host"))

        # Use a dumb terminal for tests
        self.useFixture(fixtures.EnvironmentVariable("TERM", "dumb"))

        # 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"))

        # Note that these directories won't exist when the test starts,
        # they might be created after calling the snapcraft command on the
        # project dir.
        self.parts_dir = "parts"
        self.stage_dir = "stage"
        self.prime_dir = "prime"

        self.deb_arch = platform.get_deb_arch()
        self.arch_triplet = platform.get_arch_triplet()

        self.distro_series = os_release.get_version_codename()
Esempio n. 5
0
    def setUp(self):
        super().setUp()
        if os.getenv("SNAPCRAFT_FROM_SNAP", False):
            self.snapcraft_command = "/snap/bin/snapcraft"
        elif os.getenv("SNAPCRAFT_FROM_DEB", False):
            self.snapcraft_command = "/usr/bin/snapcraft"
            self.snapcraft_parser_command = "/usr/bin/snapcraft-parser"
        elif os.getenv("VIRTUAL_ENV") and sys.platform == "win32":
            self.snapcraft_command = ["python", "-m", "snapcraft.cli.__main__"]
            self.snapcraft_parser_command = os.path.join(
                os.getenv("VIRTUAL_ENV"), "bin", "snapcraft-parser")
        elif os.getenv("VIRTUAL_ENV"):
            self.snapcraft_command = os.path.join(os.getenv("VIRTUAL_ENV"),
                                                  "bin", "snapcraft")
            self.snapcraft_parser_command = os.path.join(
                os.getenv("VIRTUAL_ENV"), "bin", "snapcraft-parser")
        elif os.getenv("SNAPCRAFT_FROM_BREW", False):
            self.snapcraft_command = "/usr/local/bin/snapcraft"
        else:
            raise EnvironmentError(
                "snapcraft is not setup correctly for testing. Either set "
                "SNAPCRAFT_FROM_SNAP, SNAPCRAFT_FROM_DEB or "
                "SNAPCRAFT_FROM_BREW to run from either the snap, deb or "
                "brew, or make sure your venv is properly setup as described "
                "in HACKING.md.")

        if os.getenv("SNAPCRAFT_FROM_SNAP", False):
            self.patchelf_command = "/snap/snapcraft/current/usr/bin/patchelf"
            self.execstack_command = "/snap/snapcraft/current/usr/sbin/execstack"
        else:
            self.patchelf_command = "patchelf"
            self.execstack_command = "execstack"

        self.snaps_dir = os.path.join(os.path.dirname(__file__), "snaps")
        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))

        # Use a dumb terminal for tests
        self.useFixture(fixtures.EnvironmentVariable("TERM", "dumb"))

        # Disable Sentry reporting for tests, otherwise they'll hang waiting
        # for input
        self.useFixture(
            fixtures.EnvironmentVariable("SNAPCRAFT_ENABLE_SENTRY", "false"))

        # Note that these directories won't exist when the test starts,
        # they might be created after calling the snapcraft command on the
        # project dir.
        self.parts_dir = "parts"
        self.stage_dir = "stage"
        self.prime_dir = "prime"

        self.deb_arch = platform.get_deb_arch()
        self.arch_triplet = platform.get_arch_triplet()

        self.distro_series = os_release.get_version_codename()