github.com/Pankov404/juju@v0.0.0-20150703034450-be266991dceb/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  	"runtime"
    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 ensureDotProfileSuite struct {
    20  	testing.FakeJujuHomeSuite
    21  	home string
    22  	ctx  upgrades.Context
    23  }
    24  
    25  var _ = gc.Suite(&ensureDotProfileSuite{})
    26  
    27  func (s *ensureDotProfileSuite) SetUpTest(c *gc.C) {
    28  	//TODO(bogdanteleaga): Fix these on windows
    29  	if runtime.GOOS == "windows" {
    30  		c.Skip("bug 1403084: tests use bash scripts, will be fixed later on windows")
    31  	}
    32  	s.FakeJujuHomeSuite.SetUpTest(c)
    33  
    34  	loggo.GetLogger("juju.upgrade").SetLogLevel(loggo.TRACE)
    35  
    36  	s.home = c.MkDir()
    37  	s.PatchValue(upgrades.UbuntuHome, s.home)
    38  	s.ctx = &mockContext{}
    39  }
    40  
    41  const expectedLine = `
    42  # Added by juju
    43  [ -f "$HOME/.juju-proxy" ] && . "$HOME/.juju-proxy"
    44  `
    45  
    46  func (s *ensureDotProfileSuite) writeDotProfile(c *gc.C, content string) {
    47  	dotProfile := path.Join(s.home, ".profile")
    48  	err := ioutil.WriteFile(dotProfile, []byte(content), 0644)
    49  	c.Assert(err, jc.ErrorIsNil)
    50  }
    51  
    52  func (s *ensureDotProfileSuite) assertProfile(c *gc.C, content string) {
    53  	dotProfile := path.Join(s.home, ".profile")
    54  	data, err := ioutil.ReadFile(dotProfile)
    55  	c.Assert(err, jc.ErrorIsNil)
    56  	c.Assert(string(data), gc.Equals, content)
    57  }
    58  
    59  func (s *ensureDotProfileSuite) TestSourceAdded(c *gc.C) {
    60  	s.writeDotProfile(c, "")
    61  	err := upgrades.EnsureUbuntuDotProfileSourcesProxyFile(s.ctx)
    62  	c.Assert(err, jc.ErrorIsNil)
    63  	s.assertProfile(c, expectedLine)
    64  }
    65  
    66  func (s *ensureDotProfileSuite) TestIdempotent(c *gc.C) {
    67  	s.writeDotProfile(c, "")
    68  	err := upgrades.EnsureUbuntuDotProfileSourcesProxyFile(s.ctx)
    69  	c.Assert(err, jc.ErrorIsNil)
    70  	err = upgrades.EnsureUbuntuDotProfileSourcesProxyFile(s.ctx)
    71  	c.Assert(err, jc.ErrorIsNil)
    72  	s.assertProfile(c, expectedLine)
    73  }
    74  
    75  func (s *ensureDotProfileSuite) TestProfileUntouchedIfJujuProxyInSource(c *gc.C) {
    76  	content := "source .juju-proxy\n"
    77  	s.writeDotProfile(c, content)
    78  	err := upgrades.EnsureUbuntuDotProfileSourcesProxyFile(s.ctx)
    79  	c.Assert(err, jc.ErrorIsNil)
    80  	s.assertProfile(c, content)
    81  }
    82  
    83  func (s *ensureDotProfileSuite) TestSkippedIfDotProfileDoesntExist(c *gc.C) {
    84  	err := upgrades.EnsureUbuntuDotProfileSourcesProxyFile(s.ctx)
    85  	c.Assert(err, jc.ErrorIsNil)
    86  	c.Assert(path.Join(s.home, ".profile"), jc.DoesNotExist)
    87  }