<commit_before>package main

import (
	"github.com/timeredbull/commandmocker"
	"github.com/timeredbull/tsuru/db"
	tEC2 "github.com/timeredbull/tsuru/ec2"
	"labix.org/v2/mgo"
	"launchpad.net/goamz/aws"
	"launchpad.net/goamz/ec2"
	"launchpad.net/goamz/ec2/ec2test"
	. "launchpad.net/gocheck"
	"testing"
)

func Test(t *testing.T) { TestingT(t) }

type S struct {
	session   *mgo.Session
	tmpdir    string
	ec2Srv    *ec2test.Server
	instances []string
	secGroup  ec2.SecurityGroup
}

var _ = Suite(&S{})

func (s *S) SetUpSuite(c *C) {
	var err error
	s.ec2Srv, err = ec2test.NewServer()
	s.ec2Srv.SetInitialInstanceState(ec2test.Running)
	s.reconfEc2Srv(c)
	c.Assert(err, IsNil)
	s.tmpdir, err = commandmocker.Add("juju", "")
	c.Assert(err, IsNil)
	db.Session, err = db.Open("127.0.0.1:27017", "tsuru_collector_test")
	c.Assert(err, IsNil)
}

func (s *S) TearDownSuite(c *C) {
	defer commandmocker.Remove(s.tmpdir)
	defer db.Session.Close()
	s.ec2Srv.Quit()
	db.Session.Apps().Database.DropDatabase()
}

func (s *S) TearDownTest(c *C) {
	_, err := db.Session.Apps().RemoveAll(nil)
	c.Assert(err, IsNil)
	_, err = db.Session.Units().RemoveAll(nil)
	c.Assert(err, IsNil)
}

func (s *S) reconfEc2Srv(c *C) {
	region := aws.Region{EC2Endpoint: s.ec2Srv.URL()}
	auth := aws.Auth{AccessKey: "blaa", SecretKey: "blee"}
	tEC2.EC2 = ec2.New(auth, region)
}

func (s *S) createTestInstances(c *C) {
	secGroupResp, err := tEC2.EC2.CreateSecurityGroup("default", "default security group")
	s.secGroup = secGroupResp.SecurityGroup
	c.Assert(err, IsNil)
	opts := ec2.RunInstances{
		ImageId:        "ami-0000007",
		SecurityGroups: []ec2.SecurityGroup{s.secGroup},
		MaxCount:       2,
		MinCount:       2,
	}
	instResp, err := tEC2.EC2.RunInstances(&opts)
	if err != nil {
		c.Fail()
	}
	s.instances = instancesIds(instResp.Instances)
}<commit_msg>collector: creating test instances in SetUpSuite and removing them in TearDownSuite<commit_after>package main

import (
	"github.com/timeredbull/commandmocker"
	"github.com/timeredbull/tsuru/db"
	tec2 "github.com/timeredbull/tsuru/ec2"
	"labix.org/v2/mgo"
	"launchpad.net/goamz/aws"
	"launchpad.net/goamz/ec2"
	"launchpad.net/goamz/ec2/ec2test"
	. "launchpad.net/gocheck"
	"testing"
)

func Test(t *testing.T) { TestingT(t) }

type S struct {
	session   *mgo.Session
	tmpdir    string
	ec2Srv    *ec2test.Server
	instances []string
	secGroup  ec2.SecurityGroup
}

var _ = Suite(&S{})

func (s *S) SetUpSuite(c *C) {
	var err error
	s.ec2Srv, err = ec2test.NewServer()
	c.Assert(err, IsNil)
	s.ec2Srv.SetInitialInstanceState(ec2test.Running)
	s.reconfEc2Srv(c)
	s.createTestInstances(c)
	s.tmpdir, err = commandmocker.Add("juju", "")
	c.Assert(err, IsNil)
	db.Session, err = db.Open("127.0.0.1:27017", "tsuru_collector_test")
	c.Assert(err, IsNil)
}

func (s *S) TearDownSuite(c *C) {
	defer commandmocker.Remove(s.tmpdir)
	defer db.Session.Close()
	defer s.ec2Srv.Quit()
	_, err := tec2.EC2.TerminateInstances(s.instances)
	if err != nil {
		c.Fail()
	}
	db.Session.Apps().Database.DropDatabase()
}

func (s *S) TearDownTest(c *C) {
	_, err := db.Session.Apps().RemoveAll(nil)
	c.Assert(err, IsNil)
	_, err = db.Session.Units().RemoveAll(nil)
	c.Assert(err, IsNil)
}

func (s *S) reconfEc2Srv(c *C) {
	region := aws.Region{EC2Endpoint: s.ec2Srv.URL()}
	auth := aws.Auth{AccessKey: "blaa", SecretKey: "blee"}
	tec2.EC2 = ec2.New(auth, region)
}

func (s *S) createTestInstances(c *C) {
	secGroupResp, err := tec2.EC2.CreateSecurityGroup("default", "default security group")
	s.secGroup = secGroupResp.SecurityGroup
	c.Assert(err, IsNil)
	opts := ec2.RunInstances{
		ImageId:        "ami-0000007",
		SecurityGroups: []ec2.SecurityGroup{s.secGroup},
		MaxCount:       2,
		MinCount:       2,
	}
	instResp, err := tec2.EC2.RunInstances(&opts)
	if err != nil {
		c.Fail()
	}
	s.instances = instancesIds(instResp.Instances)
}
{'fraction_non_alphanumeric': 0.16117311186088762, 'fraction_numerical': 0.024915650142745913, 'mean_word_length': 4.315862068965517, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '4589859', 'n_tokens_mistral': 1715, 'n_tokens_neox': 1648, 'n_words': 282}