<commit_before>// Copyright 2015 tsuru authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package docker

import (
	dtesting "github.com/fsouza/go-dockerclient/testing"
	"github.com/tsuru/config"
	"github.com/tsuru/docker-cluster/cluster"
	"github.com/tsuru/tsuru/app"
	"github.com/tsuru/tsuru/db"
	"github.com/tsuru/tsuru/iaas"
	"github.com/tsuru/tsuru/provision/provisiontest"
	"gopkg.in/check.v1"
	"gopkg.in/mgo.v2/bson"
)

func (s *S) TestAutoScaleConfigRun(c *check.C) {
	rollback := startTestRepositoryServer()
	defer rollback()
	defer func() {
		machines, _ := iaas.ListMachines()
		for _, m := range machines {
			m.Destroy()
		}
	}()
	node1, err := dtesting.NewServer("127.0.0.1:0", nil, nil)
	c.Assert(err, check.IsNil)
	node2, err := dtesting.NewServer("127.0.0.1:0", nil, nil)
	c.Assert(err, check.IsNil)
	config.Set("iaas:node-port", urlPort(node2.URL()))
	defer config.Unset("iaas:node-port")

	clusterInstance, err := cluster.New(nil, &cluster.MapStorage{},
		cluster.Node{Address: node1.URL(), Metadata: map[string]string{
			"pool": "pool1",
			"iaas": "my-scale-iaas",
		}},
	)
	c.Assert(err, check.IsNil)
	var p dockerProvisioner
	p.cluster = clusterInstance
	iaasInstance := &TestHealerIaaS{addr: "localhost"}
	iaas.RegisterIaasProvider("my-scale-iaas", iaasInstance)
	appInstance := provisiontest.NewFakeApp("myapp", "python", 0)
	defer p.Destroy(appInstance)
	p.Provision(appInstance)
	imageId, err := appCurrentImageName(appInstance.GetName())
	c.Assert(err, check.IsNil)

	conn, err := db.Conn()
	c.Assert(err, check.IsNil)
	defer conn.Close()
	appStruct := &app.App{
		Name: appInstance.GetName(),
	}
	err = conn.Apps().Insert(appStruct)
	c.Assert(err, check.IsNil)
	defer conn.Apps().Remove(bson.M{"name": appStruct.Name})

	_, err = addContainersWithHost(&changeUnitsPipelineArgs{
		unitsToAdd:  4,
		app:         appInstance,
		imageId:     imageId,
		provisioner: &p,
	})
	c.Assert(err, check.IsNil)
	a := autoScaleConfig{
		done:              make(chan bool),
		provisioner:       &p,
		groupByMetadata:   "pool",
		maxContainerCount: 2,
	}
	go a.stop()
	err = a.run()
	c.Assert(err, check.IsNil)
	nodes, err := p.cluster.Nodes()
	c.Assert(err, check.IsNil)
	c.Assert(nodes, check.HasLen, 2)
	c.Assert(nodes[0].Address, check.Not(check.Equals), nodes[1].Address)

	// Also should have rebalanced
	containers1, err := p.listContainersByHost(urlToHost(nodes[0].Address))
	c.Assert(err, check.IsNil)
	containers2, err := p.listContainersByHost(urlToHost(nodes[1].Address))
	c.Assert(err, check.IsNil)
	c.Assert(containers1, check.HasLen, 2)
	c.Assert(containers2, check.HasLen, 2)

	// Should do nothing if calling on already scaled
	go a.stop()
	err = a.run()
	c.Assert(err, check.IsNil)
	nodes, err = p.cluster.Nodes()
	c.Assert(err, check.IsNil)
	c.Assert(nodes, check.HasLen, 2)

	containers1Again, err := p.listContainersByHost(urlToHost(nodes[0].Address))
	c.Assert(err, check.IsNil)
	containers2Again, err := p.listContainersByHost(urlToHost(nodes[1].Address))
	c.Assert(err, check.IsNil)
	c.Assert(containers1, check.DeepEquals, containers1Again)
	c.Assert(containers2, check.DeepEquals, containers2Again)
}

