github.com/juju/charmrepo/v7@v7.0.1/bundlepath_test.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the LGPLv3, see LICENCE file for details.
     3  
     4  package charmrepo_test // import "github.com/juju/charmrepo/v7"
     5  
     6  import (
     7  	"io/ioutil"
     8  	"os"
     9  	"path/filepath"
    10  
    11  	"github.com/juju/charm/v9"
    12  	jc "github.com/juju/testing/checkers"
    13  	gc "gopkg.in/check.v1"
    14  	"gopkg.in/yaml.v2"
    15  
    16  	"github.com/juju/charmrepo/v7"
    17  )
    18  
    19  type bundlePathSuite struct {
    20  	repoPath string
    21  }
    22  
    23  var _ = gc.Suite(&bundlePathSuite{})
    24  
    25  func (s *bundlePathSuite) SetUpTest(c *gc.C) {
    26  	s.repoPath = c.MkDir()
    27  }
    28  
    29  func (s *bundlePathSuite) cloneCharmDir(path, name string) string {
    30  	return TestCharms.ClonedDirPath(path, name)
    31  }
    32  
    33  func (s *bundlePathSuite) TestNoPath(c *gc.C) {
    34  	_, _, err := charmrepo.NewBundleAtPath("")
    35  	c.Assert(err, gc.ErrorMatches, "path to bundle not specified")
    36  }
    37  
    38  func (s *bundlePathSuite) TestInvalidPath(c *gc.C) {
    39  	_, _, err := charmrepo.NewBundleAtPath("/foo")
    40  	c.Assert(err, gc.Equals, os.ErrNotExist)
    41  }
    42  
    43  func (s *bundlePathSuite) TestRepoURL(c *gc.C) {
    44  	_, _, err := charmrepo.NewCharmAtPath("cs:foo", "trusty")
    45  	c.Assert(err, gc.Equals, os.ErrNotExist)
    46  }
    47  
    48  func (s *bundlePathSuite) TestInvalidRelativePath(c *gc.C) {
    49  	_, _, err := charmrepo.NewBundleAtPath("./foo")
    50  	c.Assert(err, gc.Equals, os.ErrNotExist)
    51  }
    52  
    53  func (s *bundlePathSuite) TestRelativePath(c *gc.C) {
    54  	relDir := filepath.Join(TestCharms.Path(), "bundle")
    55  	cwd, err := os.Getwd()
    56  	c.Assert(err, jc.ErrorIsNil)
    57  	defer os.Chdir(cwd)
    58  	c.Assert(os.Chdir(relDir), jc.ErrorIsNil)
    59  	_, _, err = charmrepo.NewBundleAtPath("openstack")
    60  	c.Assert(charmrepo.IsInvalidPathError(err), jc.IsTrue)
    61  }
    62  
    63  func (s *bundlePathSuite) TestNoBundleAtPath(c *gc.C) {
    64  	_, _, err := charmrepo.NewBundleAtPath(c.MkDir())
    65  	c.Assert(err, gc.ErrorMatches, `bundle not found:.*`)
    66  }
    67  
    68  func (s *bundlePathSuite) TestGetBundle(c *gc.C) {
    69  	bundleDir := filepath.Join(TestCharms.Path(), "bundle", "openstack")
    70  	b, url, err := charmrepo.NewBundleAtPath(bundleDir)
    71  	c.Assert(err, jc.ErrorIsNil)
    72  	c.Assert(b.Data(), jc.DeepEquals, TestCharms.BundleDir("openstack").Data())
    73  	c.Assert(url, gc.DeepEquals, charm.MustParseURL("local:bundle/openstack-0"))
    74  }
    75  
    76  func (s *bundlePathSuite) TestGetBundleSymlink(c *gc.C) {
    77  	realPath := TestCharms.ClonedBundleDirPath(c.MkDir(), "wordpress-simple")
    78  	bundlesPath := c.MkDir()
    79  	linkPath := filepath.Join(bundlesPath, "wordpress-simple")
    80  	err := os.Symlink(realPath, linkPath)
    81  	c.Assert(err, jc.ErrorIsNil)
    82  	url := charm.MustParseURL("local:bundle/wordpress-simple")
    83  
    84  	b, url, err := charmrepo.NewBundleAtPath(filepath.Join(bundlesPath, "wordpress-simple"))
    85  	c.Assert(err, jc.ErrorIsNil)
    86  	c.Assert(b.Data(), jc.DeepEquals, TestCharms.BundleDir("wordpress-simple").Data())
    87  	c.Assert(url, gc.DeepEquals, charm.MustParseURL("local:bundle/wordpress-simple-0"))
    88  }
    89  
    90  func (s *bundlePathSuite) TestGetBundleLocalFile(c *gc.C) {
    91  	bundlePath := filepath.Join(c.MkDir(), "mybundle")
    92  	data := `
    93  applications:
    94    wordpress:
    95      charm: wordpress
    96      num_units: 1
    97  `[1:]
    98  	err := ioutil.WriteFile(bundlePath, []byte(data), 0644)
    99  	c.Assert(err, jc.ErrorIsNil)
   100  
   101  	bundleData, err := charmrepo.ReadBundleFile(bundlePath)
   102  	c.Assert(err, jc.ErrorIsNil)
   103  	out, err := yaml.Marshal(bundleData)
   104  	c.Assert(err, jc.ErrorIsNil)
   105  	c.Assert(string(out), jc.DeepEquals, data)
   106  }
   107  
   108  func (s *bundlePathSuite) TestGetBundleLocalFileNotExists(c *gc.C) {
   109  	bundlePath := filepath.Join(c.MkDir(), "mybundle")
   110  	_, err := charmrepo.ReadBundleFile(bundlePath)
   111  	c.Assert(err, gc.ErrorMatches, `bundle not found:.*`)
   112  }