You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mesos.apache.org by ji...@apache.org on 2017/12/07 23:46:02 UTC

[3/3] mesos git commit: Unit tests for storage local resource provider.

Unit tests for storage local resource provider.

This patche adds two unit tests. The first one will register a framework
to issue a `CREATE_VOLUME` then a `DESTROY_VOLUME` call to destroy
an unpublised volume. The second one will register a framework to issue
`CREATE_VOLUME`, `CREATE`, `LAUNCH`, `DESTROY_VOLUME` in sequence to
create a volume and launch a task to use a persistent volume on the CSI
volume, then destroy this published volume.

Review: https://reviews.apache.org/r/63390


Project: http://git-wip-us.apache.org/repos/asf/mesos/repo
Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/ea98a344
Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/ea98a344
Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/ea98a344

Branch: refs/heads/master
Commit: ea98a3440d00a3014a79dc88a0cc122806577de3
Parents: 4e71539
Author: Chun-Hung Hsiao <ch...@mesosphere.io>
Authored: Thu Oct 26 15:52:03 2017 -0700
Committer: Jie Yu <yu...@gmail.com>
Committed: Thu Dec 7 15:07:54 2017 -0800

----------------------------------------------------------------------
 src/Makefile.am                                 |   3 +-
 src/examples/test_csi_plugin.cpp                |  61 ++-
 src/master/validation.cpp                       |  10 +-
 src/tests/mesos.hpp                             |  38 ++
 .../storage_local_resource_provider_tests.cpp   | 530 +++++++++++++++++++
 5 files changed, 620 insertions(+), 22 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/ea98a344/src/Makefile.am
----------------------------------------------------------------------
diff --git a/src/Makefile.am b/src/Makefile.am
index be105f4..f948960 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -2613,7 +2613,8 @@ if ENABLE_GRPC
 mesos_tests_SOURCES +=						\
   tests/csi_client_tests.cpp					\
   tests/mock_csi_plugin.cpp					\
-  tests/mock_csi_plugin.hpp
+  tests/mock_csi_plugin.hpp					\
+  tests/storage_local_resource_provider_tests.cpp
 endif
 
 if ENABLE_SSL

