~wallyworld/juju-core/1304742-backport-1.18

« back to all changes in this revision

Viewing changes to container/lxc/lxc_test.go

Merge prev pipe.

Show diffs side-by-side

added added

removed removed

Lines of Context:
4
4
package lxc_test
5
5
 
6
6
import (
 
7
        "fmt"
 
8
        "os"
 
9
        "path/filepath"
7
10
        stdtesting "testing"
8
11
 
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"
10
22
)
11
23
 
12
 
func Test(t *stdtesting.T) { TestingT(t) }
 
24
func Test(t *stdtesting.T) {
 
25
        TestingT(t)
 
26
}
13
27
 
14
 
type LxcSuite struct{}
 
28
type LxcSuite struct {
 
29
        testing.LoggingSuite
 
30
        containerDir       string
 
31
        removedDir         string
 
32
        lxcDir             string
 
33
        oldContainerDir    string
 
34
        oldRemovedDir      string
 
35
        oldLxcContainerDir string
 
36
}
15
37
 
16
38
var _ = Suite(&LxcSuite{})
 
39
 
 
40
func (s *LxcSuite) SetUpSuite(c *C) {
 
41
        s.LoggingSuite.SetUpSuite(c)
 
42
}
 
43
 
 
44
func (s *LxcSuite) TearDownSuite(c *C) {
 
45
        s.LoggingSuite.TearDownSuite(c)
 
46
}
 
47
 
 
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)
 
54
        s.lxcDir = c.MkDir()
 
55
        s.oldLxcContainerDir = lxc.SetLxcContainerDir(s.lxcDir)
 
56
}
 
57
 
 
58
func (s *LxcSuite) TearDownTest(c *C) {
 
59
        lxc.SetContainerDir(s.oldContainerDir)
 
60
        lxc.SetLxcContainerDir(s.oldLxcContainerDir)
 
61
        s.LoggingSuite.TearDownTest(c)
 
62
}
 
63
 
 
64
func (s *LxcSuite) TestNewContainer(c *C) {
 
65
        factory := lxc.NewFactory(MockFactory())
 
66
        container, err := factory.NewContainer("2/lxc/0")
 
67
        c.Assert(err, IsNil)
 
68
        c.Assert(container.Id(), Equals, instance.Id("machine-2-lxc-0"))
 
69
        machineId, ok := lxc.GetMachineId(container)
 
70
        c.Assert(ok, IsTrue)
 
71
        c.Assert(machineId, Equals, "2/lxc/0")
 
72
}
 
73
 
 
74
func (s *LxcSuite) TestNewFromExisting(c *C) {
 
75
        mock := MockFactory()
 
76
        mockLxc := mock.New("machine-1-lxc-0")
 
77
        factory := lxc.NewFactory(mock)
 
78
        container, err := factory.NewFromExisting(mockLxc)
 
79
        c.Assert(err, IsNil)
 
80
        c.Assert(container.Id(), Equals, instance.Id("machine-1-lxc-0"))
 
81
        machineId, ok := lxc.GetMachineId(container)
 
82
        c.Assert(ok, IsTrue)
 
83
        c.Assert(machineId, Equals, "1/lxc/0")
 
84
}
 
85
 
 
86
func ContainerCreate(c *C, container container.Container) {
 
87
        machineId, ok := lxc.GetMachineId(container)
 
88
        c.Assert(ok, IsTrue)
 
89
        config := testing.EnvironConfig(c)
 
90
        stateInfo := jujutesting.FakeStateInfo(machineId)
 
91
        apiInfo := jujutesting.FakeAPIInfo(machineId)
 
92
 
 
93
        series := "series"
 
94
        nonce := "fake-nonce"
 
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",
 
98
        }
 
99
 
 
100
        err := container.Create(series, nonce, tools, config, stateInfo, apiInfo)
 
101
        c.Assert(err, IsNil)
 
102
}
 
103
 
 
104
func (s *LxcSuite) TestContainerCreate(c *C) {
 
105
 
 
106
        factory := lxc.NewFactory(MockFactory())
 
107
        container, err := factory.NewContainer("1/lxc/0")
 
108
        c.Assert(err, IsNil)
 
109
 
 
110
        ContainerCreate(c, container)
 
111
 
 
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"))
 
118
}
 
119
 
 
120
func (s *LxcSuite) TestContainerDestroy(c *C) {
 
121
        factory := lxc.NewFactory(MockFactory())
 
122
        container, err := factory.NewContainer("1/lxc/0")
 
123
        c.Assert(err, IsNil)
 
124
 
 
125
        ContainerCreate(c, container)
 
126
        err = container.Destroy()
 
127
        c.Assert(err, IsNil)
 
128
 
 
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))
 
134
}
 
135
 
 
136
func (s *LxcSuite) TestContainerRemovedDirNameClash(c *C) {
 
137
        factory := lxc.NewFactory(MockFactory())
 
138
        container, err := factory.NewContainer("1/lxc/0")
 
139
        c.Assert(err, IsNil)
 
140
 
 
141
        name := string(container.Id())
 
142
        targetDir := filepath.Join(s.removedDir, name)
 
143
        err = os.MkdirAll(targetDir, 0755)
 
144
        c.Assert(err, IsNil)
 
145
 
 
146
        ContainerCreate(c, container)
 
147
        err = container.Destroy()
 
148
        c.Assert(err, IsNil)
 
149
 
 
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)))
 
154
}