func TestAgentCommunicator(taskId string, taskSecret string, apiRootUrl string) *comm.HTTPCommunicator { agentCommunicator, err := comm.NewHTTPCommunicator(apiRootUrl, taskId, taskSecret, "", nil) if err != nil { panic(err) } agentCommunicator.MaxAttempts = 3 agentCommunicator.RetrySleep = 100 * time.Millisecond return agentCommunicator }
// New creates a new agent to run a given task. func New(apiServerURL, taskId, taskSecret, logFile, cert, pidFilePath string) (*Agent, error) { sh := &SignalHandler{} sh.makeChannels() // set up communicator with API server httpCommunicator, err := comm.NewHTTPCommunicator(apiServerURL, taskId, taskSecret, cert, sh.communicatorChan) if err != nil { return nil, err } // set up logger to API server apiLogger := comm.NewAPILogger(httpCommunicator) idleTimeoutWatcher := comm.NewTimeoutWatcher(sh.stopBackgroundChan) idleTimeoutWatcher.SetDuration(DefaultIdleTimeout) // set up timeout logger, local and API logger streams streamLogger, err := comm.NewStreamLogger(idleTimeoutWatcher, apiLogger, logFile) if err != nil { return nil, err } httpCommunicator.Logger = streamLogger.Execution // set up the heartbeat ticker hbTicker := comm.NewHeartbeatTicker(sh.stopBackgroundChan) hbTicker.MaxFailedHeartbeats = 10 hbTicker.SignalChan = sh.heartbeatChan hbTicker.TaskCommunicator = httpCommunicator hbTicker.Logger = httpCommunicator.Logger hbTicker.Interval = DefaultHeartbeatInterval // set up the system stats collector statsCollector := NewSimpleStatsCollector( streamLogger.System, DefaultStatsInterval, sh.stopBackgroundChan, "df -h", "${ps|ps}", ) agt := &Agent{ signalHandler: sh, logger: streamLogger, TaskCommunicator: httpCommunicator, heartbeater: hbTicker, statsCollector: statsCollector, idleTimeoutWatcher: idleTimeoutWatcher, APILogger: apiLogger, Registry: plugin.NewSimpleRegistry(), KillChan: make(chan bool), endChan: make(chan *apimodels.TaskEndDetail, 1), pidFilePath: pidFilePath, } return agt, nil }
func TestPluginFunctions(t *testing.T) { testConfig := evergreen.TestConfig() testutil.ConfigureIntegrationTest(t, testConfig, "TestPatchTask") Convey("With a SimpleRegistry", t, func() { Convey("with a project file containing functions", func() { registry := plugin.NewSimpleRegistry() err := registry.Register(&shell.ShellPlugin{}) testutil.HandleTestingErr(err, t, "Couldn't register plugin") err = registry.Register(&expansions.ExpansionsPlugin{}) testutil.HandleTestingErr(err, t, "Couldn't register plugin") testServer, err := service.CreateTestServer(testConfig, nil, plugin.APIPlugins, false) testutil.HandleTestingErr(err, t, "Couldn't set up testing server") taskConfig, err := createTestConfig("testdata/plugin_project_functions.yml", t) testutil.HandleTestingErr(err, t, "failed to create test config: %v", err) Convey("all commands in project file should parse successfully", func() { for _, newTask := range taskConfig.Project.Tasks { for _, command := range newTask.Commands { pluginCmd, err := registry.GetCommands(command, taskConfig.Project.Functions) testutil.HandleTestingErr(err, t, "Got error getting plugin command: %v") So(pluginCmd, ShouldNotBeNil) So(err, ShouldBeNil) } } }) httpCom, err := comm.NewHTTPCommunicator(testServer.URL, "mocktaskid", "mocktasksecret", "", nil) So(err, ShouldBeNil) So(httpCom, ShouldNotBeNil) Convey("all commands in test project should execute successfully", func() { sliceAppender := &evergreen.SliceAppender{[]*slogger.Log{}} logger := agentutil.NewTestLogger(sliceAppender) for _, newTask := range taskConfig.Project.Tasks { So(len(newTask.Commands), ShouldNotEqual, 0) for _, command := range newTask.Commands { pluginCmds, err := registry.GetCommands(command, taskConfig.Project.Functions) testutil.HandleTestingErr(err, t, "Couldn't get plugin command: %v") So(pluginCmds, ShouldNotBeNil) So(err, ShouldBeNil) So(len(pluginCmds), ShouldEqual, 1) cmd := pluginCmds[0] pluginCom := &comm.TaskJSONCommunicator{cmd.Plugin(), httpCom} err = cmd.Execute(logger, pluginCom, taskConfig, make(chan bool)) So(err, ShouldBeNil) } } }) }) }) }
func TestPluginExecution(t *testing.T) { Convey("With a SimpleRegistry and test project file", t, func() { registry := plugin.NewSimpleRegistry() plugins := []plugin.CommandPlugin{&MockPlugin{}, &expansions.ExpansionsPlugin{}, &shell.ShellPlugin{}} apiPlugins := []plugin.APIPlugin{&MockPlugin{}, &expansions.ExpansionsPlugin{}} for _, p := range plugins { err := registry.Register(p) testutil.HandleTestingErr(err, t, "failed to register plugin") } testServer, err := service.CreateTestServer(evergreen.TestConfig(), nil, apiPlugins, false) testutil.HandleTestingErr(err, t, "Couldn't set up testing server") httpCom, err := comm.NewHTTPCommunicator(testServer.URL, "mocktaskid", "mocktasksecret", "", nil) So(err, ShouldBeNil) So(httpCom, ShouldNotBeNil) pluginConfigPath := filepath.Join(testutil.GetDirectoryOfFile(), "testdata", "plugin_project.yml") taskConfig, err := createTestConfig(pluginConfigPath, t) testutil.HandleTestingErr(err, t, "failed to create test config: %v", err) sliceAppender := &evergreen.SliceAppender{[]*slogger.Log{}} logger := agentutil.NewTestLogger(sliceAppender) Convey("all commands in test project should execute successfully", func() { for _, newTask := range taskConfig.Project.Tasks { So(len(newTask.Commands), ShouldNotEqual, 0) for _, command := range newTask.Commands { pluginCmds, err := registry.GetCommands(command, taskConfig.Project.Functions) testutil.HandleTestingErr(err, t, "Couldn't get plugin command: %v") So(pluginCmds, ShouldNotBeNil) So(err, ShouldBeNil) for _, c := range pluginCmds { pluginCom := &comm.TaskJSONCommunicator{c.Plugin(), httpCom} err = c.Execute(logger, pluginCom, taskConfig, make(chan bool)) So(err, ShouldBeNil) } } } }) }) }