github.com/billybanfield/evergreen@v0.0.0-20170525200750-eeee692790f7/hostinit/setup_test.go (about) 1 package hostinit 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/cloud/providers" 9 "github.com/evergreen-ci/evergreen/cloud/providers/mock" 10 "github.com/evergreen-ci/evergreen/db" 11 "github.com/evergreen-ci/evergreen/model/distro" 12 "github.com/evergreen-ci/evergreen/model/host" 13 "github.com/evergreen-ci/evergreen/testutil" 14 "github.com/pkg/errors" 15 . "github.com/smartystreets/goconvey/convey" 16 ) 17 18 func init() { 19 db.SetGlobalSessionProvider(db.SessionFactoryFromConfig(testutil.TestConfig())) 20 } 21 22 func TestSetupReadyHosts(t *testing.T) { 23 testutil.ConfigureIntegrationTest(t, testutil.TestConfig(), "TestSetupReadyHosts") 24 25 hostInit := &HostInit{ 26 testutil.TestConfig(), 27 } 28 29 Convey("When hosts are spawned but not running", t, func() { 30 testutil.HandleTestingErr( 31 db.ClearCollections(host.Collection), t, "error clearing test collections") 32 mock.Clear() 33 34 hostsForTest := make([]host.Host, 10) 35 for i := 0; i < 10; i++ { 36 mockHost, err := spawnMockHost() 37 So(err, ShouldBeNil) 38 hostsForTest[i] = *mockHost 39 } 40 So(len(mock.MockInstances), ShouldEqual, 10) 41 42 for i := range hostsForTest { 43 h := hostsForTest[i] 44 So(h.Status, ShouldNotEqual, evergreen.HostRunning) 45 } 46 Convey("and all of the hosts have failed", func() { 47 for id := range mock.MockInstances { 48 instance := mock.MockInstances[id] 49 instance.Status = cloud.StatusFailed 50 instance.DNSName = "dnsName" 51 instance.IsSSHReachable = true 52 mock.MockInstances[id] = instance 53 } 54 Convey("when running setup", func() { 55 err := hostInit.setupReadyHosts() 56 So(err, ShouldBeNil) 57 58 Convey("then all of the hosts should be terminated", func() { 59 for id := range mock.MockInstances { 60 instance := mock.MockInstances[id] 61 So(instance.Status, ShouldEqual, cloud.StatusTerminated) 62 } 63 for i := range hostsForTest { 64 h := hostsForTest[i] 65 dbHost, err := host.FindOne(host.ById(h.Id)) 66 So(err, ShouldBeNil) 67 So(dbHost.Status, ShouldEqual, evergreen.HostTerminated) 68 } 69 }) 70 }) 71 }) 72 73 Convey("and all of the hosts are ready with properly set fields", func() { 74 for id := range mock.MockInstances { 75 instance := mock.MockInstances[id] 76 instance.Status = cloud.StatusRunning 77 instance.DNSName = "dnsName" 78 instance.IsSSHReachable = true 79 mock.MockInstances[id] = instance 80 } 81 Convey("when running setup", func() { 82 err := hostInit.setupReadyHosts() 83 So(err, ShouldBeNil) 84 85 Convey("then all of the 'OnUp' functions should have been run and "+ 86 "host should have been marked as provisioned", func() { 87 for id := range mock.MockInstances { 88 instance := mock.MockInstances[id] 89 So(instance.OnUpRan, ShouldBeTrue) 90 } 91 for i := range hostsForTest { 92 h := hostsForTest[i] 93 dbHost, err := host.FindOne(host.ById(h.Id)) 94 So(err, ShouldBeNil) 95 So(dbHost.Status, ShouldEqual, evergreen.HostRunning) 96 } 97 }) 98 }) 99 }) 100 }) 101 102 } 103 104 func TestHostIsReady(t *testing.T) { 105 testutil.ConfigureIntegrationTest(t, testutil.TestConfig(), "TestHostIsReady") 106 107 hostInit := &HostInit{ 108 testutil.TestConfig(), 109 } 110 111 Convey("When hosts are spawned", t, func() { 112 testutil.HandleTestingErr( 113 db.ClearCollections(host.Collection), t, "error clearing test collections") 114 mock.Clear() 115 116 hostsForTest := make([]host.Host, 10) 117 // Spawn 10 hosts 118 for i := 0; i < 10; i++ { 119 mockHost, err := spawnMockHost() 120 So(err, ShouldBeNil) 121 hostsForTest[i] = *mockHost 122 } 123 So(len(mock.MockInstances), ShouldEqual, 10) 124 125 Convey("and none of the hosts are ready", func() { 126 for id := range mock.MockInstances { 127 instance := mock.MockInstances[id] 128 instance.Status = cloud.StatusInitializing 129 mock.MockInstances[id] = instance 130 } 131 132 Convey("then checking for readiness should return false", func() { 133 for i := range hostsForTest { 134 h := hostsForTest[i] 135 ready, err := hostInit.IsHostReady(&h) 136 So(err, ShouldBeNil) 137 So(ready, ShouldBeFalse) 138 } 139 140 }) 141 }) 142 Convey("and all of the hosts are ready", func() { 143 for id := range mock.MockInstances { 144 instance := mock.MockInstances[id] 145 instance.Status = cloud.StatusRunning 146 mock.MockInstances[id] = instance 147 } 148 Convey("and all of the hosts fields are properly set", func() { 149 for id := range mock.MockInstances { 150 instance := mock.MockInstances[id] 151 instance.DNSName = "dnsName" 152 instance.IsSSHReachable = true 153 mock.MockInstances[id] = instance 154 } 155 Convey("then checking for readiness should return true", func() { 156 for i := range hostsForTest { 157 h := hostsForTest[i] 158 ready, err := hostInit.IsHostReady(&h) 159 So(err, ShouldBeNil) 160 So(ready, ShouldBeTrue) 161 } 162 163 }) 164 }) 165 Convey("and dns is not set", func() { 166 for id := range mock.MockInstances { 167 instance := mock.MockInstances[id] 168 instance.IsSSHReachable = true 169 mock.MockInstances[id] = instance 170 } 171 Convey("then checking for readiness should error", func() { 172 for i := range hostsForTest { 173 h := hostsForTest[i] 174 ready, err := hostInit.IsHostReady(&h) 175 So(err, ShouldNotBeNil) 176 So(ready, ShouldBeFalse) 177 } 178 179 }) 180 }) 181 }) 182 Convey("and all of the hosts failed", func() { 183 for id := range mock.MockInstances { 184 instance := mock.MockInstances[id] 185 instance.Status = cloud.StatusFailed 186 mock.MockInstances[id] = instance 187 } 188 Convey("then checking for readiness should terminate", func() { 189 for i := range hostsForTest { 190 h := hostsForTest[i] 191 ready, err := hostInit.IsHostReady(&h) 192 So(err, ShouldNotBeNil) 193 So(ready, ShouldBeFalse) 194 So(h.Status, ShouldEqual, evergreen.HostTerminated) 195 } 196 for _, instance := range mock.MockInstances { 197 So(instance.Status, ShouldEqual, cloud.StatusTerminated) 198 } 199 }) 200 }) 201 }) 202 203 } 204 205 func spawnMockHost() (*host.Host, error) { 206 mockDistro := distro.Distro{ 207 Id: "mock_distro", 208 Arch: "mock_arch", 209 WorkDir: "src", 210 PoolSize: 10, 211 Provider: mock.ProviderName, 212 } 213 hostOptions := cloud.HostOptions{ 214 UserName: evergreen.User, 215 UserHost: false, 216 } 217 cloudManager, err := providers.GetCloudManager(mock.ProviderName, testutil.TestConfig()) 218 if err != nil { 219 return nil, errors.WithStack(err) 220 } 221 222 newHost, err := cloudManager.SpawnInstance(&mockDistro, hostOptions) 223 if err != nil { 224 return nil, errors.Wrap(err, "Error spawning instance,") 225 } 226 227 return newHost, nil 228 }