Exemple #1
0
    def test_parallel_invocation_inject_snap(self, mock_is_snap):
        mock_is_snap.side_effect = lambda: True

        fake_snapd = fixture_setup.FakeSnapd()
        self.useFixture(fake_snapd)
        fake_snapd.snaps_result = [
            {
                "name": "core",
                "confinement": "strict",
                "id": "2kkitQurgOkL3foImG4wDwn9CIANuHlt",
                "channel": "stable",
                "revision": "123",
            },
            {
                "name": "snapcraft",
                "confinement": "classic",
                "id": "3lljuRvshPlM4gpJnH5xExo0DJBOvImu",
                "channel": "edge",
                "revision": "345",
            },
        ]

        builder1 = self.make_containerbuild()
        builder2 = self.make_containerbuild()
        builder1.execute()
        builder2.execute()
Exemple #2
0
    def test_parallel_invocation_inject_snap(self, mock_is_snap):
        mock_is_snap.side_effect = lambda: True

        fake_snapd = fixture_setup.FakeSnapd()
        self.useFixture(fake_snapd)
        fake_snapd.snaps_result = [
            {
                'name': 'core',
                'confinement': 'strict',
                'id': '2kkitQurgOkL3foImG4wDwn9CIANuHlt',
                'channel': 'stable',
                'revision': '123'
            },
            {
                'name': 'snapcraft',
                'confinement': 'classic',
                'id': '3lljuRvshPlM4gpJnH5xExo0DJBOvImu',
                'channel': 'edge',
                'revision': '345'
            },
        ]

        builder1 = self.make_containerbuild()
        builder2 = self.make_containerbuild()
        builder1.execute()
        builder2.execute()
Exemple #3
0
    def test_inject_snap_existing_container(self, mock_is_snap,
                                            mock_container_run):
        mock_is_snap.return_value = True

        fake_snapd = fixture_setup.FakeSnapd()
        self.useFixture(fake_snapd)
        fake_snapd.snaps_result = [
            {
                "name": "core",
                "confinement": "strict",
                "id": "2kkitQurgOkL3foImG4wDwn9CIANuHlt",
                "channel": "stable",
                "revision": "123",
            },
            {
                "name": "snapcraft",
                "confinement": "classic",
                "id": "3lljuRvshPlM4gpJnH5xExo0DJBOvImu",
                "channel": "edge",
                "revision": "345",
            },
        ]
        # Container was created before, and isn't running
        self.fake_lxd.name = "myremote:snapcraft-project"
        self.fake_lxd.status = "Stopped"

        self.make_containerbuild().execute()

        if hasattr(self, "cross") and self.cross:
            mock_container_run.assert_has_calls([
                call(["snap", "install", "core", "--channel", "stable"]),
                call(["snap", "refresh", "core", "--channel", "stable"]),
                call([
                    "snap",
                    "install",
                    "snapcraft",
                    "--channel",
                    "edge",
                    "--classic",
                ]),
                call([
                    "snap",
                    "refresh",
                    "snapcraft",
                    "--channel",
                    "edge",
                    "--classic",
                ]),
            ])
        else:
            mock_container_run.assert_has_calls([
                call(["snap", "ack", "/run/core_123.assert"]),
                call(["snap", "install", "/run/core_123.snap"]),
                call(["snap", "ack", "/run/snapcraft_345.assert"]),
                call([
                    "snap", "install", "/run/snapcraft_345.snap", "--classic"
                ]),
            ])
