7
10
stdtesting "testing"
9
12
. "launchpad.net/gocheck"
13
"launchpad.net/juju-core/container"
14
"launchpad.net/juju-core/container/lxc"
15
_ "launchpad.net/juju-core/environs/dummy"
16
"launchpad.net/juju-core/instance"
17
jujutesting "launchpad.net/juju-core/juju/testing"
18
"launchpad.net/juju-core/state"
19
"launchpad.net/juju-core/testing"
20
. "launchpad.net/juju-core/testing/checkers"
21
"launchpad.net/juju-core/version"
12
func Test(t *stdtesting.T) { TestingT(t) }
24
func Test(t *stdtesting.T) {
14
type LxcSuite struct{}
28
type LxcSuite struct {
33
oldContainerDir string
35
oldLxcContainerDir string
16
38
var _ = Suite(&LxcSuite{})
40
func (s *LxcSuite) SetUpSuite(c *C) {
41
s.LoggingSuite.SetUpSuite(c)
44
func (s *LxcSuite) TearDownSuite(c *C) {
45
s.LoggingSuite.TearDownSuite(c)
48
func (s *LxcSuite) SetUpTest(c *C) {
49
s.LoggingSuite.SetUpTest(c)
50
s.containerDir = c.MkDir()
51
s.oldContainerDir = lxc.SetContainerDir(s.containerDir)
52
s.removedDir = c.MkDir()
53
s.oldRemovedDir = lxc.SetRemovedContainerDir(s.removedDir)
55
s.oldLxcContainerDir = lxc.SetLxcContainerDir(s.lxcDir)
58
func (s *LxcSuite) TearDownTest(c *C) {
59
lxc.SetContainerDir(s.oldContainerDir)
60
lxc.SetLxcContainerDir(s.oldLxcContainerDir)
61
s.LoggingSuite.TearDownTest(c)
64
func (s *LxcSuite) TestNewContainer(c *C) {
65
factory := lxc.NewFactory(MockFactory())
66
container, err := factory.NewContainer("2/lxc/0")
68
c.Assert(container.Id(), Equals, instance.Id("machine-2-lxc-0"))
69
machineId, ok := lxc.GetMachineId(container)
71
c.Assert(machineId, Equals, "2/lxc/0")
74
func (s *LxcSuite) TestNewFromExisting(c *C) {
76
mockLxc := mock.New("machine-1-lxc-0")
77
factory := lxc.NewFactory(mock)
78
container, err := factory.NewFromExisting(mockLxc)
80
c.Assert(container.Id(), Equals, instance.Id("machine-1-lxc-0"))
81
machineId, ok := lxc.GetMachineId(container)
83
c.Assert(machineId, Equals, "1/lxc/0")
86
func ContainerCreate(c *C, container container.Container) {
87
machineId, ok := lxc.GetMachineId(container)
89
config := testing.EnvironConfig(c)
90
stateInfo := jujutesting.FakeStateInfo(machineId)
91
apiInfo := jujutesting.FakeAPIInfo(machineId)
95
tools := &state.Tools{
96
Binary: version.MustParseBinary("2.3.4-foo-bar"),
97
URL: "http://tools.example.com/2.3.4-foo-bar.tgz",
100
err := container.Create(series, nonce, tools, config, stateInfo, apiInfo)
104
func (s *LxcSuite) TestContainerCreate(c *C) {
106
factory := lxc.NewFactory(MockFactory())
107
container, err := factory.NewContainer("1/lxc/0")
110
ContainerCreate(c, container)
112
name := string(container.Id())
113
// Check our container config files.
114
testing.AssertNonEmptyFileExists(c, filepath.Join(s.containerDir, name, "lxc.conf"))
115
testing.AssertNonEmptyFileExists(c, filepath.Join(s.containerDir, name, "cloud-init"))
116
// Check the mount point has been created inside the container.
117
testing.AssertDirectoryExists(c, filepath.Join(s.lxcDir, name, "rootfs/var/log/juju"))
120
func (s *LxcSuite) TestContainerDestroy(c *C) {
121
factory := lxc.NewFactory(MockFactory())
122
container, err := factory.NewContainer("1/lxc/0")
125
ContainerCreate(c, container)
126
err = container.Destroy()
129
name := string(container.Id())
130
// Check that the container dir is no longer in the container dir
131
testing.AssertDirectoryDoesNotExist(c, filepath.Join(s.containerDir, name))
132
// but instead, in the removed container dir
133
testing.AssertDirectoryExists(c, filepath.Join(s.removedDir, name))
136
func (s *LxcSuite) TestContainerRemovedDirNameClash(c *C) {
137
factory := lxc.NewFactory(MockFactory())
138
container, err := factory.NewContainer("1/lxc/0")
141
name := string(container.Id())
142
targetDir := filepath.Join(s.removedDir, name)
143
err = os.MkdirAll(targetDir, 0755)
146
ContainerCreate(c, container)
147
err = container.Destroy()
150
// Check that the container dir is no longer in the container dir
151
testing.AssertDirectoryDoesNotExist(c, filepath.Join(s.containerDir, name))
152
// but instead, in the removed container dir with a ".1" suffix as there was already a directory there.
153
testing.AssertDirectoryExists(c, filepath.Join(s.removedDir, fmt.Sprintf("%s.1", name)))