github.com/altoros/juju-vmware@v0.0.0-20150312064031-f19ae857ccca/upgrades/dotprofile_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  	"io/ioutil"
     8  	"path"
     9  
    10  	"github.com/juju/loggo"
    11  	jc "github.com/juju/testing/checkers"
    12  	gc "gopkg.in/check.v1"
    13  
    14  	"github.com/juju/juju/testing"
    15  	"github.com/juju/juju/upgrades"
    16  )
    17  
    18  type ensureDotProfileSuite struct {
    19  	testing.FakeJujuHomeSuite
    20  	home string
    21  	ctx  upgrades.Context
    22  }
    23  
    24  var _ = gc.Suite(&ensureDotProfileSuite{})
    25  
    26  func (s *ensureDotProfileSuite) SetUpTest(c *gc.C) {
    27  	s.FakeJujuHomeSuite.SetUpTest(c)
    28  
    29  	loggo.GetLogger("juju.upgrade").SetLogLevel(loggo.TRACE)
    30  
    31  	s.home = c.MkDir()
    32  	s.PatchValue(upgrades.UbuntuHome, s.home)
    33  	s.ctx = &mockContext{}
    34  }
    35  
    36  const expectedLine = `
    37  # Added by juju
    38  [ -f "$HOME/.juju-proxy" ] && . "$HOME/.juju-proxy"
    39  `
    40  
    41  func (s *ensureDotProfileSuite) writeDotProfile(c *gc.C, content string) {
    42  	dotProfile := path.Join(s.home, ".profile")
    43  	err := ioutil.WriteFile(dotProfile, []byte(content), 0644)
    44  	c.Assert(err, jc.ErrorIsNil)
    45  }
    46  
    47  func (s *ensureDotProfileSuite) assertProfile(c *gc.C, content string) {
    48  	dotProfile := path.Join(s.home, ".profile")
    49  	data, err := ioutil.ReadFile(dotProfile)
    50  	c.Assert(err, jc.ErrorIsNil)
    51  	c.Assert(string(data), gc.Equals, content)
    52  }
    53  
    54  func (s *ensureDotProfileSuite) TestSourceAdded(c *gc.C) {
    55  	s.writeDotProfile(c, "")
    56  	err := upgrades.EnsureUbuntuDotProfileSourcesProxyFile(s.ctx)
    57  	c.Assert(err, jc.ErrorIsNil)
    58  	s.assertProfile(c, expectedLine)
    59  }
    60  
    61  func (s *ensureDotProfileSuite) TestIdempotent(c *gc.C) {
    62  	s.writeDotProfile(c, "")
    63  	err := upgrades.EnsureUbuntuDotProfileSourcesProxyFile(s.ctx)
    64  	c.Assert(err, jc.ErrorIsNil)
    65  	err = upgrades.EnsureUbuntuDotProfileSourcesProxyFile(s.ctx)
    66  	c.Assert(err, jc.ErrorIsNil)
    67  	s.assertProfile(c, expectedLine)
    68  }
    69  
    70  func (s *ensureDotProfileSuite) TestProfileUntouchedIfJujuProxyInSource(c *gc.C) {
    71  	content := "source .juju-proxy\n"
    72  	s.writeDotProfile(c, content)
    73  	err := upgrades.EnsureUbuntuDotProfileSourcesProxyFile(s.ctx)
    74  	c.Assert(err, jc.ErrorIsNil)
    75  	s.assertProfile(c, content)
    76  }
    77  
    78  func (s *ensureDotProfileSuite) TestSkippedIfDotProfileDoesntExist(c *gc.C) {
    79  	err := upgrades.EnsureUbuntuDotProfileSourcesProxyFile(s.ctx)
    80  	c.Assert(err, jc.ErrorIsNil)
    81  	c.Assert(path.Join(s.home, ".profile"), jc.DoesNotExist)
    82  }