Exemple #4
0
    def test_inject_snap(self,
                         mock_is_snap,
                         mock_container_run):
        mock_is_snap.side_effect = lambda: True
        mock_container_run.side_effect = lambda cmd, **kwargs: cmd

        fake_snapd = fixture_setup.FakeSnapd()
        self.useFixture(fake_snapd)
        fake_snapd.snaps_result = [
            {'name': 'core',
             'confinement': 'strict',
             'id': '2kkitQurgOkL3foImG4wDwn9CIANuHlt',
             'channel': 'stable',
             'revision': '123'},
            {'name': 'snapcraft',
             'confinement': 'classic',
             'id': '3lljuRvshPlM4gpJnH5xExo0DJBOvImu',
             'channel': 'edge',
             'revision': '345'},
        ]

        builder = self.make_containerbuild()

        builder.execute()
        if hasattr(self, 'cross') and self.cross:
            mock_container_run.assert_has_calls([
                call(['snap', 'install', 'core', '--channel', 'stable']),
                call(['snap', 'refresh', 'core', '--channel', 'stable']),
                call(['snap', 'install', 'snapcraft', '--channel', 'edge',
                      '--classic']),
                call(['snap', 'refresh', 'snapcraft', '--channel', 'edge',
                      '--classic']),
            ])
            return

        tmp_dir = self.fake_filesystem.tmp_dir
        self.fake_lxd.check_call_mock.assert_has_calls([
            call(['lxc', 'file', 'push',
                  os.path.join(tmp_dir, 'core_123.assert'),
                  '{}/run/core_123.assert'.format(self.fake_lxd.name)]),
            call(['lxc', 'file', 'push',
                  os.path.join(tmp_dir, 'core_123.snap'),
                  '{}/run/core_123.snap'.format(self.fake_lxd.name)]),
            call(['lxc', 'file', 'push',
                  os.path.join(tmp_dir, 'snapcraft_345.assert'),
                  '{}/run/snapcraft_345.assert'.format(self.fake_lxd.name)]),
            call(['lxc', 'file', 'push',
                  os.path.join(tmp_dir, 'snapcraft_345.snap'),
                  '{}/run/snapcraft_345.snap'.format(self.fake_lxd.name)]),
        ])
        mock_container_run.assert_has_calls([
            call(['apt-get', 'install', 'squashfuse', '-y']),
            call(['snap', 'ack', '/run/core_123.assert']),
            call(['snap', 'install', '/run/core_123.snap']),
            call(['snap', 'ack', '/run/snapcraft_345.assert']),
            call(['snap', 'install', '/run/snapcraft_345.snap', '--classic']),
        ])
Exemple #5
0
    def test_inject_snap_no_refresh_running(self, mock_is_snap,
                                            mock_container_run):
        mock_is_snap.return_value = True

        def call_effect(*args, **kwargs):
            if args[0][-3:] == ['snap', 'watch', '--last=auto-refresh']:
                raise CalledProcessError(returncode=1, cmd=args[0])
            return self.fake_lxd.check_output_side_effect()(*args, **kwargs)

        self.fake_lxd.check_call_mock.side_effect = call_effect

        fake_snapd = fixture_setup.FakeSnapd()
        self.useFixture(fake_snapd)
        fake_snapd.snaps_result = [
            {
                'name': 'core',
                'confinement': 'strict',
                'id': '2kkitQurgOkL3foImG4wDwn9CIANuHlt',
                'channel': 'stable',
                'revision': '123'
            },
            {
                'name': 'snapcraft',
                'confinement': 'classic',
                'id': '3lljuRvshPlM4gpJnH5xExo0DJBOvImu',
                'channel': 'edge',
                'revision': '345'
            },
        ]
        # Container was created before, and isn't running
        self.fake_lxd.name = 'myremote:snapcraft-project'
        self.fake_lxd.status = 'Stopped'

        self.make_containerbuild().execute()

        if hasattr(self, 'cross') and self.cross:
            mock_container_run.assert_has_calls([
                call(['snap', 'install', 'core', '--channel', 'stable']),
                call(['snap', 'refresh', 'core', '--channel', 'stable']),
                call([
                    'snap', 'install', 'snapcraft', '--channel', 'edge',
                    '--classic'
                ]),
                call([
                    'snap', 'refresh', 'snapcraft', '--channel', 'edge',
                    '--classic'
                ]),
            ])
        else:
            mock_container_run.assert_has_calls([
                call(['snap', 'ack', '/run/core_123.assert']),
                call(['snap', 'install', '/run/core_123.snap']),
                call(['snap', 'ack', '/run/snapcraft_345.assert']),
                call([
                    'snap', 'install', '/run/snapcraft_345.snap', '--classic'
                ]),
            ])
Exemple #6
0
    def test_inject_snap_api_error(self, mock_is_snap):
        mock_is_snap.side_effect = lambda: True

        fake_snapd = fixture_setup.FakeSnapd()
        fake_snapd.snaps_result = []
        self.useFixture(fake_snapd)

        builder = self.make_containerbuild()
        self.assertIn('Error querying \'core\' snap: not found',
                      str(self.assertRaises(SnapdError, builder.execute)))
