| Index: golxc_test.go |
| === added file 'golxc_test.go' |
| --- golxc_test.go 1970-01-01 00:00:00 +0000 |
| +++ golxc_test.go 2012-11-23 13:45:54 +0000 |
| @@ -0,0 +1,247 @@ |
| +package golxc_test |
| + |
| +import ( |
| + . "launchpad.net/gocheck" |
| + "launchpad.net/golxc" |
| + "os" |
| + "os/user" |
| + "testing" |
| +) |
| + |
| +func Test(t *testing.T) { TestingT(t) } |
| + |
| +type LXCSuite struct{} |
| + |
| +var _ = Suite(&LXCSuite{}) |
| + |
| +func (s *LXCSuite) SetUpSuite(c *C) { |
| + u, err := user.Current() |
| + c.Assert(err, IsNil) |
| + if u.Uid != "0" { |
| + // Has to be run as root! |
| + c.Skip("tests must run as root") |
| + } |
| +} |
| + |
| +func (s *LXCSuite) TestCreateDestroy(c *C) { |
| + // Test clean creation and destroying of a container. |
| + lc := golxc.New("golxc") |
| + c.Assert(lc.IsConstructed(), Equals, false) |
| + home := golxc.ContainerHome(lc) |
| + _, err := os.Stat(home) |
| + c.Assert(err, ErrorMatches, "stat .*: no such file or directory") |
| + err = lc.Create("ubuntu") |
| + c.Assert(err, IsNil) |
| + c.Assert(lc.IsConstructed(), Equals, true) |
| + defer func() { |
| + err = lc.Destroy() |
| + c.Assert(err, IsNil) |
| + _, err = os.Stat(home) |
| + c.Assert(err, ErrorMatches, "stat .*: no such file or directory") |
| + }() |
| + fi, err := os.Stat(golxc.ContainerHome(lc)) |
| + c.Assert(err, IsNil) |
| + c.Assert(fi.IsDir(), Equals, true) |
| +} |
| + |
| +func (s *LXCSuite) TestCreateTwice(c *C) { |
| + // Test that a container cannot be created twice. |
| + lc1 := golxc.New("golxc") |
| + c.Assert(lc1.IsConstructed(), Equals, false) |
| + err := lc1.Create("ubuntu") |
| + c.Assert(err, IsNil) |
| + c.Assert(lc1.IsConstructed(), Equals, true) |
| + defer func() { |
| + c.Assert(lc1.Destroy(), IsNil) |
| + }() |
| + lc2 := golxc.New("golxc") |
| + err = lc2.Create("ubuntu") |
| + c.Assert(err, ErrorMatches, "container .* is already created") |
| +} |
| + |
| +func (s *LXCSuite) TestCreateIllegalTemplate(c *C) { |
| + // Test that a container creation fails correctly in |
| + // case of an illegal template. |
| + lc := golxc.New("golxc") |
| + c.Assert(lc.IsConstructed(), Equals, false) |
| + err := lc.Create("name-of-a-not-existing-template-for-golxc") |
| + c.Assert(err, ErrorMatches, `error executing "lxc-create": No config file specified, .*`) |
| + c.Assert(lc.IsConstructed(), Equals, false) |
| +} |
| + |
| +func (l *LXCSuite) TestDestroyNotCreated(c *C) { |
| + // Test that a non-existing container can't be destroyed. |
| + lc := golxc.New("golxc") |
| + c.Assert(lc.IsConstructed(), Equals, false) |
| + err := lc.Destroy() |
| + c.Assert(err, ErrorMatches, "container .* is not yet created") |
| +} |
| + |
| +func contains(lcs []*golxc.Container, lc *golxc.Container) bool { |
| + for _, clc := range lcs { |
| + if clc.Name() == lc.Name() { |
| + return true |
| + } |
| + } |
| + return false |
| +} |
| + |
| +func (s *LXCSuite) TestList(c *C) { |
| + // Test the listing of created containers. |
| + lcs, err := golxc.List() |
| + oldLen := len(lcs) |
| + c.Assert(err, IsNil) |
| + c.Assert(oldLen >= 0, Equals, true) |
| + lc := golxc.New("golxc") |
| + c.Assert(lc.IsConstructed(), Equals, false) |
| + c.Assert(lc.Create("ubuntu"), IsNil) |
| + c.Assert(lc.IsConstructed(), Equals, true) |
| + defer func() { |
| + c.Assert(lc.Destroy(), IsNil) |
| + }() |
| + lcs, _ = golxc.List() |
| + newLen := len(lcs) |
| + c.Assert(newLen == oldLen+1, Equals, true) |
| + c.Assert(contains(lcs, lc), Equals, true) |
| +} |
| + |
| +func (s *LXCSuite) TestClone(c *C) { |
| + // Test the cloning of an existing container. |
| + lc1 := golxc.New("golxc") |
| + c.Assert(lc1.IsConstructed(), Equals, false) |
| + c.Assert(lc1.Create("ubuntu"), IsNil) |
| + c.Assert(lc1.IsConstructed(), Equals, true) |
| + defer func() { |
| + c.Assert(lc1.Destroy(), IsNil) |
| + }() |
| + lcs, _ := golxc.List() |
| + oldLen := len(lcs) |
| + lc2, err := lc1.Clone("golxcclone") |
| + c.Assert(err, IsNil) |
| + c.Assert(lc2.IsConstructed(), Equals, true) |
| + defer func() { |
| + c.Assert(lc2.Destroy(), IsNil) |
| + }() |
| + lcs, _ = golxc.List() |
| + newLen := len(lcs) |
| + c.Assert(newLen == oldLen+1, Equals, true) |
| + c.Assert(contains(lcs, lc1), Equals, true) |
| + c.Assert(contains(lcs, lc2), Equals, true) |
| +} |
| + |
| +func (s *LXCSuite) TestCloneNotCreated(c *C) { |
| + // Test the cloning of a non-existing container. |
| + lc := golxc.New("golxc") |
| + c.Assert(lc.IsConstructed(), Equals, false) |
| + _, err := lc.Clone("golxcclone") |
| + c.Assert(err, ErrorMatches, "container .* is not yet created") |
| +} |
| + |
| +func (s *LXCSuite) TestStartStop(c *C) { |
| + // Test starting and stopping a container. |
| + lc := golxc.New("golxc") |
| + c.Assert(lc.IsConstructed(), Equals, false) |
| + c.Assert(lc.Create("ubuntu"), IsNil) |
| + defer func() { |
| + c.Assert(lc.Destroy(), IsNil) |
| + }() |
| + c.Assert(lc.Start("", ""), IsNil) |
| + c.Assert(lc.IsRunning(), Equals, true) |
| + c.Assert(lc.Stop(), IsNil) |
| + c.Assert(lc.IsRunning(), Equals, false) |
| +} |
| + |
| +func (l *LXCSuite) TestStartNotCreated(c *C) { |
| + // Test that a non-existing container can't be started. |
| + lc := golxc.New("golxc") |
| + c.Assert(lc.IsConstructed(), Equals, false) |
| + c.Assert(lc.Start("", ""), ErrorMatches, "container .* is not yet created") |
| +} |
| + |
| +func (l *LXCSuite) TestStopNotRunning(c *C) { |
| + // Test that a not running container can't be stopped. |
| + lc := golxc.New("golxc") |
| + c.Assert(lc.IsConstructed(), Equals, false) |
| + c.Assert(lc.Create("ubuntu"), IsNil) |
| + defer func() { |
| + c.Assert(lc.Destroy(), IsNil) |
| + }() |
| + c.Assert(lc.Stop(), IsNil) |
| +} |
| + |
| +func (s *LXCSuite) TestWait(c *C) { |
| + // Test waiting for one of a number of states of a container. |
| + // ATTN: Using a not reached state blocks the test until timeout! |
| + lc := golxc.New("golxc") |
| + c.Assert(lc.IsConstructed(), Equals, false) |
| + c.Assert(lc.Wait(), ErrorMatches, "no states specified") |
| + c.Assert(lc.Wait(golxc.StateStopped), IsNil) |
| + c.Assert(lc.Wait(golxc.StateStopped, golxc.StateRunning), IsNil) |
| + c.Assert(lc.Create("ubuntu"), IsNil) |
| + defer func() { |
| + c.Assert(lc.Destroy(), IsNil) |
| + }() |
| + go func() { |
| + c.Assert(lc.Start("", ""), IsNil) |
| + }() |
| + c.Assert(lc.Wait(golxc.StateRunning), IsNil) |
| +} |
| + |
| +func (l *LXCSuite) TestFreezeUnfreeze(c *C) { |
| + // Test the freezing and unfreezing of a started container. |
| + lc := golxc.New("golxc") |
| + c.Assert(lc.IsConstructed(), Equals, false) |
| + c.Assert(lc.Create("ubuntu"), IsNil) |
| + defer func() { |
| + c.Assert(lc.Destroy(), IsNil) |
| + }() |
| + c.Assert(lc.Start("", ""), IsNil) |
| + defer func() { |
| + c.Assert(lc.Stop(), IsNil) |
| + }() |
| + c.Assert(lc.IsRunning(), Equals, true) |
| + c.Assert(lc.Freeze(), IsNil) |
| + c.Assert(lc.IsRunning(), Equals, false) |
| + c.Assert(lc.Unfreeze(), IsNil) |
| + c.Assert(lc.IsRunning(), Equals, true) |
| +} |
| + |
| +func (l *LXCSuite) TestFreezeNotStarted(c *C) { |
| + // Test that a not running container can't be frozen. |
| + lc := golxc.New("golxc") |
| + c.Assert(lc.IsConstructed(), Equals, false) |
| + c.Assert(lc.Create("ubuntu"), IsNil) |
| + defer func() { |
| + c.Assert(lc.Destroy(), IsNil) |
| + }() |
| + c.Assert(lc.Freeze(), ErrorMatches, "container .* is not running") |
| +} |
| + |
| +func (l *LXCSuite) TestFreezeNotCreated(c *C) { |
| + // Test that a non-existing container can't be frozen. |
| + lc := golxc.New("golxc") |
| + c.Assert(lc.IsConstructed(), Equals, false) |
| + c.Assert(lc.Freeze(), ErrorMatches, "container .* is not yet created") |
| +} |
| + |
| +func (l *LXCSuite) TestUnfreezeNotCreated(c *C) { |
| + // Test that a non-existing container can't be unfrozen. |
| + lc := golxc.New("golxc") |
| + c.Assert(lc.IsConstructed(), Equals, false) |
| + c.Assert(lc.Unfreeze(), ErrorMatches, "container .* is not yet created") |
| +} |
| + |
| +func (l *LXCSuite) TestUnfreezeNotFrozen(c *C) { |
| + // Test that a running container can't be unfrozen. |
| + lc := golxc.New("golxc") |
| + c.Assert(lc.IsConstructed(), Equals, false) |
| + c.Assert(lc.Create("ubuntu"), IsNil) |
| + defer func() { |
| + c.Assert(lc.Destroy(), IsNil) |
| + }() |
| + c.Assert(lc.Start("", ""), IsNil) |
| + defer func() { |
| + c.Assert(lc.Stop(), IsNil) |
| + }() |
| + c.Assert(lc.Unfreeze(), ErrorMatches, "container .* is not frozen") |
| +} |