github.com/justinjmoses/evergreen@v0.0.0-20170530173719-1d50e381ff0d/cloud/providers/ec2/ec2spot_test.go (about)

     1  package ec2
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/evergreen-ci/evergreen"
     7  	"github.com/evergreen-ci/evergreen/cloud"
     8  	"github.com/evergreen-ci/evergreen/db"
     9  	"github.com/evergreen-ci/evergreen/model/distro"
    10  	"github.com/evergreen-ci/evergreen/model/host"
    11  	"github.com/evergreen-ci/evergreen/testutil"
    12  	. "github.com/smartystreets/goconvey/convey"
    13  )
    14  
    15  func init() {
    16  	db.SetGlobalSessionProvider(db.SessionFactoryFromConfig(testutil.TestConfig()))
    17  }
    18  
    19  func TestSpawnSpotInstance(t *testing.T) {
    20  	testConfig = testutil.TestConfig()
    21  	testutil.ConfigureIntegrationTest(t, testConfig, "TestSpawnSpotInstance")
    22  
    23  	provider := &EC2SpotManager{}
    24  	testutil.HandleTestingErr(provider.Configure(testConfig), t, "error configuring provider")
    25  
    26  	Convey("When spawning many hosts", t, func() {
    27  
    28  		testutil.HandleTestingErr(
    29  			db.ClearCollections(host.Collection), t, "error clearing test collections")
    30  
    31  		hosts := make([]*host.Host, 1)
    32  
    33  		hostOptions := cloud.HostOptions{
    34  			UserName: evergreen.User,
    35  			UserHost: false,
    36  		}
    37  		d := fetchTestDistro()
    38  		for i := range hosts {
    39  			h, err := provider.SpawnInstance(d, hostOptions)
    40  			hosts[i] = h
    41  			So(err, ShouldBeNil)
    42  		}
    43  		Convey("and terminating all of them", func() {
    44  			foundHosts, err := host.Find(host.IsUninitialized)
    45  			So(err, ShouldBeNil)
    46  			So(len(foundHosts), ShouldEqual, 1)
    47  			for _, h := range foundHosts {
    48  				err := provider.TerminateInstance(&h)
    49  				So(err, ShouldBeNil)
    50  			}
    51  			for _, h := range hosts {
    52  				err := provider.TerminateInstance(h)
    53  				So(err, ShouldBeNil)
    54  			}
    55  		})
    56  	})
    57  
    58  }
    59  
    60  func fetchTestDistro() *distro.Distro {
    61  	return &distro.Distro{
    62  		Id:       "test_distro",
    63  		Arch:     "linux_amd64",
    64  		WorkDir:  "/data/mci",
    65  		PoolSize: 10,
    66  		Provider: SpotProviderName,
    67  		ProviderSettings: &map[string]interface{}{
    68  			"ami":            "ami-c7e7f2d0",
    69  			"instance_type":  "t1.micro",
    70  			"key_name":       "mci",
    71  			"bid_price":      .004,
    72  			"security_group": "default",
    73  		},
    74  
    75  		SetupAsSudo: true,
    76  		Setup:       "",
    77  		Teardown:    "",
    78  		User:        "root",
    79  		SSHKey:      "",
    80  	}
    81  }