def _get_container_arch(self):
     info = subprocess.check_output(['lxc', 'info',
                                     self._container_name]).decode('utf-8')
     for line in info.splitlines():
         if line.startswith("Architecture:"):
             return _get_deb_arch(line.split(None, 1)[1].strip())
     raise ContainerError("Could not find architecture for container")
Beispiel #2
0
 def _get_container_arch(self):
     info = subprocess.check_output(['lxc', 'info',
                                     self._container_name]).decode('utf-8')
     for line in info.splitlines():
         if line.startswith("Architecture:"):
             with contextlib.suppress(IndexError, KeyError):
                 return _get_deb_arch(line.split(None, 1)[1].strip())
     raise errors.ContainerArchitectureError(info)
Beispiel #3
0
 def _get_container_arch(self):
     info = subprocess.check_output(["lxc", "info", self._container_name]).decode(
         "utf-8"
     )
     for line in info.splitlines():
         if line.startswith("Architecture:"):
             with contextlib.suppress(IndexError, KeyError):
                 return _get_deb_arch(line.split(None, 1)[1].strip())
     raise errors.ContainerArchitectureError(info)
Beispiel #4
0
    def test_cleanbuild(self, mock_pet, mock_inject, mock_container_run):
        mock_container_run.side_effect = lambda cmd, **kwargs: cmd

        mock_pet.return_value = "my-pet"

        project_folder = "/root/build_project"
        self.make_containerbuild().execute()

        self.assertIn(
            "Waiting for a network connection...\n"
            "Network connection established\n"
            "Setting up container with project assets\n"
            "Retrieved snap.snap\n",
            self.fake_logger.output,
        )

        args = []
        if self.target_arch:
            self.assertIn(
                "Setting target machine to '{}'\n".format(self.target_arch),
                self.fake_logger.output,
            )
            args += ["--target-arch", self.target_arch]

        container_name = "{}:snapcraft-my-pet".format(self.remote)
        self.fake_lxd.check_call_mock.assert_has_calls([
            call(["lxc", "launch", "-e", "ubuntu:xenial", container_name]),
            call([
                "lxc",
                "config",
                "set",
                container_name,
                "environment.SNAPCRAFT_SETUP_CORE",
                "1",
            ]),
            call([
                "lxc",
                "config",
                "set",
                container_name,
                "environment.LC_ALL",
                "C.UTF-8",
            ]),
            call([
                "lxc",
                "config",
                "set",
                container_name,
                "environment.SNAPCRAFT_IMAGE_INFO",
                '{"fingerprint": "test-fingerprint", '
                '"architecture": "test-architecture", '
                '"created_at": "test-created-at"}',
            ]),
            call([
                "lxc",
                "file",
                "push",
                os.path.realpath("project.tar"),
                "{}/root/build_project/project.tar".format(container_name),
            ]),
        ])
        mock_container_run.assert_has_calls([
            call(["python3", "-c", ANY]),
            call(["apt-get", "update"]),
            call(["apt-get", "install", "squashfuse", "-y"]),
            call(["mkdir", project_folder]),
            call(["tar", "xvf", "project.tar"], cwd=project_folder),
            call(
                ["snapcraft", "snap", "--output", "snap.snap", *args],
                cwd=project_folder,
                user="******",
            ),
        ])
        # Ensure there's no unexpected calls eg. two network checks
        self.assertThat(mock_container_run.call_count, Equals(6))
        self.fake_lxd.check_call_mock.assert_has_calls([
            call([
                "lxc",
                "file",
                "pull",
                "{}{}/snap.snap".format(container_name, project_folder),
                "snap.snap",
            ]),
            call(["lxc", "stop", "-f", container_name]),
        ])
        self.fake_lxd.check_output_mock.assert_has_calls([
            call([
                "lxc",
                "image",
                "list",
                "--format=json",
                "ubuntu:xenial/{}".format(_get_deb_arch(self.server)),
            ])
        ])