Exemple #7
0
    def test_inject_snap_api_error(self, mock_is_snap):
        mock_is_snap.side_effect = lambda: True

        fake_snapd = fixture_setup.FakeSnapd()
        fake_snapd.snaps_result = []
        self.useFixture(fake_snapd)

        builder = self.make_containerbuild()

        raised = self.assertRaises(lxd.errors.ContainerSnapNotFoundError,
                                   builder.execute)
        self.assertThat(raised.snap_name, Equals("core"))
Exemple #8
0
    def setUp(self):
        super().setUp()

        patcher = patch("snapcraft.internal.repo.snaps.get_assertion")
        self.get_assertion_mock = patcher.start()
        self.addCleanup(patcher.stop)

        self.fake_snapd = fixture_setup.FakeSnapd()
        self.useFixture(self.fake_snapd)

        self.registry_filepath = os.path.join(self.path, "registry.yaml")

        self.provider = ProviderImpl(project=get_project(), echoer=lambda x: x)
Exemple #9
0
    def test_inject_apt(self, mock_is_snap, mock_container_run):
        mock_is_snap.side_effect = lambda: False

        fake_snapd = fixture_setup.FakeSnapd()
        self.useFixture(fake_snapd)

        builder = self.make_containerbuild()
        builder.execute()

        mock_container_run.assert_has_calls([
            call(["apt-get", "install", "squashfuse", "-y"]),
            call(["apt-get", "install", "snapcraft", "-y"]),
        ])
Exemple #10
0
    def test_inject_snap_dangerous(self,
                                   mock_is_snap,
                                   mock_container_run,
                                   mock_getuid):
        mock_is_snap.side_effect = lambda: True
        mock_container_run.side_effect = lambda cmd, **kwargs: cmd
        mock_getuid.return_value = 1234

        fake_snapd = fixture_setup.FakeSnapd()
        self.useFixture(fake_snapd)
        fake_snapd.snaps_result = [
            {'name': 'core',
             'confinement': 'strict',
             'id': '2kkitQurgOkL3foImG4wDwn9CIANuHlt',
             'channel': 'stable',
             'revision': '123'},
            {'name': 'snapcraft',
             'confinement': 'classic',
             'id': '',
             'channel': 'edge',
             'revision': 'x1'},
        ]

        builder = self.make_containerbuild()

        builder.execute()
        if hasattr(self, 'cross') and self.cross:
            mock_container_run.assert_has_calls([
                call(['snap', 'install', 'core', '--channel', 'stable']),
                call(['snap', 'refresh', 'core', '--channel', 'stable']),
                call(['snap', 'install', 'snapcraft', '--channel', 'edge',
                      '--classic']),
                call(['snap', 'refresh', 'snapcraft', '--channel', 'edge',
                      '--classic']),
            ])
            return

        tmp_dir = self.fake_filesystem.tmp_dir
        self.fake_lxd.check_call_mock.assert_has_calls([
            call(['sudo', 'cp', '/var/lib/snapd/snaps/snapcraft_x1.snap',
                  os.path.join(tmp_dir, 'snapcraft_x1.snap')]),
            call(['sudo', 'chown', str(os.getuid()),
                  os.path.join(tmp_dir, 'snapcraft_x1.snap')]),
            call(['lxc', 'file', 'push',
                  os.path.join(tmp_dir, 'snapcraft_x1.snap'),
                  '{}/run/snapcraft_x1.snap'.format(self.fake_lxd.name)]),
        ])
        mock_container_run.assert_has_calls([
            call(['snap', 'install', '/run/snapcraft_x1.snap',
                  '--dangerous', '--classic']),
        ])
