github.com/billybanfield/evergreen@v0.0.0-20170525200750-eeee692790f7/model/event/host_event_test.go (about) 1 package event 2 3 import ( 4 "testing" 5 "time" 6 7 "github.com/evergreen-ci/evergreen" 8 "github.com/evergreen-ci/evergreen/db" 9 "github.com/evergreen-ci/evergreen/testutil" 10 . "github.com/smartystreets/goconvey/convey" 11 ) 12 13 func init() { 14 db.SetGlobalSessionProvider(db.SessionFactoryFromConfig(testutil.TestConfig())) 15 } 16 17 func TestLoggingHostEvents(t *testing.T) { 18 Convey("When logging host events", t, func() { 19 20 So(db.Clear(AllLogCollection), ShouldBeNil) 21 22 Convey("all events logged should be persisted to the database, and"+ 23 " fetching them in order should sort by the time they were"+ 24 " logged", func() { 25 26 hostId := "host_id" 27 hostname := "hostname" 28 taskId := "task_id" 29 taskPid := "12345" 30 31 // log some events, sleeping in between to make sure the times are different 32 LogHostCreated(hostId) 33 time.Sleep(1 * time.Millisecond) 34 LogHostStatusChanged(hostId, evergreen.HostRunning, evergreen.HostTerminated) 35 time.Sleep(1 * time.Millisecond) 36 LogHostDNSNameSet(hostId, hostname) 37 time.Sleep(1 * time.Millisecond) 38 LogHostProvisioned(hostId) 39 time.Sleep(1 * time.Millisecond) 40 LogHostRunningTaskSet(hostId, taskId) 41 time.Sleep(1 * time.Millisecond) 42 LogHostRunningTaskCleared(hostId, taskId) 43 time.Sleep(1 * time.Millisecond) 44 LogHostTaskPidSet(hostId, taskPid) 45 time.Sleep(1 * time.Millisecond) 46 47 // fetch all the events from the database, make sure they are 48 // persisted correctly 49 50 eventsForHost, err := Find(AllLogCollection, HostEventsInOrder(hostId)) 51 So(err, ShouldBeNil) 52 53 event := eventsForHost[0] 54 So(event.EventType, ShouldEqual, EventHostCreated) 55 So(event.ResourceId, ShouldEqual, hostId) 56 57 eventData, ok := event.Data.Data.(*HostEventData) 58 So(ok, ShouldBeTrue) 59 So(eventData.ResourceType, ShouldEqual, ResourceTypeHost) 60 So(eventData.OldStatus, ShouldBeBlank) 61 So(eventData.NewStatus, ShouldBeBlank) 62 So(eventData.Logs, ShouldBeBlank) 63 So(eventData.Hostname, ShouldBeBlank) 64 So(eventData.TaskId, ShouldBeBlank) 65 So(eventData.TaskPid, ShouldBeBlank) 66 67 event = eventsForHost[1] 68 So(event.EventType, ShouldEqual, EventHostStatusChanged) 69 So(event.ResourceId, ShouldEqual, hostId) 70 71 eventData, ok = event.Data.Data.(*HostEventData) 72 So(ok, ShouldBeTrue) 73 So(eventData.ResourceType, ShouldEqual, ResourceTypeHost) 74 So(eventData.OldStatus, ShouldEqual, evergreen.HostRunning) 75 So(eventData.NewStatus, ShouldEqual, evergreen.HostTerminated) 76 So(eventData.Logs, ShouldBeBlank) 77 So(eventData.Hostname, ShouldBeBlank) 78 So(eventData.TaskId, ShouldBeBlank) 79 So(eventData.TaskPid, ShouldBeBlank) 80 81 event = eventsForHost[2] 82 So(event.EventType, ShouldEqual, EventHostDNSNameSet) 83 So(event.ResourceId, ShouldEqual, hostId) 84 85 eventData, ok = event.Data.Data.(*HostEventData) 86 So(ok, ShouldBeTrue) 87 So(eventData.ResourceType, ShouldEqual, ResourceTypeHost) 88 So(eventData.OldStatus, ShouldBeBlank) 89 So(eventData.NewStatus, ShouldBeBlank) 90 So(eventData.Logs, ShouldBeBlank) 91 So(eventData.Hostname, ShouldEqual, hostname) 92 So(eventData.TaskId, ShouldBeBlank) 93 So(eventData.TaskPid, ShouldBeBlank) 94 95 event = eventsForHost[3] 96 So(event.EventType, ShouldEqual, EventHostProvisioned) 97 So(event.ResourceId, ShouldEqual, hostId) 98 99 eventData, ok = event.Data.Data.(*HostEventData) 100 So(ok, ShouldBeTrue) 101 So(eventData.ResourceType, ShouldEqual, ResourceTypeHost) 102 So(eventData.OldStatus, ShouldBeBlank) 103 So(eventData.NewStatus, ShouldBeBlank) 104 So(eventData.Logs, ShouldBeBlank) 105 So(eventData.Hostname, ShouldBeBlank) 106 So(eventData.TaskId, ShouldBeBlank) 107 So(eventData.TaskPid, ShouldBeBlank) 108 109 event = eventsForHost[4] 110 So(event.EventType, ShouldEqual, EventHostRunningTaskSet) 111 So(event.ResourceId, ShouldEqual, hostId) 112 113 eventData, ok = event.Data.Data.(*HostEventData) 114 So(ok, ShouldBeTrue) 115 So(eventData.ResourceType, ShouldEqual, ResourceTypeHost) 116 So(eventData.OldStatus, ShouldBeBlank) 117 So(eventData.NewStatus, ShouldBeBlank) 118 So(eventData.Logs, ShouldBeBlank) 119 So(eventData.Hostname, ShouldBeBlank) 120 So(eventData.TaskId, ShouldEqual, taskId) 121 So(eventData.TaskPid, ShouldBeBlank) 122 123 event = eventsForHost[5] 124 So(event.EventType, ShouldEqual, EventHostRunningTaskCleared) 125 So(event.ResourceId, ShouldEqual, hostId) 126 127 eventData, ok = event.Data.Data.(*HostEventData) 128 So(ok, ShouldBeTrue) 129 So(eventData.ResourceType, ShouldEqual, ResourceTypeHost) 130 So(eventData.OldStatus, ShouldBeBlank) 131 So(eventData.NewStatus, ShouldBeBlank) 132 So(eventData.Logs, ShouldBeBlank) 133 So(eventData.Hostname, ShouldBeBlank) 134 So(eventData.TaskId, ShouldEqual, taskId) 135 So(eventData.TaskPid, ShouldBeBlank) 136 137 event = eventsForHost[6] 138 So(event.EventType, ShouldEqual, EventHostTaskPidSet) 139 So(event.ResourceId, ShouldEqual, hostId) 140 141 eventData, ok = event.Data.Data.(*HostEventData) 142 So(ok, ShouldBeTrue) 143 So(eventData.ResourceType, ShouldEqual, ResourceTypeHost) 144 So(eventData.OldStatus, ShouldBeBlank) 145 So(eventData.NewStatus, ShouldBeBlank) 146 So(eventData.Logs, ShouldBeBlank) 147 So(eventData.Hostname, ShouldBeBlank) 148 So(eventData.TaskId, ShouldBeBlank) 149 So(eventData.TaskPid, ShouldEqual, taskPid) 150 }) 151 }) 152 }