github.com/Pankov404/juju@v0.0.0-20150703034450-be266991dceb/container/lxc/lxcutils/lxcutils_test.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package lxcutils_test
     5  
     6  import (
     7  	"path/filepath"
     8  	"runtime"
     9  	stdtesting "testing"
    10  
    11  	jc "github.com/juju/testing/checkers"
    12  	ft "github.com/juju/testing/filetesting"
    13  	gc "gopkg.in/check.v1"
    14  
    15  	"github.com/juju/juju/container/lxc/lxcutils"
    16  	"github.com/juju/juju/testing"
    17  )
    18  
    19  func Test(t *stdtesting.T) {
    20  	if runtime.GOOS != "linux" {
    21  		t.Skip("LXC is a Linux thing")
    22  	}
    23  	gc.TestingT(t)
    24  }
    25  
    26  type LxcUtilsSuite struct {
    27  	testing.BaseSuite
    28  }
    29  
    30  var _ = gc.Suite(&LxcUtilsSuite{})
    31  
    32  var lxcCgroupContents = `11:hugetlb:/lxc/juju-machine-1-lxc-0
    33  10:perf_event:/lxc/juju-machine-1-lxc-0
    34  9:blkio:/lxc/juju-machine-1-lxc-0
    35  8:freezer:/lxc/juju-machine-1-lxc-0
    36  7:devices:/lxc/juju-machine-1-lxc-0
    37  6:memory:/lxc/juju-machine-1-lxc-0
    38  5:cpuacct:/lxc/juju-machine-1-lxc-0
    39  4:cpu:/lxc/juju-machine-1-lxc-0
    40  3:cpuset:/lxc/juju-machine-1-lxc-0
    41  2:name=systemd:/lxc/juju-machine-1-lxc-0
    42  `
    43  
    44  var hostCgroupContents = `11:hugetlb:/
    45  10:perf_event:/
    46  9:blkio:/
    47  8:freezer:/
    48  7:devices:/
    49  6:memory:/
    50  5:cpuacct:/
    51  4:cpu:/
    52  3:cpuset:/
    53  2:name=systemd:/
    54  `
    55  
    56  var malformedCgroupFile = `some bogus content
    57  more bogus content`
    58  
    59  func (s *LxcUtilsSuite) TestRunningInsideLXCOnHost(c *gc.C) {
    60  	baseDir := c.MkDir()
    61  	cgroup := filepath.Join(baseDir, "cgroup")
    62  
    63  	ft.File{"cgroup", hostCgroupContents, 0400}.Create(c, baseDir)
    64  
    65  	s.PatchValue(lxcutils.InitProcessCgroupFile, cgroup)
    66  	runningInLXC, err := lxcutils.RunningInsideLXC()
    67  	c.Assert(err, jc.ErrorIsNil)
    68  	c.Assert(runningInLXC, jc.IsFalse)
    69  }
    70  
    71  func (s *LxcUtilsSuite) TestRunningInsideLXCOnLXCContainer(c *gc.C) {
    72  	baseDir := c.MkDir()
    73  	cgroup := filepath.Join(baseDir, "cgroup")
    74  
    75  	ft.File{"cgroup", lxcCgroupContents, 0400}.Create(c, baseDir)
    76  
    77  	s.PatchValue(lxcutils.InitProcessCgroupFile, cgroup)
    78  	runningInLXC, err := lxcutils.RunningInsideLXC()
    79  	c.Assert(err, jc.ErrorIsNil)
    80  	c.Assert(runningInLXC, jc.IsTrue)
    81  }
    82  
    83  func (s *LxcUtilsSuite) TestRunningInsideLXCMissingCgroupFile(c *gc.C) {
    84  	s.PatchValue(lxcutils.InitProcessCgroupFile, "")
    85  	_, err := lxcutils.RunningInsideLXC()
    86  	c.Assert(err.Error(), gc.Matches, "open : no such file or directory")
    87  }
    88  
    89  func (s *LxcUtilsSuite) TestRunningInsideLXCMalformedCgroupFile(c *gc.C) {
    90  	baseDir := c.MkDir()
    91  	cgroup := filepath.Join(baseDir, "cgroup")
    92  
    93  	ft.File{"cgroup", malformedCgroupFile, 0400}.Create(c, baseDir)
    94  
    95  	s.PatchValue(lxcutils.InitProcessCgroupFile, cgroup)
    96  	_, err := lxcutils.RunningInsideLXC()
    97  	c.Assert(err.Error(), gc.Equals, "malformed cgroup file")
    98  }