Exemple #11
0
    def test_inject_snap_existing_container(self, mock_is_snap,
                                            mock_container_run):
        mock_is_snap.return_value = True

        fake_snapd = fixture_setup.FakeSnapd()
        self.useFixture(fake_snapd)
        fake_snapd.snaps_result = [
            {
                'name': 'core',
                'confinement': 'strict',
                'id': '2kkitQurgOkL3foImG4wDwn9CIANuHlt',
                'channel': 'stable',
                'revision': '123'
            },
            {
                'name': 'snapcraft',
                'confinement': 'classic',
                'id': '3lljuRvshPlM4gpJnH5xExo0DJBOvImu',
                'channel': 'edge',
                'revision': '345'
            },
        ]
        # Container was created before, and isn't running
        self.fake_lxd.name = 'myremote:snapcraft-project'
        self.fake_lxd.status = 'Stopped'

        self.make_containerbuild().execute()

        if hasattr(self, 'cross') and self.cross:
            mock_container_run.assert_has_calls([
                call(['snap', 'install', 'core', '--channel', 'stable']),
                call(['snap', 'refresh', 'core', '--channel', 'stable']),
                call([
                    'snap', 'install', 'snapcraft', '--channel', 'edge',
                    '--classic'
                ]),
                call([
                    'snap', 'refresh', 'snapcraft', '--channel', 'edge',
                    '--classic'
                ]),
            ])
        else:
            mock_container_run.assert_has_calls([
                call(['snap', 'ack', '/run/core_123.assert']),
                call(['snap', 'install', '/run/core_123.snap']),
                call(['snap', 'ack', '/run/snapcraft_345.assert']),
                call([
                    'snap', 'install', '/run/snapcraft_345.snap', '--classic'
                ]),
            ])
Exemple #12
0
    def test_inject_socket_error(self, mock_is_snap):
        mock_is_snap.side_effect = lambda: True

        def snap_details(handler_instalce, snap_name):
            raise requests.exceptions.ConnectionError(
                'Connection aborted.',
                FileNotFoundError(2, 'No such file or directory'))

        fake_snapd = fixture_setup.FakeSnapd()
        fake_snapd.snap_details_func = snap_details
        self.useFixture(fake_snapd)

        builder = self.make_containerbuild()
        self.assertIn('Error connecting to',
                      str(self.assertRaises(SnapdError, builder.execute)))
Exemple #13
0
    def test_inject_socket_error(self, mock_is_snap):
        mock_is_snap.side_effect = lambda: True

        def snap_details(handler_instalce, snap_name):
            raise requests.exceptions.ConnectionError(
                "Connection aborted.",
                FileNotFoundError(2, "No such file or directory"))

        fake_snapd = fixture_setup.FakeSnapd()
        fake_snapd.snap_details_func = snap_details
        self.useFixture(fake_snapd)

        builder = self.make_containerbuild()

        raised = self.assertRaises(repo.errors.SnapdConnectionError,
                                   builder.execute)
        self.assertThat(raised.snap_name, Equals("core"))
Exemple #14
0
    def setUp(self):
        super().setUp()

        patcher = mock.patch("snapcraft._get_version", return_value="3.0")
        patcher.start()
        self.addCleanup(patcher.stop)

        original_check_output = subprocess.check_output

        def fake_uname(cmd, *args, **kwargs):
            if "uname" in cmd:
                return b"Linux test uname 4.10 x86_64"
            else:
                return original_check_output(cmd, *args, **kwargs)

        check_output_patcher = mock.patch(
            "subprocess.check_output", side_effect=fake_uname
        )
        check_output_patcher.start()
        self.addCleanup(check_output_patcher.stop)

        original_check_call = subprocess.check_call

        def _fake_dpkg_deb(command, *args, **kwargs):
            if "dpkg-deb" not in command:
                return original_check_call(command, *args, **kwargs)

        check_call_patcher = mock.patch(
            "subprocess.check_call", side_effect=_fake_dpkg_deb
        )
        check_call_patcher.start()
        self.addCleanup(check_call_patcher.stop)

        self.fake_apt_cache = fixture_setup.FakeAptCache()
        self.useFixture(self.fake_apt_cache)
        self.fake_apt_cache.add_package(
            fixture_setup.FakeAptCachePackage("patchelf", "0.9", installed=True)
        )

        self.fake_snapd = fixture_setup.FakeSnapd()
        self.useFixture(self.fake_snapd)
        self.fake_snapd.snaps_result = []

        self.useFixture(FakeOsRelease())
Exemple #15
0
 def setUpClass(cls):
     cls.fake_snapd = fixture_setup.FakeSnapd()
     cls.fake_snapd.setUp()
