mesos-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ji...@apache.org
Subject [2/3] mesos git commit: Added validation functions for resource provider calls.
Date Thu, 27 Jul 2017 00:15:31 GMT
Added validation functions for resource provider calls.

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


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

Branch: refs/heads/master
Commit: 7528f595534c9c182ee82468a3e0e8520449283f
Parents: 610ef40
Author: Jan Schlicht <jan@mesosphere.io>
Authored: Wed Jul 26 17:04:48 2017 -0700
Committer: Jie Yu <yujie.jay@gmail.com>
Committed: Wed Jul 26 17:04:48 2017 -0700

----------------------------------------------------------------------
 src/CMakeLists.txt                              |  1 +
 src/Makefile.am                                 |  3 +
 src/internal/devolve.cpp                        |  9 ++-
 src/internal/devolve.hpp                        |  5 +-
 src/resource_provider/validation.cpp            | 73 +++++++++++++++++
 src/resource_provider/validation.hpp            | 39 +++++++++
 src/tests/CMakeLists.txt                        |  1 +
 .../resource_provider_validation_tests.cpp      | 83 ++++++++++++++++++++
 8 files changed, 208 insertions(+), 6 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/7528f595/src/CMakeLists.txt
----------------------------------------------------------------------
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 51b6742..98ccaf4 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -403,6 +403,7 @@ set(RESOURCE_PROVIDER_SRC
   resource_provider/driver.cpp
   resource_provider/local.cpp
   resource_provider/manager.cpp
+  resource_provider/validation.cpp
   resource_provider/storage/provider.cpp
   )
 

http://git-wip-us.apache.org/repos/asf/mesos/blob/7528f595/src/Makefile.am
----------------------------------------------------------------------
diff --git a/src/Makefile.am b/src/Makefile.am
index 8b91716..5712bad 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -956,6 +956,7 @@ libmesos_no_3rdparty_la_SOURCES +=					\
   resource_provider/driver.cpp						\
   resource_provider/local.cpp						\
   resource_provider/manager.cpp						\
+  resource_provider/validation.cpp					\
   resource_provider/storage/provider.cpp				\
   sched/sched.cpp							\
   scheduler/scheduler.cpp						\
@@ -1094,6 +1095,7 @@ libmesos_no_3rdparty_la_SOURCES +=					\
   resource_provider/local.hpp						\
   resource_provider/manager.hpp						\
   resource_provider/message.hpp						\
+  resource_provider/validation.hpp					\
   resource_provider/storage/provider.hpp				\
   sched/constants.hpp							\
   sched/flags.hpp							\
@@ -2357,6 +2359,7 @@ mesos_tests_SOURCES =						\
   tests/reservation_tests.cpp					\
   tests/resource_offers_tests.cpp				\
   tests/resource_provider_http_api_tests.cpp			\
+  tests/resource_provider_validation_tests.cpp			\
   tests/resources_tests.cpp					\
   tests/resources_utils.cpp					\
   tests/role_tests.cpp						\

http://git-wip-us.apache.org/repos/asf/mesos/blob/7528f595/src/internal/devolve.cpp
----------------------------------------------------------------------
diff --git a/src/internal/devolve.cpp b/src/internal/devolve.cpp
index 79c668f..7eb58e9 100644
--- a/src/internal/devolve.cpp
+++ b/src/internal/devolve.cpp
@@ -166,15 +166,16 @@ executor::Event devolve(const v1::executor::Event& event)
 }
 
 
-resource_provider::Call devolve(const v1::resource_provider::Call& call)
+mesos::resource_provider::Call devolve(const v1::resource_provider::Call& call)
 {
-  return devolve<resource_provider::Call>(call);
+  return devolve<mesos::resource_provider::Call>(call);
 }
 
 
-resource_provider::Event devolve(const v1::resource_provider::Event& event)
+mesos::resource_provider::Event devolve(
+    const v1::resource_provider::Event& event)
 {
-  return devolve<resource_provider::Event>(event);
+  return devolve<mesos::resource_provider::Event>(event);
 }
 
 

http://git-wip-us.apache.org/repos/asf/mesos/blob/7528f595/src/internal/devolve.hpp
----------------------------------------------------------------------
diff --git a/src/internal/devolve.hpp b/src/internal/devolve.hpp
index 214fcef..656173d 100644
--- a/src/internal/devolve.hpp
+++ b/src/internal/devolve.hpp
@@ -67,8 +67,9 @@ SlaveInfo devolve(const v1::AgentInfo& agentInfo);
 TaskID devolve(const v1::TaskID& taskId);
 TaskStatus devolve(const v1::TaskStatus& status);
 
-resource_provider::Call devolve(const v1::resource_provider::Call& call);
-resource_provider::Event devolve(const v1::resource_provider::Event& event);
+mesos::resource_provider::Call devolve(const v1::resource_provider::Call& call);
+mesos::resource_provider::Event devolve(
+    const v1::resource_provider::Event& event);
 
 scheduler::Call devolve(const v1::scheduler::Call& call);
 scheduler::Event devolve(const v1::scheduler::Event& event);

