Пример #1
0
    def test_create_for_type_base(self):
        self.project.info.name = "core18"
        self.project.info.type = "base"
        self.project.info.base = None

        instance = LXD(project=self.project, echoer=self.echoer_mock)
        with mock.patch.object(LXD,
                               "_get_cloud_user_data_string",
                               return_value="fake-cloud"):
            instance.create()

        self.fake_pylxd_client.containers.create_mock.assert_called_once_with(
            config={
                "name": "snapcraft-core18",
                "environment.SNAPCRAFT_BUILD_ENVIRONMENT": "managed-host",
                "raw.idmap": "both 1000 0",
                "source": {
                    "mode": "pull",
                    "type": "image",
                    "server":
                    "https://cloud-images.ubuntu.com/minimal/releases/",
                    "protocol": "simplestreams",
                    "alias": "18.04",
                },
                "environment.SNAPCRAFT_HAS_TTY": "False",
                "user.user-data": "fake-cloud",
            },
            wait=True,
        )
Пример #2
0
    def test_install(self):
        LXD.setup_provider(echoer=self.echoer_mock)

        self.assertThat(self.check_output_mock.call_count, Equals(2))
        self.check_output_mock.assert_has_calls([
            mock.call(["/snap/bin/lxd", "waitready", "--timeout=30"]),
            mock.call(["/snap/bin/lxd", "init", "--auto"]),
        ])
Пример #3
0
    def test_linux(self):
        self.fake_snapd.snaps_result = [
            dict(name="lxd", channel="stable", revision="10")
        ]

        # Thou shall not fail
        with mock.patch("snapcraft.internal.repo.Repo.is_package_installed",
                        return_value=False):
            LXD.ensure_provider()
Пример #4
0
    def setUp(self):
        super().setUp()

        self.instance = LXD(project=self.project, echoer=self.echoer_mock)
        self.instance.create()
        self.fake_container = self.fake_pylxd_client.containers.get(
            self.instance_name)
        # reset for the tests to only be concerned about what they are testing
        self.fake_container._reset_mocks()
        self.fake_pylxd_client.containers.create_mock.reset_mock()
        self.check_call_mock.reset_mock()
Пример #5
0
    def test_create(self):
        instance = LXD(project=self.project, echoer=self.echoer_mock)
        with mock.patch.object(LXD,
                               "_get_cloud_user_data_string",
                               return_value="fake-cloud"):
            instance.create()

        self.fake_pylxd_client.containers.create_mock.assert_called_once_with(
            config={
                "name": "snapcraft-project-name",
                "environment.SNAPCRAFT_BUILD_ENVIRONMENT": "managed-host",
                "raw.idmap": "both 1000 0",
                "source": {
                    "mode": "pull",
                    "type": "image",
                    "server":
                    "https://cloud-images.ubuntu.com/minimal/releases/",
                    "protocol": "simplestreams",
                    "alias": "16.04",
                },
                "environment.SNAPCRAFT_HAS_TTY": "False",
                "user.user-data": "fake-cloud",
            },
            wait=True,
        )

        container = self.fake_pylxd_client.containers.get(self.instance_name)
        container.start_mock.assert_called_once_with(wait=True)
        self.assertThat(container.save_mock.call_count, Equals(2))
        self.assertThat(container.sync_mock.call_count, Equals(3))
        self.assertThat(self.check_call_mock.call_count, Equals(2))
        self.check_call_mock.assert_has_calls([
            mock.call([
                "/snap/bin/lxc",
                "exec",
                self.instance_name,
                "--",
                "env",
                "SNAPCRAFT_HAS_TTY=False",
                "cloud-init",
                "status",
                "--wait",
            ]),
            mock.call([
                "/snap/bin/lxc",
                "exec",
                self.instance_name,
                "--",
                "env",
                "SNAPCRAFT_HAS_TTY=False",
                "snapcraft",
                "refresh",
            ]),
        ])