Exemple #16
0
    def test_inject_snap_already_installed(self, mock_is_snap,
                                           mock_container_run):
        mock_is_snap.side_effect = lambda: True
        mock_container_run.side_effect = lambda cmd, **kwargs: cmd

        def call_effect(*args, **kwargs):
            if args[0][:2] == ["lxc", "exec"]:
                if "readlink" in args[0]:
                    if args[0][-1].endswith("/current"):
                        return "123\n".encode("utf-8")
                if "sha384sum" in args[0]:
                    if args[0][-1].endswith("core_123.snap"):
                        return "deadbeef {}".format(args[0][1]).encode("utf-8")
                    return "abcdef {}".format(args[0][1]).encode("utf-8")
            return default_side_effect(*args, **kwargs)

        default_side_effect = self.fake_lxd.check_output_mock.side_effect
        self.fake_lxd.check_output_mock.side_effect = call_effect

        fake_snapd = fixture_setup.FakeSnapd()
        self.useFixture(fake_snapd)
        fake_snapd.snaps_result = [
            {
                "name": "core",
                "confinement": "strict",
                "id": "2kkitQurgOkL3foImG4wDwn9CIANuHlt",
                "channel": "stable",
                "revision": "123",
            },
            {
                "name": "snapcraft",
                "confinement": "classic",
                "id": "",
                "channel": "edge",
                "revision": "123",
            },
        ]

        builder = self.make_containerbuild()

        builder.execute()
        if hasattr(self, "cross") and self.cross:
            mock_container_run.assert_has_calls([
                call(["snap", "install", "core", "--channel", "stable"]),
                call(["snap", "refresh", "core", "--channel", "stable"]),
                call([
                    "snap",
                    "install",
                    "snapcraft",
                    "--channel",
                    "edge",
                    "--classic",
                ]),
                call([
                    "snap",
                    "refresh",
                    "snapcraft",
                    "--channel",
                    "edge",
                    "--classic",
                ]),
            ])
            return

        tmp_dir = self.fake_filesystem.tmp_dir
        self.fake_lxd.check_call_mock.assert_has_calls([
            call([
                "lxc",
                "file",
                "push",
                os.path.join(tmp_dir, "snapcraft_123.assert"),
                "{}/run/snapcraft_123.assert".format(self.fake_lxd.name),
            ]),
            call([
                "lxc",
                "file",
                "push",
                os.path.join(tmp_dir, "snapcraft_123.snap"),
                "{}/run/snapcraft_123.snap".format(self.fake_lxd.name),
            ]),
        ])
        mock_container_run.assert_has_calls([
            call(["apt-get", "install", "squashfuse", "-y"]),
            call(["snap", "watch", "--last=auto-refresh"]),
            call(["snap", "ack", "/run/snapcraft_123.assert"]),
            call(["snap", "install", "/run/snapcraft_123.snap", "--classic"]),
        ])
Exemple #17
0
    def test_inject_snap_dangerous(self, mock_is_snap, mock_container_run,
                                   mock_getuid):
        mock_is_snap.side_effect = lambda: True
        mock_container_run.side_effect = lambda cmd, **kwargs: cmd
        mock_getuid.return_value = 1234

        fake_snapd = fixture_setup.FakeSnapd()
        self.useFixture(fake_snapd)
        fake_snapd.snaps_result = [
            {
                "name": "core",
                "confinement": "strict",
                "id": "2kkitQurgOkL3foImG4wDwn9CIANuHlt",
                "channel": "stable",
                "revision": "123",
            },
            {
                "name": "snapcraft",
                "confinement": "classic",
                "id": "",
                "channel": "edge",
                "revision": "x1",
            },
        ]

        builder = self.make_containerbuild()

        builder.execute()
        if hasattr(self, "cross") and self.cross:
            mock_container_run.assert_has_calls([
                call(["snap", "install", "core", "--channel", "stable"]),
                call(["snap", "refresh", "core", "--channel", "stable"]),
                call([
                    "snap",
                    "install",
                    "snapcraft",
                    "--channel",
                    "edge",
                    "--classic",
                ]),
                call([
                    "snap",
                    "refresh",
                    "snapcraft",
                    "--channel",
                    "edge",
                    "--classic",
                ]),
            ])
            return

        tmp_dir = self.fake_filesystem.tmp_dir
        self.fake_lxd.check_call_mock.assert_has_calls([
            call([
                "sudo",
                "cp",
                "/var/lib/snapd/snaps/snapcraft_x1.snap",
                os.path.join(tmp_dir, "snapcraft_x1.snap"),
            ]),
            call([
                "sudo",
                "chown",
                str(os.getuid()),
                os.path.join(tmp_dir, "snapcraft_x1.snap"),
            ]),
            call([
                "lxc",
                "file",
                "push",
                os.path.join(tmp_dir, "snapcraft_x1.snap"),
                "{}/run/snapcraft_x1.snap".format(self.fake_lxd.name),
            ]),
        ])
        mock_container_run.assert_has_calls([
            call([
                "snap",
                "install",
                "/run/snapcraft_x1.snap",
                "--dangerous",
                "--classic",
            ])
        ])
