github.com/justinjmoses/evergreen@v0.0.0-20170530173719-1d50e381ff0d/monitor/tasks_test.go (about)

     1  package monitor
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/evergreen-ci/evergreen"
     7  	"github.com/evergreen-ci/evergreen/db"
     8  	"github.com/evergreen-ci/evergreen/model"
     9  	"github.com/evergreen-ci/evergreen/model/build"
    10  	"github.com/evergreen-ci/evergreen/model/host"
    11  	"github.com/evergreen-ci/evergreen/model/task"
    12  	"github.com/evergreen-ci/evergreen/model/version"
    13  	"github.com/evergreen-ci/evergreen/testutil"
    14  	. "github.com/smartystreets/goconvey/convey"
    15  )
    16  
    17  func TestCleanupTask(t *testing.T) {
    18  
    19  	testConfig := testutil.TestConfig()
    20  
    21  	db.SetGlobalSessionProvider(db.SessionFactoryFromConfig(testConfig))
    22  
    23  	Convey("When cleaning up a task", t, func() {
    24  
    25  		// reset the db
    26  		testutil.HandleTestingErr(db.ClearCollections(task.Collection, task.OldCollection, build.Collection),
    27  			t, "error clearing tasks collection")
    28  		testutil.HandleTestingErr(db.ClearCollections(host.Collection),
    29  			t, "error clearing hosts collection")
    30  
    31  		Convey("an error should be thrown if the passed-in projects slice"+
    32  			" does not contain the task's project", func() {
    33  
    34  			wrapper := doomedTaskWrapper{
    35  				task: task.Task{
    36  					Project: "proj",
    37  				},
    38  			}
    39  			projects := map[string]model.Project{}
    40  			err := cleanUpTask(wrapper, projects)
    41  			So(err, ShouldNotBeNil)
    42  			So(err.Error(), ShouldContainSubstring, "could not find project")
    43  
    44  		})
    45  
    46  		Convey("if the task's heartbeat timed out", func() {
    47  
    48  			// reset the db
    49  			testutil.HandleTestingErr(db.ClearCollections(task.Collection),
    50  				t, "error clearing tasks collection")
    51  			testutil.HandleTestingErr(db.ClearCollections(host.Collection),
    52  				t, "error clearing hosts collection")
    53  			testutil.HandleTestingErr(db.ClearCollections(build.Collection),
    54  				t, "error clearing builds collection")
    55  			testutil.HandleTestingErr(db.ClearCollections(task.OldCollection),
    56  				t, "error clearing old tasks collection")
    57  			testutil.HandleTestingErr(db.ClearCollections(version.Collection),
    58  				t, "error clearing versions collection")
    59  
    60  			Convey("the task should be reset", func() {
    61  
    62  				newTask := &task.Task{
    63  					Id:       "t1",
    64  					Status:   "started",
    65  					HostId:   "h1",
    66  					BuildId:  "b1",
    67  					Project:  "proj",
    68  					Restarts: 1,
    69  				}
    70  				testutil.HandleTestingErr(newTask.Insert(), t, "error inserting task")
    71  
    72  				wrapper := doomedTaskWrapper{
    73  					reason: HeartbeatTimeout,
    74  					task:   *newTask,
    75  				}
    76  
    77  				projects := map[string]model.Project{
    78  					"proj": {
    79  						Identifier: "proj",
    80  						Stepback:   false,
    81  					},
    82  				}
    83  
    84  				host := &host.Host{
    85  					Id:          "h1",
    86  					RunningTask: "t1",
    87  				}
    88  				So(host.Insert(), ShouldBeNil)
    89  
    90  				build := &build.Build{
    91  					Id:      "b1",
    92  					Tasks:   []build.TaskCache{{Id: "t1"}},
    93  					Version: "v1",
    94  				}
    95  				So(build.Insert(), ShouldBeNil)
    96  
    97  				v := &version.Version{
    98  					Id: "v1",
    99  				}
   100  				So(v.Insert(), ShouldBeNil)
   101  
   102  				// cleaning up the task should work
   103  				So(cleanUpTask(wrapper, projects), ShouldBeNil)
   104  
   105  				// refresh the task - it should be reset
   106  				newTask, err := task.FindOne(task.ById("t1"))
   107  				So(err, ShouldBeNil)
   108  				So(newTask.Status, ShouldEqual, evergreen.TaskUndispatched)
   109  				So(newTask.Restarts, ShouldEqual, 2)
   110  
   111  			})
   112  
   113  			Convey("the running task field on the task's host should be"+
   114  				" reset", func() {
   115  
   116  				newTask := &task.Task{
   117  					Id:       "t1",
   118  					Status:   "started",
   119  					HostId:   "h1",
   120  					BuildId:  "b1",
   121  					Project:  "proj",
   122  					Restarts: 1,
   123  				}
   124  				testutil.HandleTestingErr(newTask.Insert(), t, "error inserting task")
   125  
   126  				wrapper := doomedTaskWrapper{
   127  					reason: HeartbeatTimeout,
   128  					task:   *newTask,
   129  				}
   130  
   131  				projects := map[string]model.Project{
   132  					"proj": {
   133  						Identifier: "proj",
   134  						Stepback:   false,
   135  					},
   136  				}
   137  
   138  				h := &host.Host{
   139  					Id:          "h1",
   140  					RunningTask: "t1",
   141  				}
   142  				So(h.Insert(), ShouldBeNil)
   143  
   144  				build := &build.Build{
   145  					Id:      "b1",
   146  					Tasks:   []build.TaskCache{{Id: "t1"}},
   147  					Version: "v1",
   148  				}
   149  				So(build.Insert(), ShouldBeNil)
   150  
   151  				v := &version.Version{Id: "v1"}
   152  				So(v.Insert(), ShouldBeNil)
   153  
   154  				// cleaning up the task should work
   155  				So(cleanUpTask(wrapper, projects), ShouldBeNil)
   156  
   157  				// refresh the host, make sure its running task field has
   158  				// been reset
   159  				h, err := host.FindOne(host.ById("h1"))
   160  				So(err, ShouldBeNil)
   161  				So(h.RunningTask, ShouldEqual, "")
   162  
   163  			})
   164  
   165  		})
   166  
   167  	})
   168  
   169  }