Пример #6
0
    def test_clean_project(self):
        instance = LXD(project=self.project, echoer=self.echoer_mock)
        instance.create()

        instance.clean_project()

        container = self.fake_pylxd_client.containers.get(self.instance_name)
        container.stop_mock.assert_called_once_with(wait=True)
        container.delete_mock.assert_called_once_with(wait=True)
        self.assertThat(instance.clean_project(), Equals(True))
Пример #7
0
class LXDLaunchedTest(LXDBaseTest):
    def setUp(self):
        super().setUp()

        self.instance = LXD(project=self.project, echoer=self.echoer_mock)
        self.instance.create()
        self.fake_container = self.fake_pylxd_client.containers.get(
            self.instance_name)
        # reset for the tests to only be concerned about what they are testing
        self.fake_container._reset_mocks()
        self.fake_pylxd_client.containers.create_mock.reset_mock()
        self.check_call_mock.reset_mock()

    def test_destroy(self):
        self.instance.destroy()

        self.fake_container.stop_mock.assert_called_once_with(wait=True)

    def test_destroy_and_launch(self):
        self.instance.destroy()

        self.fake_container.stop_mock.assert_called_once_with(wait=True)

        self.instance.create()

        self.fake_pylxd_client.containers.create_mock.assert_not_called()
        self.fake_container.start_mock.assert_called_once_with(wait=True)

    def test_pull_file(self):
        self.instance.pull_file("src.txt", "dest.txt")

        self.fake_container.files_get_mock.assert_called_once_with(
            file_name="src.txt")
        self.fake_container.files_delete_mock.assert_not_called()
        self.assertThat("dest.txt", FileExists())
        self.assertThat("dest.txt", FileContains("fake-pull"))

    def test_pull_file_and_delete(self):
        self.instance.pull_file("src.txt", "dest.txt", delete=True)

        self.fake_container.files_get_mock.assert_called_once_with(
            file_name="src.txt")
        self.fake_container.files_delete_mock.assert_called_once_with(
            file_name="src.txt")
        self.assertThat("dest.txt", FileExists())
        self.assertThat("dest.txt", FileContains("fake-pull"))

    def test_push_file(self):
        with open("src.txt", "w") as f:
            f.write("fake-put")

        self.instance._push_file(source="src.txt", destination="dest.txt")

        self.fake_container.files_put_mock.assert_called_once_with(
            destination="dest.txt", contents=b"fake-put")

    def test_shell(self):
        self.instance.shell()

        self.check_call_mock.assert_called_once_with([
            "/snap/bin/lxc",
            "exec",
            "snapcraft-project-name",
            "--",
            "env",
            "SNAPCRAFT_HAS_TTY=False",
            "/bin/bash",
        ])

    def test_mount_project(self):
        self.check_output_mock.return_value = b"/root"

        self.instance.mount_project()

        self.assertThat(
            self.fake_container.devices,
            Equals({
                "snapcraft-project":
                dict(path="/root/project", source=self.path, type="disk")
            }),
        )
        self.assertThat(self.fake_container.sync_mock.call_count, Equals(2))
        self.fake_container.save_mock.assert_called_once_with(wait=True)
        self.check_output_mock.assert_called_once_with([
            "/snap/bin/lxc",
            "exec",
            self.instance_name,
            "--",
            "env",
            "SNAPCRAFT_HAS_TTY=False",
            "printenv",
            "HOME",
        ])

    def test_mount_prime_directory(self):
        self.check_output_mock.return_value = b"/root"

        self.instance._mount_prime_directory()

        self.assertThat(
            self.fake_container.devices,
            Equals({
                "snapcraft-project-prime":
                dict(
                    path="/root/prime",
                    source=os.path.join(self.path, "prime"),
                    type="disk",
                )
            }),
        )
        self.assertThat(self.fake_container.sync_mock.call_count, Equals(2))
        self.fake_container.save_mock.assert_called_once_with(wait=True)
        self.check_output_mock.assert_called_once_with([
            "/snap/bin/lxc",
            "exec",
            self.instance_name,
            "--",
            "env",
            "SNAPCRAFT_HAS_TTY=False",
            "printenv",
            "HOME",
        ])

    def test_run(self):
        self.instance._run(["ls", "/root/project"])

        self.check_call_mock.assert_called_once_with([
            "/snap/bin/lxc",
            "exec",
            "snapcraft-project-name",
            "--",
            "env",
            "SNAPCRAFT_HAS_TTY=False",
            "ls",
            "/root/project",
        ])
