github.com/altoros/juju-vmware@v0.0.0-20150312064031-f19ae857ccca/worker/uniter/charm/git_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  	"os"
     9  	"os/exec"
    10  	"path/filepath"
    11  
    12  	jc "github.com/juju/testing/checkers"
    13  	gc "gopkg.in/check.v1"
    14  	corecharm "gopkg.in/juju/charm.v4"
    15  
    16  	"github.com/juju/juju/testing"
    17  	"github.com/juju/juju/worker/uniter/charm"
    18  )
    19  
    20  var curl = corecharm.MustParseURL("cs:series/blah-blah-123")
    21  
    22  type GitDirSuite struct {
    23  	testing.GitSuite
    24  }
    25  
    26  var _ = gc.Suite(&GitDirSuite{})
    27  
    28  func (s *GitDirSuite) TestInitConfig(c *gc.C) {
    29  	base := c.MkDir()
    30  	repo := charm.NewGitDir(filepath.Join(base, "repo"))
    31  	err := repo.Init()
    32  	c.Assert(err, jc.ErrorIsNil)
    33  
    34  	cmd := exec.Command("git", "config", "--list", "--local")
    35  	cmd.Dir = repo.Path()
    36  	out, err := cmd.Output()
    37  	c.Assert(err, jc.ErrorIsNil)
    38  	outstr := string(out)
    39  	c.Assert(outstr, jc.Contains, "user.email=juju@localhost")
    40  	c.Assert(outstr, jc.Contains, "user.name=juju")
    41  }
    42  
    43  func (s *GitDirSuite) TestCreate(c *gc.C) {
    44  	base := c.MkDir()
    45  	repo := charm.NewGitDir(filepath.Join(base, "repo"))
    46  	exists, err := repo.Exists()
    47  	c.Assert(err, jc.ErrorIsNil)
    48  	c.Assert(exists, jc.IsFalse)
    49  
    50  	err = ioutil.WriteFile(repo.Path(), nil, 0644)
    51  	c.Assert(err, jc.ErrorIsNil)
    52  	_, err = repo.Exists()
    53  	c.Assert(err, gc.ErrorMatches, `".*/repo" is not a directory`)
    54  	err = os.Remove(repo.Path())
    55  	c.Assert(err, jc.ErrorIsNil)
    56  
    57  	err = os.Chmod(base, 0555)
    58  	c.Assert(err, jc.ErrorIsNil)
    59  	defer os.Chmod(base, 0755)
    60  	err = repo.Init()
    61  	c.Assert(err, gc.ErrorMatches, ".* permission denied")
    62  	exists, err = repo.Exists()
    63  	c.Assert(err, jc.ErrorIsNil)
    64  	c.Assert(exists, jc.IsFalse)
    65  
    66  	err = os.Chmod(base, 0755)
    67  	c.Assert(err, jc.ErrorIsNil)
    68  	err = repo.Init()
    69  	c.Assert(err, jc.ErrorIsNil)
    70  	exists, err = repo.Exists()
    71  	c.Assert(err, jc.ErrorIsNil)
    72  	c.Assert(exists, jc.IsTrue)
    73  
    74  	_, err = repo.ReadCharmURL()
    75  	c.Assert(err, jc.Satisfies, os.IsNotExist)
    76  
    77  	err = repo.Init()
    78  	c.Assert(err, jc.ErrorIsNil)
    79  }
    80  
    81  func (s *GitDirSuite) TestAddCommitPullRevert(c *gc.C) {
    82  	target := charm.NewGitDir(c.MkDir())
    83  	err := target.Init()
    84  	c.Assert(err, jc.ErrorIsNil)
    85  	err = ioutil.WriteFile(filepath.Join(target.Path(), "initial"), []byte("initial"), 0644)
    86  	c.Assert(err, jc.ErrorIsNil)
    87  	err = target.WriteCharmURL(curl)
    88  	c.Assert(err, jc.ErrorIsNil)
    89  	err = target.AddAll()
    90  	c.Assert(err, jc.ErrorIsNil)
    91  	dirty, err := target.Dirty()
    92  	c.Assert(err, jc.ErrorIsNil)
    93  	c.Assert(dirty, jc.IsTrue)
    94  	err = target.Commitf("initial")
    95  	c.Assert(err, jc.ErrorIsNil)
    96  	dirty, err = target.Dirty()
    97  	c.Assert(err, jc.ErrorIsNil)
    98  	c.Assert(dirty, jc.IsFalse)
    99  
   100  	source := newRepo(c)
   101  	err = target.Pull(source)
   102  	c.Assert(err, jc.ErrorIsNil)
   103  	url, err := target.ReadCharmURL()
   104  	c.Assert(err, jc.ErrorIsNil)
   105  	c.Assert(url, gc.DeepEquals, curl)
   106  	fi, err := os.Stat(filepath.Join(target.Path(), "some-dir"))
   107  	c.Assert(err, jc.ErrorIsNil)
   108  	c.Assert(fi, jc.Satisfies, os.FileInfo.IsDir)
   109  	data, err := ioutil.ReadFile(filepath.Join(target.Path(), "some-file"))
   110  	c.Assert(err, jc.ErrorIsNil)
   111  	c.Assert(string(data), gc.Equals, "hello")
   112  	dirty, err = target.Dirty()
   113  	c.Assert(err, jc.ErrorIsNil)
   114  	c.Assert(dirty, jc.IsFalse)
   115  
   116  	err = ioutil.WriteFile(filepath.Join(target.Path(), "another-file"), []byte("blah"), 0644)
   117  	c.Assert(err, jc.ErrorIsNil)
   118  	dirty, err = target.Dirty()
   119  	c.Assert(err, jc.ErrorIsNil)
   120  	c.Assert(dirty, jc.IsTrue)
   121  	err = source.AddAll()
   122  	c.Assert(err, jc.ErrorIsNil)
   123  	dirty, err = target.Dirty()
   124  	c.Assert(err, jc.ErrorIsNil)
   125  	c.Assert(dirty, jc.IsTrue)
   126  
   127  	err = target.Revert()
   128  	c.Assert(err, jc.ErrorIsNil)
   129  	_, err = os.Stat(filepath.Join(target.Path(), "some-file"))
   130  	c.Assert(err, jc.Satisfies, os.IsNotExist)
   131  	_, err = os.Stat(filepath.Join(target.Path(), "some-dir"))
   132  	c.Assert(err, jc.Satisfies, os.IsNotExist)
   133  	data, err = ioutil.ReadFile(filepath.Join(target.Path(), "initial"))
   134  	c.Assert(err, jc.ErrorIsNil)
   135  	c.Assert(string(data), gc.Equals, "initial")
   136  	dirty, err = target.Dirty()
   137  	c.Assert(err, jc.ErrorIsNil)
   138  	c.Assert(dirty, jc.IsFalse)
   139  }
   140  
   141  func (s *GitDirSuite) TestClone(c *gc.C) {
   142  	repo, err := newRepo(c).Clone(c.MkDir())
   143  	c.Assert(err, jc.ErrorIsNil)
   144  	_, err = os.Stat(filepath.Join(repo.Path(), "some-file"))
   145  	c.Assert(err, jc.Satisfies, os.IsNotExist)
   146  	_, err = os.Stat(filepath.Join(repo.Path(), "some-dir"))
   147  	c.Assert(err, jc.Satisfies, os.IsNotExist)
   148  	dirty, err := repo.Dirty()
   149  	c.Assert(err, jc.ErrorIsNil)
   150  	c.Assert(dirty, jc.IsTrue)
   151  
   152  	err = repo.AddAll()
   153  	c.Assert(err, jc.ErrorIsNil)
   154  	dirty, err = repo.Dirty()
   155  	c.Assert(err, jc.ErrorIsNil)
   156  	c.Assert(dirty, jc.IsTrue)
   157  	err = repo.Commitf("blank overwrite")
   158  	c.Assert(err, jc.ErrorIsNil)
   159  	dirty, err = repo.Dirty()
   160  	c.Assert(err, jc.ErrorIsNil)
   161  	c.Assert(dirty, jc.IsFalse)
   162  
   163  	lines, err := repo.Log()
   164  	c.Assert(err, jc.ErrorIsNil)
   165  	c.Assert(lines, gc.HasLen, 2)
   166  	c.Assert(lines[0], gc.Matches, "[a-f0-9]{7} blank overwrite")
   167  	c.Assert(lines[1], gc.Matches, "[a-f0-9]{7} im in ur repo committin ur files")
   168  }
   169  
   170  func (s *GitDirSuite) TestConflictRevert(c *gc.C) {
   171  	source := newRepo(c)
   172  	updated, err := source.Clone(c.MkDir())
   173  	c.Assert(err, jc.ErrorIsNil)
   174  	err = ioutil.WriteFile(filepath.Join(updated.Path(), "some-dir"), []byte("hello"), 0644)
   175  	c.Assert(err, jc.ErrorIsNil)
   176  	err = updated.Snapshotf("potential conflict src")
   177  	c.Assert(err, jc.ErrorIsNil)
   178  	conflicted, err := updated.Conflicted()
   179  	c.Assert(err, jc.ErrorIsNil)
   180  	c.Assert(conflicted, jc.IsFalse)
   181  
   182  	target := charm.NewGitDir(c.MkDir())
   183  	err = target.Init()
   184  	c.Assert(err, jc.ErrorIsNil)
   185  	err = target.Pull(source)
   186  	c.Assert(err, jc.ErrorIsNil)
   187  	err = ioutil.WriteFile(filepath.Join(target.Path(), "some-dir", "conflicting-file"), []byte("hello"), 0644)
   188  	c.Assert(err, jc.ErrorIsNil)
   189  	err = target.Snapshotf("potential conflict dst")
   190  	c.Assert(err, jc.ErrorIsNil)
   191  	conflicted, err = target.Conflicted()
   192  	c.Assert(err, jc.ErrorIsNil)
   193  	c.Assert(conflicted, jc.IsFalse)
   194  
   195  	err = target.Pull(updated)
   196  	c.Assert(err, gc.Equals, charm.ErrConflict)
   197  	conflicted, err = target.Conflicted()
   198  	c.Assert(err, jc.ErrorIsNil)
   199  	c.Assert(conflicted, jc.IsTrue)
   200  	dirty, err := target.Dirty()
   201  	c.Assert(err, jc.ErrorIsNil)
   202  	c.Assert(dirty, jc.IsTrue)
   203  
   204  	err = target.Revert()
   205  	c.Assert(err, jc.ErrorIsNil)
   206  	conflicted, err = target.Conflicted()
   207  	c.Assert(err, jc.ErrorIsNil)
   208  	c.Assert(conflicted, jc.IsFalse)
   209  	dirty, err = target.Dirty()
   210  	c.Assert(err, jc.ErrorIsNil)
   211  	c.Assert(dirty, jc.IsFalse)
   212  }
   213  
   214  func newRepo(c *gc.C) *charm.GitDir {
   215  	repo := charm.NewGitDir(c.MkDir())
   216  	err := repo.Init()
   217  	c.Assert(err, jc.ErrorIsNil)
   218  	err = os.Mkdir(filepath.Join(repo.Path(), "some-dir"), 0755)
   219  	c.Assert(err, jc.ErrorIsNil)
   220  	err = ioutil.WriteFile(filepath.Join(repo.Path(), "some-file"), []byte("hello"), 0644)
   221  	c.Assert(err, jc.ErrorIsNil)
   222  	err = repo.AddAll()
   223  	c.Assert(err, jc.ErrorIsNil)
   224  	err = repo.Commitf("im in ur repo committin ur %s", "files")
   225  	c.Assert(err, jc.ErrorIsNil)
   226  	return repo
   227  }