<commit_before>package app

import (
	"bytes"
	"encoding/json"
	"errors"
	"github.com/timeredbull/commandmocker"
	"github.com/timeredbull/tsuru/api/auth"
	"github.com/timeredbull/tsuru/api/bind"
	"github.com/timeredbull/tsuru/config"
	"github.com/timeredbull/tsuru/db"
	"github.com/timeredbull/tsuru/log"
	"github.com/timeredbull/tsuru/repository"
	"io/ioutil"
	"labix.org/v2/mgo/bson"
	. "launchpad.net/gocheck"
	"launchpad.net/goyaml"
	stdlog "log"
	"strings"
)

func (s *S) TestGet(c *C) {
	newApp := App{Name: "myApp", Framework: "Django", ec2Auth: &fakeAuthorizer{}}
	err := createApp(&newApp)
	c.Assert(err, IsNil)
	defer db.Session.Apps().Remove(bson.M{"name": newApp.Name})
	newApp.Env = map[string]bind.EnvVar{}
	newApp.Logs = []applog{}
	err = db.Session.Apps().Update(bson.M{"name": newApp.Name}, &newApp)
	c.Assert(err, IsNil)
	myApp := App{Name: "myApp"}
	err = myApp.Get()
	c.Assert(err, IsNil)
	c.Assert(myApp.Name, Equals, newApp.Name)
	c.Assert(myApp.State, Equals, newApp.State)
}

func (s *S) TestDestroy(c *C) {
	dir, err := commandmocker.Add("juju", "$*")
	c.Assert(err, IsNil)
	defer commandmocker.Remove(dir)
	u := Unit{Name: "duvido", Machine: 3}
	authorizer := &fakeAuthorizer{}
	a := App{
		Name:      "duvido",
		Framework: "django",
		Units:     []Unit{u},
		EC2Creds:  map[string]string{"access": "access-key-here", "secret": "secret-key-here"},
		ec2Auth:   authorizer,
	}
	err = createApp(&a)
	c.Assert(err, IsNil)
	err = a.destroy()
	c.Assert(err, IsNil)
	qtd, err := db.Session.Apps().Find(bson.M{"name": a.Name}).Count()
	c.Assert(qtd, Equals, 0)
	c.Assert(authorizer.actions, DeepEquals, []string{"setCreds access-key-here secret-key-here", "authorize " + a.Name})
}

func (s *S) TestDestroyWithMultiTenancyOnCallsJujuDestroyEnvironment(c *C) {
	dir, err := commandmocker.Add("juju", "$*")
	c.Assert(err, IsNil)
	defer commandmocker.Remove(dir)
	u := Unit{Name: "duvido", Machine: 3}
	a := App{
		Name:      "duvido",
		Framework: "django",
		Units:     []Unit{u},
		EC2Creds:  map[string]string{"access": "91232f6796b54ca2a2b87ef50548b123"},
		ec2Auth:   &fakeAuthorizer{},
	}
	err = createApp(&a)
	c.Assert(err, IsNil)
	defer db.Session.Apps().Remove(bson.M{"name": a.Name})
	err = a.destroy()
	c.Assert(err, IsNil)
	c.Assert(commandmocker.Ran(dir), Equals, true)
}

func (s *S) TestDestroyWithnMultiTenancyOnDoesNotDeleteTheAppIfTheDestroyEnvironmentFail(c *C) {
	dir, err := commandmocker.Add("juju", "$*")
	c.Assert(err, IsNil)
	defer commandmocker.Remove(dir)
	u := Unit{Name: "duvido", Machine: 3}
	a := App{
		Name:      "duvido",
		Framework: "django",
		Units:     []Unit{u},
		ec2Auth:   &fakeAuthorizer{},
	}
	err = createApp(&a)
	c.Assert(err, IsNil)
	defer db.Session.Apps().Remove(bson.M{"name": a.Name})
	dir, err = commandmocker.Error("juju", "juju failed to destroy the environment", 1)
	c.Assert(err, IsNil)
	defer commandmocker.Remove(dir)
	err = a.destroy()
	c.Assert(err, NotNil)
	c.Assert(err, ErrorMatches, ".*juju failed to destroy the environment.*")
	err = a.Get()
	c.Assert(err, IsNil)
}

