github.com/altoros/juju-vmware@v0.0.0-20150312064031-f19ae857ccca/upgrades/lockdirectory_test.go (about)

     1  // Copyright 2014 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package upgrades_test
     5  
     6  import (
     7  	"fmt"
     8  	"io/ioutil"
     9  	"path/filepath"
    10  
    11  	"github.com/juju/loggo"
    12  	jc "github.com/juju/testing/checkers"
    13  	gc "gopkg.in/check.v1"
    14  
    15  	"github.com/juju/juju/testing"
    16  	"github.com/juju/juju/upgrades"
    17  )
    18  
    19  type ensureLockDirSuite struct {
    20  	testing.FakeJujuHomeSuite
    21  	bin     string
    22  	home    string
    23  	datadir string
    24  	lockdir string
    25  	ctx     upgrades.Context
    26  }
    27  
    28  var _ = gc.Suite(&ensureLockDirSuite{})
    29  
    30  // fakecommand outputs its arguments to stdout for verification
    31  var fakecommand = `#!/bin/bash
    32  
    33  echo $@ | tee $0.args
    34  `
    35  
    36  func (s *ensureLockDirSuite) SetUpTest(c *gc.C) {
    37  	s.FakeJujuHomeSuite.SetUpTest(c)
    38  
    39  	s.bin = c.MkDir()
    40  	s.PatchEnvPathPrepend(s.bin)
    41  
    42  	err := ioutil.WriteFile(
    43  		filepath.Join(s.bin, "chown"),
    44  		[]byte(fakecommand), 0777)
    45  	c.Assert(err, jc.ErrorIsNil)
    46  
    47  	loggo.GetLogger("juju.upgrade").SetLogLevel(loggo.TRACE)
    48  
    49  	s.home = c.MkDir()
    50  	s.PatchValue(upgrades.UbuntuHome, s.home)
    51  
    52  	s.datadir = c.MkDir()
    53  	s.lockdir = filepath.Join(s.datadir, "locks")
    54  	s.ctx = &mockContext{agentConfig: &mockAgentConfig{dataDir: s.datadir}}
    55  }
    56  
    57  func (s *ensureLockDirSuite) assertChownCalled(c *gc.C) {
    58  	bytes, err := ioutil.ReadFile(filepath.Join(s.bin, "chown.args"))
    59  	c.Assert(err, jc.ErrorIsNil)
    60  	c.Assert(string(bytes), gc.Equals, fmt.Sprintf("ubuntu:ubuntu %s\n", s.lockdir))
    61  }
    62  
    63  func (s *ensureLockDirSuite) assertNoChownCalled(c *gc.C) {
    64  	c.Assert(filepath.Join(s.bin, "chown.args"), jc.DoesNotExist)
    65  }
    66  
    67  func (s *ensureLockDirSuite) TestLockDirCreated(c *gc.C) {
    68  	err := upgrades.EnsureLockDirExistsAndUbuntuWritable(s.ctx)
    69  	c.Assert(err, jc.ErrorIsNil)
    70  
    71  	c.Assert(s.lockdir, jc.IsDirectory)
    72  	s.assertChownCalled(c)
    73  }
    74  
    75  func (s *ensureLockDirSuite) TestIdempotent(c *gc.C) {
    76  	err := upgrades.EnsureLockDirExistsAndUbuntuWritable(s.ctx)
    77  	c.Assert(err, jc.ErrorIsNil)
    78  
    79  	err = upgrades.EnsureLockDirExistsAndUbuntuWritable(s.ctx)
    80  	c.Assert(err, jc.ErrorIsNil)
    81  
    82  	c.Assert(s.lockdir, jc.IsDirectory)
    83  	s.assertChownCalled(c)
    84  }
    85  
    86  func (s *ensureLockDirSuite) TestNoChownIfNoHome(c *gc.C) {
    87  	s.PatchValue(upgrades.UbuntuHome, filepath.Join(s.home, "not-exist"))
    88  	err := upgrades.EnsureLockDirExistsAndUbuntuWritable(s.ctx)
    89  	c.Assert(err, jc.ErrorIsNil)
    90  
    91  	c.Assert(s.lockdir, jc.IsDirectory)
    92  	s.assertNoChownCalled(c)
    93  }