Skip to content

Commit a11e5d0

Browse files
committed
Merge pull request flynn#948 from flynn/test-vanilla-vms
test: Support booting vanilla VMs
2 parents 2f0d550 + 8bc97ad commit a11e5d0

File tree

5 files changed

+92
-58
lines changed

5 files changed

+92
-58
lines changed

test/cluster/client/client.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,26 +45,33 @@ func (c *Client) BackoffPeriod() time.Duration {
4545
return c.cluster.BackoffPeriod
4646
}
4747

48-
func (c *Client) AddHost(ch chan *host.HostEvent) (string, error) {
48+
func (c *Client) AddHost(ch chan *host.HostEvent, vanilla bool) (*tc.Instance, error) {
4949
var instance tc.Instance
50-
if err := c.Post("", nil, &instance); err != nil {
51-
return "", err
50+
path := ""
51+
if vanilla {
52+
path = "?vanilla=true"
53+
}
54+
if err := c.Post(path, nil, &instance); err != nil {
55+
return nil, err
5256
}
5357
c.cluster.Instances = append(c.cluster.Instances, &instance)
58+
if vanilla {
59+
return &instance, nil
60+
}
5461
for {
5562
select {
5663
case event := <-ch:
5764
if event.HostID == instance.ID {
58-
return instance.ID, nil
65+
return &instance, nil
5966
}
6067
case <-time.After(60 * time.Second):
61-
return "", fmt.Errorf("timed out waiting for new host")
68+
return nil, fmt.Errorf("timed out waiting for new host")
6269
}
6370
}
6471
}
6572

66-
func (c *Client) RemoveHost(id string) error {
67-
return c.Delete("/" + id)
73+
func (c *Client) RemoveHost(host *tc.Instance) error {
74+
return c.Delete("/" + host.ID)
6875
}
6976

7077
func (c *Client) DumpLogs(out io.Writer) error {

test/cluster/cluster.go

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -149,31 +149,30 @@ func (c *Cluster) BuildFlynn(rootFS, commit string, merge bool, runTests bool) (
149149
return build.Drive("hda").FS, nil
150150
}
151151

152-
func (c *Cluster) Boot(rootFS string, count int, dumpLogs io.Writer, killOnFailure bool) error {
152+
func (c *Cluster) Boot(rootFS string, count int, dumpLogs io.Writer, killOnFailure bool) (err error) {
153153
if err := c.setup(); err != nil {
154154
return err
155155
}
156156

157+
defer func() {
158+
if err != nil {
159+
if dumpLogs != nil && len(c.Instances) > 0 {
160+
c.DumpLogs(dumpLogs)
161+
}
162+
if killOnFailure {
163+
c.Shutdown()
164+
}
165+
}
166+
}()
167+
157168
c.log("Booting", count, "VMs")
158-
_, err := c.startVMs(rootFS, true, count)
169+
_, err = c.startVMs(rootFS, count, true, true)
159170
if err != nil {
160-
if dumpLogs != nil && len(c.Instances) > 0 {
161-
c.DumpLogs(dumpLogs)
162-
}
163-
if killOnFailure {
164-
c.Shutdown()
165-
}
166171
return err
167172
}
168173

169174
c.log("Bootstrapping layer 1...")
170175
if err := c.bootstrapLayer1(); err != nil {
171-
if dumpLogs != nil {
172-
c.DumpLogs(dumpLogs)
173-
}
174-
if killOnFailure {
175-
c.Shutdown()
176-
}
177176
return err
178177
}
179178
c.rootFS = rootFS
@@ -192,7 +191,13 @@ func (c *Cluster) AddHost() (*Instance, error) {
192191
return nil, errors.New("cluster not yet booted")
193192
}
194193
c.log("Booting 1 VM")
195-
instances, err := c.startVMs(c.rootFS, false, 1)
194+
instances, err := c.startVMs(c.rootFS, 1, true, false)
195+
return instances[0], err
196+
}
197+
198+
func (c *Cluster) AddVanillaHost(rootFS string) (*Instance, error) {
199+
c.log("Booting 1 VM")
200+
instances, err := c.startVMs(rootFS, 1, false, false)
196201
return instances[0], err
197202
}
198203

@@ -245,7 +250,7 @@ func (c *Cluster) Size() int {
245250
return len(c.Instances)
246251
}
247252

248-
func (c *Cluster) startVMs(rootFS string, initial bool, count int) ([]*Instance, error) {
253+
func (c *Cluster) startVMs(rootFS string, count int, startFlynnHost, initial bool) ([]*Instance, error) {
249254
tmpl, ok := flynnHostScripts[c.bc.Backend]
250255
if !ok {
251256
return nil, fmt.Errorf("unknown host backend: %s", c.bc.Backend)
@@ -278,6 +283,9 @@ func (c *Cluster) startVMs(rootFS string, initial bool, count int) ([]*Instance,
278283
instances[i] = inst
279284
c.Instances = append(c.Instances, inst)
280285
}
286+
if !startFlynnHost {
287+
return instances, nil
288+
}
281289
peers := make([]string, 0, len(c.Instances))
282290
for _, inst := range c.Instances {
283291
if !inst.initial {

test/helper.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ import (
1414
"github.com/flynn/flynn/controller/client"
1515
ct "github.com/flynn/flynn/controller/types"
1616
"github.com/flynn/flynn/discoverd/client"
17+
"github.com/flynn/flynn/host/types"
1718
"github.com/flynn/flynn/pkg/cluster"
19+
tc "github.com/flynn/flynn/test/cluster"
1820
)
1921

2022
type Helper struct {
@@ -157,6 +159,44 @@ func (h *Helper) stopJob(t *c.C, id string) {
157159
t.Assert(hc.StopJob(jobID), c.IsNil)
158160
}
159161

162+
func (h *Helper) addHost(t *c.C) *tc.Instance {
163+
return h.addHosts(t, 1, false)[0]
164+
}
165+
166+
func (h *Helper) addVanillaHost(t *c.C) *tc.Instance {
167+
return h.addHosts(t, 1, true)[0]
168+
}
169+
170+
func (h *Helper) addHosts(t *c.C, count int, vanilla bool) []*tc.Instance {
171+
debugf(t, "adding %d hosts", count)
172+
173+
ch := make(chan *host.HostEvent)
174+
stream, err := h.clusterClient(t).StreamHostEvents(ch)
175+
t.Assert(err, c.IsNil)
176+
defer stream.Close()
177+
178+
hosts := make([]*tc.Instance, count)
179+
for i := 0; i < count; i++ {
180+
host, err := testCluster.AddHost(ch, vanilla)
181+
t.Assert(err, c.IsNil)
182+
debugf(t, "host added: %s", host.ID)
183+
hosts[i] = host
184+
}
185+
return hosts
186+
}
187+
188+
func (h *Helper) removeHost(t *c.C, host *tc.Instance) {
189+
h.removeHosts(t, []*tc.Instance{host})
190+
}
191+
192+
func (h *Helper) removeHosts(t *c.C, hosts []*tc.Instance) {
193+
debugf(t, "removing %d hosts", len(hosts))
194+
for _, host := range hosts {
195+
t.Assert(testCluster.RemoveHost(host), c.IsNil)
196+
debugf(t, "host removed: %s", host.ID)
197+
}
198+
}
199+
160200
type gitRepo struct {
161201
dir string
162202
ssh *sshData

test/runner/runner.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"log"
1212
"net"
1313
"net/http"
14+
"net/url"
1415
"os"
1516
"path"
1617
"strconv"
@@ -594,7 +595,7 @@ func (r *Runner) save(b *Build) error {
594595
})
595596
}
596597

597-
type clusterHandle func(c *cluster.Cluster, w http.ResponseWriter, ps httprouter.Params) error
598+
type clusterHandle func(*cluster.Cluster, http.ResponseWriter, url.Values, httprouter.Params) error
598599

599600
func (r *Runner) clusterAPI(handle clusterHandle) httprouter.Handle {
600601
return func(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
@@ -609,25 +610,30 @@ func (r *Runner) clusterAPI(handle clusterHandle) httprouter.Handle {
609610
http.Error(w, "cluster not found", 404)
610611
return
611612
}
612-
if err := handle(c, w, ps); err != nil {
613+
if err := handle(c, w, req.URL.Query(), ps); err != nil {
613614
http.Error(w, err.Error(), 500)
614615
}
615616
}
616617
}
617618

618-
func (r *Runner) getCluster(c *cluster.Cluster, w http.ResponseWriter, ps httprouter.Params) error {
619+
func (r *Runner) getCluster(c *cluster.Cluster, w http.ResponseWriter, q url.Values, ps httprouter.Params) error {
619620
return json.NewEncoder(w).Encode(c)
620621
}
621622

622-
func (r *Runner) addHost(c *cluster.Cluster, w http.ResponseWriter, ps httprouter.Params) error {
623-
instance, err := c.AddHost()
623+
func (r *Runner) addHost(c *cluster.Cluster, w http.ResponseWriter, q url.Values, ps httprouter.Params) (err error) {
624+
var instance *cluster.Instance
625+
if q.Get("vanilla") == "" {
626+
instance, err = c.AddHost()
627+
} else {
628+
instance, err = c.AddVanillaHost(args.RootFS)
629+
}
624630
if err != nil {
625631
return err
626632
}
627633
return json.NewEncoder(w).Encode(instance)
628634
}
629635

630-
func (r *Runner) removeHost(c *cluster.Cluster, w http.ResponseWriter, ps httprouter.Params) error {
636+
func (r *Runner) removeHost(c *cluster.Cluster, w http.ResponseWriter, q url.Values, ps httprouter.Params) error {
631637
hostID := ps.ByName("host")
632638
if err := c.RemoveHost(hostID); err != nil {
633639
return err
@@ -636,7 +642,7 @@ func (r *Runner) removeHost(c *cluster.Cluster, w http.ResponseWriter, ps httpro
636642
return nil
637643
}
638644

639-
func (r *Runner) dumpLogs(c *cluster.Cluster, w http.ResponseWriter, ps httprouter.Params) error {
645+
func (r *Runner) dumpLogs(c *cluster.Cluster, w http.ResponseWriter, q url.Values, ps httprouter.Params) error {
640646
c.DumpLogs(w)
641647
return nil
642648
}

test/test_scheduler.go

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
c "github.com/flynn/flynn/Godeps/_workspace/src/github.com/flynn/go-check"
1010
"github.com/flynn/flynn/controller/client"
1111
ct "github.com/flynn/flynn/controller/types"
12-
"github.com/flynn/flynn/host/types"
1312
"github.com/flynn/flynn/pkg/attempt"
1413
"github.com/flynn/flynn/pkg/cluster"
1514
"github.com/flynn/flynn/pkg/stream"
@@ -27,32 +26,6 @@ func (s *SchedulerSuite) checkJobState(t *c.C, appID, jobID, state string) {
2726
t.Assert(job.State, c.Equals, state)
2827
}
2928

30-
func (s *SchedulerSuite) addHosts(t *c.C, count int) []string {
31-
debugf(t, "adding %d hosts", count)
32-
33-
ch := make(chan *host.HostEvent)
34-
stream, err := s.clusterClient(t).StreamHostEvents(ch)
35-
t.Assert(err, c.IsNil)
36-
defer stream.Close()
37-
38-
hostIDs := make([]string, count)
39-
for i := 0; i < count; i++ {
40-
id, err := testCluster.AddHost(ch)
41-
t.Assert(err, c.IsNil)
42-
debugf(t, "host added: %s", id)
43-
hostIDs[i] = id
44-
}
45-
return hostIDs
46-
}
47-
48-
func (s *SchedulerSuite) removeHosts(t *c.C, ids []string) {
49-
debugf(t, "removing %d hosts", len(ids))
50-
for _, id := range ids {
51-
t.Assert(testCluster.RemoveHost(id), c.IsNil)
52-
debugf(t, "host removed: %s", id)
53-
}
54-
}
55-
5629
func jobEventsEqual(expected, actual jobEvents) bool {
5730
for typ, events := range expected {
5831
diff, ok := actual[typ]
@@ -395,7 +368,7 @@ func (s *SchedulerSuite) TestOmniJobs(t *c.C) {
395368
}
396369

397370
// Check that new hosts get omni jobs
398-
newHosts := s.addHosts(t, 2)
371+
newHosts := s.addHosts(t, 2, false)
399372
defer s.removeHosts(t, newHosts)
400373
waitForJobEvents(t, stream, events, jobEvents{"omni": {"up": 2}})
401374
}

0 commit comments

Comments
 (0)