func (s *S) TestDestroyWithMultiTenancyOff(c *C) {
	dir, err := commandmocker.Add("juju", "$*")
	c.Assert(err, IsNil)
	defer commandmocker.Remove(dir)
	config.Set("multi-tenant", false)
	defer config.Set("multi-tenant", true)
	a := App{
		Name:      "ritual",
		Framework: "ruby",
		Teams:     []string{s.team.Name},
		Units: []Unit{
			Unit{
				Name:    "duvido",
				Machine: 3,
			},
		},
		ec2Auth: &fakeAuthorizer{},
	}
	err = createApp(&a)
	c.Assert(err, IsNil)
	w := bytes.NewBuffer([]byte{})
	l := stdlog.New(w, "", stdlog.LstdFlags)
	log.Target = l
	err = a.destroy()
	c.Assert(err, IsNil)
	err = a.Get()
	c.Assert(err, NotNil)
	logStr := strings.Replace(w.String(), "\n", "", -1)
	c.Assert(logStr, Matches, ".*destroy-service -e [a-z]+ ritual.*")
	c.Assert(logStr, Matches, ".*terminate-machine -e [a-z]+ 3.*")
}

func (s *S) TestDestroyWithMultiTenancyOffDoesNotDeleteTheAppIfJujuFailToDestroyTheService(c *C) {
	dir, err := commandmocker.Add("juju", "$*")
	c.Assert(err, IsNil)
	defer commandmocker.Remove(dir)
	config.Set("multi-tenant", false)
	defer config.Set("multi-tenant", true)
	a := App{
		Name:      "ritual",
		Framework: "ruby",
		Teams:     []string{s.team.Name},
		Units: []Unit{
			Unit{Name: "duvido", Machine: 3},
		},
		ec2Auth: &fakeAuthorizer{},
	}
	err = createApp(&a)
	c.Assert(err, IsNil)
	dir, err = commandmocker.Error("juju", "juju failed to destroy the service", 1)
	c.Assert(err, IsNil)
	defer commandmocker.Remove(dir)
	err = a.destroy()
	c.Assert(err, NotNil)
	c.Assert(err, ErrorMatches, ".*juju failed to destroy the service.*")
	err = a.Get()
	c.Assert(err, IsNil)
}

func (s *S) TestCreateApp(c *C) {
	dir, err := commandmocker.Add("juju", "$*")
	c.Assert(err, IsNil)
	defer commandmocker.Remove(dir)
	w := bytes.NewBuffer([]byte{})
	l := stdlog.New(w, "", stdlog.LstdFlags)
	log.Target = l
	authorizer := fakeAuthorizer{}
	a := App{
		Name:      "appName",
		Framework: "django",
		EC2Creds:  map[string]string{"access": "access-key-here", "secret": "secret-key-here"},
		ec2Auth:   &authorizer,
	}
	err = createApp(&a)
	c.Assert(err, IsNil)
	c.Assert(a.State, Equals, "pending")
	defer db.Session.Apps().Remove(bson.M{"name": a.Name})
	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)
	c.Assert(retrievedApp.JujuEnv, Equals, a.Name)
	str := strings.Replace(w.String(), "\n", "", -1)
	c.Assert(str, Matches, ".*bootstraping juju environment appName for the app appName.*")
	c.Assert(str, Matches, ".*deploy -e appName --repository=/home/charms local:django appName.*")
	c.Assert(authorizer.actions, DeepEquals, []string{"setCreds access-key-here secret-key-here", "authorize appName"})
}

