Пример #1
0
func (s *DeploySuite) TestDeployList(c *check.C) {
	user, _ := s.token.User()
	app1 := app.App{Name: "g1", Platform: "python", Teams: []string{s.team.Name}}
	err := app.CreateApp(&app1, user)
	c.Assert(err, check.IsNil)
	defer app.Delete(&app1)
	app2 := app.App{Name: "ge", Platform: "python", Teams: []string{s.team.Name}}
	err = app.CreateApp(&app2, user)
	c.Assert(err, check.IsNil)
	defer app.Delete(&app2)
	var result []app.DeployData
	request, err := http.NewRequest("GET", "/deploys", nil)
	c.Assert(err, check.IsNil)
	recorder := httptest.NewRecorder()
	timestamp := time.Date(2013, time.November, 1, 0, 0, 0, 0, time.Local)
	duration := time.Since(timestamp)
	err = s.conn.Deploys().Insert(app.DeployData{App: "g1", Timestamp: timestamp.Add(time.Minute), Duration: duration})
	c.Assert(err, check.IsNil)
	err = s.conn.Deploys().Insert(app.DeployData{App: "ge", Timestamp: timestamp.Add(time.Second), Duration: duration})
	c.Assert(err, check.IsNil)
	defer s.conn.Deploys().RemoveAll(nil)
	request.Header.Set("Authorization", "bearer "+s.token.GetValue())
	server := RunServer(true)
	server.ServeHTTP(recorder, request)
	c.Assert(recorder.Code, check.Equals, http.StatusOK)
	err = json.Unmarshal(recorder.Body.Bytes(), &result)
	c.Assert(err, check.IsNil)
	c.Assert(result[0].ID, check.NotNil)
	c.Assert(result[0].App, check.Equals, "g1")
	c.Assert(result[0].Timestamp.In(time.UTC), check.DeepEquals, timestamp.Add(time.Minute).In(time.UTC))
	c.Assert(result[0].Duration, check.DeepEquals, duration)
	c.Assert(result[1].App, check.Equals, "ge")
	c.Assert(result[1].Timestamp.In(time.UTC), check.DeepEquals, timestamp.Add(time.Second).In(time.UTC))
	c.Assert(result[1].Duration, check.DeepEquals, duration)
}
Пример #2
0
func (s *DeploySuite) TestDeployRollbackHandlerWithOnlyVersionImage(c *check.C) {
	user, _ := s.token.User()
	a := app.App{Name: "otherapp", Platform: "python", Teams: []string{s.team.Name}}
	err := app.CreateApp(&a, user)
	c.Assert(err, check.IsNil)
	defer app.Delete(&a, nil)
	defer s.logConn.Logs(a.Name).DropCollection()
	timestamp := time.Date(2013, time.November, 1, 0, 0, 0, 0, time.Local)
	duration := time.Since(timestamp)
	deploys := []app.DeployData{
		{App: "otherapp", Timestamp: timestamp, Duration: duration, Image: "registry.tsuru.globoi.com/tsuru/app-example:v2", CanRollback: true},
		{App: "otherapp", Timestamp: timestamp, Duration: duration, Image: "127.0.0.1:5000/tsuru/app-tsuru-dashboard:v1", CanRollback: true},
	}
	for _, deploy := range deploys {
		err = s.conn.Deploys().Insert(deploy)
		c.Assert(err, check.IsNil)
	}
	defer s.conn.Deploys().RemoveAll(nil)
	url := fmt.Sprintf("/apps/%s/deploy/rollback", a.Name)
	request, err := http.NewRequest("POST", url, strings.NewReader("image=v1"))
	c.Assert(err, check.IsNil)
	request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
	recorder := httptest.NewRecorder()
	request.Header.Set("Authorization", "bearer "+s.token.GetValue())
	server := RunServer(true)
	server.ServeHTTP(recorder, request)
	c.Assert(recorder.Code, check.Equals, http.StatusOK)
	c.Assert(recorder.Header().Get("Content-Type"), check.Equals, "application/json")
	c.Assert(recorder.Code, check.Equals, http.StatusOK)
	c.Assert(recorder.Body.String(), check.Equals, "{\"Message\":\"Image deploy called\"}\n")
}
Пример #3
0
func appDelete(w http.ResponseWriter, r *http.Request, t auth.Token) error {
	u, err := t.User()
	if err != nil {
		return err
	}
	a, err := getAppFromContext(r.URL.Query().Get(":app"), r)
	if err != nil {
		return err
	}
	canDelete := permission.Check(t, permission.PermAppDelete,
		append(permission.Contexts(permission.CtxTeam, a.Teams),
			permission.Context(permission.CtxApp, a.Name),
			permission.Context(permission.CtxPool, a.Pool),
		)...,
	)
	if !canDelete {
		return permission.ErrUnauthorized
	}
	rec.Log(u.Email, "app-delete", "app="+a.Name)
	keepAliveWriter := tsuruIo.NewKeepAliveWriter(w, 30*time.Second, "")
	defer keepAliveWriter.Stop()
	writer := &tsuruIo.SimpleJsonMessageEncoderWriter{Encoder: json.NewEncoder(keepAliveWriter)}
	err = app.Delete(&a, writer)
	if err != nil {
		writer.Encode(tsuruIo.SimpleJsonMessage{Error: err.Error()})
	}
	return nil
}
Пример #4
0
func (s *DeploySuite) TestDeployWithTokenForInternalAppName(c *check.C) {
	token, err := nativeScheme.AppLogin(app.InternalAppName)
	c.Assert(err, check.IsNil)
	a := app.App{
		Name:      "otherapp",
		Platform:  "python",
		TeamOwner: s.team.Name,
		Plan:      app.Plan{Router: "fake"},
	}
	user, _ := s.token.User()
	err = app.CreateApp(&a, user)
	c.Assert(err, check.IsNil)
	defer app.Delete(&a, nil)
	url := fmt.Sprintf("/apps/%s/repository/clone?:appname=%s", a.Name, a.Name)
	request, err := http.NewRequest("POST", url, strings.NewReader("archive-url=http://something.tar.gz&user=fulano"))
	c.Assert(err, check.IsNil)
	request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
	recorder := httptest.NewRecorder()
	request.Header.Set("Authorization", "bearer "+token.GetValue())
	server := RunServer(true)
	server.ServeHTTP(recorder, request)
	c.Assert(recorder.Code, check.Equals, http.StatusOK)
	c.Assert(recorder.Header().Get("Content-Type"), check.Equals, "text")
	c.Assert(recorder.Code, check.Equals, http.StatusOK)
	c.Assert(recorder.Body.String(), check.Equals, "Archive deploy called\nOK\n")
}
Пример #5
0
func (s *DeploySuite) TestDeployWithCommitUserToken(c *check.C) {
	user, _ := s.token.User()
	a := app.App{
		Name:      "otherapp",
		Platform:  "python",
		TeamOwner: s.team.Name,
		Plan:      app.Plan{Router: "fake"},
	}
	err := app.CreateApp(&a, user)
	c.Assert(err, check.IsNil)
	defer app.Delete(&a, nil)
	url := fmt.Sprintf("/apps/%s/repository/clone?:appname=%s", a.Name, a.Name)
	request, err := http.NewRequest("POST", url, strings.NewReader("archive-url=http://something.tar.gz&user=fulano&commit=123"))
	c.Assert(err, check.IsNil)
	request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
	recorder := httptest.NewRecorder()
	request.Header.Set("Authorization", "bearer "+s.token.GetValue())
	server := RunServer(true)
	server.ServeHTTP(recorder, request)
	c.Assert(recorder.Code, check.Equals, http.StatusOK)
	c.Assert(recorder.Header().Get("Content-Type"), check.Equals, "text")
	c.Assert(recorder.Code, check.Equals, http.StatusOK)
	c.Assert(recorder.Body.String(), check.Equals, "Archive deploy called\nOK\n")
	deploys, err := s.conn.Deploys().Find(bson.M{"commit": "123"}).Count()
	c.Assert(err, check.IsNil)
	c.Assert(deploys, check.Equals, 0)
}
Пример #6
0
func (s *DeploySuite) TestDeployUploadFile(c *check.C) {
	user, _ := s.token.User()
	a := app.App{Name: "otherapp", Platform: "python", Teams: []string{s.team.Name}}
	err := app.CreateApp(&a, user)
	c.Assert(err, check.IsNil)
	defer app.Delete(&a, nil)
	url := fmt.Sprintf("/apps/%s/repository/clone?:appname=%s", a.Name, a.Name)
	var body bytes.Buffer
	writer := multipart.NewWriter(&body)
	file, err := writer.CreateFormFile("file", "archive.tar.gz")
	c.Assert(err, check.IsNil)
	file.Write([]byte("hello world!"))
	writer.Close()
	request, err := http.NewRequest("POST", url, &body)
	c.Assert(err, check.IsNil)
	request.Header.Set("Content-Type", "multipart/form-data; boundary="+writer.Boundary())
	recorder := httptest.NewRecorder()
	request.Header.Set("Authorization", "bearer "+s.token.GetValue())
	server := RunServer(true)
	server.ServeHTTP(recorder, request)
	c.Assert(recorder.Code, check.Equals, http.StatusOK)
	c.Assert(recorder.Header().Get("Content-Type"), check.Equals, "text")
	c.Assert(recorder.Code, check.Equals, http.StatusOK)
	c.Assert(recorder.Body.String(), check.Equals, "Upload deploy called\nOK\n")
}
Пример #7
0
func (s *S) BenchmarkScanLogs(c *check.C) {
	c.StopTimer()
	var apps []app.App
	for i := 0; i < 200; i++ {
		a := app.App{Name: fmt.Sprintf("myapp-%d", i), Platform: "zend", TeamOwner: s.team.Name}
		apps = append(apps, a)
		err := app.CreateApp(&a, s.user)
		c.Assert(err, check.IsNil)
		defer app.Delete(&a, nil)
	}
	baseMsg := `{"date": "2015-06-16T15:00:00.000Z", "message": "msg-%d", "source": "web", "appname": "%s", "unit": "unit1"}` + "\n"
	for i := range apps {
		// Remove overhead for first message from app from benchmark.
		err := scanLogs(strings.NewReader(fmt.Sprintf(baseMsg, 0, apps[i].Name)))
		c.Assert(err, check.IsNil)
	}
	r, w := io.Pipe()
	go func() {
		for i := 0; i < c.N; i++ {
			msg := fmt.Sprintf(baseMsg, i, apps[i%len(apps)].Name)
			_, err := w.Write([]byte(msg))
			c.Assert(err, check.IsNil)
		}
		w.Close()
	}()
	c.StartTimer()
	err := scanLogs(r)
	c.Assert(err, check.IsNil)
	c.StopTimer()
}
Пример #8
0
func (s *DeploySuite) TestDeployInfoByAdminUser(c *check.C) {
	a := app.App{Name: "g1", Platform: "python", Teams: []string{s.team.Name}}
	user, _ := s.token.User()
	err := app.CreateApp(&a, user)
	c.Assert(err, check.IsNil)
	defer app.Delete(&a, nil)
	recorder := httptest.NewRecorder()
	timestamp := time.Now()
	duration := time.Duration(10e9)
	previousDeploy := app.DeployData{App: "g1", Timestamp: timestamp.Add(-3600 * time.Second), Duration: duration, Commit: "e293e3e3me03ejm3puejmp3ej3iejop32", Error: ""}
	err = s.conn.Deploys().Insert(previousDeploy)
	c.Assert(err, check.IsNil)
	lastDeploy := app.DeployData{App: "g1", Timestamp: timestamp, Duration: duration, Commit: "e82nn93nd93mm12o2ueh83dhbd3iu112", Error: ""}
	err = s.conn.Deploys().Insert(lastDeploy)
	c.Assert(err, check.IsNil)
	defer s.conn.Deploys().RemoveAll(nil)
	var d map[string]interface{}
	err = s.conn.Deploys().Find(bson.M{"commit": lastDeploy.Commit}).One(&d)
	c.Assert(err, check.IsNil)
	lastDeployId := d["_id"].(bson.ObjectId).Hex()
	url := fmt.Sprintf("/deploys/%s", lastDeployId)
	request, err := http.NewRequest("GET", url, nil)
	c.Assert(err, check.IsNil)
	request.Header.Set("Authorization", "bearer "+s.token.GetValue())
	server := RunServer(true)
	server.ServeHTTP(recorder, request)
	c.Assert(recorder.Code, check.Equals, http.StatusOK)
	var result app.DeployData
	err = json.Unmarshal(recorder.Body.Bytes(), &result)
	c.Assert(err, check.IsNil)
	lastDeploy.ID = d["_id"].(bson.ObjectId)
	result.Timestamp = lastDeploy.Timestamp
	result.RemoveDate = lastDeploy.RemoveDate
	c.Assert(result, check.DeepEquals, lastDeploy)
}
Пример #9
0
func (s *DeploySuite) TestDeployListByAppWithImage(c *check.C) {
	user, _ := s.token.User()
	a := app.App{Name: "myblog", Platform: "python", Teams: []string{s.team.Name}}
	err := app.CreateApp(&a, user)
	c.Assert(err, check.IsNil)
	defer app.Delete(&a, nil)
	defer s.logConn.Logs(a.Name).DropCollection()
	timestamp := time.Date(2013, time.November, 1, 0, 0, 0, 0, time.Local)
	duration := time.Since(timestamp)
	deploys := []app.DeployData{
		{App: "myblog", Timestamp: timestamp, Duration: duration, Image: "registry.tsuru.globoi.com/tsuru/app-example:v2", CanRollback: true},
		{App: "yourblog", Timestamp: timestamp, Duration: duration, Image: "127.0.0.1:5000/tsuru/app-tsuru-dashboard:v1", CanRollback: true},
	}
	for _, deploy := range deploys {
		err = s.conn.Deploys().Insert(deploy)
		c.Assert(err, check.IsNil)
	}
	defer s.conn.Deploys().RemoveAll(nil)
	recorder := httptest.NewRecorder()
	request, err := http.NewRequest("GET", "/deploys?app=myblog", nil)
	request.Header.Set("Authorization", "bearer "+s.token.GetValue())
	server := RunServer(true)
	server.ServeHTTP(recorder, request)
	c.Assert(recorder.Code, check.Equals, http.StatusOK)
	var result []app.DeployData
	err = json.Unmarshal(recorder.Body.Bytes(), &result)
	c.Assert(err, check.IsNil)
	c.Assert(result, check.HasLen, 1)
	c.Assert(result[0].Image, check.Equals, "v2")
	c.Assert(result[0].App, check.Equals, "myblog")
	c.Assert(result[0].Timestamp.In(time.UTC), check.DeepEquals, timestamp.In(time.UTC))
	c.Assert(result[0].Duration, check.DeepEquals, duration)
}
Пример #10
0
Файл: app.go Проект: tsuru/tsuru
// title: remove app
// path: /apps/{name}
// method: DELETE
// produce: application/x-json-stream
// responses:
//   200: App removed
//   401: Unauthorized
//   404: Not found
func appDelete(w http.ResponseWriter, r *http.Request, t auth.Token) (err error) {
	r.ParseForm()
	a, err := getAppFromContext(r.URL.Query().Get(":app"), r)
	if err != nil {
		return err
	}
	canDelete := permission.Check(t, permission.PermAppDelete,
		contextsForApp(&a)...,
	)
	if !canDelete {
		return permission.ErrUnauthorized
	}
	evt, err := event.New(&event.Opts{
		Target:     appTarget(a.Name),
		Kind:       permission.PermAppDelete,
		Owner:      t,
		CustomData: event.FormToCustomData(r.Form),
		Allowed:    event.Allowed(permission.PermAppReadEvents, contextsForApp(&a)...),
	})
	if err != nil {
		return err
	}
	defer func() { evt.Done(err) }()
	keepAliveWriter := tsuruIo.NewKeepAliveWriter(w, 30*time.Second, "")
	defer keepAliveWriter.Stop()
	writer := &tsuruIo.SimpleJsonMessageEncoderWriter{Encoder: json.NewEncoder(keepAliveWriter)}
	w.Header().Set("Content-Type", "application/x-json-stream")
	return app.Delete(&a, writer)
}
Пример #11
0
func (s *DeploySuite) TestDeployListAppWithNoDeploys(c *check.C) {
	user, _ := s.token.User()
	a := app.App{Name: "myblog", Platform: "python", Teams: []string{s.team.Name}}
	err := app.CreateApp(&a, user)
	c.Assert(err, check.IsNil)
	defer app.Delete(&a, nil)
	defer s.logConn.Logs(a.Name).DropCollection()
	recorder := httptest.NewRecorder()
	request, err := http.NewRequest("GET", "/deploys?app=myblog", nil)
	request.Header.Set("Authorization", "bearer "+s.token.GetValue())
	server := RunServer(true)
	server.ServeHTTP(recorder, request)
	c.Assert(recorder.Code, check.Equals, http.StatusNoContent)
}
Пример #12
0
func appDelete(w http.ResponseWriter, r *http.Request, t *auth.Token) error {
	u, err := t.User()
	if err != nil {
		return err
	}
	rec.Log(u.Email, "app-delete", r.URL.Query().Get(":app"))
	a, err := getApp(r.URL.Query().Get(":app"), u)
	if err != nil {
		return err
	}
	app.Delete(&a)
	fmt.Fprint(w, "success")
	return nil
}
Пример #13
0
func (s *DeploySuite) TestDeployShouldReturnForbiddenWhenTokenIsntFromTheApp(c *check.C) {
	user, _ := s.token.User()
	app1 := app.App{Name: "otherapp", Platform: "python", TeamOwner: s.team.Name}
	err := app.CreateApp(&app1, user)
	c.Assert(err, check.IsNil)
	defer app.Delete(&app1, nil)
	app2 := app.App{Name: "superapp", Platform: "python", TeamOwner: s.team.Name}
	err = app.CreateApp(&app2, user)
	c.Assert(err, check.IsNil)
	defer app.Delete(&app2, nil)
	token, err := nativeScheme.AppLogin(app2.Name)
	c.Assert(err, check.IsNil)
	url := fmt.Sprintf("/apps/%s/repository/clone?:appname=%s", app1.Name, app2.Name)
	request, err := http.NewRequest("POST", url, strings.NewReader("archive-url=http://something.tar.gz&user=fulano"))
	c.Assert(err, check.IsNil)
	request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
	recorder := httptest.NewRecorder()
	request.Header.Set("Authorization", "bearer "+token.GetValue())
	server := RunServer(true)
	server.ServeHTTP(recorder, request)
	c.Assert(recorder.Code, check.Equals, http.StatusUnauthorized)
	c.Assert(recorder.Body.String(), check.Equals, "invalid app token\n")
}
Пример #14
0
func appDelete(w http.ResponseWriter, r *http.Request, t auth.Token) error {
	u, err := t.User()
	if err != nil {
		return err
	}
	rec.Log(u.Email, "app-delete", "app="+r.URL.Query().Get(":app"))
	a, err := getApp(r.URL.Query().Get(":app"), u, r)
	if err != nil {
		return err
	}
	context.SetPreventUnlock(r)
	app.Delete(&a)
	fmt.Fprint(w, "success")
	return nil
}
Пример #15
0
func appDelete(w http.ResponseWriter, r *http.Request, t auth.Token) error {
	u, err := t.User()
	if err != nil {
		return err
	}
	rec.Log(u.Email, "app-delete", "app="+r.URL.Query().Get(":app"))
	a, err := getApp(r.URL.Query().Get(":app"), u, r)
	if err != nil {
		return err
	}
	keepAliveWriter := tsuruIo.NewKeepAliveWriter(w, 30*time.Second, "")
	defer keepAliveWriter.Stop()
	writer := &tsuruIo.SimpleJsonMessageEncoderWriter{Encoder: json.NewEncoder(keepAliveWriter)}
	app.Delete(&a, writer)
	return nil
}
Пример #16
0
func (s *DeploySuite) TestDeployInvalidOrigin(c *check.C) {
	a := app.App{Name: "otherapp", Platform: "python", TeamOwner: s.team.Name}
	user, _ := s.token.User()
	err := app.CreateApp(&a, user)
	c.Assert(err, check.IsNil)
	defer app.Delete(&a, nil)
	url := fmt.Sprintf("/apps/%s/repository/clone?:appname=%s&origin=drag", a.Name, a.Name)
	request, err := http.NewRequest("POST", url, strings.NewReader("archive-url=http://something.tar.gz&user=fulano"))
	c.Assert(err, check.IsNil)
	request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
	recorder := httptest.NewRecorder()
	request.Header.Set("Authorization", "bearer "+s.token.GetValue())
	server := RunServer(true)
	server.ServeHTTP(recorder, request)
	c.Assert(recorder.Code, check.Equals, http.StatusBadRequest)
	c.Assert(recorder.Body.String(), check.Equals, "Invalid deployment origin\n")
}
Пример #17
0
func (s *DeploySuite) TestDiffDeployWhenUserDoesNotHaveAccessToApp(c *check.C) {
	diff := `--- hello.go	2015-11-25 16:04:22.409241045 +0000
+++ hello.go	2015-11-18 18:40:21.385697080 +0000
@@ -1,10 +1,7 @@
 package main

-import (
-    "fmt"
-)
+import "fmt"

-func main() {
-	fmt.Println("Hello")
+func main2() {
+	fmt.Println("Hello World!")
 }
	`

	user, _ := s.token.User()
	user1 := &auth.User{Email: "*****@*****.**", Password: "******"}
	_, err := nativeScheme.Create(user1)
	c.Assert(err, check.IsNil)
	defer nativeScheme.Remove(user1)
	token, err := nativeScheme.Login(map[string]string{"email": user1.Email, "password": "******"})
	c.Assert(err, check.IsNil)
	a := app.App{Name: "otherapp", Platform: "python", TeamOwner: s.team.Name}
	err = app.CreateApp(&a, user)
	c.Assert(err, check.IsNil)
	defer app.Delete(&a, nil)
	defer s.conn.Deploys().Remove(bson.M{"app": a.Name})
	v := url.Values{}
	v.Set("customdata", diff)
	body := strings.NewReader(v.Encode())
	url := fmt.Sprintf("/apps/%s/diff?:appname=%s", a.Name, a.Name)
	request, err := http.NewRequest("POST", url, body)
	c.Assert(err, check.IsNil)
	request.Header.Set("Authorization", "bearer "+token.GetValue())
	recorder := httptest.NewRecorder()
	m := RunServer(true)
	m.ServeHTTP(recorder, request)
	c.Assert(recorder.Code, check.Equals, http.StatusOK)
	expected := `Saving the difference between the old and new code
`
	c.Assert(recorder.Body.String(), check.Equals, expected+permission.ErrUnauthorized.Error()+"\n")
}
Пример #18
0
func (s *DeploySuite) TestDeployDockerImage(c *check.C) {
	user, _ := s.token.User()
	a := app.App{Name: "otherapp", Platform: "python", TeamOwner: s.team.Name}
	err := app.CreateApp(&a, user)
	c.Assert(err, check.IsNil)
	defer app.Delete(&a, nil)
	defer s.logConn.Logs(a.Name).DropCollection()
	url := fmt.Sprintf("/apps/%s/deploy", a.Name)
	request, err := http.NewRequest("POST", url, strings.NewReader("image=127.0.0.1:5000/tsuru/otherapp"))
	c.Assert(err, check.IsNil)
	request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
	recorder := httptest.NewRecorder()
	request.Header.Set("Authorization", "bearer "+s.token.GetValue())
	server := RunServer(true)
	server.ServeHTTP(recorder, request)
	c.Assert(recorder.Code, check.Equals, http.StatusOK)
	c.Assert(recorder.Body.String(), check.Equals, "Image deploy called\nOK\n")
}
Пример #19
0
func (s *DeploySuite) TestDeployWithoutVersionAndArchiveURL(c *check.C) {
	user, _ := s.token.User()
	a := app.App{Name: "abc", Platform: "python", Teams: []string{s.team.Name}}
	err := app.CreateApp(&a, user)
	c.Assert(err, check.IsNil)
	defer app.Delete(&a, nil)
	defer s.logConn.Logs(a.Name).DropCollection()
	request, err := http.NewRequest("POST", "/apps/abc/repository/clone", nil)
	c.Assert(err, check.IsNil)
	request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
	recorder := httptest.NewRecorder()
	request.Header.Set("Authorization", "bearer "+s.token.GetValue())
	server := RunServer(true)
	server.ServeHTTP(recorder, request)
	c.Assert(recorder.Code, check.Equals, http.StatusBadRequest)
	message := recorder.Body.String()
	c.Assert(message, check.Equals, "you must specify either the version, the archive-url or upload a file\n")
}
Пример #20
0
func (s *DeploySuite) TestDeployRollbackHandler(c *check.C) {
	user, _ := s.token.User()
	a := app.App{Name: "otherapp", Platform: "python", Teams: []string{s.team.Name}}
	err := app.CreateApp(&a, user)
	c.Assert(err, check.IsNil)
	defer app.Delete(&a, nil)
	url := fmt.Sprintf("/apps/%s/deploy/rollback", a.Name)
	request, err := http.NewRequest("POST", url, strings.NewReader("image=my-image-123:v1"))
	c.Assert(err, check.IsNil)
	request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
	recorder := httptest.NewRecorder()
	request.Header.Set("Authorization", "bearer "+s.token.GetValue())
	server := RunServer(true)
	server.ServeHTTP(recorder, request)
	c.Assert(recorder.Code, check.Equals, http.StatusOK)
	c.Assert(recorder.Header().Get("Content-Type"), check.Equals, "application/json")
	c.Assert(recorder.Code, check.Equals, http.StatusOK)
	c.Assert(recorder.Body.String(), check.Equals, "{\"Message\":\"Image deploy called\"}\n")
}
Пример #21
0
func (s *DeploySuite) TestDeployListByService(c *check.C) {
	user, _ := s.token.User()
	a := app.App{Name: "g1", Platform: "python", Teams: []string{s.team.Name}}
	err := app.CreateApp(&a, user)
	c.Assert(err, check.IsNil)
	defer app.Delete(&a, nil)
	var result []app.DeployData
	srv := service.Service{Name: "redis", Teams: []string{s.team.Name}}
	err = srv.Create()
	c.Assert(err, check.IsNil)
	instance := service.ServiceInstance{
		Name:        "redis-g1",
		ServiceName: "redis",
		Apps:        []string{"g1", "qwerty"},
		Teams:       []string{s.team.Name},
	}
	err = instance.Create()
	c.Assert(err, check.IsNil)
	defer func() {
		srv.Delete()
		service.DeleteInstance(&instance)
	}()
	request, err := http.NewRequest("GET", "/deploys?service=redis", nil)
	c.Assert(err, check.IsNil)
	recorder := httptest.NewRecorder()
	timestamp := time.Date(2013, time.November, 1, 0, 0, 0, 0, time.Local)
	duration := time.Since(timestamp)
	err = s.conn.Deploys().Insert(app.DeployData{App: "g1", Timestamp: timestamp, Duration: duration})
	c.Assert(err, check.IsNil)
	err = s.conn.Deploys().Insert(app.DeployData{App: "ge", Timestamp: timestamp, Duration: duration})
	c.Assert(err, check.IsNil)
	defer s.conn.Deploys().RemoveAll(nil)
	request.Header.Set("Authorization", "bearer "+s.token.GetValue())
	server := RunServer(true)
	server.ServeHTTP(recorder, request)
	c.Assert(recorder.Code, check.Equals, http.StatusOK)
	err = json.Unmarshal(recorder.Body.Bytes(), &result)
	c.Assert(err, check.IsNil)
	c.Assert(result, check.HasLen, 1)
	c.Assert(result[0].App, check.Equals, "g1")
	c.Assert(result[0].Timestamp.In(time.UTC), check.DeepEquals, timestamp.In(time.UTC))
	c.Assert(result[0].Duration, check.DeepEquals, duration)
}
Пример #22
0
func (s *DeploySuite) TestDeployInfoByNonAdminUser(c *check.C) {
	user, _ := s.token.User()
	a := app.App{Name: "g1", Platform: "python", Teams: []string{s.team.Name}}
	err := app.CreateApp(&a, user)
	c.Assert(err, check.IsNil)
	defer app.Delete(&a)
	user = &auth.User{Email: "*****@*****.**", Password: "******"}
	nativeScheme := auth.ManagedScheme(native.NativeScheme{})
	app.AuthScheme = nativeScheme
	_, err = nativeScheme.Create(user)
	c.Assert(err, check.IsNil)
	defer user.Delete()
	team := &auth.Team{Name: "team", Users: []string{user.Email}}
	err = s.conn.Teams().Insert(team)
	c.Assert(err, check.IsNil)
	defer s.conn.Teams().Remove(team)
	token, err := nativeScheme.Login(map[string]string{"email": user.Email, "password": "******"})
	c.Assert(err, check.IsNil)
	recorder := httptest.NewRecorder()
	timestamp := time.Now()
	duration := time.Duration(10e9)
	previousDeploy := app.DeployData{App: "g1", Timestamp: timestamp.Add(-3600 * time.Second), Duration: duration, Commit: "e293e3e3me03ejm3puejmp3ej3iejop32", Error: ""}
	err = s.conn.Deploys().Insert(previousDeploy)
	c.Assert(err, check.IsNil)
	lastDeploy := app.DeployData{App: "g1", Timestamp: timestamp, Duration: duration, Commit: "e82nn93nd93mm12o2ueh83dhbd3iu112", Error: ""}
	err = s.conn.Deploys().Insert(lastDeploy)
	c.Assert(err, check.IsNil)
	defer s.conn.Deploys().RemoveAll(nil)
	var d map[string]interface{}
	err = s.conn.Deploys().Find(bson.M{"commit": lastDeploy.Commit}).One(&d)
	c.Assert(err, check.IsNil)
	lastDeployId := d["_id"].(bson.ObjectId).Hex()
	url := fmt.Sprintf("/deploys/%s", lastDeployId)
	request, err := http.NewRequest("GET", url, nil)
	c.Assert(err, check.IsNil)
	request.Header.Set("Authorization", "bearer "+token.GetValue())
	server := RunServer(true)
	server.ServeHTTP(recorder, request)
	c.Assert(recorder.Code, check.Equals, http.StatusNotFound)
	body := recorder.Body.String()
	c.Assert(body, check.Equals, "Deploy not found.\n")
}
Пример #23
0
func (s *DeploySuite) TestDeployHandler(c *check.C) {
	a := app.App{Name: "otherapp", Platform: "python", Teams: []string{s.team.Name}}
	user, _ := s.token.User()
	err := app.CreateApp(&a, user)
	c.Assert(err, check.IsNil)
	defer app.Delete(&a, nil)
	url := fmt.Sprintf("/apps/%s/repository/clone?:appname=%s", a.Name, a.Name)
	request, err := http.NewRequest("POST", url, strings.NewReader("version=a345f3e&user=fulano"))
	c.Assert(err, check.IsNil)
	request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
	recorder := httptest.NewRecorder()
	request.Header.Set("Authorization", "bearer "+s.token.GetValue())
	server := RunServer(true)
	server.ServeHTTP(recorder, request)
	c.Assert(recorder.Code, check.Equals, http.StatusOK)
	c.Assert(recorder.Header().Get("Content-Type"), check.Equals, "text")
	c.Assert(recorder.Code, check.Equals, http.StatusOK)
	c.Assert(recorder.Body.String(), check.Equals, "Git deploy called\nOK\n")
	c.Assert(s.provisioner.Version(&a), check.Equals, "a345f3e")
}
Пример #24
0
func (s *DeploySuite) TestDiffDeploy(c *check.C) {
	diff := `--- hello.go	2015-11-25 16:04:22.409241045 +0000
+++ hello.go	2015-11-18 18:40:21.385697080 +0000
@@ -1,10 +1,7 @@
 package main

-import (
-    "fmt"
-)
+import "fmt"

-func main() {
-	fmt.Println("Hello")
+func main2() {
+	fmt.Println("Hello World!")
 }
`
	user, _ := s.token.User()
	a := app.App{Name: "otherapp", Platform: "python", TeamOwner: s.team.Name}
	err := app.CreateApp(&a, user)
	c.Assert(err, check.IsNil)
	defer app.Delete(&a, nil)
	defer s.conn.Deploys().Remove(bson.M{"app": a.Name})
	v := url.Values{}
	v.Set("customdata", diff)
	body := strings.NewReader(v.Encode())
	url := fmt.Sprintf("/apps/%s/diff?:appname=%s", a.Name, a.Name)
	request, err := http.NewRequest("POST", url, body)
	c.Assert(err, check.IsNil)
	request.Header.Set("Authorization", "bearer "+s.token.GetValue())
	recorder := httptest.NewRecorder()
	m := RunServer(true)
	m.ServeHTTP(recorder, request)
	c.Assert(recorder.Code, check.Equals, http.StatusOK)
	c.Assert(recorder.Body.String(), check.Equals, "Saving the difference between the old and new code\n")
	var deploy []app.DeployData
	err = s.conn.Deploys().Find(bson.M{"app": a.Name}).All(&deploy)
	c.Assert(err, check.IsNil)
	c.Assert(deploy, check.HasLen, 1)
	c.Assert(deploy[0].Diff, check.Equals, diff)
}
Пример #25
0
func (s *DeploySuite) TestDeployOriginDragAndDrop(c *check.C) {
	user, _ := s.token.User()
	a := app.App{Name: "otherapp", Platform: "python", TeamOwner: s.team.Name}
	err := app.CreateApp(&a, user)
	c.Assert(err, check.IsNil)
	defer app.Delete(&a, nil)
	url := fmt.Sprintf("/apps/%s/repository/clone?:appname=%s&origin=drag-and-drop", a.Name, a.Name)
	request, err := http.NewRequest("POST", url, strings.NewReader("archive-url=http://something.tar.gz&user=fulano"))
	c.Assert(err, check.IsNil)
	request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
	recorder := httptest.NewRecorder()
	request.Header.Set("Authorization", "bearer "+s.token.GetValue())
	server := RunServer(true)
	server.ServeHTTP(recorder, request)
	c.Assert(recorder.Code, check.Equals, http.StatusOK)
	c.Assert(recorder.Header().Get("Content-Type"), check.Equals, "text")
	c.Assert(recorder.Body.String(), check.Equals, "Archive deploy called\nOK\n")
	var result app.DeployData
	s.conn.Deploys().Find(bson.M{"app": a.Name}).One(&result)
	c.Assert(result.Origin, check.Equals, "drag-and-drop")
}
Пример #26
0
func (s *DeploySuite) TestDeployListByAppAndService(c *check.C) {
	srv := service.Service{Name: "redis", Teams: []string{s.team.Name}}
	err := srv.Create()
	c.Assert(err, check.IsNil)
	instance := service.ServiceInstance{
		Name:        "redis-myblog",
		ServiceName: "redis",
		Apps:        []string{"yourblog"},
		Teams:       []string{s.team.Name},
	}
	err = instance.Create()
	c.Assert(err, check.IsNil)
	defer func() {
		srv.Delete()
		service.DeleteInstance(&instance)
	}()
	user, _ := s.token.User()
	a := app.App{Name: "myblog", Platform: "python", Teams: []string{s.team.Name}}
	err = app.CreateApp(&a, user)
	c.Assert(err, check.IsNil)
	defer app.Delete(&a, nil)
	defer s.logConn.Logs(a.Name).DropCollection()
	timestamp := time.Date(2013, time.November, 1, 0, 0, 0, 0, time.Local)
	duration := time.Since(timestamp)
	deploys := []app.DeployData{
		{App: "myblog", Timestamp: timestamp, Duration: duration},
		{App: "yourblog", Timestamp: timestamp, Duration: duration},
	}
	for _, deploy := range deploys {
		err = s.conn.Deploys().Insert(deploy)
		c.Assert(err, check.IsNil)
	}
	defer s.conn.Deploys().RemoveAll(nil)
	recorder := httptest.NewRecorder()
	request, err := http.NewRequest("GET", "/deploys?app=myblog&service=redis", nil)
	request.Header.Set("Authorization", "bearer "+s.token.GetValue())
	server := RunServer(true)
	server.ServeHTTP(recorder, request)
	c.Assert(recorder.Code, check.Equals, http.StatusNoContent)
}
Пример #27
0
func (s *DeploySuite) TestDeployListNonAdmin(c *check.C) {
	user := &auth.User{Email: "*****@*****.**", Password: "******"}
	app.AuthScheme = nativeScheme
	_, err := nativeScheme.Create(user)
	c.Assert(err, check.IsNil)
	team := &auth.Team{Name: "newteam"}
	err = s.conn.Teams().Insert(team)
	c.Assert(err, check.IsNil)
	token := customUserWithPermission(c, "apponlyg1", permission.Permission{
		Scheme:  permission.PermAppReadDeploy,
		Context: permission.Context(permission.CtxApp, "g1"),
	})
	a := app.App{Name: "g1", Platform: "python", TeamOwner: team.Name}
	err = app.CreateApp(&a, user)
	c.Assert(err, check.IsNil)
	defer app.Delete(&a, nil)
	var result []app.DeployData
	request, err := http.NewRequest("GET", "/deploys", nil)
	c.Assert(err, check.IsNil)
	recorder := httptest.NewRecorder()
	timestamp := time.Date(2013, time.November, 1, 0, 0, 0, 0, time.Local)
	duration := time.Since(timestamp)
	err = s.conn.Deploys().Insert(app.DeployData{App: "g1", Timestamp: timestamp.Add(time.Minute), Duration: duration})
	c.Assert(err, check.IsNil)
	err = s.conn.Deploys().Insert(app.DeployData{App: "ge", Timestamp: timestamp.Add(time.Second), Duration: duration})
	c.Assert(err, check.IsNil)
	defer s.conn.Deploys().RemoveAll(nil)
	request.Header.Set("Authorization", "bearer "+token.GetValue())
	server := RunServer(true)
	server.ServeHTTP(recorder, request)
	c.Assert(recorder.Code, check.Equals, http.StatusOK)
	err = json.Unmarshal(recorder.Body.Bytes(), &result)
	c.Assert(err, check.IsNil)
	c.Assert(result, check.HasLen, 1)
	c.Assert(result[0].ID, check.NotNil)
	c.Assert(result[0].App, check.Equals, "g1")
	c.Assert(result[0].Timestamp.In(time.UTC), check.DeepEquals, timestamp.Add(time.Minute).In(time.UTC))
	c.Assert(result[0].Duration, check.DeepEquals, duration)
}
Пример #28
0
func (s *DeploySuite) TestDeployShouldReturnForbiddenWhenUserDoesNotHaveAccessToApp(c *check.C) {
	user := &auth.User{Email: "*****@*****.**", Password: "******"}
	_, err := nativeScheme.Create(user)
	c.Assert(err, check.IsNil)
	defer nativeScheme.Remove(user)
	token, err := nativeScheme.Login(map[string]string{"email": user.Email, "password": "******"})
	c.Assert(err, check.IsNil)
	adminUser, _ := s.token.User()
	a := app.App{Name: "otherapp", Platform: "python", TeamOwner: s.team.Name}
	err = app.CreateApp(&a, adminUser)
	c.Assert(err, check.IsNil)
	defer app.Delete(&a, nil)
	url := fmt.Sprintf("/apps/%s/repository/clone?:appname=%s", a.Name, a.Name)
	request, err := http.NewRequest("POST", url, strings.NewReader("archive-url=http://something.tar.gz&user=fulano"))
	c.Assert(err, check.IsNil)
	request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
	recorder := httptest.NewRecorder()
	request.Header.Set("Authorization", "bearer "+token.GetValue())
	server := RunServer(true)
	server.ServeHTTP(recorder, request)
	c.Assert(recorder.Code, check.Equals, http.StatusForbidden)
	c.Assert(recorder.Body.String(), check.Equals, "user does not have access to this app\n")
}
Пример #29
0
func (s *DeploySuite) TestDeployListNonAdmin(c *check.C) {
	user := &auth.User{Email: "*****@*****.**", Password: "******"}
	nativeScheme := auth.ManagedScheme(native.NativeScheme{})
	app.AuthScheme = nativeScheme
	_, err := nativeScheme.Create(user)
	c.Assert(err, check.IsNil)
	team := &auth.Team{Name: "newteam", Users: []string{user.Email}}
	err = s.conn.Teams().Insert(team)
	c.Assert(err, check.IsNil)
	token, err := nativeScheme.Login(map[string]string{"email": user.Email, "password": "******"})
	c.Assert(err, check.IsNil)
	a := app.App{Name: "g1", Platform: "python", Teams: []string{team.Name}}
	err = app.CreateApp(&a, user)
	c.Assert(err, check.IsNil)
	defer app.Delete(&a)
	var result []app.DeployData
	request, err := http.NewRequest("GET", "/deploys", nil)
	c.Assert(err, check.IsNil)
	recorder := httptest.NewRecorder()
	timestamp := time.Date(2013, time.November, 1, 0, 0, 0, 0, time.Local)
	duration := time.Since(timestamp)
	err = s.conn.Deploys().Insert(app.DeployData{App: "g1", Timestamp: timestamp.Add(time.Minute), Duration: duration})
	c.Assert(err, check.IsNil)
	err = s.conn.Deploys().Insert(app.DeployData{App: "ge", Timestamp: timestamp.Add(time.Second), Duration: duration})
	c.Assert(err, check.IsNil)
	defer s.conn.Deploys().RemoveAll(nil)
	request.Header.Set("Authorization", "bearer "+token.GetValue())
	server := RunServer(true)
	server.ServeHTTP(recorder, request)
	c.Assert(recorder.Code, check.Equals, http.StatusOK)
	err = json.Unmarshal(recorder.Body.Bytes(), &result)
	c.Assert(err, check.IsNil)
	c.Assert(result[0].ID, check.NotNil)
	c.Assert(result[0].App, check.Equals, "g1")
	c.Assert(result[0].Timestamp.In(time.UTC), check.DeepEquals, timestamp.Add(time.Minute).In(time.UTC))
	c.Assert(result[0].Duration, check.DeepEquals, duration)
}
Пример #30
0
func (s *DeploySuite) TestDeployShouldIncrementDeployNumberOnApp(c *check.C) {
	user, _ := s.token.User()
	a := app.App{Name: "otherapp", Platform: "python", Teams: []string{s.team.Name}}
	err := app.CreateApp(&a, user)
	c.Assert(err, check.IsNil)
	defer app.Delete(&a, nil)
	url := fmt.Sprintf("/apps/%s/repository/clone?:appname=%s", a.Name, a.Name)
	request, err := http.NewRequest("POST", url, strings.NewReader("version=a345f3e"))
	c.Assert(err, check.IsNil)
	request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
	recorder := httptest.NewRecorder()
	request.Header.Set("Authorization", "bearer "+s.token.GetValue())
	server := RunServer(true)
	server.ServeHTTP(recorder, request)
	c.Assert(recorder.Code, check.Equals, http.StatusOK)
	s.conn.Apps().Find(bson.M{"name": a.Name}).One(&a)
	c.Assert(a.Deploys, check.Equals, uint(1))
	var result map[string]interface{}
	s.conn.Deploys().Find(bson.M{"app": a.Name}).One(&result)
	c.Assert(result["app"], check.Equals, a.Name)
	now := time.Now()
	diff := now.Sub(result["timestamp"].(time.Time))
	c.Assert(diff < 60*time.Second, check.Equals, true)
}