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

     1  package host
     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/testutil"
     9  	. "github.com/smartystreets/goconvey/convey"
    10  )
    11  
    12  func TestDecommissionInactiveStaticHosts(t *testing.T) {
    13  
    14  	Convey("When decommissioning unused static hosts", t, func() {
    15  
    16  		testutil.HandleTestingErr(db.Clear(Collection), t, "Error clearing"+
    17  			" '%v' collection", Collection)
    18  
    19  		Convey("if a nil slice is passed in, no host(s) should"+
    20  			" be decommissioned in the database", func() {
    21  
    22  			inactiveOne := &Host{
    23  				Id:       "inactiveOne",
    24  				Status:   evergreen.HostRunning,
    25  				Provider: evergreen.HostTypeStatic,
    26  			}
    27  			So(inactiveOne.Insert(), ShouldBeNil)
    28  
    29  			inactiveTwo := &Host{
    30  				Id:       "inactiveTwo",
    31  				Status:   evergreen.HostRunning,
    32  				Provider: evergreen.HostTypeStatic,
    33  			}
    34  			So(inactiveTwo.Insert(), ShouldBeNil)
    35  
    36  			So(DecommissionInactiveStaticHosts(nil), ShouldBeNil)
    37  
    38  			found, err := Find(All)
    39  			So(err, ShouldBeNil)
    40  			So(len(found), ShouldEqual, 2)
    41  			So(found[0].Status, ShouldEqual, evergreen.HostRunning)
    42  			So(found[1].Status, ShouldEqual, evergreen.HostRunning)
    43  
    44  		})
    45  
    46  		Convey("if a non-nil slice is passed in, any static hosts with ids not in"+
    47  			" the slice should be removed from the database", func() {
    48  
    49  			activeOne := &Host{
    50  				Id:       "activeStaticOne",
    51  				Status:   evergreen.HostRunning,
    52  				Provider: evergreen.HostTypeStatic,
    53  			}
    54  			So(activeOne.Insert(), ShouldBeNil)
    55  
    56  			activeTwo := &Host{
    57  				Id:       "activeStaticTwo",
    58  				Status:   evergreen.HostRunning,
    59  				Provider: evergreen.HostTypeStatic,
    60  			}
    61  			So(activeTwo.Insert(), ShouldBeNil)
    62  
    63  			inactiveOne := &Host{
    64  				Id:       "inactiveStaticOne",
    65  				Status:   evergreen.HostRunning,
    66  				Provider: evergreen.HostTypeStatic,
    67  			}
    68  			So(inactiveOne.Insert(), ShouldBeNil)
    69  
    70  			inactiveTwo := &Host{
    71  				Id:       "inactiveStaticTwo",
    72  				Status:   evergreen.HostRunning,
    73  				Provider: evergreen.HostTypeStatic,
    74  			}
    75  			So(inactiveTwo.Insert(), ShouldBeNil)
    76  
    77  			inactiveEC2One := &Host{
    78  				Id:       "inactiveEC2One",
    79  				Status:   evergreen.HostRunning,
    80  				Provider: "ec2-spot",
    81  			}
    82  			So(inactiveEC2One.Insert(), ShouldBeNil)
    83  
    84  			inactiveUnknownTypeOne := &Host{
    85  				Id:     "inactiveUnknownTypeOne",
    86  				Status: evergreen.HostRunning,
    87  			}
    88  			So(inactiveUnknownTypeOne.Insert(), ShouldBeNil)
    89  
    90  			activeStaticHosts := []string{"activeStaticOne", "activeStaticTwo"}
    91  			So(DecommissionInactiveStaticHosts(activeStaticHosts), ShouldBeNil)
    92  
    93  			found, err := Find(IsDecommissioned)
    94  			So(err, ShouldBeNil)
    95  			So(len(found), ShouldEqual, 2)
    96  			So(hostIdInSlice(found, inactiveOne.Id), ShouldBeTrue)
    97  			So(hostIdInSlice(found, inactiveTwo.Id), ShouldBeTrue)
    98  		})
    99  	})
   100  }