func (s *S) TestCantNewAppTwoAppsWithTheSameName(c *C) {
	err := db.Session.Apps().Insert(bson.M{"name": "appName"})
	c.Assert(err, IsNil)
	defer db.Session.Apps().Remove(bson.M{"name": "appName"})
	a := App{Name: "appName", ec2Auth: &fakeAuthorizer{}}
	err = createApp(&a)
	c.Assert(err, NotNil)<commit_msg>api/app: fix test name<commit_after>package app

import (
	"bytes"
	"encoding/json"
	"errors"
	"github.com/timeredbull/commandmocker"
	"github.com/timeredbull/tsuru/api/auth"
	"github.com/timeredbull/tsuru/api/bind"
	"github.com/timeredbull/tsuru/config"
	"github.com/timeredbull/tsuru/db"
	"github.com/timeredbull/tsuru/log"
	"github.com/timeredbull/tsuru/repository"
	"io/ioutil"
	"labix.org/v2/mgo/bson"
	. "launchpad.net/gocheck"
	"launchpad.net/goyaml"
	stdlog "log"
	"strings"
)

func (s *S) TestGet(c *C) {
	newApp := App{Name: "myApp", Framework: "Django", ec2Auth: &fakeAuthorizer{}}
	err := createApp(&newApp)
	c.Assert(err, IsNil)
	defer db.Session.Apps().Remove(bson.M{"name": newApp.Name})
	newApp.Env = map[string]bind.EnvVar{}
	newApp.Logs = []applog{}
	err = db.Session.Apps().Update(bson.M{"name": newApp.Name}, &newApp)
	c.Assert(err, IsNil)
	myApp := App{Name: "myApp"}
	err = myApp.Get()
	c.Assert(err, IsNil)
	c.Assert(myApp.Name, Equals, newApp.Name)
	c.Assert(myApp.State, Equals, newApp.State)
}

func (s *S) TestDestroy(c *C) {
	dir, err := commandmocker.Add("juju", "$*")
	c.Assert(err, IsNil)
	defer commandmocker.Remove(dir)
	u := Unit{Name: "duvido", Machine: 3}
	authorizer := &fakeAuthorizer{}
	a := App{
		Name:      "duvido",
		Framework: "django",
		Units:     []Unit{u},
		EC2Creds:  map[string]string{"access": "access-key-here", "secret": "secret-key-here"},
		ec2Auth:   authorizer,
	}
	err = createApp(&a)
	c.Assert(err, IsNil)
	err = a.destroy()
	c.Assert(err, IsNil)
	qtd, err := db.Session.Apps().Find(bson.M{"name": a.Name}).Count()
	c.Assert(qtd, Equals, 0)
	c.Assert(authorizer.actions, DeepEquals, []string{"setCreds access-key-here secret-key-here", "authorize " + a.Name})
}

func (s *S) TestDestroyWithMultiTenancyOnCallsJujuDestroyEnvironment(c *C) {
	dir, err := commandmocker.Add("juju", "$*")
	c.Assert(err, IsNil)
	defer commandmocker.Remove(dir)
	u := Unit{Name: "duvido", Machine: 3}
	a := App{
		Name:      "duvido",
		Framework: "django",
		Units:     []Unit{u},
		EC2Creds:  map[string]string{"access": "91232f6796b54ca2a2b87ef50548b123"},
		ec2Auth:   &fakeAuthorizer{},
	}
	err = createApp(&a)
	c.Assert(err, IsNil)
	defer db.Session.Apps().Remove(bson.M{"name": a.Name})
	err = a.destroy()
	c.Assert(err, IsNil)
	c.Assert(commandmocker.Ran(dir), Equals, true)
}

func (s *S) TestDestroyWithnMultiTenancyOnDoesNotDeleteTheAppIfTheDestroyEnvironmentFail(c *C) {
	dir, err := commandmocker.Add("juju", "$*")
	c.Assert(err, IsNil)
	defer commandmocker.Remove(dir)
	u := Unit{Name: "duvido", Machine: 3}
	a := App{
		Name:      "duvido",
		Framework: "django",
		Units:     []Unit{u},
		ec2Auth:   &fakeAuthorizer{},
	}
	err = createApp(&a)
	c.Assert(err, IsNil)
	defer db.Session.Apps().Remove(bson.M{"name": a.Name})
	dir, err = commandmocker.Error("juju", "juju failed to destroy the environment", 1)
	c.Assert(err, IsNil)
	defer commandmocker.Remove(dir)
	err = a.destroy()
	c.Assert(err, NotNil)
	c.Assert(err, ErrorMatches, ".*juju failed to destroy the environment.*")
	err = a.Get()
	c.Assert(err, IsNil)
}

func (s *S) TestDestroyWithMultiTenancyOff(c *C) {
	dir, err := commandmocker.Add("juju", "$*")
	c.Assert(err, IsNil)
	defer commandmocker.Remove(dir)
	config.Set("multi-tenant", false)
	defer config.Set("multi-tenant", true)
	a := App{
		Name:      "ritual",
		Framework: "ruby",
		Teams:     []string{s.team.Name},
		Units: []Unit{
			Unit{
				Name:    "duvido",
				Machine: 3,
			},
		},
		ec2Auth: &fakeAuthorizer{},
	}
	err = createApp(&a)
	c.Assert(err, IsNil)
	w := bytes.NewBuffer([]byte{})
	l := stdlog.New(w, "", stdlog.LstdFlags)
	log.Target = l
	err = a.destroy()
	c.Assert(err, IsNil)
	err = a.Get()
	c.Assert(err, NotNil)
	logStr := strings.Replace(w.String(), "\n", "", -1)
	c.Assert(logStr, Matches, ".*destroy-service -e [a-z]+ ritual.*")
	c.Assert(logStr, Matches, ".*terminate-machine -e [a-z]+ 3.*")
}

func (s *S) TestDestroyWithMultiTenancyOffDoesNotDeleteTheAppIfJujuFailToDestroyTheService(c *C) {
	dir, err := commandmocker.Add("juju", "$*")
	c.Assert(err, IsNil)
	defer commandmocker.Remove(dir)
	config.Set("multi-tenant", false)
	defer config.Set("multi-tenant", true)
	a := App{
		Name:      "ritual",
		Framework: "ruby",
		Teams:     []string{s.team.Name},
		Units: []Unit{
			Unit{Name: "duvido", Machine: 3},
		},
		ec2Auth: &fakeAuthorizer{},
	}
	err = createApp(&a)
	c.Assert(err, IsNil)
	dir, err = commandmocker.Error("juju", "juju failed to destroy the service", 1)
	c.Assert(err, IsNil)
	defer commandmocker.Remove(dir)
	err = a.destroy()
	c.Assert(err, NotNil)
	c.Assert(err, ErrorMatches, ".*juju failed to destroy the service.*")
	err = a.Get()
	c.Assert(err, IsNil)
}

func (s *S) TestCreateApp(c *C) {
	dir, err := commandmocker.Add("juju", "$*")
	c.Assert(err, IsNil)
	defer commandmocker.Remove(dir)
	w := bytes.NewBuffer([]byte{})
	l := stdlog.New(w, "", stdlog.LstdFlags)
	log.Target = l
	authorizer := fakeAuthorizer{}
	a := App{
		Name:      "appName",
		Framework: "django",
		EC2Creds:  map[string]string{"access": "access-key-here", "secret": "secret-key-here"},
		ec2Auth:   &authorizer,
	}
	err = createApp(&a)
	c.Assert(err, IsNil)
	c.Assert(a.State, Equals, "pending")
	defer db.Session.Apps().Remove(bson.M{"name": a.Name})
	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)
	c.Assert(retrievedApp.JujuEnv, Equals, a.Name)
	str := strings.Replace(w.String(), "\n", "", -1)
	c.Assert(str, Matches, ".*bootstraping juju environment appName for the app appName.*")
	c.Assert(str, Matches, ".*deploy -e appName --repository=/home/charms local:django appName.*")
	c.Assert(authorizer.actions, DeepEquals, []string{"setCreds access-key-here secret-key-here", "authorize appName"})
}

func (s *S) TestCantCreateTwoAppsWithTheSameName(c *C) {
	err := db.Session.Apps().Insert(bson.M{"name": "appName"})
	c.Assert(err, IsNil)
	defer db.Session.Apps().Remove(bson.M{"name": "appName"})
	a := App{Name: "appName", ec2Auth: &fakeAuthorizer{}}
	err = createApp(&a)
	c.Assert(err, NotNil)
{'fraction_non_alphanumeric': 0.1896870726409782, 'fraction_numerical': 0.007400852706942322, 'mean_word_length': 4.648341662880509, 'pattern_counts': {'":': 28, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '613850', 'n_tokens_mistral': 5244, 'n_tokens_neox': 5054, 'n_words': 956}