func TestOffer(t *testing.T) { offer := util.NewOffer(util.NewOfferID("487c73d8-9951-f23c-34bd-8085bfd30c49"), util.NewFrameworkID("20150903-065451-84125888-5050-10715-0053"), util.NewSlaveID("20150903-065451-84125888-5050-10715-S1"), "slave0") if Offer(offer) != "slave0#30c49" { t.Errorf(`util.NewOffer(util.NewOfferID("487c73d8-9951-f23c-34bd-8085bfd30c49"), util.NewFrameworkID("20150903-065451-84125888-5050-10715-0053"), util.NewSlaveID("20150903-065451-84125888-5050-10715-S1"), "slave0") != "slave0#30c49"; actual %s`, Offer(offer)) } offer.Resources = []*mesos.Resource{util.NewScalarResource("cpus", 4), util.NewScalarResource("mem", 512), util.NewRangesResource("ports", []*mesos.Value_Range{util.NewValueRange(31000, 32000)})} if Offer(offer) != "slave0#30c49 cpus:4.00 mem:512.00 ports:[31000..32000]" { t.Errorf("Expected slave0#30c49 cpus:4.00 mem:512.00 ports:[31000..32000]; actual %s", Offer(offer)) } offer.Attributes = []*mesos.Attribute{&mesos.Attribute{ Name: proto.String("rack"), Type: mesos.Value_SCALAR.Enum(), Scalar: &mesos.Value_Scalar{Value: proto.Float64(2)}, }} if Offer(offer) != "slave0#30c49 cpus:4.00 mem:512.00 ports:[31000..32000] rack:2.00" { t.Errorf("Expected slave0#30c49 cpus:4.00 mem:512.00 ports:[31000..32000] rack:2.00; actual %s", Offer(offer)) } offer.Resources = nil if Offer(offer) != "slave0#30c49 rack:2.00" { t.Errorf("Expected slave0#30c49 rack:2.00; actual %s", Offer(offer)) } }
func TestRemoveOffer(t *testing.T) { s := newAgent("SID", nil) assert.Empty(t, s.offers) s.addOffer(mesosutil.NewOffer(mesosutil.NewOfferID("ID1"), nil, nil, "hostname1")) s.addOffer(mesosutil.NewOffer(mesosutil.NewOfferID("ID2"), nil, nil, "hostname1")) assert.Equal(t, len(s.offers), 2) assert.Equal(t, len(s.getOffers()), 2) assert.True(t, s.removeOffer("ID1")) assert.Equal(t, len(s.offers), 1) assert.Equal(t, len(s.getOffers()), 1) assert.False(t, s.removeOffer("ID1")) }
func TestAddOffer(t *testing.T) { s := newSlave("SID", nil) assert.Empty(t, s.offers) assert.True(t, s.empty()) s.addOffer(mesosutil.NewOffer(mesosutil.NewOfferID("ID1"), nil, nil, "hostname1")) s.addOffer(mesosutil.NewOffer(mesosutil.NewOfferID("ID2"), nil, nil, "hostname1")) assert.Equal(t, len(s.offers), 2) assert.Equal(t, len(s.getOffers()), 2) assert.False(t, s.empty()) s.addOffer(mesosutil.NewOffer(mesosutil.NewOfferID("ID1"), nil, nil, "hostname1")) assert.Equal(t, len(s.offers), 2) assert.Equal(t, len(s.getOffers()), 2) }
func createTestOffer(idSuffix string) *mesos.Offer { return util.NewOffer( util.NewOfferID("test-offer-"+idSuffix), util.NewFrameworkID("test-framework-"+idSuffix), util.NewSlaveID("test-slave-"+idSuffix), "localhost."+idSuffix, ) }
func TestOffers(t *testing.T) { offer1 := util.NewOffer(util.NewOfferID("487c73d8-9951-f23c-34bd-8085bfd30c49"), util.NewFrameworkID("20150903-065451-84125888-5050-10715-0053"), util.NewSlaveID("20150903-065451-84125888-5050-10715-S1"), "slave0") offer1.Resources = []*mesos.Resource{util.NewScalarResource("cpus", 4), util.NewScalarResource("mem", 512), util.NewRangesResource("ports", []*mesos.Value_Range{util.NewValueRange(31000, 32000)})} offer2 := util.NewOffer(util.NewOfferID("26d5b34c-ef81-638d-5ad5-32c743c9c033"), util.NewFrameworkID("20150903-065451-84125888-5050-10715-0037"), util.NewSlaveID("20150903-065451-84125888-5050-10715-S0"), "master") offer2.Resources = []*mesos.Resource{util.NewScalarResource("cpus", 2), util.NewScalarResource("mem", 1024), util.NewRangesResource("ports", []*mesos.Value_Range{util.NewValueRange(4000, 7000)})} offer2.Attributes = []*mesos.Attribute{&mesos.Attribute{ Name: proto.String("rack"), Type: mesos.Value_SCALAR.Enum(), Scalar: &mesos.Value_Scalar{Value: proto.Float64(2)}, }} offers := Offers([]*mesos.Offer{offer1, offer2}) if len(strings.Split(offers, "\n")) != 2 { t.Errorf("Offers([]*mesos.Offer{offer1, offer2}) should contain two offers split by new line, actual: %s", offers) } }
func (suite *SchedulerTestSuite) TestSchdulerDriverAcceptOffersWithError() { sched := mock_scheduler.New() sched.On("StatusUpdate").Return(nil) sched.On("Error").Return() msgr := mockedMessenger() driver := newTestDriver(suite.T(), driverConfigMessenger(sched, suite.framework, suite.master, nil, msgr)) driver.OnDispatch(func(_ context.Context, _ *upid.UPID, _ proto.Message) error { return fmt.Errorf("Unable to send message") }) go func() { driver.Run() }() <-driver.Started() driver.SetConnected(true) // simulated suite.True(driver.Running()) // setup an offer offer := util.NewOffer( util.NewOfferID("test-offer-001"), suite.framework.Id, util.NewSlaveID("test-slave-001"), "test-slave(1)@localhost:5050", ) pid, err := upid.Parse("test-slave(1)@localhost:5050") suite.NoError(err) driver.CacheOffer(offer, pid) // launch task task := util.NewTaskInfo( "simple-task", util.NewTaskID("simpe-task-1"), util.NewSlaveID("test-slave-001"), []*mesos.Resource{util.NewScalarResourceWithReservation("mem", 400, "principal", "role")}, ) task.Command = util.NewCommandInfo("pwd") task.Executor = util.NewExecutorInfo(util.NewExecutorID("test-exec"), task.Command) tasks := []*mesos.TaskInfo{task} operations := []*mesos.Offer_Operation{util.NewLaunchOperation(tasks)} stat, err := driver.AcceptOffers( []*mesos.OfferID{offer.Id}, operations, &mesos.Filters{}, ) suite.Equal(mesos.Status_DRIVER_RUNNING, stat) suite.Error(err) }
func TestFilterResources(t *testing.T) { rf := ResourceFilter{} o := util.NewOffer(util.NewOfferID("offerid"), util.NewFrameworkID("frameworkid"), util.NewSlaveID("slaveId"), "hostname") o.Resources = []*mesos.Resource{ util.NewScalarResource("name", 1.0), util.NewScalarResource("ub0r-resource", 2.0), util.NewScalarResource("ub0r-resource", 3.0), } res := rf.FilterResources(o, "ub0r-resource") assert.Equal(t, 2, len(res)) assert.Equal(t, "ub0r-resource", res[0].GetName()) }
func (suite *SchedulerIntegrationTestSuite) TestSchedulerDriverResourceOffersEvent() { ok := suite.configureServerWithRegisteredFramework() suite.True(ok, "failed to establish running test server and driver") // Send a event to this SchedulerDriver (via http) to test handlers. offer := util.NewOffer( util.NewOfferID("test-offer-001"), suite.registeredFrameworkId, util.NewSlaveID("test-slave-001"), "test-localhost", ) pbMsg := &mesos.ResourceOffersMessage{ Offers: []*mesos.Offer{offer}, Pids: []string{"test-offer-001@test-slave-001:5051"}, } c := suite.newMockClient() c.SendMessage(suite.driver.UPID(), pbMsg) suite.sched.waitForCallback(0) }
func (suite *SchedulerTestSuite) TestSchdulerDriverLaunchTasksWithError() { sched := NewMockScheduler() sched.On("StatusUpdate").Return(nil) sched.On("Error").Return() msgr := messenger.NewMockedMessenger() msgr.On("Start").Return(nil) msgr.On("Send").Return(nil) msgr.On("UPID").Return(&upid.UPID{}) msgr.On("Stop").Return(nil) msgr.On("Route").Return(nil) driver := newTestSchedulerDriver(suite.T(), sched, suite.framework, suite.master, nil) driver.messenger = msgr suite.True(driver.Stopped()) go func() { driver.Run() }() time.Sleep(time.Millisecond * 1) driver.setConnected(true) // simulated suite.False(driver.Stopped()) suite.Equal(mesos.Status_DRIVER_RUNNING, driver.Status()) // to trigger error msgr2 := messenger.NewMockedMessenger() msgr2.On("Start").Return(nil) msgr2.On("UPID").Return(&upid.UPID{}) msgr2.On("Send").Return(fmt.Errorf("Unable to send message")) msgr2.On("Stop").Return(nil) msgr.On("Route").Return(nil) driver.messenger = msgr2 // setup an offer offer := util.NewOffer( util.NewOfferID("test-offer-001"), suite.framework.Id, util.NewSlaveID("test-slave-001"), "test-slave(1)@localhost:5050", ) pid, err := upid.Parse("test-slave(1)@localhost:5050") suite.NoError(err) driver.cache.putOffer(offer, pid) // launch task task := util.NewTaskInfo( "simple-task", util.NewTaskID("simpe-task-1"), util.NewSlaveID("test-slave-001"), []*mesos.Resource{util.NewScalarResource("mem", 400)}, ) task.Command = util.NewCommandInfo("pwd") task.Executor = util.NewExecutorInfo(util.NewExecutorID("test-exec"), task.Command) tasks := []*mesos.TaskInfo{task} stat, err := driver.LaunchTasks( []*mesos.OfferID{offer.Id}, tasks, &mesos.Filters{}, ) suite.Error(err) suite.Equal(mesos.Status_DRIVER_RUNNING, stat) }