func (s *S) TestAutoScaleConfigRunNoGroup(c *check.C) {
	rollback := startTestRepositoryServer()
	defer rollback()
	defer func() {
		machines, _ := iaas.ListMachines()
		for _, m := range machines {
			m.Destroy()
		}
	}()
	node1, err := dtesting.NewServer("127.0.0.1:0", nil, nil)
	c.Assert(err, check.IsNil)
	node2, err := dtesting.NewServer("127.0.0.1:0", nil, nil)
	c.Assert(err, check.IsNil)
	config.Set("iaas:node-port", urlPort(node2.URL()))
	defer config.Unset("iaas:node-port")

	clusterInstance, err := cluster.New(nil, &cluster.MapStorage{},
		cluster.Node{Address: node1.URL(), Metadata: map[string]string{
			"iaas": "my-scale-iaas",
		}},
	)
	c.Assert(err, check.IsNil)
	var p dockerProvisioner
	p.cluster = clusterInstance
	iaasInstance := &TestHealerIaaS{addr: "localhost"}
	iaas.RegisterIaasProvider("my-scale-iaas", iaasInstance)
	appInstance := provisiontest.NewFakeApp("myapp", "python", 0)
	defer p.Destroy(appInstance)
	p.Provision(appInstance)
	imageId, err := appCurrentImageName(appInstance.GetName())
	c.Assert(err, check.IsNil)<commit_msg>provision/docker: auto scale tests with correct cluster instance<commit_after>// Copyright 2015 tsuru authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package docker

import (
	dtesting "github.com/fsouza/go-dockerclient/testing"
	"github.com/tsuru/config"
	"github.com/tsuru/docker-cluster/cluster"
	"github.com/tsuru/tsuru/app"
	"github.com/tsuru/tsuru/db"
	"github.com/tsuru/tsuru/iaas"
	"github.com/tsuru/tsuru/provision/provisiontest"
	"gopkg.in/check.v1"
	"gopkg.in/mgo.v2/bson"
)

func (s *S) TestAutoScaleConfigRun(c *check.C) {
	rollback := startTestRepositoryServer()
	defer rollback()
	defer func() {
		machines, _ := iaas.ListMachines()
		for _, m := range machines {
			m.Destroy()
		}
	}()
	node1, err := dtesting.NewServer("127.0.0.1:0", nil, nil)
	c.Assert(err, check.IsNil)
	node2, err := dtesting.NewServer("127.0.0.1:0", nil, nil)
	c.Assert(err, check.IsNil)
	config.Set("iaas:node-port", urlPort(node2.URL()))
	defer config.Unset("iaas:node-port")

	var p dockerProvisioner
	p.storage = &cluster.MapStorage{}
	clusterInstance, err := cluster.New(nil, p.storage,
		cluster.Node{Address: node1.URL(), Metadata: map[string]string{
			"pool": "pool1",
			"iaas": "my-scale-iaas",
		}},
	)
	c.Assert(err, check.IsNil)
	p.cluster = clusterInstance
	iaasInstance := &TestHealerIaaS{addr: "localhost"}
	iaas.RegisterIaasProvider("my-scale-iaas", iaasInstance)
	appInstance := provisiontest.NewFakeApp("myapp", "python", 0)
	defer p.Destroy(appInstance)
	p.Provision(appInstance)
	imageId, err := appCurrentImageName(appInstance.GetName())
	c.Assert(err, check.IsNil)

	conn, err := db.Conn()
	c.Assert(err, check.IsNil)
	defer conn.Close()
	appStruct := &app.App{
		Name: appInstance.GetName(),
	}
	err = conn.Apps().Insert(appStruct)
	c.Assert(err, check.IsNil)
	defer conn.Apps().Remove(bson.M{"name": appStruct.Name})

	_, err = addContainersWithHost(&changeUnitsPipelineArgs{
		unitsToAdd:  4,
		app:         appInstance,
		imageId:     imageId,
		provisioner: &p,
	})
	c.Assert(err, check.IsNil)
	a := autoScaleConfig{
		done:              make(chan bool),
		provisioner:       &p,
		groupByMetadata:   "pool",
		maxContainerCount: 2,
	}
	go a.stop()
	err = a.run()
	c.Assert(err, check.IsNil)
	nodes, err := p.cluster.Nodes()
	c.Assert(err, check.IsNil)
	c.Assert(nodes, check.HasLen, 2)
	c.Assert(nodes[0].Address, check.Not(check.Equals), nodes[1].Address)

	// Also should have rebalanced
	containers1, err := p.listContainersByHost(urlToHost(nodes[0].Address))
	c.Assert(err, check.IsNil)
	containers2, err := p.listContainersByHost(urlToHost(nodes[1].Address))
	c.Assert(err, check.IsNil)
	c.Assert(containers1, check.HasLen, 2)
	c.Assert(containers2, check.HasLen, 2)

	// Should do nothing if calling on already scaled
	go a.stop()
	err = a.run()
	c.Assert(err, check.IsNil)
	nodes, err = p.cluster.Nodes()
	c.Assert(err, check.IsNil)
	c.Assert(nodes, check.HasLen, 2)

	containers1Again, err := p.listContainersByHost(urlToHost(nodes[0].Address))
	c.Assert(err, check.IsNil)
	containers2Again, err := p.listContainersByHost(urlToHost(nodes[1].Address))
	c.Assert(err, check.IsNil)
	c.Assert(containers1, check.DeepEquals, containers1Again)
	c.Assert(containers2, check.DeepEquals, containers2Again)
}

func (s *S) TestAutoScaleConfigRunNoGroup(c *check.C) {
	rollback := startTestRepositoryServer()
	defer rollback()
	defer func() {
		machines, _ := iaas.ListMachines()
		for _, m := range machines {
			m.Destroy()
		}
	}()
	node1, err := dtesting.NewServer("127.0.0.1:0", nil, nil)
	c.Assert(err, check.IsNil)
	node2, err := dtesting.NewServer("127.0.0.1:0", nil, nil)
	c.Assert(err, check.IsNil)
	config.Set("iaas:node-port", urlPort(node2.URL()))
	defer config.Unset("iaas:node-port")

	var p dockerProvisioner
	p.storage = &cluster.MapStorage{}
	clusterInstance, err := cluster.New(nil, p.storage,
		cluster.Node{Address: node1.URL(), Metadata: map[string]string{
			"iaas": "my-scale-iaas",
		}},
	)
	c.Assert(err, check.IsNil)
	p.cluster = clusterInstance
	iaasInstance := &TestHealerIaaS{addr: "localhost"}
	iaas.RegisterIaasProvider("my-scale-iaas", iaasInstance)
	appInstance := provisiontest.NewFakeApp("myapp", "python", 0)
	defer p.Destroy(appInstance)
	p.Provision(appInstance)
	imageId, err := appCurrentImageName(appInstance.GetName())
	c.Assert(err, check.IsNil)
{'fraction_non_alphanumeric': 0.15958549222797927, 'fraction_numerical': 0.015428900402993667, 'mean_word_length': 4.771428571428571, 'pattern_counts': {'":': 8, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 8, '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': '3380540', 'n_tokens_mistral': 3457, 'n_tokens_neox': 3318, 'n_words': 606}