<commit_before>// Copyright 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.

package lxc_test

import (
	"fmt"
	"os"
	"path/filepath"
	stdtesting "testing"

	. "launchpad.net/gocheck"
	"launchpad.net/juju-core/container/lxc"
	"launchpad.net/juju-core/container/lxc/mock"
	"launchpad.net/juju-core/instance"
	jujutesting "launchpad.net/juju-core/juju/testing"
	"launchpad.net/juju-core/state"
	"launchpad.net/juju-core/testing"
	"launchpad.net/juju-core/version"
)

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

type LxcSuite struct {
	testing.LoggingSuite
	containerDir       string
	removedDir         string
	lxcDir             string
	oldContainerDir    string
	oldRemovedDir      string
	oldLxcContainerDir string
}

var _ = Suite(&LxcSuite{})

func (s *LxcSuite) SetUpSuite(c *C) {
	s.LoggingSuite.SetUpSuite(c)
}

func (s *LxcSuite) TearDownSuite(c *C) {
	s.LoggingSuite.TearDownSuite(c)
}

func (s *LxcSuite) SetUpTest(c *C) {
	s.LoggingSuite.SetUpTest(c)
	s.containerDir = c.MkDir()
	s.oldContainerDir = lxc.SetContainerDir(s.containerDir)
	s.removedDir = c.MkDir()
	s.oldRemovedDir = lxc.SetRemovedContainerDir(s.removedDir)
	s.lxcDir = c.MkDir()
	s.oldLxcContainerDir = lxc.SetLxcContainerDir(s.lxcDir)
}

func (s *LxcSuite) TearDownTest(c *C) {
	lxc.SetContainerDir(s.oldContainerDir)
	lxc.SetLxcContainerDir(s.oldLxcContainerDir)
	lxc.SetRemovedContainerDir(s.oldRemovedDir)
	s.LoggingSuite.TearDownTest(c)
}

func StartContainer(c *C, manager lxc.ContainerManager, machineId string) instance.Instance {
	config := testing.EnvironConfig(c)
	stateInfo := jujutesting.FakeStateInfo(machineId)
	apiInfo := jujutesting.FakeAPIInfo(machineId)

	series := "series"
	nonce := "fake-nonce"
	tools := &state.Tools{
		Binary: version.MustParseBinary("2.3.4-foo-bar"),
		URL:    "http://tools.example.com/2.3.4-foo-bar.tgz",
	}

	inst, err := manager.StartContainer(machineId, series, nonce, tools, config, stateInfo, apiInfo)
	c.Assert(err, IsNil)
	return inst
}

func (s *LxcSuite) TestStartContainer(c *C) {
	manager := lxc.NewContainerManager(mock.MockFactory(), "")
	instance := StartContainer(c, manager, "1/lxc/0")

	name := string(instance.Id())
	// Check our container config files.
	testing.AssertNonEmptyFileExists(c, filepath.Join(s.containerDir, name, "lxc.conf"))
	testing.AssertNonEmptyFileExists(c, filepath.Join(s.containerDir, name, "cloud-init"))
	// Check the mount point has been created inside the container.
	testing.AssertDirectoryExists(c, filepath.Join(s.lxcDir, name, "rootfs/var/log/juju"))
}

func (s *LxcSuite) TestStopContainer(c *C) {
	manager := lxc.NewContainerManager(mock.MockFactory(), "")
	instance := StartContainer(c, manager, "1/lxc/0")

	err := manager.StopContainer(instance)
	c.Assert(err, IsNil)

	name := string(instance.Id())
	// Check that the container dir is no longer in the container dir
	testing.AssertDirectoryDoesNotExist(c, filepath.Join(s.containerDir, name))
	// but instead, in the removed container dir
	testing.AssertDirectoryExists(c, filepath.Join(s.removedDir, name))
}

func (s *LxcSuite) TestStopContainerNameClash(c *C) {
	manager := lxc.NewContainerManager(mock.MockFactory(), "")
	instance := StartContainer(c, manager, "1/lxc/0")

	name := string(instance.Id())
	targetDir := filepath.Join(s.removedDir, name)
	err := os.MkdirAll(targetDir, 0755)
	c.Assert(err, IsNil)

	err = manager.StopContainer(instance)
	c.Assert(err, IsNil)

	// Check that the container dir is no longer in the container dir
	testing.AssertDirectoryDoesNotExist(c, filepath.Join(s.containerDir, name))
	// but instead, in the removed container dir with a ".1" suffix as there was already a directory there.
	testing.AssertDirectoryExists(c, filepath.Join(s.removedDir, fmt.Sprintf("%s.1", name)))
}

func (s *LxcSuite) TestNamedManagerPrefix(c *C) {
	manager := lxc.NewContainerManager(MockFactory(), "eric")
	instance := StartContainer(c, manager, "1/lxc/0")
	c.Assert(string(instance.Id()), Equals, "eric-machine-1-lxc-0")
}

