Fix failing test: SlaveTest.ROOT_RunTaskWithCommandInfoWithUser. Review: https://reviews.apache.org/r/35728
Project: http://git-wip-us.apache.org/repos/asf/mesos/repo Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/3d2dec44 Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/3d2dec44 Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/3d2dec44 Branch: refs/heads/master Commit: 3d2dec44d2476e216a3b735868f7bcee9917f5e2 Parents: b8007aa Author: haosdent huang <[email protected]> Authored: Mon Jun 29 01:59:03 2015 -0700 Committer: Adam B <[email protected]> Committed: Mon Jun 29 03:33:11 2015 -0700 ---------------------------------------------------------------------- src/tests/slave_tests.cpp | 77 +++++++++++++++++++++++++++++++++++------- 1 file changed, 65 insertions(+), 12 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/mesos/blob/3d2dec44/src/tests/slave_tests.cpp ---------------------------------------------------------------------- diff --git a/src/tests/slave_tests.cpp b/src/tests/slave_tests.cpp index e9002e8..036a0c2 100644 --- a/src/tests/slave_tests.cpp +++ b/src/tests/slave_tests.cpp @@ -668,10 +668,10 @@ TEST_F(SlaveTest, ROOT_RunTaskWithCommandInfoWithoutUser) // This test runs a command _with_ the command user field set. The // command will verify the assumption that the command is run as the -// specified user. We use (and assume the precense) of the +// specified user. We use (and assume the presence) of the // unprivileged 'nobody' user which should be available on both Linux // and Mac OS X. -TEST_F(SlaveTest, DISABLED_ROOT_RunTaskWithCommandInfoWithUser) +TEST_F(SlaveTest, ROOT_RunTaskWithCommandInfoWithUser) { // TODO(nnielsen): Introduce STOUT abstraction for user verification // instead of flat getpwnam call. @@ -705,6 +705,11 @@ TEST_F(SlaveTest, DISABLED_ROOT_RunTaskWithCommandInfoWithUser) EXPECT_CALL(sched, registered(&driver, _, _)) .Times(1); + Future<TaskStatus> statusRunning; + Future<TaskStatus> statusFinished; + const string helper = + path::join(tests::flags.build_dir, "src", "active-user-test-helper"); + Future<vector<Offer>> offers; EXPECT_CALL(sched, resourceOffers(&driver, _)) .WillOnce(FutureArg<1>(&offers)) @@ -715,15 +720,66 @@ TEST_F(SlaveTest, DISABLED_ROOT_RunTaskWithCommandInfoWithUser) AWAIT_READY(offers); EXPECT_NE(0u, offers.get().size()); + // HACK: Launch a prepare task as root to prepare the binaries. + // This task creates the lt-mesos-executor binary in the build dir. + // Because the real task is run as a test user (nobody), it does not + // have permission to create files in the build directory. + TaskInfo prepareTask; + prepareTask.set_name("prepare task"); + prepareTask.mutable_task_id()->set_value("1"); + prepareTask.mutable_slave_id()->CopyFrom(offers.get()[0].slave_id()); + prepareTask.mutable_resources()->CopyFrom( + offers.get()[0].resources()); + + Result<string> user = os::user(); + CHECK_SOME(user) << "Failed to get current user name" + << (user.isError() ? ": " + user.error() : ""); + // Current user should be root. + EXPECT_EQ("root", user.get()); + + // This prepare command executor will run as the current user + // running the tests (root). After this command executor finishes, + // we know that the lt-mesos-executor binary file exists. + CommandInfo prepareCommand; + prepareCommand.set_shell(false); + prepareCommand.set_value(helper); + prepareCommand.add_arguments(helper); + prepareCommand.add_arguments(user.get()); + prepareTask.mutable_command()->CopyFrom(prepareCommand); + + vector<TaskInfo> prepareTasks; + prepareTasks.push_back(prepareTask); + + EXPECT_CALL(sched, statusUpdate(&driver, _)) + .WillOnce(FutureArg<1>(&statusRunning)) + .WillOnce(FutureArg<1>(&statusFinished)); + + driver.launchTasks(offers.get()[0].id(), prepareTasks); + + // Scheduler should first receive TASK_RUNNING followed by the + // TASK_FINISHED from the executor. + AWAIT_READY(statusRunning); + EXPECT_EQ(TASK_RUNNING, statusRunning.get().state()); + EXPECT_EQ(TaskStatus::SOURCE_EXECUTOR, statusRunning.get().source()); + + AWAIT_READY(statusFinished); + EXPECT_EQ(TASK_FINISHED, statusFinished.get().state()); + EXPECT_EQ(TaskStatus::SOURCE_EXECUTOR, statusFinished.get().source()); + + // Start to launch a task with different user. + EXPECT_CALL(sched, resourceOffers(&driver, _)) + .WillOnce(FutureArg<1>(&offers)) + .WillRepeatedly(Return()); // Ignore subsequent offers. + + AWAIT_READY(offers); + EXPECT_NE(0u, offers.get().size()); + // Launch a task with the command executor. TaskInfo task; task.set_name(""); - task.mutable_task_id()->set_value("1"); - task.mutable_slave_id()->MergeFrom(offers.get()[0].slave_id()); - task.mutable_resources()->MergeFrom(offers.get()[0].resources()); - - const string helper = - path::join(tests::flags.build_dir, "src", "active-user-test-helper"); + task.mutable_task_id()->set_value("2"); + task.mutable_slave_id()->CopyFrom(offers.get()[0].slave_id()); + task.mutable_resources()->CopyFrom(offers.get()[0].resources()); CommandInfo command; command.set_user(testUser); @@ -732,13 +788,10 @@ TEST_F(SlaveTest, DISABLED_ROOT_RunTaskWithCommandInfoWithUser) command.add_arguments(helper); command.add_arguments(testUser); - task.mutable_command()->MergeFrom(command); - + task.mutable_command()->CopyFrom(command); vector<TaskInfo> tasks; tasks.push_back(task); - Future<TaskStatus> statusRunning; - Future<TaskStatus> statusFinished; EXPECT_CALL(sched, statusUpdate(&driver, _)) .WillOnce(FutureArg<1>(&statusRunning)) .WillOnce(FutureArg<1>(&statusFinished));