http://git-wip-us.apache.org/repos/asf/mesos/blob/7528f595/src/resource_provider/validation.cpp
----------------------------------------------------------------------
diff --git a/src/resource_provider/validation.cpp b/src/resource_provider/validation.cpp
new file mode 100644
index 0000000..d292722
--- /dev/null
+++ b/src/resource_provider/validation.cpp
@@ -0,0 +1,73 @@
+// 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 "resource_provider/validation.hpp"
+
+#include <stout/none.hpp>
+#include <stout/unreachable.hpp>
+
+using mesos::resource_provider::Call;
+
+namespace mesos {
+namespace internal {
+namespace resource_provider {
+namespace validation {
+namespace call {
+
+Option<Error> validate(const Call& call)
+{
+  if (!call.IsInitialized()) {
+    return Error("Not initialized: " + call.InitializationErrorString());
+  }
+
+  if (!call.has_type()) {
+    return Error("Expecting 'type' to be present");
+  }
+
+  switch(call.type()) {
+    case Call::UNKNOWN: {
+      return None();
+    }
+
+    case Call::SUBSCRIBE: {
+      if (!call.has_subscribe()) {
+        return Error("Expecting 'subscribe' to be present");
+      }
+
+      return None();
+    }
+
+    case Call::UPDATE: {
+      if (!call.has_resource_provider_id()) {
+        return Error("Expecting 'resource_provider_id' to be present");
+      }
+
+      if (!call.has_update()) {
+        return Error("Expecting 'update' to be present");
+      }
+
+      return None();
+    }
+  }
+
+  UNREACHABLE();
+}
+
+} // namespace call {
+} // namespace validation {
+} // namespace resource_provider {
+} // namespace internal {
+} // namespace mesos {

http://git-wip-us.apache.org/repos/asf/mesos/blob/7528f595/src/resource_provider/validation.hpp
----------------------------------------------------------------------
diff --git a/src/resource_provider/validation.hpp b/src/resource_provider/validation.hpp
new file mode 100644
index 0000000..0068f56
--- /dev/null
+++ b/src/resource_provider/validation.hpp
@@ -0,0 +1,39 @@
+// 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.
+
+#ifndef __RESOURCE_PROVIDER_VALIDATION_HPP__
+#define __RESOURCE_PROVIDER_VALIDATION_HPP__
+
+#include <mesos/resource_provider/resource_provider.hpp>
+
+#include <stout/error.hpp>
+#include <stout/option.hpp>
+
+namespace mesos {
+namespace internal {
+namespace resource_provider {
+namespace validation {
+namespace call {
+
+Option<Error> validate(const mesos::resource_provider::Call& call);
+
+} // namespace call {
+} // namespace validation {
+} // namespace resource_provider {
+} // namespace internal {
+} // namespace mesos {
+
+#endif // __RESOURCE_PROVIDER_VALIDATION_HPP__

http://git-wip-us.apache.org/repos/asf/mesos/blob/7528f595/src/tests/CMakeLists.txt
----------------------------------------------------------------------
diff --git a/src/tests/CMakeLists.txt b/src/tests/CMakeLists.txt
index f5fe5a6..6dd2716 100644
--- a/src/tests/CMakeLists.txt
+++ b/src/tests/CMakeLists.txt
@@ -116,6 +116,7 @@ set(MESOS_TESTS_SRC
   rate_limiting_tests.cpp
   resource_offers_tests.cpp
   resource_provider_http_api_tests.cpp
+  resource_provider_validation_tests.cpp
   resources_tests.cpp
   role_tests.cpp
   scheduler_driver_tests.cpp

http://git-wip-us.apache.org/repos/asf/mesos/blob/7528f595/src/tests/resource_provider_validation_tests.cpp
----------------------------------------------------------------------
diff --git a/src/tests/resource_provider_validation_tests.cpp b/src/tests/resource_provider_validation_tests.cpp
new file mode 100644
index 0000000..f182bff
--- /dev/null
+++ b/src/tests/resource_provider_validation_tests.cpp
@@ -0,0 +1,83 @@
+// 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 <gtest/gtest.h>
+
+#include <mesos/mesos.hpp>
+
+#include <mesos/resource_provider/resource_provider.hpp>
+
+#include <stout/error.hpp>
+#include <stout/gtest.hpp>
+#include <stout/option.hpp>
+#include <stout/uuid.hpp>
+
+#include "resource_provider/validation.hpp"
+
+namespace call = mesos::internal::resource_provider::validation::call;
+
+using mesos::resource_provider::Call;
+
+namespace mesos {
+namespace internal {
+namespace tests {
+
+TEST(ResourceProviderCallValidationTest, Subscribe)
+{
+  Call call;
+  call.set_type(Call::SUBSCRIBE);
+
+  // Expecting `Call::Subscribe`.
+  Option<Error> error = call::validate(call);
+  EXPECT_SOME(error);
+
+  Call::Subscribe* subscribe = call.mutable_subscribe();
+  ResourceProviderInfo* info = subscribe->mutable_resource_provider_info();
+  info->set_type("org.apache.mesos.rp.test");
+  info->set_name("test");
+
+  error = call::validate(call);
+  EXPECT_NONE(error);
+}
+
+
+TEST(ResourceProviderCallValidationTest, Update)
+{
+  Call call;
+  call.set_type(Call::UPDATE);
+
+  // Expecting a resource provider ID and `Call::Update`.
+  Option<Error> error = call::validate(call);
+  EXPECT_SOME(error);
+
+  ResourceProviderID* id = call.mutable_resource_provider_id();
+  id->set_value(UUID::random().toString());
+
+  // Still expecting `Call::Update`.
+  error = call::validate(call);
+  EXPECT_SOME(error);
+
+  Call::Update* update = call.mutable_update();
+  update->set_state(Call::Update::OK);
+  update->mutable_operation();
+
+  error = call::validate(call);
+  EXPECT_NONE(error);
+}
+
+} // namespace tests {
+} // namespace internal {
+} // namespace mesos {


Mime
View raw message