github.com/billybanfield/evergreen@v0.0.0-20170525200750-eeee692790f7/model/event/task_resource_test.go (about)

     1  package event
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/evergreen-ci/evergreen/db"
     7  	"github.com/evergreen-ci/evergreen/testutil"
     8  	"github.com/mongodb/grip/message"
     9  	. "github.com/smartystreets/goconvey/convey"
    10  )
    11  
    12  func TestTaskInfoEvent(t *testing.T) {
    13  	Convey("Test task resource utilization collection and persistence", t, func() {
    14  
    15  		testutil.HandleTestingErr(db.Clear(TaskLogCollection), t,
    16  			"Error clearing '%s' collection", TaskLogCollection)
    17  
    18  		Convey("when logging system task info;", func() {
    19  			taskId := "testId"
    20  
    21  			Convey("before logging tasks, the query should not return any results", func() {
    22  				results, err := Find(TaskLogCollection, TaskSystemInfoEvents(taskId, 0))
    23  				So(err, ShouldBeNil)
    24  				So(len(results), ShouldEqual, 0)
    25  			})
    26  
    27  			Convey("logging a task should be retrievable,", func() {
    28  				sysInfo, ok := message.CollectSystemInfo().(*message.SystemInfo)
    29  				So(ok, ShouldBeTrue)
    30  
    31  				LogTaskSystemData(taskId, sysInfo)
    32  				results, err := Find(TaskLogCollection, TaskSystemInfoEvents(taskId, 0))
    33  				So(err, ShouldBeNil)
    34  				So(len(results), ShouldEqual, 1)
    35  			})
    36  
    37  			Convey("when logging many tasks they're all retrievable", func() {
    38  				taskId += "-batch"
    39  				for i := 0; i < 10; i++ {
    40  					info, ok := message.CollectSystemInfo().(*message.SystemInfo)
    41  					So(ok, ShouldBeTrue)
    42  					LogTaskSystemData(taskId, info)
    43  				}
    44  
    45  				results, err := Find(TaskLogCollection, TaskSystemInfoEvents(taskId, 0))
    46  				So(err, ShouldBeNil)
    47  				So(len(results), ShouldEqual, 10)
    48  
    49  			})
    50  		})
    51  
    52  		testutil.HandleTestingErr(db.Clear(TaskLogCollection), t,
    53  			"Error clearing '%s' collection", TaskLogCollection)
    54  
    55  		Convey("when logging process tree", func() {
    56  			taskId := "taskId"
    57  			Convey("before logging tasks, the query should not return any results", func() {
    58  				results, err := Find(TaskLogCollection, TaskProcessInfoEvents(taskId, 0))
    59  				So(err, ShouldBeNil)
    60  				So(len(results), ShouldEqual, 0)
    61  			})
    62  
    63  			Convey("log events should be retrievable", func() {
    64  				pm, ok := message.CollectProcessInfoSelf().(*message.ProcessInfo)
    65  				So(ok, ShouldBeTrue)
    66  
    67  				LogTaskProcessData(taskId, []*message.ProcessInfo{pm})
    68  
    69  				results, err := Find(TaskLogCollection, TaskProcessInfoEvents(taskId, 0))
    70  				So(err, ShouldBeNil)
    71  				So(len(results), ShouldEqual, 1)
    72  			})
    73  
    74  			Convey("logging multiple events should be retrievable", func() {
    75  				var count int
    76  				taskId += "batch"
    77  
    78  				infos := []*message.ProcessInfo{}
    79  				msgs := message.CollectProcessInfoSelfWithChildren()
    80  
    81  				for _, m := range msgs {
    82  					count++
    83  
    84  					info, ok := m.(*message.ProcessInfo)
    85  					So(ok, ShouldBeTrue)
    86  					infos = append(infos, info)
    87  				}
    88  				So(len(infos), ShouldEqual, len(msgs))
    89  				LogTaskProcessData(taskId, infos)
    90  
    91  				So(count, ShouldEqual, len(infos))
    92  				results, err := Find(TaskLogCollection, TaskProcessInfoEvents(taskId, 0))
    93  				So(err, ShouldBeNil)
    94  				So(len(results), ShouldEqual, count)
    95  			})
    96  		})
    97  	})
    98  }