src/tests/slave_tests.cpp
TEST_F(SlaveTest, ROOT_RunTaskWithCommandInfoWithoutUser)
{
  Try<Owned<cluster::Master>> master = StartMaster();
  ASSERT_SOME(master);

  // Need flags for 'executor_registration_timeout'.
  slave::Flags flags = CreateSlaveFlags();
  flags.isolation = "posix/cpu,posix/mem";

  Fetcher fetcher;

  Try<MesosContainerizer*> _containerizer =
    MesosContainerizer::create(flags, false, &fetcher);

  CHECK_SOME(_containerizer);
  Owned<MesosContainerizer> containerizer(_containerizer.get());

  Owned<MasterDetector> detector = master.get()->createDetector();

  Try<Owned<cluster::Slave>> slave =
    StartSlave(detector.get(), containerizer.get());
  ASSERT_SOME(slave);

  MockScheduler sched;
  MesosSchedulerDriver driver(
      &sched, DEFAULT_FRAMEWORK_INFO, master.get()->pid, DEFAULT_CREDENTIAL);

  EXPECT_CALL(sched, registered(&driver, _, _))
    .Times(1);

  Future<vector<Offer>> offers;
  EXPECT_CALL(sched, resourceOffers(&driver, _))
    .WillOnce(FutureArg<1>(&offers))
    .WillRepeatedly(Return()); // Ignore subsequent offers.

  driver.start();

  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());

  Result<string> user = os::user();
  CHECK_SOME(user) << "Failed to get current user name"
                   << (user.isError() ? ": " + user.error() : "");

  const string helper = getTestHelperPath("active-user-test-helper");

  // Command executor will run as user running test.
  CommandInfo command;
  command.set_shell(false);
  command.set_value(helper);
  command.add_arguments(helper);
  command.add_arguments(user.get());

  task.mutable_command()->MergeFrom(command);

  Future<TaskStatus> statusRunning;
  Future<TaskStatus> statusFinished;
  EXPECT_CALL(sched, statusUpdate(&driver, _))
    .WillOnce(FutureArg<1>(&statusRunning))
    .WillOnce(FutureArg<1>(&statusFinished));

  driver.launchTasks(offers.get()[0].id(), {task});

  // 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());

  driver.stop();
  driver.join();
}
// This test runs a command without the command user field set. The
// command will verify the assumption that the command is run as the
// slave user (in this case, root).
This test runs a command without the command user field set. The
command will verify the assumption that the command is run as the
slave user (in this case, root).
This test runs a command without the command user field set . The command will verify the assumption that the command is run as the slave user ( in this case root ) .
TEST_F(SlaveTest, ROOT_RunTaskWithCommandInfoWithoutUser)
{
  Try<Owned<cluster::Master>> master = StartMaster();
  ASSERT_SOME(master);
  slave::Flags flags = CreateSlaveFlags();
  flags.isolation = "posix/cpu,posix/mem";
  Fetcher fetcher;
  Try<MesosContainerizer*> _containerizer =
    MesosContainerizer::create(flags, false, &fetcher);
  CHECK_SOME(_containerizer);
  Owned<MesosContainerizer> containerizer(_containerizer.get());
  Owned<MasterDetector> detector = master.get()->createDetector();
  Try<Owned<cluster::Slave>> slave =
    StartSlave(detector.get(), containerizer.get());
  ASSERT_SOME(slave);
  MockScheduler sched;
  MesosSchedulerDriver driver(
      &sched, DEFAULT_FRAMEWORK_INFO, master.get()->pid, DEFAULT_CREDENTIAL);
  EXPECT_CALL(sched, registered(&driver, _, _))
    .Times(1);
  Future<vector<Offer>> offers;
  EXPECT_CALL(sched, resourceOffers(&driver, _))
    .WillOnce(FutureArg<1>(&offers))
    .WillRepeatedly(Return()); 
  driver.start();
  AWAIT_READY(offers);
  EXPECT_NE(0u, offers.get().size());
  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());
  Result<string> user = os::user();
  CHECK_SOME(user) << "Failed to get current user name"
                   << (user.isError() ? ": " + user.error() : "");
  const string helper = getTestHelperPath("active-user-test-helper");
  CommandInfo command;
  command.set_shell(false);
  command.set_value(helper);
  command.add_arguments(helper);
  command.add_arguments(user.get());
  task.mutable_command()->MergeFrom(command);
  Future<TaskStatus> statusRunning;
  Future<TaskStatus> statusFinished;
  EXPECT_CALL(sched, statusUpdate(&driver, _))
    .WillOnce(FutureArg<1>(&statusRunning))
    .WillOnce(FutureArg<1>(&statusFinished));
  driver.launchTasks(offers.get()[0].id(), {task});
  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());
  driver.stop();
  driver.join();
}
TEST_F ( SlaveTest , ROOT_RunTaskWithCommandInfoWithoutUser ) { Try < Owned < cluster :: Master > > master = StartMaster ( ) ; ASSERT_SOME ( master ) ; slave :: Flags flags = CreateSlaveFlags ( ) ; flags . isolation = " " ; Fetcher fetcher ; Try < MesosContainerizer * > _containerizer = MesosContainerizer :: create ( flags , false , & fetcher ) ; CHECK_SOME ( _containerizer ) ; Owned < MesosContainerizer > containerizer ( _containerizer . get ( ) ) ; Owned < MasterDetector > detector = master . get ( ) -> createDetector ( ) ; Try < Owned < cluster :: Slave > > slave = StartSlave ( detector . get ( ) , containerizer . get ( ) ) ; ASSERT_SOME ( slave ) ; MockScheduler sched ; MesosSchedulerDriver driver ( & sched , DEFAULT_FRAMEWORK_INFO , master . get ( ) -> pid , DEFAULT_CREDENTIAL ) ; EXPECT_CALL ( sched , registered ( & driver , _ , _ ) ) . Times ( 1 ) ; Future < vector < Offer > > offers ; EXPECT_CALL ( sched , resourceOffers ( & driver , _ ) ) . WillOnce ( FutureArg < 1 > ( & offers ) ) . WillRepeatedly ( Return ( ) ) ; driver . start ( ) ; AWAIT_READY ( offers ) ; EXPECT_NE ( 0u , offers . get ( ) . size ( ) ) ; TaskInfo task ; task . set_name ( " " ) ; task . mutable_task_id ( ) -> set_value ( " " ) ; task . mutable_slave_id ( ) -> MergeFrom ( offers . get ( ) [ 0 ] . slave_id ( ) ) ; task . mutable_resources ( ) -> MergeFrom ( offers . get ( ) [ 0 ] . resources ( ) ) ; Result < string > user = os :: user ( ) ; CHECK_SOME ( user ) << " " << ( user . isError ( ) ? " " + user . error ( ) : " " ) ; const string helper = getTestHelperPath ( " " ) ; CommandInfo command ; command . set_shell ( false ) ; command . set_value ( helper ) ; command . add_arguments ( helper ) ; command . add_arguments ( user . get ( ) ) ; task . mutable_command ( ) -> MergeFrom ( command ) ; Future < TaskStatus > statusRunning ; Future < TaskStatus > statusFinished ; EXPECT_CALL ( sched , statusUpdate ( & driver , _ ) ) . WillOnce ( FutureArg < 1 > ( & statusRunning ) ) . WillOnce ( FutureArg < 1 > ( & statusFinished ) ) ; driver . launchTasks ( offers . get ( ) [ 0 ] . id ( ) , { task } ) ; 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 ( ) ) ; driver . stop ( ) ; driver . join ( ) ; }
This test runs a command without the command user field set.
This test runs a command without the command user field set .
// Need flags for 'executor_registration_timeout'. // Ignore subsequent offers. // Launch a task with the command executor. // Command executor will run as user running test. // Scheduler should first receive TASK_RUNNING followed by the // TASK_FINISHED from the executor.