<commit_before>// Copyright 2013 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 juju

import (
	"fmt"
	"github.com/globocom/commandmocker"
	"github.com/globocom/tsuru/heal"
	. "launchpad.net/gocheck"
	"net"
)

func (s *S) TestInstanceUnitShouldBeRegistered(c *C) {
	h, err := heal.Get("instance-unit")
	c.Assert(err, IsNil)
	c.Assert(h, FitsTypeOf, &InstanceUnitHealer{})
}

func (s *S) TestInstaceUnitHeal(c *C) {
	jujuTmpdir, err := commandmocker.Add("juju", collectOutputInstanceDown)
	c.Assert(err, IsNil)
	defer commandmocker.Remove(jujuTmpdir)
	sshTmpdir, err := commandmocker.Add("ssh", "$*")
	c.Assert(err, IsNil)
	defer commandmocker.Remove(sshTmpdir)
	jujuOutput := []string{
		"status", // for juju status that gets the output
	}
	sshOutput := []string{
		"-o",
		"StrictHostKeyChecking no",
		"-q",
		"-l",
		"ubuntu",
		"server-1081.novalocal",
		"sudo",
		"stop",
		"juju-as_i_rise-0",
		"-o",
		"StrictHostKeyChecking no",
		"-q",
		"-l",
		"ubuntu",<commit_msg>Added a test for instance unit healer.<commit_after>// Copyright 2013 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 juju

import (
	"fmt"
	"github.com/globocom/commandmocker"
	"github.com/globocom/tsuru/heal"
	. "launchpad.net/gocheck"
	"net"
)

func (s *S) TestInstanceUnitShouldBeRegistered(c *C) {
	h, err := heal.Get("instance-unit")
	c.Assert(err, IsNil)
	c.Assert(h, FitsTypeOf, &InstanceUnitHealer{})
}

func (s *S) TestInstaceUnitHealWhenEverythingIsOk(c *C) {
	jujuTmpdir, err := commandmocker.Add("juju", collectOutputInstanceDown)
	c.Assert(err, IsNil)
	defer commandmocker.Remove(jujuTmpdir)
	sshTmpdir, err := commandmocker.Add("ssh", "$*")
	c.Assert(err, IsNil)
	defer commandmocker.Remove(sshTmpdir)
	jujuOutput := []string{
		"status", // for juju status that gets the output
	}
	h := InstanceUnitHealer{}
	err = h.Heal()
	c.Assert(err, IsNil)
	c.Assert(commandmocker.Ran(jujuTmpdir), Equals, true)
	c.Assert(commandmocker.Parameters(jujuTmpdir), DeepEquals, jujuOutput)
	c.Assert(commandmocker.Ran(sshTmpdir), Equals, false)
}

func (s *S) TestInstaceUnitHeal(c *C) {
	jujuTmpdir, err := commandmocker.Add("juju", collectOutputInstanceDown)
	c.Assert(err, IsNil)
	defer commandmocker.Remove(jujuTmpdir)
	sshTmpdir, err := commandmocker.Add("ssh", "$*")
	c.Assert(err, IsNil)
	defer commandmocker.Remove(sshTmpdir)
	jujuOutput := []string{
		"status", // for juju status that gets the output
	}
	sshOutput := []string{
		"-o",
		"StrictHostKeyChecking no",
		"-q",
		"-l",
		"ubuntu",
		"server-1081.novalocal",
		"sudo",
		"stop",
		"juju-as_i_rise-0",
		"-o",
		"StrictHostKeyChecking no",
		"-q",
		"-l",
		"ubuntu",
{'fraction_non_alphanumeric': 0.156904508342208, 'fraction_numerical': 0.006389776357827476, 'mean_word_length': 4.591269841269841, 'pattern_counts': {'":': 0, '<': 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': '6485872', 'n_tokens_mistral': 1200, 'n_tokens_neox': 1102, 'n_words': 242}