Beispiel #5
0
    def test_cleanbuild(self, mock_pet, mock_inject, mock_container_run):
        mock_container_run.side_effect = lambda cmd, **kwargs: cmd

        mock_pet.return_value = 'my-pet'

        project_folder = '/root/build_project'
        self.make_containerbuild().execute()

        self.assertIn(
            'Waiting for a network connection...\n'
            'Network connection established\n'
            'Setting up container with project assets\n'
            'Retrieved snap.snap\n', self.fake_logger.output)

        args = []
        if self.target_arch:
            self.assertIn(
                'Setting target machine to \'{}\'\n'.format(self.target_arch),
                self.fake_logger.output)
            args += ['--target-arch', self.target_arch]

        container_name = '{}:snapcraft-my-pet'.format(self.remote)
        self.fake_lxd.check_call_mock.assert_has_calls([
            call(['lxc', 'launch', '-e', 'ubuntu:xenial', container_name]),
            call([
                'lxc', 'config', 'set', container_name,
                'environment.SNAPCRAFT_SETUP_CORE', '1'
            ]),
            call([
                'lxc', 'config', 'set', container_name, 'environment.LC_ALL',
                'C.UTF-8'
            ]),
            call([
                'lxc', 'config', 'set', container_name,
                'environment.SNAPCRAFT_IMAGE_INFO',
                '{"fingerprint": "test-fingerprint", '
                '"architecture": "test-architecture", '
                '"created_at": "test-created-at"}'
            ]),
            call([
                'lxc', 'file', 'push',
                os.path.realpath('project.tar'),
                '{}/root/build_project/project.tar'.format(container_name)
            ]),
        ])
        mock_container_run.assert_has_calls([
            call(['python3', '-c', ANY]),
            call(['apt-get', 'update']),
            call(['apt-get', 'install', 'squashfuse', '-y']),
            call(['mkdir', project_folder]),
            call(['tar', 'xvf', 'project.tar'], cwd=project_folder),
            call(['snapcraft', 'snap', '--output', 'snap.snap', *args],
                 cwd=project_folder,
                 user='******'),
        ])
        # Ensure there's no unexpected calls eg. two network checks
        self.assertThat(mock_container_run.call_count, Equals(6))
        self.fake_lxd.check_call_mock.assert_has_calls([
            call([
                'lxc', 'file', 'pull',
                '{}{}/snap.snap'.format(container_name,
                                        project_folder), 'snap.snap'
            ]),
            call(['lxc', 'stop', '-f', container_name]),
        ])
        self.fake_lxd.check_output_mock.assert_has_calls([
            call([
                'lxc', 'image', 'list', '--format=json',
                'ubuntu:xenial/{}'.format(_get_deb_arch(self.server))
            ]),
        ])
Beispiel #6
0
    def test_cleanbuild(self, mock_pet, mock_inject, mock_container_run):
        mock_container_run.side_effect = lambda cmd, **kwargs: cmd

        mock_pet.return_value = "my-pet"

        project_folder = "/root/build_project"
        self.make_containerbuild().execute()

        self.assertIn(
            "Waiting for a network connection...\n"
            "Network connection established\n"
            "Setting up container with project assets\n"
            "Retrieved snap.snap\n",
            self.fake_logger.output,
        )

        args = []
        if self.target_arch:
            self.assertIn(
                "Setting target machine to '{}'\n".format(self.target_arch),
                self.fake_logger.output,
            )
            args += ["--target-arch", self.target_arch]

        container_name = "{}:snapcraft-my-pet".format(self.remote)
        self.fake_lxd.check_call_mock.assert_has_calls(
            [
                call(["lxc", "launch", "-e", "ubuntu:xenial", container_name]),
                call(
                    [
                        "lxc",
                        "config",
                        "set",
                        container_name,
                        "environment.SNAPCRAFT_SETUP_CORE",
                        "1",
                    ]
                ),
                call(
                    [
                        "lxc",
                        "config",
                        "set",
                        container_name,
                        "environment.SNAPCRAFT_MANAGED_HOST",
                        "yes",
                    ]
                ),
                call(
                    [
                        "lxc",
                        "config",
                        "set",
                        container_name,
                        "environment.LC_ALL",
                        "C.UTF-8",
                    ]
                ),
                call(
                    [
                        "lxc",
                        "config",
                        "set",
                        container_name,
                        "environment.SNAPCRAFT_IMAGE_INFO",
                        '{"fingerprint": "test-fingerprint", '
                        '"architecture": "test-architecture", '
                        '"created_at": "test-created-at"}',
                    ]
                ),
                call(
                    [
                        "lxc",
                        "file",
                        "push",
                        os.path.realpath("project.tar"),
                        "{}/root/build_project/project.tar".format(container_name),
                    ]
                ),
            ]
        )
        mock_container_run.assert_has_calls(
            [
                call(["python3", "-c", ANY]),
                call(["apt-get", "update"]),
                call(["apt-get", "install", "squashfuse", "-y"]),
                call(["mkdir", project_folder]),
                call(["tar", "xvf", "project.tar"], cwd=project_folder),
                call(
                    ["snapcraft", "snap", "--output", "snap.snap", *args],
                    cwd=project_folder,
                    user="******",
                ),
            ]
        )
        # Ensure there's no unexpected calls eg. two network checks
        self.assertThat(mock_container_run.call_count, Equals(6))
        self.fake_lxd.check_call_mock.assert_has_calls(
            [
                call(
                    [
                        "lxc",
                        "file",
                        "pull",
                        "{}{}/snap.snap".format(container_name, project_folder),
                        "snap.snap",
                    ]
                ),
                call(["lxc", "stop", "-f", container_name]),
            ]
        )
        self.fake_lxd.check_output_mock.assert_has_calls(
            [
                call(
                    [
                        "lxc",
                        "image",
                        "list",
                        "--format=json",
                        "ubuntu:xenial/{}".format(_get_deb_arch(self.server)),
                    ]
                )
            ]
        )