Exemplo n.º 1
0
func (s *S) TestHandleMessageErrors(c *gocheck.C) {
	var data = []struct {
		action      string
		args        []string
		unitName    string
		expectedLog string
	}{
		{
			action:      "unknown-action",
			args:        []string{"does not matter"},
			expectedLog: `Error handling "unknown-action": invalid action.`,
		},
		{
			action: startApp,
			args:   []string{"nemesis"},
			expectedLog: `Error handling "start-app" for the app "nemesis":` +
				` all units must be started.`,
		},
		{
			action: startApp,
			args:   []string{"totem", "totem/0", "totem/1"},
			expectedLog: `Error handling "start-app" for the app "totem":` +
				` all units must be started.`,
		},
		{
			action: regenerateApprc,
			args:   []string{"nemesis"},
			expectedLog: `Error handling "regenerate-apprc" for the app "nemesis":` +
				` all units must be started.`,
		},
		{
			action: regenerateApprc,
			args:   []string{"totem", "totem/0", "totem/1"},
			expectedLog: `Error handling "regenerate-apprc" for the app "totem":` +
				` all units must be started.`,
		},
		{
			action:      regenerateApprc,
			args:        []string{"unknown-app"},
			expectedLog: `Error handling "regenerate-apprc": app "unknown-app" does not exist.`,
		},
		{
			action:      regenerateApprc,
			expectedLog: `Error handling "regenerate-apprc": this action requires at least 1 argument.`,
		},
		{
			action: regenerateApprc,
			args:   []string{"marathon", "marathon/0"},
			expectedLog: `Error handling "regenerate-apprc" for the app "marathon":` +
				` units are in "error" state.`,
		},
		{
			action: regenerateApprc,
			args:   []string{"territories", "territories/0"},
			expectedLog: `Error handling "regenerate-apprc" for the app "territories":` +
				` units are in "down" state.`,
		},
	}
	a := App{Name: "nemesis"}
	err := s.conn.Apps().Insert(a)
	c.Assert(err, gocheck.IsNil)
	defer s.conn.Apps().Remove(bson.M{"name": a.Name})
	a = App{
		Name: "totem",
		Units: []Unit{
			{Name: "totem/0", State: "pending"},
			{Name: "totem/1", State: "started"},
		},
	}
	err = s.conn.Apps().Insert(a)
	c.Assert(err, gocheck.IsNil)
	defer s.conn.Apps().Remove(bson.M{"name": a.Name})
	a = App{Name: "marathon", Units: []Unit{{Name: "marathon/0", State: "error"}}}
	err = s.conn.Apps().Insert(a)
	c.Assert(err, gocheck.IsNil)
	defer s.conn.Apps().Remove(bson.M{"name": a.Name})
	a = App{Name: "territories", Units: []Unit{{Name: "territories/0", State: "down"}}}
	err = s.conn.Apps().Insert(a)
	c.Assert(err, gocheck.IsNil)
	defer s.conn.Apps().Remove(bson.M{"name": a.Name})
	logger := testing.NewFakeLogger().(*testing.FakeLogger)
	for _, d := range data {
		message := queue.Message{Action: d.action}
		if len(d.args) > 0 {
			message.Args = d.args
		}
		handle(&message)
	}
	content := logger.Buf.String()
	lines := strings.Split(content, "\n")
	for i, d := range data {
		if lines[i] != d.expectedLog {
			c.Errorf("\nWant: %q.\nGot:\n%s", d.expectedLog, content)
		}
	}
}
Exemplo n.º 2
0
func (s *S) TestHandleMessageErrors(c *C) {
	var data = []struct {
		action      string
		args        []string
		unitName    string
		expectedLog string
		visits      int
	}{
		{
			action:      "unknown-action",
			args:        []string{"does not matter"},
			expectedLog: `Error handling "unknown-action": invalid action.`,
		},
		{
			action: app.StartApp,
			args:   []string{"nemesis"},
			expectedLog: `Error handling "start-app" for the app "nemesis":` +
				` The status of the app and all units should be "started" (the app is "pending").`,
		},
		{
			action: app.StartApp,
			args:   []string{"totem", "totem/0", "totem/1"},
			expectedLog: `Error handling "start-app" for the app "totem":` +
				` The status of the app and all units should be "started" (the app is "started").`,
		},
		{
			action: app.RegenerateApprc,
			args:   []string{"nemesis"},
			expectedLog: `Error handling "regenerate-apprc" for the app "nemesis":` +
				` The status of the app and all units should be "started" (the app is "pending").`,
		},
		{
			action: app.RegenerateApprc,
			args:   []string{"totem", "totem/0", "totem/1"},
			expectedLog: `Error handling "regenerate-apprc" for the app "totem":` +
				` The status of the app and all units should be "started" (the app is "started").`,
		},
		{
			action:      app.RegenerateApprc,
			args:        []string{"unknown-app"},
			expectedLog: `Error handling "regenerate-apprc": app "unknown-app" does not exist.`,
		},
		{
			action:      app.RegenerateApprc,
			expectedLog: `Error handling "regenerate-apprc": this action requires at least 1 argument.`,
		},
		{
			action:      "does not matter",
			args:        []string{"does not matter"},
			expectedLog: `Error handling "does not matter": this message has been visited more than 50 times.`,
			visits:      MaxVisits,
		},
		{
			action: app.RegenerateApprc,
			args:   []string{"marathon"},
			expectedLog: `Error handling "regenerate-apprc" for the app "marathon":` +
				` the app is in "error" state.`,
		},
		{
			action: app.RegenerateApprc,
			args:   []string{"territories"},
			expectedLog: `Error handling "regenerate-apprc" for the app "territories":` +
				` the app is down.`,
		},
	}
	var buf bytes.Buffer
	a := app.App{Name: "nemesis", State: "pending"}
	err := db.Session.Apps().Insert(a)
	c.Assert(err, IsNil)
	defer db.Session.Apps().Remove(bson.M{"name": a.Name})
	a = app.App{
		Name:  "totem",
		State: "started",
		Units: []app.Unit{
			{Name: "totem/0", State: "pending"},
			{Name: "totem/1", State: "started"},
		},
	}
	err = db.Session.Apps().Insert(a)
	c.Assert(err, IsNil)
	defer db.Session.Apps().Remove(bson.M{"name": a.Name})
	a = app.App{Name: "marathon", State: "error"}
	err = db.Session.Apps().Insert(a)
	c.Assert(err, IsNil)
	defer db.Session.Apps().Remove(bson.M{"name": a.Name})
	a = app.App{Name: "territories", State: "down"}
	err = db.Session.Apps().Insert(a)
	c.Assert(err, IsNil)
	defer db.Session.Apps().Remove(bson.M{"name": a.Name})
	log.SetLogger(stdlog.New(&buf, "", 0))
	handler := MessageHandler{}
	handler.start()
	handler.closed = 1
	defer handler.stop()
	for _, d := range data {
		message := queue.Message{
			Action: d.action,
			Visits: d.visits,
		}
		if len(d.args) > 0 {
			message.Args = d.args
		}
		handler.handle(message)
	}
	content := buf.String()
	lines := strings.Split(content, "\n")
	for i, d := range data {
		var found bool
		for j := i; j < len(lines); j++ {
			if lines[j] == d.expectedLog {
				found = true
				break
			}
		}
		if !found {
			c.Errorf("\nWant: %q.\nGot:\n%s", d.expectedLog, content)
		}
	}
}