Exemple #18
0
    def test_inject_snap(self, mock_is_snap, mock_container_run):
        mock_is_snap.side_effect = lambda: True
        mock_container_run.side_effect = lambda cmd, **kwargs: cmd

        fake_snapd = fixture_setup.FakeSnapd()
        self.useFixture(fake_snapd)
        fake_snapd.snaps_result = [
            {
                "name": "core",
                "confinement": "strict",
                "id": "2kkitQurgOkL3foImG4wDwn9CIANuHlt",
                "channel": "stable",
                "revision": "123",
            },
            {
                "name": "snapcraft",
                "confinement": "classic",
                "id": "3lljuRvshPlM4gpJnH5xExo0DJBOvImu",
                "channel": "edge",
                "revision": "345",
            },
        ]

        builder = self.make_containerbuild()

        builder.execute()
        if hasattr(self, "cross") and self.cross:
            mock_container_run.assert_has_calls([
                call(["snap", "install", "core", "--channel", "stable"]),
                call(["snap", "refresh", "core", "--channel", "stable"]),
                call([
                    "snap",
                    "install",
                    "snapcraft",
                    "--channel",
                    "edge",
                    "--classic",
                ]),
                call([
                    "snap",
                    "refresh",
                    "snapcraft",
                    "--channel",
                    "edge",
                    "--classic",
                ]),
            ])
            return

        tmp_dir = self.fake_filesystem.tmp_dir
        self.fake_lxd.check_call_mock.assert_has_calls([
            call([
                "lxc",
                "file",
                "push",
                os.path.join(tmp_dir, "core_123.assert"),
                "{}/run/core_123.assert".format(self.fake_lxd.name),
            ]),
            call([
                "lxc",
                "file",
                "push",
                os.path.join(tmp_dir, "core_123.snap"),
                "{}/run/core_123.snap".format(self.fake_lxd.name),
            ]),
            call([
                "lxc",
                "file",
                "push",
                os.path.join(tmp_dir, "snapcraft_345.assert"),
                "{}/run/snapcraft_345.assert".format(self.fake_lxd.name),
            ]),
            call([
                "lxc",
                "file",
                "push",
                os.path.join(tmp_dir, "snapcraft_345.snap"),
                "{}/run/snapcraft_345.snap".format(self.fake_lxd.name),
            ]),
        ])
        mock_container_run.assert_has_calls([
            call(["apt-get", "install", "squashfuse", "-y"]),
            call(["snap", "watch", "--last=auto-refresh"]),
            call(["snap", "ack", "/run/core_123.assert"]),
            call(["snap", "install", "/run/core_123.snap"]),
            call(["snap", "ack", "/run/snapcraft_345.assert"]),
            call(["snap", "install", "/run/snapcraft_345.snap", "--classic"]),
        ])
