github.com/justinjmoses/evergreen@v0.0.0-20170530173719-1d50e381ff0d/monitor/host_monitoring_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/cloud"
     9  	"github.com/evergreen-ci/evergreen/cloud/providers/mock"
    10  	"github.com/evergreen-ci/evergreen/db"
    11  	"github.com/evergreen-ci/evergreen/model/host"
    12  	"github.com/evergreen-ci/evergreen/testutil"
    13  	. "github.com/smartystreets/goconvey/convey"
    14  )
    15  
    16  func TestMonitorReachability(t *testing.T) {
    17  
    18  	testConfig := testutil.TestConfig()
    19  
    20  	db.SetGlobalSessionProvider(db.SessionFactoryFromConfig(testConfig))
    21  
    22  	Convey("When checking the reachability of hosts", t, func() {
    23  
    24  		// reset the db
    25  		testutil.HandleTestingErr(db.ClearCollections(host.Collection),
    26  			t, "error clearing hosts collection")
    27  
    28  		Convey("hosts that have been checked up on recently should"+
    29  			" not be checked", func() {
    30  
    31  			// mock provider hosts are always returned as reachable. therefore,
    32  			// if this host were picked up by the monitoring check then it
    33  			// would be updated to be reachable
    34  			h := &host.Host{
    35  				Id: "h1",
    36  				LastReachabilityCheck: time.Now().Add(-time.Minute),
    37  				Status:                evergreen.HostUnreachable,
    38  				Provider:              mock.ProviderName,
    39  				StartedBy:             evergreen.User,
    40  			}
    41  			testutil.HandleTestingErr(h.Insert(), t, "error inserting host")
    42  
    43  			So(monitorReachability(nil), ShouldBeNil)
    44  
    45  			// refresh the host - its status should not have been updated
    46  			h, err := host.FindOne(host.ById("h1"))
    47  			So(err, ShouldBeNil)
    48  			So(h.Status, ShouldEqual, evergreen.HostUnreachable)
    49  
    50  		})
    51  
    52  		Convey("hosts eligible for a check should have their statuses"+
    53  			" updated appropriately", func() {
    54  
    55  			m1 := mock.MockInstance{
    56  				IsUp:           true,
    57  				IsSSHReachable: true,
    58  				Status:         cloud.StatusRunning,
    59  			}
    60  			mock.MockInstances["h1"] = m1
    61  
    62  			// this host should be picked up and updated to running
    63  			host1 := &host.Host{
    64  				Id: "h1",
    65  				LastReachabilityCheck: time.Now().Add(-15 * time.Minute),
    66  				Status:                evergreen.HostUnreachable,
    67  				Provider:              mock.ProviderName,
    68  				StartedBy:             evergreen.User,
    69  			}
    70  			testutil.HandleTestingErr(host1.Insert(), t, "error inserting host")
    71  
    72  			m2 := mock.MockInstance{
    73  				IsUp:           true,
    74  				IsSSHReachable: true,
    75  				Status:         cloud.StatusRunning,
    76  			}
    77  			mock.MockInstances["h2"] = m2
    78  
    79  			// this host should not be picked up, since it is quarantined
    80  			host2 := &host.Host{
    81  				Id: "h2",
    82  				LastReachabilityCheck: time.Now().Add(-15 * time.Minute),
    83  				Status:                evergreen.HostQuarantined,
    84  				Provider:              mock.ProviderName,
    85  				StartedBy:             evergreen.User,
    86  			}
    87  			testutil.HandleTestingErr(host2.Insert(), t, "error inserting host")
    88  
    89  			So(monitorReachability(nil), ShouldBeNil)
    90  
    91  			// refresh the first host - its status should have been updated
    92  			host1, err := host.FindOne(host.ById("h1"))
    93  			So(err, ShouldBeNil)
    94  			So(host1.Status, ShouldEqual, evergreen.HostRunning)
    95  
    96  			// refresh the second host - its status should not have been updated
    97  			host1, err = host.FindOne(host.ById("h2"))
    98  			So(err, ShouldBeNil)
    99  			So(host1.Status, ShouldEqual, evergreen.HostQuarantined)
   100  
   101  		})
   102  
   103  	})
   104  
   105  }