github.com/altoros/juju-vmware@v0.0.0-20150312064031-f19ae857ccca/worker/uniter/charm/git_deployer_test.go (about)

     1  // Copyright 2012-2014 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package charm_test
     5  
     6  import (
     7  	"io/ioutil"
     8  	"path/filepath"
     9  
    10  	jc "github.com/juju/testing/checkers"
    11  	"github.com/juju/utils/symlink"
    12  	gc "gopkg.in/check.v1"
    13  	corecharm "gopkg.in/juju/charm.v4"
    14  
    15  	"github.com/juju/juju/testing"
    16  	"github.com/juju/juju/worker/uniter/charm"
    17  )
    18  
    19  type GitDeployerSuite struct {
    20  	testing.GitSuite
    21  	bundles    *bundleReader
    22  	targetPath string
    23  	deployer   charm.Deployer
    24  }
    25  
    26  var _ = gc.Suite(&GitDeployerSuite{})
    27  
    28  func (s *GitDeployerSuite) SetUpTest(c *gc.C) {
    29  	s.GitSuite.SetUpTest(c)
    30  	s.bundles = &bundleReader{}
    31  	s.targetPath = filepath.Join(c.MkDir(), "target")
    32  	deployerPath := filepath.Join(c.MkDir(), "deployer")
    33  	s.deployer = charm.NewGitDeployer(s.targetPath, deployerPath, s.bundles)
    34  }
    35  
    36  func (s *GitDeployerSuite) TestUnsetCharm(c *gc.C) {
    37  	err := s.deployer.Deploy()
    38  	c.Assert(err, gc.ErrorMatches, "charm deployment failed: no charm set")
    39  }
    40  
    41  func (s *GitDeployerSuite) TestInstall(c *gc.C) {
    42  	// Prepare.
    43  	info := s.bundles.AddCustomBundle(c, corecharm.MustParseURL("cs:s/c-1"), func(path string) {
    44  		err := ioutil.WriteFile(filepath.Join(path, "some-file"), []byte("hello"), 0644)
    45  		c.Assert(err, jc.ErrorIsNil)
    46  	})
    47  	err := s.deployer.Stage(info, nil)
    48  	c.Assert(err, jc.ErrorIsNil)
    49  	checkCleanup(c, s.deployer)
    50  
    51  	// Install.
    52  	err = s.deployer.Deploy()
    53  	c.Assert(err, jc.ErrorIsNil)
    54  	checkCleanup(c, s.deployer)
    55  
    56  	// Check content.
    57  	data, err := ioutil.ReadFile(filepath.Join(s.targetPath, "some-file"))
    58  	c.Assert(err, jc.ErrorIsNil)
    59  	c.Assert(string(data), gc.Equals, "hello")
    60  
    61  	target := charm.NewGitDir(s.targetPath)
    62  	url, err := target.ReadCharmURL()
    63  	c.Assert(err, jc.ErrorIsNil)
    64  	c.Assert(url, gc.DeepEquals, corecharm.MustParseURL("cs:s/c-1"))
    65  	lines, err := target.Log()
    66  	c.Assert(err, jc.ErrorIsNil)
    67  	c.Assert(lines, gc.HasLen, 2)
    68  	c.Assert(lines[0], gc.Matches, `[0-9a-f]{7} Deployed charm "cs:s/c-1"\.`)
    69  	c.Assert(lines[1], gc.Matches, `[0-9a-f]{7} Imported charm "cs:s/c-1"\.`)
    70  }
    71  
    72  func (s *GitDeployerSuite) TestUpgrade(c *gc.C) {
    73  	// Install.
    74  	info1 := s.bundles.AddCustomBundle(c, corecharm.MustParseURL("cs:s/c-1"), func(path string) {
    75  		err := ioutil.WriteFile(filepath.Join(path, "some-file"), []byte("hello"), 0644)
    76  		c.Assert(err, jc.ErrorIsNil)
    77  		err = symlink.New("./some-file", filepath.Join(path, "a-symlink"))
    78  		c.Assert(err, jc.ErrorIsNil)
    79  	})
    80  	err := s.deployer.Stage(info1, nil)
    81  	c.Assert(err, jc.ErrorIsNil)
    82  	err = s.deployer.Deploy()
    83  	c.Assert(err, jc.ErrorIsNil)
    84  
    85  	// Upgrade.
    86  	info2 := s.bundles.AddCustomBundle(c, corecharm.MustParseURL("cs:s/c-2"), func(path string) {
    87  		err := ioutil.WriteFile(filepath.Join(path, "some-file"), []byte("goodbye"), 0644)
    88  		c.Assert(err, jc.ErrorIsNil)
    89  		err = ioutil.WriteFile(filepath.Join(path, "a-symlink"), []byte("not any more!"), 0644)
    90  		c.Assert(err, jc.ErrorIsNil)
    91  	})
    92  	err = s.deployer.Stage(info2, nil)
    93  	c.Assert(err, jc.ErrorIsNil)
    94  	checkCleanup(c, s.deployer)
    95  	err = s.deployer.Deploy()
    96  	c.Assert(err, jc.ErrorIsNil)
    97  	checkCleanup(c, s.deployer)
    98  
    99  	// Check content.
   100  	data, err := ioutil.ReadFile(filepath.Join(s.targetPath, "some-file"))
   101  	c.Assert(err, jc.ErrorIsNil)
   102  	c.Assert(string(data), gc.Equals, "goodbye")
   103  	data, err = ioutil.ReadFile(filepath.Join(s.targetPath, "a-symlink"))
   104  	c.Assert(err, jc.ErrorIsNil)
   105  	c.Assert(string(data), gc.Equals, "not any more!")
   106  
   107  	target := charm.NewGitDir(s.targetPath)
   108  	url, err := target.ReadCharmURL()
   109  	c.Assert(err, jc.ErrorIsNil)
   110  	c.Assert(url, gc.DeepEquals, corecharm.MustParseURL("cs:s/c-2"))
   111  	lines, err := target.Log()
   112  	c.Assert(err, jc.ErrorIsNil)
   113  	c.Assert(lines, gc.HasLen, 5)
   114  	c.Assert(lines[0], gc.Matches, `[0-9a-f]{7} Upgraded charm to "cs:s/c-2".`)
   115  }
   116  
   117  func (s *GitDeployerSuite) TestConflictRevertResolve(c *gc.C) {
   118  	// Install.
   119  	info1 := s.bundles.AddCustomBundle(c, corecharm.MustParseURL("cs:s/c-1"), func(path string) {
   120  		err := ioutil.WriteFile(filepath.Join(path, "some-file"), []byte("hello"), 0644)
   121  		c.Assert(err, jc.ErrorIsNil)
   122  	})
   123  	err := s.deployer.Stage(info1, nil)
   124  	c.Assert(err, jc.ErrorIsNil)
   125  	err = s.deployer.Deploy()
   126  	c.Assert(err, jc.ErrorIsNil)
   127  
   128  	// Mess up target.
   129  	err = ioutil.WriteFile(filepath.Join(s.targetPath, "some-file"), []byte("mu!"), 0644)
   130  	c.Assert(err, jc.ErrorIsNil)
   131  
   132  	// Upgrade.
   133  	info2 := s.bundles.AddCustomBundle(c, corecharm.MustParseURL("cs:s/c-2"), func(path string) {
   134  		err := ioutil.WriteFile(filepath.Join(path, "some-file"), []byte("goodbye"), 0644)
   135  		c.Assert(err, jc.ErrorIsNil)
   136  	})
   137  	err = s.deployer.Stage(info2, nil)
   138  	c.Assert(err, jc.ErrorIsNil)
   139  	err = s.deployer.Deploy()
   140  	c.Assert(err, gc.Equals, charm.ErrConflict)
   141  	checkCleanup(c, s.deployer)
   142  
   143  	// Check state.
   144  	target := charm.NewGitDir(s.targetPath)
   145  	conflicted, err := target.Conflicted()
   146  	c.Assert(err, jc.ErrorIsNil)
   147  	c.Assert(conflicted, jc.IsTrue)
   148  
   149  	// Revert and check initial content.
   150  	err = s.deployer.NotifyRevert()
   151  	c.Assert(err, jc.ErrorIsNil)
   152  	data, err := ioutil.ReadFile(filepath.Join(s.targetPath, "some-file"))
   153  	c.Assert(err, jc.ErrorIsNil)
   154  	c.Assert(string(data), gc.Equals, "mu!")
   155  	conflicted, err = target.Conflicted()
   156  	c.Assert(err, jc.ErrorIsNil)
   157  	c.Assert(conflicted, jc.IsFalse)
   158  
   159  	// Try to upgrade again.
   160  	err = s.deployer.Deploy()
   161  	c.Assert(err, gc.Equals, charm.ErrConflict)
   162  	conflicted, err = target.Conflicted()
   163  	c.Assert(err, jc.ErrorIsNil)
   164  	c.Assert(conflicted, jc.IsTrue)
   165  	checkCleanup(c, s.deployer)
   166  
   167  	// And again.
   168  	err = s.deployer.Deploy()
   169  	c.Assert(err, gc.Equals, charm.ErrConflict)
   170  	conflicted, err = target.Conflicted()
   171  	c.Assert(err, jc.ErrorIsNil)
   172  	c.Assert(conflicted, jc.IsTrue)
   173  	checkCleanup(c, s.deployer)
   174  
   175  	// Manually resolve, and commit.
   176  	err = ioutil.WriteFile(filepath.Join(target.Path(), "some-file"), []byte("nu!"), 0644)
   177  	c.Assert(err, jc.ErrorIsNil)
   178  	err = s.deployer.NotifyResolved()
   179  	c.Assert(err, jc.ErrorIsNil)
   180  	conflicted, err = target.Conflicted()
   181  	c.Assert(err, jc.ErrorIsNil)
   182  	c.Assert(conflicted, jc.IsFalse)
   183  
   184  	// Try a final upgrade to the same charm and check it doesn't write anything
   185  	// except the upgrade log line.
   186  	err = s.deployer.Deploy()
   187  	c.Assert(err, jc.ErrorIsNil)
   188  	checkCleanup(c, s.deployer)
   189  
   190  	data, err = ioutil.ReadFile(filepath.Join(target.Path(), "some-file"))
   191  	c.Assert(err, jc.ErrorIsNil)
   192  	c.Assert(string(data), gc.Equals, "nu!")
   193  	conflicted, err = target.Conflicted()
   194  	c.Assert(err, jc.ErrorIsNil)
   195  	c.Assert(conflicted, jc.IsFalse)
   196  	lines, err := target.Log()
   197  	c.Assert(err, jc.ErrorIsNil)
   198  	c.Assert(lines[0], gc.Matches, `[0-9a-f]{7} Upgraded charm to "cs:s/c-2".`)
   199  }
   200  
   201  func checkCleanup(c *gc.C, d charm.Deployer) {
   202  	// Only one update dir should exist and be pointed to by the 'current'
   203  	// symlink since extra ones should have been cleaned up by
   204  	// cleanupOrphans.
   205  	deployerPath := charm.GitDeployerDataPath(d)
   206  	updateDirs, err := filepath.Glob(filepath.Join(deployerPath, "update-*"))
   207  	c.Assert(err, jc.ErrorIsNil)
   208  	c.Assert(updateDirs, gc.HasLen, 1)
   209  	deployerCurrent := charm.GitDeployerCurrent(d)
   210  	current, err := symlink.Read(deployerCurrent.Path())
   211  	c.Assert(err, jc.ErrorIsNil)
   212  	c.Assert(updateDirs[0], gc.Equals, current)
   213  
   214  	// No install dirs should be left behind since the one created is
   215  	// renamed to the target path.
   216  	installDirs, err := filepath.Glob(filepath.Join(deployerPath, "install-*"))
   217  	c.Assert(err, jc.ErrorIsNil)
   218  	c.Assert(installDirs, gc.HasLen, 0)
   219  }