func (s *LxcSuite) TestListContainers(c *C) {
	factory := MockFactory()
	foo := lxc.NewContainerManager(factory, "foo")
	bar := lxc.NewContainerManager(factory, "bar")

	foo1 := StartContainer(c, foo, "1/lxc/0")<commit_msg>Fix the mock factory from the non-clashing bits.<commit_after>// Copyright 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.

package lxc_test

import (
	"fmt"
	"os"
	"path/filepath"
	stdtesting "testing"

	. "launchpad.net/gocheck"
	"launchpad.net/juju-core/container/lxc"
	"launchpad.net/juju-core/container/lxc/mock"
	"launchpad.net/juju-core/instance"
	jujutesting "launchpad.net/juju-core/juju/testing"
	"launchpad.net/juju-core/state"
	"launchpad.net/juju-core/testing"
	"launchpad.net/juju-core/version"
)

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

type LxcSuite struct {
	testing.LoggingSuite
	containerDir       string
	removedDir         string
	lxcDir             string
	oldContainerDir    string
	oldRemovedDir      string
	oldLxcContainerDir string
}

var _ = Suite(&LxcSuite{})

func (s *LxcSuite) SetUpSuite(c *C) {
	s.LoggingSuite.SetUpSuite(c)
}

func (s *LxcSuite) TearDownSuite(c *C) {
	s.LoggingSuite.TearDownSuite(c)
}

func (s *LxcSuite) SetUpTest(c *C) {
	s.LoggingSuite.SetUpTest(c)
	s.containerDir = c.MkDir()
	s.oldContainerDir = lxc.SetContainerDir(s.containerDir)
	s.removedDir = c.MkDir()
	s.oldRemovedDir = lxc.SetRemovedContainerDir(s.removedDir)
	s.lxcDir = c.MkDir()
	s.oldLxcContainerDir = lxc.SetLxcContainerDir(s.lxcDir)
}

func (s *LxcSuite) TearDownTest(c *C) {
	lxc.SetContainerDir(s.oldContainerDir)
	lxc.SetLxcContainerDir(s.oldLxcContainerDir)
	lxc.SetRemovedContainerDir(s.oldRemovedDir)
	s.LoggingSuite.TearDownTest(c)
}

func StartContainer(c *C, manager lxc.ContainerManager, machineId string) instance.Instance {
	config := testing.EnvironConfig(c)
	stateInfo := jujutesting.FakeStateInfo(machineId)
	apiInfo := jujutesting.FakeAPIInfo(machineId)

	series := "series"
	nonce := "fake-nonce"
	tools := &state.Tools{
		Binary: version.MustParseBinary("2.3.4-foo-bar"),
		URL:    "http://tools.example.com/2.3.4-foo-bar.tgz",
	}

	inst, err := manager.StartContainer(machineId, series, nonce, tools, config, stateInfo, apiInfo)
	c.Assert(err, IsNil)
	return inst
}

func (s *LxcSuite) TestStartContainer(c *C) {
	manager := lxc.NewContainerManager(mock.MockFactory(), "")
	instance := StartContainer(c, manager, "1/lxc/0")

	name := string(instance.Id())
	// Check our container config files.
	testing.AssertNonEmptyFileExists(c, filepath.Join(s.containerDir, name, "lxc.conf"))
	testing.AssertNonEmptyFileExists(c, filepath.Join(s.containerDir, name, "cloud-init"))
	// Check the mount point has been created inside the container.
	testing.AssertDirectoryExists(c, filepath.Join(s.lxcDir, name, "rootfs/var/log/juju"))
}

func (s *LxcSuite) TestStopContainer(c *C) {
	manager := lxc.NewContainerManager(mock.MockFactory(), "")
	instance := StartContainer(c, manager, "1/lxc/0")

	err := manager.StopContainer(instance)
	c.Assert(err, IsNil)

	name := string(instance.Id())
	// Check that the container dir is no longer in the container dir
	testing.AssertDirectoryDoesNotExist(c, filepath.Join(s.containerDir, name))
	// but instead, in the removed container dir
	testing.AssertDirectoryExists(c, filepath.Join(s.removedDir, name))
}

func (s *LxcSuite) TestStopContainerNameClash(c *C) {
	manager := lxc.NewContainerManager(mock.MockFactory(), "")
	instance := StartContainer(c, manager, "1/lxc/0")

	name := string(instance.Id())
	targetDir := filepath.Join(s.removedDir, name)
	err := os.MkdirAll(targetDir, 0755)
	c.Assert(err, IsNil)

	err = manager.StopContainer(instance)
	c.Assert(err, IsNil)

	// Check that the container dir is no longer in the container dir
	testing.AssertDirectoryDoesNotExist(c, filepath.Join(s.containerDir, name))
	// but instead, in the removed container dir with a ".1" suffix as there was already a directory there.
	testing.AssertDirectoryExists(c, filepath.Join(s.removedDir, fmt.Sprintf("%s.1", name)))
}

func (s *LxcSuite) TestNamedManagerPrefix(c *C) {
	manager := lxc.NewContainerManager(mock.MockFactory(), "eric")
	instance := StartContainer(c, manager, "1/lxc/0")
	c.Assert(string(instance.Id()), Equals, "eric-machine-1-lxc-0")
}

func (s *LxcSuite) TestListContainers(c *C) {
	factory := mock.MockFactory()
	foo := lxc.NewContainerManager(factory, "foo")
	bar := lxc.NewContainerManager(factory, "bar")

	foo1 := StartContainer(c, foo, "1/lxc/0")
{'fraction_non_alphanumeric': 0.13954582892104953, 'fraction_numerical': 0.0070596540769502295, 'mean_word_length': 5.0455192034139404, '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': '4118391', 'n_tokens_mistral': 3167, 'n_tokens_neox': 3078, 'n_words': 616}