github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/worker/uniter/charm/converter_test.go (about)

     1  // Copyright 2014 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package charm_test
     5  
     6  import (
     7  	jc "github.com/juju/testing/checkers"
     8  	ft "github.com/juju/testing/filetesting"
     9  	gc "gopkg.in/check.v1"
    10  
    11  	"github.com/juju/juju/testing"
    12  	"github.com/juju/juju/worker/uniter/charm"
    13  )
    14  
    15  type ConverterSuite struct {
    16  	testing.GitSuite
    17  	targetPath string
    18  	dataPath   string
    19  	bundles    *bundleReader
    20  }
    21  
    22  var _ = gc.Suite(&ConverterSuite{})
    23  
    24  func (s *ConverterSuite) SetUpTest(c *gc.C) {
    25  	testing.SkipIfGitNotAvailable(c)
    26  	s.GitSuite.SetUpTest(c)
    27  	s.targetPath = c.MkDir()
    28  	s.dataPath = c.MkDir()
    29  	s.bundles = &bundleReader{}
    30  }
    31  
    32  func (s *ConverterSuite) TestNewDeployerCreatesManifestDeployer(c *gc.C) {
    33  	deployer, err := charm.NewDeployer(s.targetPath, s.dataPath, s.bundles)
    34  	c.Assert(err, jc.ErrorIsNil)
    35  	c.Assert(deployer, jc.Satisfies, charm.IsManifestDeployer)
    36  }
    37  
    38  func (s *ConverterSuite) TestNewDeployerCreatesGitDeployerOnceStaged(c *gc.C) {
    39  	gitDeployer := charm.NewGitDeployer(s.targetPath, s.dataPath, s.bundles)
    40  	info := s.bundles.AddBundle(c, charmURL(1), mockBundle{})
    41  	err := gitDeployer.Stage(info, nil)
    42  	c.Assert(err, jc.ErrorIsNil)
    43  
    44  	deployer, err := charm.NewDeployer(s.targetPath, s.dataPath, s.bundles)
    45  	c.Assert(err, jc.ErrorIsNil)
    46  	c.Assert(deployer, jc.Satisfies, charm.IsGitDeployer)
    47  }
    48  
    49  func (s *ConverterSuite) TestConvertGitDeployerBeforeDeploy(c *gc.C) {
    50  	gitDeployer := charm.NewGitDeployer(s.targetPath, s.dataPath, s.bundles)
    51  	info := s.bundles.AddBundle(c, charmURL(1), mockBundle{})
    52  	err := gitDeployer.Stage(info, nil)
    53  	c.Assert(err, jc.ErrorIsNil)
    54  
    55  	deployer, err := charm.NewDeployer(s.targetPath, s.dataPath, s.bundles)
    56  	c.Assert(err, jc.ErrorIsNil)
    57  	err = charm.FixDeployer(&deployer)
    58  	c.Assert(err, jc.ErrorIsNil)
    59  	c.Assert(deployer, jc.Satisfies, charm.IsManifestDeployer)
    60  	ft.Removed{"current"}.Check(c, s.dataPath)
    61  
    62  	err = deployer.Stage(info, nil)
    63  	c.Assert(err, jc.ErrorIsNil)
    64  	err = deployer.Deploy()
    65  	c.Assert(err, jc.ErrorIsNil)
    66  	ft.Removed{".git"}.Check(c, s.targetPath)
    67  }
    68  
    69  func (s *ConverterSuite) TestConvertGitDeployerAfterDeploy(c *gc.C) {
    70  	gitDeployer := charm.NewGitDeployer(s.targetPath, s.dataPath, s.bundles)
    71  	info1 := s.bundles.AddBundle(c, charmURL(1), mockBundle{})
    72  	err := gitDeployer.Stage(info1, nil)
    73  	c.Assert(err, jc.ErrorIsNil)
    74  	err = gitDeployer.Deploy()
    75  	c.Assert(err, jc.ErrorIsNil)
    76  
    77  	deployer, err := charm.NewDeployer(s.targetPath, s.dataPath, s.bundles)
    78  	c.Assert(err, jc.ErrorIsNil)
    79  	err = charm.FixDeployer(&deployer)
    80  	c.Assert(err, jc.ErrorIsNil)
    81  	c.Assert(deployer, jc.Satisfies, charm.IsManifestDeployer)
    82  	ft.Removed{"current"}.Check(c, s.dataPath)
    83  	ft.Dir{"manifests", 0755}.Check(c, s.dataPath)
    84  
    85  	info2 := s.bundles.AddBundle(c, charmURL(2), mockBundle{})
    86  	err = deployer.Stage(info2, nil)
    87  	c.Assert(err, jc.ErrorIsNil)
    88  	err = deployer.Deploy()
    89  	c.Assert(err, jc.ErrorIsNil)
    90  	ft.Removed{".git"}.Check(c, s.targetPath)
    91  }
    92  
    93  func (s *ConverterSuite) TestPathological(c *gc.C) {
    94  	initial := s.bundles.AddCustomBundle(c, charmURL(1), func(path string) {
    95  		ft.File{"common", "initial", 0644}.Create(c, path)
    96  		ft.File{"initial", "blah", 0644}.Create(c, path)
    97  	})
    98  	staged := s.bundles.AddCustomBundle(c, charmURL(2), func(path string) {
    99  		ft.File{"common", "staged", 0644}.Create(c, path)
   100  		ft.File{"user", "badwrong", 0644}.Create(c, path)
   101  	})
   102  	final := s.bundles.AddCustomBundle(c, charmURL(3), func(path string) {
   103  		ft.File{"common", "final", 0644}.Create(c, path)
   104  		ft.File{"final", "blah", 0644}.Create(c, path)
   105  	})
   106  
   107  	gitDeployer := charm.NewGitDeployer(s.targetPath, s.dataPath, s.bundles)
   108  	err := gitDeployer.Stage(initial, nil)
   109  	c.Assert(err, jc.ErrorIsNil)
   110  	err = gitDeployer.Deploy()
   111  	c.Assert(err, jc.ErrorIsNil)
   112  
   113  	preserveUser := ft.File{"user", "preserve", 0644}.Create(c, s.targetPath)
   114  	err = gitDeployer.Stage(staged, nil)
   115  	c.Assert(err, jc.ErrorIsNil)
   116  
   117  	deployer, err := charm.NewDeployer(s.targetPath, s.dataPath, s.bundles)
   118  	c.Assert(err, jc.ErrorIsNil)
   119  	err = charm.FixDeployer(&deployer)
   120  	c.Assert(err, jc.ErrorIsNil)
   121  
   122  	err = deployer.Stage(final, nil)
   123  	c.Assert(err, jc.ErrorIsNil)
   124  	err = deployer.Deploy()
   125  	c.Assert(err, jc.ErrorIsNil)
   126  	ft.Removed{".git"}.Check(c, s.targetPath)
   127  	ft.Removed{"initial"}.Check(c, s.targetPath)
   128  	ft.Removed{"staged"}.Check(c, s.targetPath)
   129  	ft.File{"common", "final", 0644}.Check(c, s.targetPath)
   130  	preserveUser.Check(c, s.targetPath)
   131  }