github.com/Pankov404/juju@v0.0.0-20150703034450-be266991dceb/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  	"runtime"
    11  
    12  	"github.com/juju/loggo"
    13  	jc "github.com/juju/testing/checkers"
    14  	gc "gopkg.in/check.v1"
    15  
    16  	"github.com/juju/juju/testing"
    17  	"github.com/juju/juju/upgrades"
    18  )
    19  
    20  type ensureLockDirSuite struct {
    21  	testing.FakeJujuHomeSuite
    22  	bin     string
    23  	home    string
    24  	datadir string
    25  	lockdir string
    26  	ctx     upgrades.Context
    27  }
    28  
    29  var _ = gc.Suite(&ensureLockDirSuite{})
    30  
    31  // fakecommand outputs its arguments to stdout for verification
    32  var fakecommand = `#!/bin/bash
    33  
    34  echo $@ | tee $0.args
    35  `
    36  
    37  func (s *ensureLockDirSuite) SetUpTest(c *gc.C) {
    38  	//TODO(bogdanteleaga): Fix this on windows
    39  	if runtime.GOOS == "windows" {
    40  		c.Skip("bug 1403084: tests use bash scripts, will be fixed later on windows")
    41  	}
    42  	s.FakeJujuHomeSuite.SetUpTest(c)
    43  
    44  	s.bin = c.MkDir()
    45  	s.PatchEnvPathPrepend(s.bin)
    46  
    47  	err := ioutil.WriteFile(
    48  		filepath.Join(s.bin, "chown"),
    49  		[]byte(fakecommand), 0777)
    50  	c.Assert(err, jc.ErrorIsNil)
    51  
    52  	loggo.GetLogger("juju.upgrade").SetLogLevel(loggo.TRACE)
    53  
    54  	s.home = c.MkDir()
    55  	s.PatchValue(upgrades.UbuntuHome, s.home)
    56  
    57  	s.datadir = c.MkDir()
    58  	s.lockdir = filepath.Join(s.datadir, "locks")
    59  	s.ctx = &mockContext{agentConfig: &mockAgentConfig{dataDir: s.datadir}}
    60  }
    61  
    62  func (s *ensureLockDirSuite) assertChownCalled(c *gc.C) {
    63  	bytes, err := ioutil.ReadFile(filepath.Join(s.bin, "chown.args"))
    64  	c.Assert(err, jc.ErrorIsNil)
    65  	c.Assert(string(bytes), gc.Equals, fmt.Sprintf("ubuntu:ubuntu %s\n", s.lockdir))
    66  }
    67  
    68  func (s *ensureLockDirSuite) assertNoChownCalled(c *gc.C) {
    69  	c.Assert(filepath.Join(s.bin, "chown.args"), jc.DoesNotExist)
    70  }
    71  
    72  func (s *ensureLockDirSuite) TestLockDirCreated(c *gc.C) {
    73  	err := upgrades.EnsureLockDirExistsAndUbuntuWritable(s.ctx)
    74  	c.Assert(err, jc.ErrorIsNil)
    75  
    76  	c.Assert(s.lockdir, jc.IsDirectory)
    77  	s.assertChownCalled(c)
    78  }
    79  
    80  func (s *ensureLockDirSuite) TestIdempotent(c *gc.C) {
    81  	err := upgrades.EnsureLockDirExistsAndUbuntuWritable(s.ctx)
    82  	c.Assert(err, jc.ErrorIsNil)
    83  
    84  	err = upgrades.EnsureLockDirExistsAndUbuntuWritable(s.ctx)
    85  	c.Assert(err, jc.ErrorIsNil)
    86  
    87  	c.Assert(s.lockdir, jc.IsDirectory)
    88  	s.assertChownCalled(c)
    89  }
    90  
    91  func (s *ensureLockDirSuite) TestNoChownIfNoHome(c *gc.C) {
    92  	s.PatchValue(upgrades.UbuntuHome, filepath.Join(s.home, "not-exist"))
    93  	err := upgrades.EnsureLockDirExistsAndUbuntuWritable(s.ctx)
    94  	c.Assert(err, jc.ErrorIsNil)
    95  
    96  	c.Assert(s.lockdir, jc.IsDirectory)
    97  	s.assertNoChownCalled(c)
    98  }