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", ])
def test_destroy_when_not_created(self): instance = LXD(project=self.project, echoer=self.echoer_mock) # This call should not fail instance.destroy()