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