Exemple #19
0
    def test_inject_snap_no_refresh_running(self, mock_is_snap,
                                            mock_container_run):
        mock_is_snap.return_value = True

        def call_effect(*args, **kwargs):
            if args[0][-3:] == ["snap", "watch", "--last=auto-refresh"]:
                raise CalledProcessError(returncode=1, cmd=args[0])
            return self.fake_lxd.check_output_side_effect()(*args, **kwargs)

        self.fake_lxd.check_call_mock.side_effect = call_effect

        fake_snapd = fixture_setup.FakeSnapd()
        self.useFixture(fake_snapd)
        fake_snapd.snaps_result = [
            {
                "name": "core",
                "confinement": "strict",
                "id": "2kkitQurgOkL3foImG4wDwn9CIANuHlt",
                "channel": "stable",
                "revision": "123",
            },
            {
                "name": "snapcraft",
                "confinement": "classic",
                "id": "3lljuRvshPlM4gpJnH5xExo0DJBOvImu",
                "channel": "edge",
                "revision": "345",
            },
        ]
        # Container was created before, and isn't running
        self.fake_lxd.name = "myremote:snapcraft-project"
        self.fake_lxd.status = "Stopped"

        self.make_containerbuild().execute()

        if hasattr(self, "cross") and self.cross:
            mock_container_run.assert_has_calls([
                call(["snap", "install", "core", "--channel", "stable"]),
                call(["snap", "refresh", "core", "--channel", "stable"]),
                call([
                    "snap",
                    "install",
                    "snapcraft",
                    "--channel",
                    "edge",
                    "--classic",
                ]),
                call([
                    "snap",
                    "refresh",
                    "snapcraft",
                    "--channel",
                    "edge",
                    "--classic",
                ]),
            ])
        else:
            mock_container_run.assert_has_calls([
                call(["snap", "ack", "/run/core_123.assert"]),
                call(["snap", "install", "/run/core_123.snap"]),
                call(["snap", "ack", "/run/snapcraft_345.assert"]),
                call([
                    "snap", "install", "/run/snapcraft_345.snap", "--classic"
                ]),
            ])
Exemple #20
0
    def test_inject_snap_already_installed(self, mock_is_snap,
                                           mock_container_run):
        mock_is_snap.side_effect = lambda: True
        mock_container_run.side_effect = lambda cmd, **kwargs: cmd

        def call_effect(*args, **kwargs):
            if args[0][:2] == ['lxc', 'exec']:
                if 'readlink' in args[0]:
                    if args[0][-1].endswith('/current'):
                        return '123\n'.encode('utf-8')
                if 'sha384sum' in args[0]:
                    if args[0][-1].endswith('core_123.snap'):
                        return 'deadbeef {}'.format(args[0][1]).encode('utf-8')
                    return 'abcdef {}'.format(args[0][1]).encode('utf-8')
            return default_side_effect(*args, **kwargs)

        default_side_effect = self.fake_lxd.check_output_mock.side_effect
        self.fake_lxd.check_output_mock.side_effect = call_effect

        fake_snapd = fixture_setup.FakeSnapd()
        self.useFixture(fake_snapd)
        fake_snapd.snaps_result = [
            {
                'name': 'core',
                'confinement': 'strict',
                'id': '2kkitQurgOkL3foImG4wDwn9CIANuHlt',
                'channel': 'stable',
                'revision': '123'
            },
            {
                'name': 'snapcraft',
                'confinement': 'classic',
                'id': '',
                'channel': 'edge',
                'revision': '123'
            },
        ]

        builder = self.make_containerbuild()

        builder.execute()
        if hasattr(self, 'cross') and self.cross:
            mock_container_run.assert_has_calls([
                call(['snap', 'install', 'core', '--channel', 'stable']),
                call(['snap', 'refresh', 'core', '--channel', 'stable']),
                call([
                    'snap', 'install', 'snapcraft', '--channel', 'edge',
                    '--classic'
                ]),
                call([
                    'snap', 'refresh', 'snapcraft', '--channel', 'edge',
                    '--classic'
                ]),
            ])
            return

        tmp_dir = self.fake_filesystem.tmp_dir
        self.fake_lxd.check_call_mock.assert_has_calls([
            call([
                'lxc', 'file', 'push',
                os.path.join(tmp_dir, 'snapcraft_123.assert'),
                '{}/run/snapcraft_123.assert'.format(self.fake_lxd.name)
            ]),
            call([
                'lxc', 'file', 'push',
                os.path.join(tmp_dir, 'snapcraft_123.snap'),
                '{}/run/snapcraft_123.snap'.format(self.fake_lxd.name)
            ]),
        ])
        mock_container_run.assert_has_calls([
            call(['apt-get', 'install', 'squashfuse', '-y']),
            call(['snap', 'ack', '/run/snapcraft_123.assert']),
            call(['snap', 'install', '/run/snapcraft_123.snap', '--classic']),
        ])
Exemple #21
0
    def setUp(self):
        super().setUp()

        self.fake_snapd = fixture_setup.FakeSnapd()
        self.useFixture(self.fake_snapd)