You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mesos.apache.org by vi...@apache.org on 2016/06/17 17:41:57 UTC

[5/9] mesos git commit: Added test case `MasterAPITest.UpdateAndGetMaintenanceSchedule`.

Added test case `MasterAPITest.UpdateAndGetMaintenanceSchedule`.

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


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

Branch: refs/heads/master
Commit: 87c079e979b8fbf04c4ed491f843f92266f3d7da
Parents: b73f0a5
Author: haosdent huang <ha...@gmail.com>
Authored: Fri Jun 17 10:40:45 2016 -0700
Committer: Vinod Kone <vi...@gmail.com>
Committed: Fri Jun 17 10:40:45 2016 -0700

----------------------------------------------------------------------
 src/tests/api_tests.cpp | 98 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 98 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/87c079e9/src/tests/api_tests.cpp
----------------------------------------------------------------------
diff --git a/src/tests/api_tests.cpp b/src/tests/api_tests.cpp
index 03e46cc..de7d1cf 100644
--- a/src/tests/api_tests.cpp
+++ b/src/tests/api_tests.cpp
@@ -32,6 +32,7 @@
 #include <stout/try.hpp>
 
 #include "common/http.hpp"
+#include "common/protobuf_utils.hpp"
 
 #include "master/detector/standalone.hpp"
 
@@ -43,6 +44,10 @@ using mesos::master::detector::StandaloneMasterDetector;
 
 using mesos::internal::slave::Slave;
 
+using mesos::internal::protobuf::maintenance::createSchedule;
+using mesos::internal::protobuf::maintenance::createUnavailability;
+using mesos::internal::protobuf::maintenance::createWindow;
+
 using process::Clock;
 using process::Failure;
 using process::Future;
@@ -90,6 +95,32 @@ public:
         return deserialize<v1::master::Response>(contentType, response.body);
       });
   }
+
+  // Helper for evolving a type by serializing/parsing when the types
+  // have not changed across versions.
+  template <typename T>
+  static T evolve(const google::protobuf::Message& message)
+  {
+    T t;
+
+    string data;
+
+    // NOTE: We need to use 'SerializePartialToString' instead of
+    // 'SerializeToString' because some required fields might not be set
+    // and we don't want an exception to get thrown.
+    CHECK(message.SerializePartialToString(&data))
+      << "Failed to serialize " << message.GetTypeName()
+      << " while evolving to " << t.GetTypeName();
+
+    // NOTE: We need to use 'ParsePartialFromString' instead of
+    // 'ParsePartialFromString' because some required fields might not
+    // be set and we don't want an exception to get thrown.
+    CHECK(t.ParsePartialFromString(data))
+      << "Failed to parse " << t.GetTypeName()
+      << " while evolving from " << message.GetTypeName();
+
+    return t;
+  }
 };
 
 
@@ -291,6 +322,73 @@ TEST_P(MasterAPITest, GetLeadingMaster)
 }
 
 
+// Test updates a maintenance schedule and verifies it saved via query.
+TEST_P(MasterAPITest, UpdateAndGetMaintenanceSchedule)
+{
+  // Set up a master.
+  Try<Owned<cluster::Master>> master = this->StartMaster();
+  ASSERT_SOME(master);
+
+  ContentType contentType = GetParam();
+  process::http::Headers headers = createBasicAuthHeaders(DEFAULT_CREDENTIAL);
+  headers["Accept"] = stringify(contentType);
+
+  // Generate `MachineID`s that can be used in this test.
+  MachineID machine1;
+  MachineID machine2;
+  machine1.set_hostname("Machine1");
+  machine2.set_ip("0.0.0.2");
+
+  // Try to schedule maintenance on an unscheduled machine.
+  maintenance::Schedule schedule = createSchedule(
+      {createWindow({machine1, machine2}, createUnavailability(Clock::now()))});
+  v1::maintenance::Schedule v1Schedule =
+    evolve<v1::maintenance::Schedule>(schedule);
+
+  v1::master::Call v1UpdateScheduleCall;
+  v1UpdateScheduleCall.set_type(v1::master::Call::UPDATE_MAINTENANCE_SCHEDULE);
+  v1::master::Call_UpdateMaintenanceSchedule* maintenanceSchedule =
+    v1UpdateScheduleCall.mutable_update_maintenance_schedule();
+  maintenanceSchedule->mutable_schedule()->CopyFrom(v1Schedule);
+
+  Future<Nothing> v1UpdateScheduleResponse = process::http::post(
+      master.get()->pid,
+      "api/v1",
+      headers,
+      serialize(contentType, v1UpdateScheduleCall),
+      stringify(contentType))
+    .then([contentType](const Response& response) -> Future<Nothing> {
+      if (response.status != OK().status) {
+        return Failure("Unexpected response status " + response.status);
+      }
+      return Nothing();
+    });
+
+  AWAIT_READY(v1UpdateScheduleResponse);
+
+  // Query maintenance schedule.
+  v1::master::Call v1GetScheduleCall;
+  v1GetScheduleCall.set_type(v1::master::Call::GET_MAINTENANCE_SCHEDULE);
+
+  Future<v1::master::Response> v1GetScheduleResponse =
+    post(master.get()->pid, v1GetScheduleCall, contentType);
+
+  AWAIT_READY(v1GetScheduleResponse);
+  ASSERT_TRUE(v1GetScheduleResponse.get().IsInitialized());
+  ASSERT_EQ(
+      v1::master::Response::GET_MAINTENANCE_SCHEDULE,
+      v1GetScheduleResponse.get().type());
+
+  // Verify maintenance schedule matches the expectation.
+  v1::maintenance::Schedule respSchedule =
+    v1GetScheduleResponse.get().get_maintenance_schedule().schedule();
+  ASSERT_EQ(1, respSchedule.windows().size());
+  ASSERT_EQ(2, respSchedule.windows(0).machine_ids().size());
+  ASSERT_EQ("Machine1", respSchedule.windows(0).machine_ids(0).hostname());
+  ASSERT_EQ("0.0.0.2", respSchedule.windows(0).machine_ids(1).ip());
+}
+
+
 class AgentAPITest
   : public MesosTest,
     public WithParamInterface<ContentType>