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

     1  package monitor
     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/model/host"
    10  	"github.com/evergreen-ci/evergreen/testutil"
    11  	. "github.com/smartystreets/goconvey/convey"
    12  )
    13  
    14  func TestWarnExpiringSpawnedHosts(t *testing.T) {
    15  
    16  	testConfig := testutil.TestConfig()
    17  
    18  	db.SetGlobalSessionProvider(db.SessionFactoryFromConfig(testConfig))
    19  
    20  	Convey("When building warnings for spawned hosts that will be expiring"+
    21  		" soon", t, func() {
    22  
    23  		// reset the db
    24  		testutil.HandleTestingErr(db.ClearCollections(host.Collection),
    25  			t, "error clearing hosts collection")
    26  
    27  		Convey("any hosts not expiring within a threshold should not trigger"+
    28  			" warnings", func() {
    29  
    30  			// this host does not expire within the first notification
    31  			// threshold
    32  			host1 := &host.Host{
    33  				Id:             "h1",
    34  				ExpirationTime: time.Now().Add(time.Hour * 15),
    35  			}
    36  			testutil.HandleTestingErr(host1.Insert(), t, "error inserting host")
    37  
    38  			warnings, err := spawnHostExpirationWarnings(testConfig)
    39  			So(err, ShouldBeNil)
    40  			So(len(warnings), ShouldEqual, 0)
    41  
    42  		})
    43  
    44  		Convey("any thresholds for which warnings have already been sent"+
    45  			" should be ignored", func() {
    46  
    47  			// this host meets the first notification warning threshold
    48  			host1 := &host.Host{
    49  				Id:             "h1",
    50  				ExpirationTime: time.Now().Add(time.Hour * 10),
    51  				Notifications: map[string]bool{
    52  					"720": true, // the first threshold in minutes
    53  				},
    54  			}
    55  			testutil.HandleTestingErr(host1.Insert(), t, "error inserting host")
    56  
    57  			warnings, err := spawnHostExpirationWarnings(testConfig)
    58  			So(err, ShouldBeNil)
    59  			So(len(warnings), ShouldEqual, 0)
    60  
    61  		})
    62  
    63  		Convey("the most recent threshold crossed should be used to create"+
    64  			" the warning", func() {
    65  
    66  			// this host meets both notification warning thresholds
    67  			host1 := &host.Host{
    68  				Id:             "h1",
    69  				ExpirationTime: time.Now().Add(time.Minute * 10),
    70  			}
    71  			testutil.HandleTestingErr(host1.Insert(), t, "error inserting host")
    72  
    73  			warnings, err := spawnHostExpirationWarnings(testConfig)
    74  			So(err, ShouldBeNil)
    75  			So(len(warnings), ShouldEqual, 1)
    76  
    77  			// execute the callback, make sure the correct threshold is set
    78  			So(warnings[0].callback(warnings[0].host, warnings[0].threshold), ShouldBeNil)
    79  			host1, err = host.FindOne(host.ById("h1"))
    80  			So(err, ShouldBeNil)
    81  			So(host1.Notifications["120"], ShouldBeTrue)
    82  
    83  		})
    84  
    85  		Convey("hosts that are quarantined or have already expired should not"+
    86  			" merit warnings", func() {
    87  
    88  			// quarantined host
    89  			host1 := &host.Host{
    90  				Id:             "h1",
    91  				Status:         evergreen.HostQuarantined,
    92  				ExpirationTime: time.Now().Add(time.Minute * 10),
    93  			}
    94  			testutil.HandleTestingErr(host1.Insert(), t, "error inserting host")
    95  
    96  			// terminated host
    97  			host2 := &host.Host{
    98  				Id:             "h2",
    99  				Status:         evergreen.HostTerminated,
   100  				ExpirationTime: time.Now().Add(time.Minute * 10),
   101  			}
   102  			testutil.HandleTestingErr(host2.Insert(), t, "error inserting host")
   103  
   104  			// past the expiration. no warning needs to be sent since this host
   105  			// is theoretically about to be terminated, at which time a
   106  			// notification will be sent
   107  			host3 := &host.Host{
   108  				Id:             "h3",
   109  				ExpirationTime: time.Now().Add(-time.Minute * 10),
   110  			}
   111  			testutil.HandleTestingErr(host3.Insert(), t, "error inserting host")
   112  
   113  			warnings, err := spawnHostExpirationWarnings(testConfig)
   114  			So(err, ShouldBeNil)
   115  			So(len(warnings), ShouldEqual, 0)
   116  
   117  		})
   118  
   119  	})
   120  
   121  }
   122  
   123  func TestWarnSlowProvisioningHosts(t *testing.T) {
   124  
   125  	testConfig := testutil.TestConfig()
   126  
   127  	db.SetGlobalSessionProvider(db.SessionFactoryFromConfig(testConfig))
   128  
   129  	Convey("When building warnings for hosts that are taking a long time to"+
   130  		" provision", t, func() {
   131  
   132  		// reset the db
   133  		testutil.HandleTestingErr(db.ClearCollections(host.Collection),
   134  			t, "error clearing hosts collection")
   135  
   136  		Convey("hosts that have not hit the threshold should not trigger a"+
   137  			" warning", func() {
   138  
   139  			host1 := &host.Host{
   140  				Id:           "h1",
   141  				StartedBy:    evergreen.User,
   142  				CreationTime: time.Now().Add(-10 * time.Minute),
   143  			}
   144  			testutil.HandleTestingErr(host1.Insert(), t, "error inserting host")
   145  
   146  			warnings, err := slowProvisioningWarnings(testConfig)
   147  			So(err, ShouldBeNil)
   148  			So(len(warnings), ShouldEqual, 0)
   149  
   150  		})
   151  
   152  		Convey("hosts that have already triggered a notification should not"+
   153  			" trigger another", func() {
   154  
   155  			host1 := &host.Host{
   156  				Id:           "h1",
   157  				StartedBy:    evergreen.User,
   158  				CreationTime: time.Now().Add(-1 * time.Hour),
   159  				Notifications: map[string]bool{
   160  					slowProvisioningWarning: true,
   161  				},
   162  			}
   163  			testutil.HandleTestingErr(host1.Insert(), t, "error inserting host")
   164  
   165  			warnings, err := slowProvisioningWarnings(testConfig)
   166  			So(err, ShouldBeNil)
   167  			So(len(warnings), ShouldEqual, 0)
   168  
   169  		})
   170  
   171  		Convey("terminated hosts should not trigger a warning", func() {
   172  
   173  			host1 := &host.Host{
   174  				Id:           "h1",
   175  				StartedBy:    evergreen.User,
   176  				Status:       evergreen.HostTerminated,
   177  				CreationTime: time.Now().Add(-1 * time.Hour),
   178  			}
   179  			testutil.HandleTestingErr(host1.Insert(), t, "error inserting host")
   180  
   181  			warnings, err := slowProvisioningWarnings(testConfig)
   182  			So(err, ShouldBeNil)
   183  			So(len(warnings), ShouldEqual, 0)
   184  
   185  		})
   186  
   187  		Convey("hosts that are at the threshold and have not previously"+
   188  			" triggered a warning should trigger one", func() {
   189  
   190  			host1 := &host.Host{
   191  				Id:           "h1",
   192  				StartedBy:    evergreen.User,
   193  				CreationTime: time.Now().Add(-1 * time.Hour),
   194  			}
   195  			testutil.HandleTestingErr(host1.Insert(), t, "error inserting host")
   196  
   197  			warnings, err := slowProvisioningWarnings(testConfig)
   198  			So(err, ShouldBeNil)
   199  			So(len(warnings), ShouldEqual, 1)
   200  
   201  			// make sure running the callback sets the notification key
   202  			So(warnings[0].callback(warnings[0].host, warnings[0].threshold), ShouldBeNil)
   203  			host1, err = host.FindOne(host.ById("h1"))
   204  			So(err, ShouldBeNil)
   205  			So(host1.Notifications[slowProvisioningWarning], ShouldBeTrue)
   206  
   207  		})
   208  
   209  	})
   210  }