launchpad.net/~rogpeppe/juju-core/500-errgo-fix@v0.0.0-20140213181702-000000002356/container/directory_test.go (about)

     1  // Copyright 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package container_test
     5  
     6  import (
     7  	"os"
     8  	"path/filepath"
     9  
    10  	gc "launchpad.net/gocheck"
    11  
    12  	"launchpad.net/juju-core/container"
    13  	jc "launchpad.net/juju-core/testing/checkers"
    14  	"launchpad.net/juju-core/testing/testbase"
    15  )
    16  
    17  type DirectorySuite struct {
    18  	testbase.LoggingSuite
    19  	containerDir string
    20  	removedDir   string
    21  }
    22  
    23  var _ = gc.Suite(&DirectorySuite{})
    24  
    25  func (s *DirectorySuite) SetUpTest(c *gc.C) {
    26  	s.LoggingSuite.SetUpTest(c)
    27  	s.containerDir = c.MkDir()
    28  	s.PatchValue(&container.ContainerDir, s.containerDir)
    29  	s.removedDir = c.MkDir()
    30  	s.PatchValue(&container.RemovedContainerDir, s.removedDir)
    31  }
    32  
    33  func (*DirectorySuite) TestNewContainerDir(c *gc.C) {
    34  	dir, err := container.NewDirectory("testing")
    35  	c.Assert(err, gc.IsNil)
    36  	c.Assert(dir, jc.IsDirectory)
    37  }
    38  
    39  func (s *DirectorySuite) TestRemoveContainerDir(c *gc.C) {
    40  	dir, err := container.NewDirectory("testing")
    41  	c.Assert(err, gc.IsNil)
    42  	err = container.RemoveDirectory("testing")
    43  	c.Assert(err, gc.IsNil)
    44  	c.Assert(dir, jc.DoesNotExist)
    45  	c.Assert(filepath.Join(s.removedDir, "testing"), jc.IsDirectory)
    46  }
    47  
    48  func (s *DirectorySuite) TestRemoveContainerDirWithClash(c *gc.C) {
    49  	dir, err := container.NewDirectory("testing")
    50  	c.Assert(err, gc.IsNil)
    51  
    52  	clash := filepath.Join(s.removedDir, "testing")
    53  	err = os.MkdirAll(clash, 0755)
    54  	c.Assert(err, gc.IsNil)
    55  
    56  	err = container.RemoveDirectory("testing")
    57  	c.Assert(err, gc.IsNil)
    58  	c.Assert(dir, jc.DoesNotExist)
    59  	c.Assert(filepath.Join(s.removedDir, "testing.1"), jc.IsDirectory)
    60  }