Exemple #1
0
func (s *S) TestHandleRestartAppMessage(c *C) {
	s.provisioner.PrepareOutput([]byte("started"))
	handler := MessageHandler{}
	err := handler.start()
	c.Assert(err, IsNil)
	defer handler.stop()
	a := app.App{
		Name: "nemesis",
		Units: []app.Unit{
			{
				Name:    "i-00800",
				State:   "started",
				Machine: 19,
			},
		},
		State: string(provision.StatusStarted),
	}
	err = db.Session.Apps().Insert(a)
	c.Assert(err, IsNil)
	defer db.Session.Apps().Remove(bson.M{"name": a.Name})
	messages, _, err := queue.Dial(handler.server.Addr())
	c.Assert(err, IsNil)
	messages <- queue.Message{Action: app.StartApp, Args: []string{a.Name}}
	time.Sleep(1e9)
	cmds := s.provisioner.GetCmds("/var/lib/tsuru/hooks/restart", &a)
	c.Assert(cmds, HasLen, 1)
}
Exemple #2
0
func (a *App) enqueue(msgs ...queue.Message) error {
	addr, err := config.GetString("queue-server")
	if err != nil {
		return err
	}
	messages, _, err := queue.Dial(addr)
	if err != nil {
		return err
	}
	for _, msg := range msgs {
		messages <- msg
	}
	close(messages)
	return nil
}
Exemple #3
0
// This example demonstrates how to dial to a queue server.
func ExampleDial() {
	messages, errors, err := queue.Dial("127.0.0.1:2021")
	if err != nil {
		log.Panicf("Failed to dial: %s", err)
	}
	message := queue.Message{
		Action: "delete-files",
		Args:   []string{"/home/gopher/file.txt"},
	}
	messages <- message
	close(messages) // closes the channel and the connection.
	if err, ok := <-errors; ok && err != nil {
		log.Panicf("Failed to send the message: %s", err)
	}
}
Exemple #4
0
func (s *S) TestHandleMessageWithSpecificUnit(c *C) {
	s.provisioner.PrepareOutput([]byte("exported"))
	handler := MessageHandler{}
	err := handler.start()
	c.Assert(err, IsNil)
	defer handler.stop()
	a := app.App{
		Name: "nemesis",
		Units: []app.Unit{
			{
				Name:    "nemesis/0",
				State:   "started",
				Machine: 19,
			},
			{
				Name:    "nemesis/1",
				State:   "started",
				Machine: 20,
			},
			{
				Name:    "nemesis/2",
				State:   "started",
				Machine: 23,
			},
		},
		Env: map[string]bind.EnvVar{
			"http_proxy": {
				Name:   "http_proxy",
				Value:  "http://myproxy.com:3128/",
				Public: true,
			},
		},
		State: string(provision.StatusStarted),
	}
	err = db.Session.Apps().Insert(a)
	c.Assert(err, IsNil)
	defer db.Session.Apps().Remove(bson.M{"name": a.Name})
	messages, _, err := queue.Dial(handler.server.Addr())
	c.Assert(err, IsNil)
	messages <- queue.Message{Action: app.RegenerateApprc, Args: []string{a.Name, "nemesis/1"}}
	time.Sleep(1e9)
	cmds := s.provisioner.GetCmds("", &a)
	c.Assert(cmds, HasLen, 1)
	output := strings.Replace(cmds[0].Cmd, "\n", " ", -1)
	outputRegexp := `^cat > /home/application/apprc <<END # generated by tsuru.*`
	outputRegexp += `export http_proxy="http://myproxy.com:3128/" END $`
	c.Assert(output, Matches, outputRegexp)
}