Skip to content

Commit

Permalink
Merge #812
Browse files Browse the repository at this point in the history
812: launch: Clean up instance data if an error occurs during image prep r=townsend2010,ricab a=townsend2010

Fixes #680

Co-authored-by: Chris Townsend <[email protected]>
Co-authored-by: Ricardo Abreu <[email protected]>
  • Loading branch information
3 people committed Jun 17, 2019
1 parent bd2aeed commit 3e2ff13
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/daemon/daemon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1927,6 +1927,9 @@ void mp::Daemon::create_vm(const CreateRequest* request, grpc::ServerWriter<Crea
catch (const std::exception& e)
{
preparing_instances.erase(name);
release_resources(name);
vm_instances.erase(name);
persist_instances();
status_promise->set_value(grpc::Status(grpc::StatusCode::FAILED_PRECONDITION, e.what(), ""));
}

Expand Down
18 changes: 17 additions & 1 deletion tests/test_daemon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
#include <memory>
#include <ostream>
#include <sstream>
#include <stdexcept>
#include <string>

namespace mp = multipass;
Expand Down Expand Up @@ -127,7 +128,7 @@ class TestCreate final : public mp::cmd::Command
create_error.ParseFromString(status.error_details());
const auto errors = create_error.error_codes();

cerr << "fail: ";
cerr << "failed: " << status.error_message();
if (errors.size() == 1)
{
const auto& error = errors[0];
Expand Down Expand Up @@ -398,6 +399,21 @@ TEST_P(DaemonCreateLaunchTestSuite, on_creation_hooks_up_platform_prepare_instan
send_command({GetParam()});
}

TEST_P(DaemonCreateLaunchTestSuite, on_creation_handles_instance_image_preparation_failure)
{
auto mock_factory = use_a_mock_vm_factory();
mp::Daemon daemon{config_builder.build()};

std::string cause = "motive";
EXPECT_CALL(*mock_factory, prepare_instance_image(_, _)).WillOnce(Throw(std::runtime_error{cause}));
EXPECT_CALL(*mock_factory, remove_resources_for(_));

std::stringstream err_stream;
send_command({GetParam()}, trash_stream, err_stream);

EXPECT_THAT(err_stream.str(), AllOf(HasSubstr("failed"), HasSubstr(cause)));
}

TEST_P(DaemonCreateLaunchTestSuite, generates_name_on_creation_when_client_does_not_provide_one)
{
const std::string expected_name{"pied-piper-valley"};
Expand Down

0 comments on commit 3e2ff13

Please sign in to comment.