http://git-wip-us.apache.org/repos/asf/mesos/blob/ea98a344/src/examples/test_csi_plugin.cpp
----------------------------------------------------------------------
diff --git a/src/examples/test_csi_plugin.cpp b/src/examples/test_csi_plugin.cpp
index 8ab936b..6359db8 100644
--- a/src/examples/test_csi_plugin.cpp
+++ b/src/examples/test_csi_plugin.cpp
@@ -241,7 +241,7 @@ Status TestCSIPlugin::GetSupportedVersions(
     const csi::GetSupportedVersionsRequest* request,
     csi::GetSupportedVersionsResponse* response)
 {
-  LOG(INFO) << request->GetDescriptor()->name() << " '" << request << "'";
+  LOG(INFO) << request->GetDescriptor()->name() << " '" << *request << "'";
 
   response->add_supported_versions()->CopyFrom(version);
 
@@ -254,7 +254,7 @@ Status TestCSIPlugin::GetPluginInfo(
     const csi::GetPluginInfoRequest* request,
     csi::GetPluginInfoResponse* response)
 {
-  LOG(INFO) << request->GetDescriptor()->name() << " '" << request << "'";
+  LOG(INFO) << request->GetDescriptor()->name() << " '" << *request << "'";
 
   Option<Error> error = validateVersion(request->version());
   if (error.isSome()) {
@@ -273,7 +273,7 @@ Status TestCSIPlugin::CreateVolume(
     const csi::CreateVolumeRequest* request,
     csi::CreateVolumeResponse* response)
 {
-  LOG(INFO) << request->GetDescriptor()->name() << " '" << request << "'";
+  LOG(INFO) << request->GetDescriptor()->name() << " '" << *request << "'";
 
   // TODO(chhsiao): Validate required fields.
 
@@ -337,6 +337,8 @@ Status TestCSIPlugin::CreateVolume(
 
   response->mutable_volume_info()->set_id(volume.id);
   response->mutable_volume_info()->set_capacity_bytes(volume.size.bytes());
+  (*response->mutable_volume_info()->mutable_attributes())["path"] =
+    getVolumePath(volume);
 
   if (alreadyExists) {
     return Status(
@@ -353,7 +355,7 @@ Status TestCSIPlugin::DeleteVolume(
     const csi::DeleteVolumeRequest* request,
     csi::DeleteVolumeResponse* response)
 {
-  LOG(INFO) << request->GetDescriptor()->name() << " '" << request << "'";
+  LOG(INFO) << request->GetDescriptor()->name() << " '" << *request << "'";
 
   // TODO(chhsiao): Validate required fields.
 
@@ -391,7 +393,7 @@ Status TestCSIPlugin::ControllerPublishVolume(
     const csi::ControllerPublishVolumeRequest* request,
     csi::ControllerPublishVolumeResponse* response)
 {
-  LOG(INFO) << request->GetDescriptor()->name() << " '" << request << "'";
+  LOG(INFO) << request->GetDescriptor()->name() << " '" << *request << "'";
 
   // TODO(chhsiao): Validate required fields.
 
@@ -406,6 +408,14 @@ Status TestCSIPlugin::ControllerPublishVolume(
         "Volume '" + request->volume_id() + "' is not found");
   }
 
+  const Volume& volume = volumes.at(request->volume_id());
+  const string path = getVolumePath(volume);
+
+  auto it = request->volume_attributes().find("path");
+  if (it == request->volume_attributes().end() || it->second != path) {
+    return Status(grpc::INVALID_ARGUMENT, "Invalid volume attributes");
+  }
+
   if (request->node_id() != NODE_ID) {
     return Status(
         grpc::NOT_FOUND,
@@ -422,7 +432,7 @@ Status TestCSIPlugin::ControllerUnpublishVolume(
     const csi::ControllerUnpublishVolumeRequest* request,
     csi::ControllerUnpublishVolumeResponse* response)
 {
-  LOG(INFO) << request->GetDescriptor()->name() << " '" << request << "'";
+  LOG(INFO) << request->GetDescriptor()->name() << " '" << *request << "'";
 
   // TODO(chhsiao): Validate required fields.
 
@@ -453,7 +463,7 @@ Status TestCSIPlugin::ValidateVolumeCapabilities(
     const csi::ValidateVolumeCapabilitiesRequest* request,
     csi::ValidateVolumeCapabilitiesResponse* response)
 {
-  LOG(INFO) << request->GetDescriptor()->name() << " '" << request << "'";
+  LOG(INFO) << request->GetDescriptor()->name() << " '" << *request << "'";
 
   // TODO(chhsiao): Validate required fields.
 
@@ -468,6 +478,14 @@ Status TestCSIPlugin::ValidateVolumeCapabilities(
         "Volume '" + request->volume_id() + "' is not found");
   }
 
+  const Volume& volume = volumes.at(request->volume_id());
+  const string path = getVolumePath(volume);
+
+  auto it = request->volume_attributes().find("path");
+  if (it == request->volume_attributes().end() || it->second != path) {
+    return Status(grpc::INVALID_ARGUMENT, "Invalid volume attributes");
+  }
+
   foreach (const csi::VolumeCapability& capability,
            request->volume_capabilities()) {
     if (!capability.has_mount()) {
@@ -497,7 +515,7 @@ Status TestCSIPlugin::ListVolumes(
     const csi::ListVolumesRequest* request,
     csi::ListVolumesResponse* response)
 {
-  LOG(INFO) << request->GetDescriptor()->name() << " '" << request << "'";
+  LOG(INFO) << request->GetDescriptor()->name() << " '" << *request << "'";
 
   Option<Error> error = validateVersion(request->version());
   if (error.isSome()) {
@@ -529,7 +547,7 @@ Status TestCSIPlugin::GetCapacity(
     const csi::GetCapacityRequest* request,
     csi::GetCapacityResponse* response)
 {
-  LOG(INFO) << request->GetDescriptor()->name() << " '" << request << "'";
+  LOG(INFO) << request->GetDescriptor()->name() << " '" << *request << "'";
 
   Option<Error> error = validateVersion(request->version());
   if (error.isSome()) {
@@ -563,7 +581,7 @@ Status TestCSIPlugin::ControllerProbe(
     const csi::ControllerProbeRequest* request,
     csi::ControllerProbeResponse* response)
 {
-  LOG(INFO) << request->GetDescriptor()->name() << " '" << request << "'";
+  LOG(INFO) << request->GetDescriptor()->name() << " '" << *request << "'";
 
   Option<Error> error = validateVersion(request->version());
   if (error.isSome()) {
@@ -579,7 +597,7 @@ Status TestCSIPlugin::ControllerGetCapabilities(
     const csi::ControllerGetCapabilitiesRequest* request,
     csi::ControllerGetCapabilitiesResponse* response)
 {
-  LOG(INFO) << request->GetDescriptor()->name() << " '" << request << "'";
+  LOG(INFO) << request->GetDescriptor()->name() << " '" << *request << "'";
 
   Option<Error> error = validateVersion(request->version());
   if (error.isSome()) {
@@ -604,7 +622,7 @@ Status TestCSIPlugin::NodePublishVolume(
     const csi::NodePublishVolumeRequest* request,
     csi::NodePublishVolumeResponse* response)
 {
-  LOG(INFO) << request->GetDescriptor()->name() << " '" << request << "'";
+  LOG(INFO) << request->GetDescriptor()->name() << " '" << *request << "'";
 
   // TODO(chhsiao): Validate required fields.
 
@@ -619,6 +637,14 @@ Status TestCSIPlugin::NodePublishVolume(
         "Volume '" + request->volume_id() + "' is not found");
   }
 
+  const Volume& volume = volumes.at(request->volume_id());
+  const string path = getVolumePath(volume);
+
+  auto it = request->volume_attributes().find("path");
+  if (it == request->volume_attributes().end() || it->second != path) {
+    return Status(grpc::INVALID_ARGUMENT, "Invalid volume attributes");
+  }
+
   if (!os::exists(request->target_path())) {
     return Status(
         grpc::INVALID_ARGUMENT,
@@ -638,9 +664,6 @@ Status TestCSIPlugin::NodePublishVolume(
     }
   }
 
-  const Volume& volume = volumes.at(request->volume_id());
-  const string path = getVolumePath(volume);
-
   Try<Nothing> mount = fs::mount(
       path,
       request->target_path(),
@@ -678,7 +701,7 @@ Status TestCSIPlugin::NodeUnpublishVolume(
     const csi::NodeUnpublishVolumeRequest* request,
     csi::NodeUnpublishVolumeResponse* response)
 {
-  LOG(INFO) << request->GetDescriptor()->name() << " '" << request << "'";
+  LOG(INFO) << request->GetDescriptor()->name() << " '" << *request << "'";
 
   Option<Error> error = validateVersion(request->version());
   if (error.isSome()) {
@@ -730,7 +753,7 @@ Status TestCSIPlugin::GetNodeID(
     const csi::GetNodeIDRequest* request,
     csi::GetNodeIDResponse* response)
 {
-  LOG(INFO) << request->GetDescriptor()->name() << " '" << request << "'";
+  LOG(INFO) << request->GetDescriptor()->name() << " '" << *request << "'";
 
   Option<Error> error = validateVersion(request->version());
   if (error.isSome()) {
@@ -748,7 +771,7 @@ Status TestCSIPlugin::NodeProbe(
     const csi::NodeProbeRequest* request,
     csi::NodeProbeResponse* response)
 {
-  LOG(INFO) << request->GetDescriptor()->name() << " '" << request << "'";
+  LOG(INFO) << request->GetDescriptor()->name() << " '" << *request << "'";
 
   Option<Error> error = validateVersion(request->version());
   if (error.isSome()) {
@@ -764,7 +787,7 @@ Status TestCSIPlugin::NodeGetCapabilities(
     const csi::NodeGetCapabilitiesRequest* request,
     csi::NodeGetCapabilitiesResponse* response)
 {
-  LOG(INFO) << request->GetDescriptor()->name() << " '" << request << "'";
+  LOG(INFO) << request->GetDescriptor()->name() << " '" << *request << "'";
 
   Option<Error> error = validateVersion(request->version());
   if (error.isSome()) {

http://git-wip-us.apache.org/repos/asf/mesos/blob/ea98a344/src/master/validation.cpp
----------------------------------------------------------------------
diff --git a/src/master/validation.cpp b/src/master/validation.cpp
index bf7ae65..38d9a3c 100644
--- a/src/master/validation.cpp
+++ b/src/master/validation.cpp
@@ -2236,8 +2236,14 @@ Option<Error> validate(
     return Error("Not a persistent volume: " + error.get().message);
   }
 
-  if (!checkpointedResources.contains(volumes)) {
-    return Error("Persistent volumes not found");
+  foreach (const Resource volume, volumes) {
+    if (Resources::hasResourceProvider(volume)) {
+      continue;
+    }
+
+    if (!checkpointedResources.contains(volume)) {
+      return Error("Persistent volumes not found");
+    }
   }
 
   // Ensure the volumes being destroyed are not in use currently.

http://git-wip-us.apache.org/repos/asf/mesos/blob/ea98a344/src/tests/mesos.hpp
----------------------------------------------------------------------
diff --git a/src/tests/mesos.hpp b/src/tests/mesos.hpp
index 657f925..76fbd90 100644
--- a/src/tests/mesos.hpp
+++ b/src/tests/mesos.hpp
@@ -3449,6 +3449,44 @@ void ExpectNoFutureUnionHttpProtobufs(
 }
 
 
+// This matcher is used to match a vector of resource offers that
+// contains an offer having any resource that passes the filter.
+MATCHER_P(OffersHaveAnyResource, filter, "")
+{
+  foreach (const Offer& offer, arg) {
+    foreach (const Resource& resource, offer.resources()) {
+      if (filter(resource)) {
+        return true;
+      }
+    }
+  }
+
+  return false;
+}
+
+
+// This matcher is used to match a vector of resource offers that
+// contains an offer having the specified resource.
+MATCHER_P(OffersHaveResource, resource, "")
+{
+  foreach (const Offer& offer, arg) {
+    Resources resources = offer.resources();
+
+    // If `resource` is not allocated, we are matching offers against
+    // resources constructed from scratch, so we strip off allocations.
+    if (!resource.has_allocation_info()) {
+      resources.unallocate();
+    }
+
+    if (resources.contains(resource)) {
+      return true;
+    }
+  }
+
+  return false;
+}
+
+
 // This matcher is used to match the task id of `TaskStatus` message.
 MATCHER_P(TaskStatusTaskIdEq, taskInfo, "")
 {

http://git-wip-us.apache.org/repos/asf/mesos/blob/ea98a344/src/tests/storage_local_resource_provider_tests.cpp
----------------------------------------------------------------------
diff --git a/src/tests/storage_local_resource_provider_tests.cpp b/src/tests/storage_local_resource_provider_tests.cpp
new file mode 100644
index 0000000..0f0c627
--- /dev/null
+++ b/src/tests/storage_local_resource_provider_tests.cpp
@@ -0,0 +1,530 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include <process/future.hpp>
+#include <process/gtest.hpp>
+#include <process/gmock.hpp>
+
+#include <stout/hashmap.hpp>
+
+#include "tests/flags.hpp"
+#include "tests/mesos.hpp"
+
+using std::shared_ptr;
+using std::string;
+using std::vector;
+
+using mesos::master::detector::MasterDetector;
+
+using process::Future;
+using process::Owned;
+
+using testing::Args;
+using testing::Sequence;
+
+namespace mesos {
+namespace internal {
+namespace tests {
+
+class StorageLocalResourceProviderTest : public MesosTest
+{
+public:
+  virtual void SetUp()
+  {
+    MesosTest::SetUp();
+
+    const string testCsiPluginWorkDir = path::join(sandbox.get(), "test");
+    ASSERT_SOME(os::mkdir(testCsiPluginWorkDir));
+
+    resourceProviderConfigDir =
+      path::join(sandbox.get(), "resource_provider_configs");
+
+    ASSERT_SOME(os::mkdir(resourceProviderConfigDir));
+
+    string testCsiPluginPath =
+      path::join(tests::flags.build_dir, "src", "test-csi-plugin");
+
+    Try<string> resourceProviderConfig = strings::format(
+        R"~(
+        {
+          "type": "org.apache.mesos.rp.local.storage",
+          "name": "test",
+          "default_reservations": [
+            {
+              "type": "STATIC",
+              "role": "storage"
+            }
+          ],
+          "storage": {
+            "plugin": {
+              "type": "org.apache.mesos.csi.test",
+              "name": "slrp_test",
+              "containers": [
+                {
+                  "services": [
+                    "CONTROLLER_SERVICE",
+                    "NODE_SERVICE"
+                  ],
+                  "command": {
+                    "shell": false,
+                    "value": "%s",
+                    "arguments": [
+                      "%s",
+                      "--total_capacity=4GB",
+                      "--work_dir=%s"
+                    ]
+                  }
+                }
+              ]
+            }
+          }
+        }
+        )~",
+        testCsiPluginPath,
+        testCsiPluginPath,
+        testCsiPluginWorkDir);
+
+    ASSERT_SOME(resourceProviderConfig);
+
+    ASSERT_SOME(os::write(
+        path::join(resourceProviderConfigDir, "test.json"),
+        resourceProviderConfig.get()));
+  }
+
+protected:
+  string resourceProviderConfigDir;
+};
+
+
+// This test verifies that a framework can create then destroy a volume
+// from the resources provided by a storage local resource provider that
+// uses the test CSI plugin.
+TEST_F(StorageLocalResourceProviderTest, ROOT_CreateVolumeAndDestroyVolume)
+{
+  Try<Owned<cluster::Master>> master = StartMaster();
+  ASSERT_SOME(master);
+
+  Owned<MasterDetector> detector = master.get()->createDetector();
+
+  slave::Flags flags = CreateSlaveFlags();
+
+  // Disable HTTP authentication to simplify resource provider interactions.
+  flags.authenticate_http_readwrite = false;
+
+  // Set the resource provider capability and other required capabilities.
+  constexpr SlaveInfo::Capability::Type capabilities[] = {
+    SlaveInfo::Capability::MULTI_ROLE,
+    SlaveInfo::Capability::HIERARCHICAL_ROLE,
+    SlaveInfo::Capability::RESERVATION_REFINEMENT,
+    SlaveInfo::Capability::RESOURCE_PROVIDER
+  };
+
+  flags.agent_features = SlaveCapabilities();
+  foreach (SlaveInfo::Capability::Type type, capabilities) {
+    flags.agent_features->add_capabilities()->set_type(type);
+  }
+
+  flags.resource_provider_config_dir = resourceProviderConfigDir;
+
+  Future<SlaveRegisteredMessage> slaveRegisteredMessage =
+    FUTURE_PROTOBUF(SlaveRegisteredMessage(), _, _);
+
+  Try<Owned<cluster::Slave>> slave = StartSlave(detector.get(), flags);
+  ASSERT_SOME(slave);
+
+  AWAIT_READY(slaveRegisteredMessage);
+
+  // Register a framework to exercise offer operations.
+  FrameworkInfo framework = DEFAULT_FRAMEWORK_INFO;
+  framework.set_roles(0, "storage");
+
+  MockScheduler sched;
+  MesosSchedulerDriver driver(
+      &sched, framework, master.get()->pid, DEFAULT_CREDENTIAL);
+
+  // We use the filter explicitly here so that the resources will not
+  // be filtered for 5 seconds (the default).
+  Filters filters;
+  filters.set_refuse_seconds(0);
+
+  EXPECT_CALL(sched, registered(&driver, _, _));
+
+  // The framework is expected to see the following offers in sequence:
+  //   1. One containing a RAW disk resource before `CREATE_VOLUME`.
+  //   2. One containing a MOUNT disk resource after `CREATE_VOLUME`.
+  //   3. One containing a RAW disk resource after `DSTROY_VOLUME`.
+  Future<vector<Offer>> rawDiskOffers;
+  Future<vector<Offer>> volumeCreatedOffers;
+  Future<vector<Offer>> volumeDestroyedOffers;
+
+  Sequence offers;
+
+  auto isSourceType = [](
+      const Resource& r, const Resource::DiskInfo::Source::Type& type) {
+    return r.has_disk() &&
+      r.disk().has_source() &&
+      r.disk().source().type() == type;
+  };
+
+  // Decline offers that contain only the agent's default resources.
+  EXPECT_CALL(sched, resourceOffers(&driver, _))
+    .WillRepeatedly(DeclineOffers());
+
+  EXPECT_CALL(sched, resourceOffers(&driver, OffersHaveAnyResource(
+      std::bind(isSourceType, lambda::_1, Resource::DiskInfo::Source::RAW))))
+    .InSequence(offers)
+    .WillOnce(FutureArg<1>(&rawDiskOffers));
+
+  EXPECT_CALL(sched, resourceOffers(&driver, OffersHaveAnyResource(
+      std::bind(isSourceType, lambda::_1, Resource::DiskInfo::Source::MOUNT))))
+    .InSequence(offers)
+    .WillOnce(FutureArg<1>(&volumeCreatedOffers));
+
+  EXPECT_CALL(sched, resourceOffers(&driver, OffersHaveAnyResource(
+      std::bind(isSourceType, lambda::_1, Resource::DiskInfo::Source::RAW))))
+    .InSequence(offers)
+    .WillOnce(FutureArg<1>(&volumeDestroyedOffers));
+
+  driver.start();
+
+  AWAIT_READY(rawDiskOffers);
+  ASSERT_FALSE(rawDiskOffers->empty());
+
+  Option<Resource> source;
+
+  foreach (const Resource& resource, rawDiskOffers->at(0).resources()) {
+    if (resource.has_disk() &&
+        resource.disk().has_source() &&
+        resource.disk().source().type() == Resource::DiskInfo::Source::RAW) {
+      source = resource;
+      break;
+    }
+  }
+
+  ASSERT_SOME(source);
+
+  // Create a volume.
+  driver.acceptOffers(
+      {rawDiskOffers->at(0).id()},
+      {CREATE_VOLUME(source.get(), Resource::DiskInfo::Source::MOUNT)},
+      filters);
+
+  AWAIT_READY(volumeCreatedOffers);
+  ASSERT_FALSE(volumeCreatedOffers->empty());
+
+  Option<Resource> volume;
+
+  foreach (const Resource& resource, volumeCreatedOffers->at(0).resources()) {
+    if (resource.has_disk() &&
+        resource.disk().has_source() &&
+        resource.disk().source().type() == Resource::DiskInfo::Source::MOUNT) {
+      volume = resource;
+      break;
+    }
+  }
+
+  ASSERT_SOME(volume);
+  ASSERT_TRUE(volume->disk().source().has_id());
+  ASSERT_TRUE(volume->disk().source().has_metadata());
+  ASSERT_TRUE(volume->disk().source().has_mount());
+  ASSERT_TRUE(volume->disk().source().mount().has_root());
+  EXPECT_FALSE(path::absolute(volume->disk().source().mount().root()));
+
+  // Check if the volume is actually created by the test CSI plugin.
+  Option<string> volumePath;
+
+  foreach (const Label& label, volume->disk().source().metadata().labels()) {
+    if (label.key() == "path") {
+      volumePath = label.value();
+      break;
+    }
+  }
+
+  ASSERT_SOME(volumePath);
+  EXPECT_TRUE(os::exists(volumePath.get()));
+
+  // Destroy the created volume.
+  driver.acceptOffers(
+      {volumeCreatedOffers->at(0).id()},
+      {DESTROY_VOLUME(volume.get())},
+      filters);
+
+  AWAIT_READY(volumeDestroyedOffers);
+  ASSERT_FALSE(volumeDestroyedOffers->empty());
+
+  Option<Resource> destroyed;
+
+  foreach (const Resource& resource, volumeDestroyedOffers->at(0).resources()) {
+    if (resource.has_disk() &&
+        resource.disk().has_source() &&
+        resource.disk().source().type() == Resource::DiskInfo::Source::RAW) {
+      destroyed = resource;
+      break;
+    }
+  }
+
+  ASSERT_SOME(destroyed);
+  ASSERT_FALSE(destroyed->disk().source().has_id());
+  ASSERT_FALSE(destroyed->disk().source().has_metadata());
+  ASSERT_FALSE(destroyed->disk().source().has_mount());
+
+  // Check if the volume is actually deleted by the test CSI plugin.
+  EXPECT_FALSE(os::exists(volumePath.get()));
+}
+
+
+// This test verifies that a framework can launch a task using a created
+// volume provided by a storage local resource provider that uses the
+// test CSI plugin, then destroy the volume while it is published.
+TEST_F(StorageLocalResourceProviderTest, ROOT_LaunchAndDestroyVolume)
+{
+  Try<Owned<cluster::Master>> master = StartMaster();
+  ASSERT_SOME(master);
+
+  Owned<MasterDetector> detector = master.get()->createDetector();
+
+  slave::Flags flags = CreateSlaveFlags();
+
+  // Disable HTTP authentication to simplify resource provider interactions.
+  flags.authenticate_http_readwrite = false;
+
+  // Set the resource provider capability and other required capabilities.
+  constexpr SlaveInfo::Capability::Type capabilities[] = {
+    SlaveInfo::Capability::MULTI_ROLE,
+    SlaveInfo::Capability::HIERARCHICAL_ROLE,
+    SlaveInfo::Capability::RESERVATION_REFINEMENT,
+    SlaveInfo::Capability::RESOURCE_PROVIDER
+  };
+
+  flags.agent_features = SlaveCapabilities();
+  foreach (SlaveInfo::Capability::Type type, capabilities) {
+    flags.agent_features->add_capabilities()->set_type(type);
+  }
+
+  flags.resource_provider_config_dir = resourceProviderConfigDir;
+
+  Future<SlaveRegisteredMessage> slaveRegisteredMessage =
+    FUTURE_PROTOBUF(SlaveRegisteredMessage(), _, _);
+
+  Try<Owned<cluster::Slave>> slave = StartSlave(detector.get(), flags);
+  ASSERT_SOME(slave);
+
+  AWAIT_READY(slaveRegisteredMessage);
+
+  // Register a framework to exercise offer operations.
+  FrameworkInfo framework = DEFAULT_FRAMEWORK_INFO;
+  framework.set_roles(0, "storage");
+
+  MockScheduler sched;
+  MesosSchedulerDriver driver(
+      &sched, framework, master.get()->pid, DEFAULT_CREDENTIAL);
+
+  // We use the filter explicitly here so that the resources will not
+  // be filtered for 5 seconds (the default).
+  Filters filters;
+  filters.set_refuse_seconds(0);
+
+  EXPECT_CALL(sched, registered(&driver, _, _));
+
+  // The framework is expected to see the following offers in sequence:
+  //   1. One containing a RAW disk resource before `CREATE_VOLUME`.
+  //   2. One containing a MOUNT disk resource after `CREATE_VOLUME`.
+  //   3. One containing a persistent volume after `CREATE`.
+  //   4. One containing the same persistent volume after `LAUNCH`.
+  //   5. One containing the same MOUNT disk resource after `DESTROY`.
+  //   6. One containing the same RAW disk resource after `DESTROY_VOLUME`.
+  //
+  // We set up the expectations for these offers as the test progresses.
+  Future<vector<Offer>> rawDiskOffers;
+  Future<vector<Offer>> volumeCreatedOffers;
+  Future<vector<Offer>> persistenceCreatedOffers;
+  Future<vector<Offer>> taskFinishedOffers;
+  Future<vector<Offer>> persistenceDestroyedOffers;
+  Future<vector<Offer>> volumeDestroyedOffers;
+
+  Sequence offers;
+
+  auto isSourceType = [](
+      const Resource& r, const Resource::DiskInfo::Source::Type& type) {
+    return r.has_disk() &&
+      r.disk().has_source() &&
+      r.disk().source().type() == type;
+  };
+
+  // Decline offers that contain only the agent's default resources.
+  EXPECT_CALL(sched, resourceOffers(&driver, _))
+    .WillRepeatedly(DeclineOffers());
+
+  EXPECT_CALL(sched, resourceOffers(&driver, OffersHaveAnyResource(
+      std::bind(isSourceType, lambda::_1, Resource::DiskInfo::Source::RAW))))
+    .InSequence(offers)
+    .WillOnce(FutureArg<1>(&rawDiskOffers));
+
+  driver.start();
+
+  AWAIT_READY(rawDiskOffers);
+  ASSERT_FALSE(rawDiskOffers->empty());
+
+  Option<Resource> source;
+
+  foreach (const Resource& resource, rawDiskOffers->at(0).resources()) {
+    if (resource.has_disk() &&
+        resource.disk().has_source() &&
+        resource.disk().source().type() == Resource::DiskInfo::Source::RAW) {
+      source = resource;
+      break;
+    }
+  }
+
+  ASSERT_SOME(source);
+
+  // Create a volume.
+  EXPECT_CALL(sched, resourceOffers(&driver, OffersHaveAnyResource(
+      std::bind(isSourceType, lambda::_1, Resource::DiskInfo::Source::MOUNT))))
+    .InSequence(offers)
+    .WillOnce(FutureArg<1>(&volumeCreatedOffers));
+
+  driver.acceptOffers(
+      {rawDiskOffers->at(0).id()},
+      {CREATE_VOLUME(source.get(), Resource::DiskInfo::Source::MOUNT)},
+      filters);
+
+  AWAIT_READY(volumeCreatedOffers);
+  ASSERT_FALSE(volumeCreatedOffers->empty());
+
+  Option<Resource> volume;
+
+  foreach (const Resource& resource, volumeCreatedOffers->at(0).resources()) {
+    if (resource.has_disk() &&
+        resource.disk().has_source() &&
+        resource.disk().source().type() == Resource::DiskInfo::Source::MOUNT) {
+      volume = resource;
+      break;
+    }
+  }
+
+  ASSERT_SOME(volume);
+  ASSERT_TRUE(volume->disk().source().has_id());
+  ASSERT_TRUE(volume->disk().source().has_metadata());
+  ASSERT_TRUE(volume->disk().source().has_mount());
+  ASSERT_TRUE(volume->disk().source().mount().has_root());
+  EXPECT_FALSE(path::absolute(volume->disk().source().mount().root()));
+
+  // Check if the volume is actually created by the test CSI plugin.
+  Option<string> volumePath;
+
+  foreach (const Label& label, volume->disk().source().metadata().labels()) {
+    if (label.key() == "path") {
+      volumePath = label.value();
+      break;
+    }
+  }
+
+  ASSERT_SOME(volumePath);
+  EXPECT_TRUE(os::exists(volumePath.get()));
+
+  // Put a file into the volume.
+  ASSERT_SOME(os::touch(path::join(volumePath.get(), "file")));
+
+  // Create a persistent volume on the CSI volume.
+  Resource persistentVolume = volume.get();
+  persistentVolume.mutable_disk()->mutable_persistence()
+    ->set_id(UUID::random().toString());
+  persistentVolume.mutable_disk()->mutable_persistence()
+    ->set_principal(framework.principal());
+  persistentVolume.mutable_disk()->mutable_volume()
+    ->set_container_path("volume");
+  persistentVolume.mutable_disk()->mutable_volume()->set_mode(Volume::RW);
+
+  EXPECT_CALL(sched, resourceOffers(&driver, OffersHaveResource(
+      persistentVolume)))
+    .InSequence(offers)
+    .WillOnce(FutureArg<1>(&persistenceCreatedOffers));
+
+  driver.acceptOffers(
+      {volumeCreatedOffers->at(0).id()},
+      {CREATE(persistentVolume)},
+      filters);
+
+  AWAIT_READY(persistenceCreatedOffers);
+  ASSERT_FALSE(persistenceCreatedOffers->empty());
+
+  // Launch a task to use the persistent volume.
+  Future<TaskStatus> taskStarting;
+  Future<TaskStatus> taskRunning;
+  Future<TaskStatus> taskFinished;
+
+  EXPECT_CALL(sched, statusUpdate(&driver, _))
+    .WillOnce(FutureArg<1>(&taskStarting))
+    .WillOnce(FutureArg<1>(&taskRunning))
+    .WillOnce(FutureArg<1>(&taskFinished));
+
+  EXPECT_CALL(sched, resourceOffers(&driver, OffersHaveResource(
+      persistentVolume)))
+    .InSequence(offers)
+    .WillOnce(FutureArg<1>(&taskFinishedOffers));
+
+  driver.acceptOffers(
+      {persistenceCreatedOffers->at(0).id()},
+      {LAUNCH({createTask(
+           persistenceCreatedOffers->at(0).slave_id(),
+           persistentVolume,
+           createCommandInfo("test -f " + path::join("volume", "file")))})},
+      filters);
+
+  AWAIT_READY(taskStarting);
+  EXPECT_EQ(TASK_STARTING, taskStarting->state());
+
+  AWAIT_READY(taskRunning);
+  EXPECT_EQ(TASK_RUNNING, taskRunning->state());
+
+  AWAIT_READY(taskFinished);
+  EXPECT_EQ(TASK_FINISHED, taskFinished->state());
+
+  AWAIT_READY(taskFinishedOffers);
+
+  // Destroy the persistent volume on the CSI volume.
+  EXPECT_CALL(sched, resourceOffers(&driver, OffersHaveResource(volume.get())))
+    .InSequence(offers)
+    .WillOnce(FutureArg<1>(&persistenceDestroyedOffers));
+
+  driver.acceptOffers(
+      {taskFinishedOffers->at(0).id()},
+      {DESTROY(persistentVolume)},
+      filters);
+
+  AWAIT_READY(persistenceDestroyedOffers);
+
+  // Destroy the created volume.
+  EXPECT_CALL(sched, resourceOffers(&driver, OffersHaveResource(source.get())))
+    .InSequence(offers)
+    .WillOnce(FutureArg<1>(&volumeDestroyedOffers));
+
+  driver.acceptOffers(
+      {persistenceDestroyedOffers->at(0).id()},
+      {DESTROY_VOLUME(volume.get())},
+      filters);
+
+  AWAIT_READY(volumeDestroyedOffers);
+  ASSERT_FALSE(volumeDestroyedOffers->empty());
+
+  // Check if the volume is actually deleted by the test CSI plugin.
+  EXPECT_FALSE(os::exists(volumePath.get()));
+}
+
+} // namespace tests {
+} // namespace internal {
+} // namespace mesos {