Пример #8
0
 def test_destroy_when_not_created(self):
     instance = LXD(project=self.project, echoer=self.echoer_mock)
     # This call should not fail
     instance.destroy()
Пример #9
0
    def test_create(self):
        instance = LXD(project=self.project, echoer=self.echoer_mock)

        instance.create()

        self.fake_pylxd_client.containers.create_mock.assert_called_once_with(
            config={
                "name": "snapcraft-project-name",
                "environment.SNAPCRAFT_BUILD_ENVIRONMENT": "managed-host",
                "raw.idmap": "both 1000 0",
                "source": {
                    "mode": "pull",
                    "type": "image",
                    "server":
                    "https://cloud-images.ubuntu.com/minimal/releases/",
                    "protocol": "simplestreams",
                    "alias": "16.04",
                },
                "environment.SNAPCRAFT_HAS_TTY": "False",
            },
            wait=True,
        )

        container = self.fake_pylxd_client.containers.get(self.instance_name)
        container.start_mock.assert_called_once_with(wait=True)
        self.assertThat(container.save_mock.call_count, Equals(2))
        self.assertThat(container.sync_mock.call_count, Equals(11))
        self.assertThat(self.check_call_mock.call_count, Equals(8))
        self.check_call_mock.assert_has_calls([
            mock.call([
                "/snap/bin/lxc",
                "exec",
                "snapcraft-project-name",
                "--",
                "env",
                "SNAPCRAFT_HAS_TTY=False",
                "cloud-init",
                "status",
                "--wait",
            ]),
            mock.call([
                "/snap/bin/lxc",
                "exec",
                "snapcraft-project-name",
                "--",
                "env",
                "SNAPCRAFT_HAS_TTY=False",
                "snapcraft",
                "refresh",
            ]),
            mock.call([
                "/snap/bin/lxc",
                "exec",
                "snapcraft-project-name",
                "--",
                "env",
                "SNAPCRAFT_HAS_TTY=False",
                "mv",
                "/tmp/L3Jvb3QvLmJhc2hyYw==",
                "/root/.bashrc",
            ]),
            mock.call([
                "/snap/bin/lxc",
                "exec",
                "snapcraft-project-name",
                "--",
                "env",
                "SNAPCRAFT_HAS_TTY=False",
                "chown",
                "root:root",
                "/root/.bashrc",
            ]),
            mock.call([
                "/snap/bin/lxc",
                "exec",
                "snapcraft-project-name",
                "--",
                "env",
                "SNAPCRAFT_HAS_TTY=False",
                "chmod",
                "0600",
                "/root/.bashrc",
            ]),
            mock.call([
                "/snap/bin/lxc",
                "exec",
                "snapcraft-project-name",
                "--",
                "env",
                "SNAPCRAFT_HAS_TTY=False",
                "mv",
                "/tmp/L2Jpbi9fc25hcGNyYWZ0X3Byb21wdA==",
                "/bin/_snapcraft_prompt",
            ]),
            mock.call([
                "/snap/bin/lxc",
                "exec",
                "snapcraft-project-name",
                "--",
                "env",
                "SNAPCRAFT_HAS_TTY=False",
                "chown",
                "root:root",
                "/bin/_snapcraft_prompt",
            ]),
            mock.call([
                "/snap/bin/lxc",
                "exec",
                "snapcraft-project-name",
                "--",
                "env",
                "SNAPCRAFT_HAS_TTY=False",
                "chmod",
                "0755",
                "/bin/_snapcraft_prompt",
            ]),
        ])