示例#1
0
// TODO(fss): simplify this test. Right now, it's a little monster.
func (s *S) TestCreateApp(c *C) {
	patchRandomReader()
	defer unpatchRandomReader()
	h := testHandler{}
	ts := s.t.StartGandalfTestServer(&h)
	defer ts.Close()
	server := testing.FakeQueueServer{}
	server.Start("127.0.0.1:0")
	defer server.Stop()
	a := App{
		Name:      "appname",
		Framework: "django",
		Units:     []Unit{{Machine: 3}},
	}
	expectedHost := "localhost"
	config.Set("host", expectedHost)
	old, err := config.Get("queue-server")
	if err != nil {
		defer config.Set("queue-server", old)
	}
	config.Set("queue-server", server.Addr())

	err = CreateApp(&a, 3)
	c.Assert(err, IsNil)
	defer a.Destroy()
	c.Assert(a.State, Equals, "pending")
	var retrievedApp App
	err = db.Session.Apps().Find(bson.M{"name": a.Name}).One(&retrievedApp)
	c.Assert(err, IsNil)
	c.Assert(retrievedApp.Name, Equals, a.Name)
	c.Assert(retrievedApp.Framework, Equals, a.Framework)
	c.Assert(retrievedApp.State, Equals, a.State)
	env := a.InstanceEnv(s3InstanceName)
	c.Assert(env["TSURU_S3_ENDPOINT"].Value, Equals, s.t.S3Server.URL())
	c.Assert(env["TSURU_S3_ENDPOINT"].Public, Equals, false)
	c.Assert(env["TSURU_S3_LOCATIONCONSTRAINT"].Value, Equals, "true")
	c.Assert(env["TSURU_S3_LOCATIONCONSTRAINT"].Public, Equals, false)
	e, ok := env["TSURU_S3_ACCESS_KEY_ID"]
	c.Assert(ok, Equals, true)
	c.Assert(e.Public, Equals, false)
	e, ok = env["TSURU_S3_SECRET_KEY"]
	c.Assert(ok, Equals, true)
	c.Assert(e.Public, Equals, false)
	c.Assert(env["TSURU_S3_BUCKET"].Value, HasLen, maxBucketSize)
	c.Assert(env["TSURU_S3_BUCKET"].Value, Equals, "appnamee3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3")
	c.Assert(env["TSURU_S3_BUCKET"].Public, Equals, false)
	env = a.InstanceEnv("")
	c.Assert(env["APPNAME"].Value, Equals, a.Name)
	c.Assert(env["APPNAME"].Public, Equals, false)
	c.Assert(env["TSURU_HOST"].Value, Equals, expectedHost)
	c.Assert(env["TSURU_HOST"].Public, Equals, false)
	expectedMessage := queue.Message{
		Action: RegenerateApprc,
		Args:   []string{a.Name},
	}
	c.Assert(server.Messages(), DeepEquals, []queue.Message{expectedMessage})
	c.Assert(s.provisioner.GetUnits(&a), HasLen, 3)
}
示例#2
0
func (s *S) TestCreateBucketForward(c *C) {
	patchRandomReader()
	defer unpatchRandomReader()
	server := testing.FakeQueueServer{}
	server.Start("127.0.0.1:0")
	defer server.Stop()
	a := App{
		Name:      "appname",
		Framework: "django",
		Units:     []Unit{{Machine: 3}},
	}
	expectedHost := "localhost"
	config.Set("host", expectedHost)
	old, err := config.Get("queue-server")
	if err != nil {
		defer config.Set("queue-server", old)
	}
	config.Set("queue-server", server.Addr())
	insert := new(insertApp)
	err = insert.forward(&a)
	c.Assert(err, IsNil)
	defer insert.backward(&a)
	bucket := new(createBucketIam)
	err = bucket.forward(&a)
	c.Assert(err, IsNil)
	defer bucket.backward(&a)
	de := new(provisionApp)
	err = de.forward(&a)
	c.Assert(err, IsNil)
	defer Provisioner.Destroy(&a)
	env := a.InstanceEnv(s3InstanceName)
	c.Assert(env["TSURU_S3_ENDPOINT"].Value, Equals, s.t.S3Server.URL())
	c.Assert(env["TSURU_S3_ENDPOINT"].Public, Equals, false)
	c.Assert(env["TSURU_S3_LOCATIONCONSTRAINT"].Value, Equals, "true")
	c.Assert(env["TSURU_S3_LOCATIONCONSTRAINT"].Public, Equals, false)
	e, ok := env["TSURU_S3_ACCESS_KEY_ID"]
	c.Assert(ok, Equals, true)
	c.Assert(e.Public, Equals, false)
	e, ok = env["TSURU_S3_SECRET_KEY"]
	c.Assert(ok, Equals, true)
	c.Assert(e.Public, Equals, false)
	c.Assert(env["TSURU_S3_BUCKET"].Value, HasLen, maxBucketSize)
	c.Assert(env["TSURU_S3_BUCKET"].Value, Equals, "appnamee3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3")
	c.Assert(env["TSURU_S3_BUCKET"].Public, Equals, false)
	env = a.InstanceEnv("")
	c.Assert(env["APPNAME"].Value, Equals, a.Name)
	c.Assert(env["APPNAME"].Public, Equals, false)
	c.Assert(env["TSURU_HOST"].Value, Equals, expectedHost)
	c.Assert(env["TSURU_HOST"].Public, Equals, false)
	expectedMessage := queue.Message{
		Action: RegenerateApprc,
		Args:   []string{a.Name},
	}
	c.Assert(server.Messages(), DeepEquals, []queue.Message{expectedMessage})
}
示例#3
0
func (s *S) TestAddUnits(c *C) {
	server := testing.FakeQueueServer{}
	server.Start("127.0.0.1:0")
	defer server.Stop()
	old, err := config.Get("queue-server")
	if err != nil {
		defer config.Set("queue-server", old)
	}
	config.Set("queue-server", server.Addr())
	app := App{Name: "warpaint", Framework: "python"}
	err = db.Session.Apps().Insert(app)
	c.Assert(err, IsNil)
	defer db.Session.Apps().Remove(bson.M{"name": app.Name})
	s.provisioner.Provision(&app)
	defer s.provisioner.Destroy(&app)
	err = app.AddUnits(5)
	c.Assert(err, IsNil)
	units := s.provisioner.GetUnits(&app)
	c.Assert(units, HasLen, 6)
	err = app.AddUnits(2)
	c.Assert(err, IsNil)
	units = s.provisioner.GetUnits(&app)
	c.Assert(units, HasLen, 8)
	for _, unit := range units {
		c.Assert(unit.AppName, Equals, app.Name)
	}
	err = app.Get()
	c.Assert(err, IsNil)
	c.Assert(app.Units, HasLen, 7)
	var expectedMessages []queue.Message
	names := make([]string, len(app.Units))
	for i, unit := range app.Units {
		names[i] = unit.Name
		expected := fmt.Sprintf("%s/%d", app.Name, i+1)
		c.Assert(unit.Name, Equals, expected)
		messages := []queue.Message{
			{Action: RegenerateApprc, Args: []string{app.Name, unit.Name}},
			{Action: StartApp, Args: []string{app.Name, unit.Name}},
		}
		expectedMessages = append(expectedMessages, messages...)
	}
	time.Sleep(1e6)
	c.Assert(server.Messages(), DeepEquals, expectedMessages)
}
示例#4
0
func (s *S) TestCreateBucketForward(c *C) {
	patchRandomReader()
	defer unpatchRandomReader()
	server := testing.FakeQueueServer{}
	server.Start("127.0.0.1:0")
	defer server.Stop()
	a := App{
		Name:      "appname",
		Framework: "django",
		Units:     []Unit{{Machine: 3}},
	}
	expectedHost := "localhost"
	config.Set("host", expectedHost)
	old, err := config.Get("queue-server")
	if err != nil {
		defer config.Set("queue-server", old)
	}
	config.Set("queue-server", server.Addr())
	insert := new(insertApp)
	err = insert.forward(&a)
	c.Assert(err, IsNil)
	defer insert.backward(&a)
	bucket := new(createBucketIam)
	err = bucket.forward(&a)
	c.Assert(err, IsNil)
	defer bucket.backward(&a)
	err = a.Get()
	c.Assert(err, IsNil)
	env := a.InstanceEnv(s3InstanceName)
	c.Assert(env["TSURU_S3_ENDPOINT"].Value, Equals, s.t.S3Server.URL())
	c.Assert(env["TSURU_S3_ENDPOINT"].Public, Equals, false)
	c.Assert(env["TSURU_S3_LOCATIONCONSTRAINT"].Value, Equals, "true")
	c.Assert(env["TSURU_S3_LOCATIONCONSTRAINT"].Public, Equals, false)
	e, ok := env["TSURU_S3_ACCESS_KEY_ID"]
	c.Assert(ok, Equals, true)
	c.Assert(e.Public, Equals, false)
	e, ok = env["TSURU_S3_SECRET_KEY"]
	c.Assert(ok, Equals, true)
	c.Assert(e.Public, Equals, false)
	c.Assert(env["TSURU_S3_BUCKET"].Value, HasLen, maxBucketSize)
	c.Assert(env["TSURU_S3_BUCKET"].Value, Equals, "appnamee3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3")
	c.Assert(env["TSURU_S3_BUCKET"].Public, Equals, false)
	env = a.InstanceEnv("")
	c.Assert(env["APPNAME"].Value, Equals, a.Name)
	c.Assert(env["APPNAME"].Public, Equals, false)
	c.Assert(env["TSURU_HOST"].Value, Equals, expectedHost)
	c.Assert(env["TSURU_HOST"].Public, Equals, false)
	ran := make(chan bool, 1)
	go func() {
		expectedMessages := []queue.Message{{
			Action: RegenerateApprc,
			Args:   []string{a.Name},
		}}
		for {
			if reflect.DeepEqual(expectedMessages, server.Messages()) {
				ran <- true
			}
		}
	}()
	select {
	case <-ran:
	case <-time.After(2e9):
		c.Fatal("TIMEOUT: Did not received the expected message in the queue after 2 seconds.")
	}
}