github.com/juju/juju@v0.0.0-20240327075706-a90865de2538/core/charm/charmpath_test.go (about)

     1  // Copyright 2021 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package charm_test
     5  
     6  import (
     7  	"os"
     8  	"path/filepath"
     9  
    10  	"github.com/juju/charm/v12"
    11  	jc "github.com/juju/testing/checkers"
    12  	gc "gopkg.in/check.v1"
    13  
    14  	"github.com/juju/juju/core/base"
    15  	corecharm "github.com/juju/juju/core/charm"
    16  	"github.com/juju/juju/testcharms"
    17  )
    18  
    19  type charmPathSuite struct {
    20  	repoPath string
    21  }
    22  
    23  var _ = gc.Suite(&charmPathSuite{})
    24  
    25  func (s *charmPathSuite) SetUpTest(c *gc.C) {
    26  	s.repoPath = c.MkDir()
    27  }
    28  
    29  func (s *charmPathSuite) cloneCharmDir(path, name string) string {
    30  	return testcharms.Repo.ClonedDirPath(path, name)
    31  }
    32  
    33  func (s *charmPathSuite) TestNoPath(c *gc.C) {
    34  	_, _, err := corecharm.NewCharmAtPath("", base.MustParseBaseFromString("ubuntu@22.04"))
    35  	c.Assert(err, gc.ErrorMatches, "empty charm path")
    36  }
    37  
    38  func (s *charmPathSuite) TestInvalidPath(c *gc.C) {
    39  	_, _, err := corecharm.NewCharmAtPath("/foo", base.MustParseBaseFromString("ubuntu@22.04"))
    40  	c.Assert(err, gc.Equals, os.ErrNotExist)
    41  }
    42  
    43  func (s *charmPathSuite) TestRepoURL(c *gc.C) {
    44  	_, _, err := corecharm.NewCharmAtPath("ch:foo", base.MustParseBaseFromString("ubuntu@22.04"))
    45  	c.Assert(err, gc.Equals, os.ErrNotExist)
    46  }
    47  
    48  func (s *charmPathSuite) TestInvalidRelativePath(c *gc.C) {
    49  	_, _, err := corecharm.NewCharmAtPath("./foo", base.MustParseBaseFromString("ubuntu@22.04"))
    50  	c.Assert(err, gc.Equals, os.ErrNotExist)
    51  }
    52  
    53  func (s *charmPathSuite) TestRelativePath(c *gc.C) {
    54  	s.cloneCharmDir(s.repoPath, "mysql")
    55  	cwd, err := os.Getwd()
    56  	c.Assert(err, jc.ErrorIsNil)
    57  	defer func() { _ = os.Chdir(cwd) }()
    58  	c.Assert(os.Chdir(s.repoPath), jc.ErrorIsNil)
    59  	_, _, err = corecharm.NewCharmAtPath("mysql", base.MustParseBaseFromString("ubuntu@22.04"))
    60  	c.Assert(corecharm.IsInvalidPathError(err), jc.IsTrue)
    61  }
    62  
    63  func (s *charmPathSuite) TestNoCharmAtPath(c *gc.C) {
    64  	_, _, err := corecharm.NewCharmAtPath(c.MkDir(), base.MustParseBaseFromString("ubuntu@22.04"))
    65  	c.Assert(err, gc.ErrorMatches, "charm not found.*")
    66  }
    67  
    68  func (s *charmPathSuite) TestCharm(c *gc.C) {
    69  	charmDir := filepath.Join(s.repoPath, "dummy")
    70  	s.cloneCharmDir(s.repoPath, "dummy")
    71  	ch, url, err := corecharm.NewCharmAtPath(charmDir, base.MustParseBaseFromString("ubuntu@20.04"))
    72  	c.Assert(err, jc.ErrorIsNil)
    73  	c.Assert(ch.Meta().Name, gc.Equals, "dummy")
    74  	c.Assert(ch.Revision(), gc.Equals, 1)
    75  	c.Assert(url, gc.DeepEquals, charm.MustParseURL("local:focal/dummy-1"))
    76  }
    77  
    78  func (s *charmPathSuite) TestCharmArchive(c *gc.C) {
    79  	charmDir := filepath.Join(s.repoPath, "dummy")
    80  	s.cloneCharmDir(s.repoPath, "dummy")
    81  	chDir, err := charm.ReadCharmDir(charmDir)
    82  	c.Assert(err, jc.ErrorIsNil)
    83  
    84  	dir := c.MkDir()
    85  	archivePath := filepath.Join(dir, "archive.charm")
    86  	file, err := os.Create(archivePath)
    87  	c.Assert(err, jc.ErrorIsNil)
    88  	defer file.Close()
    89  
    90  	err = chDir.ArchiveTo(file)
    91  	c.Assert(err, jc.ErrorIsNil)
    92  
    93  	ch, url, err := corecharm.NewCharmAtPath(archivePath, base.MustParseBaseFromString("ubuntu@20.04"))
    94  	c.Assert(err, jc.ErrorIsNil)
    95  	c.Assert(ch.Meta().Name, gc.Equals, "dummy")
    96  	c.Assert(ch.Revision(), gc.Equals, 1)
    97  	c.Assert(url, gc.DeepEquals, charm.MustParseURL("local:focal/dummy-1"))
    98  }
    99  
   100  func (s *charmPathSuite) TestCharmWithManifest(c *gc.C) {
   101  	repo := testcharms.RepoForSeries("focal")
   102  	charmDir := repo.CharmDir("cockroach")
   103  	ch, url, err := corecharm.NewCharmAtPath(charmDir.Path, base.Base{})
   104  	c.Assert(err, jc.ErrorIsNil)
   105  	c.Assert(ch.Meta().Name, gc.Equals, "cockroachdb")
   106  	c.Assert(ch.Revision(), gc.Equals, 0)
   107  	c.Assert(url, gc.DeepEquals, charm.MustParseURL("local:focal/cockroachdb-0"))
   108  }
   109  
   110  func (s *charmPathSuite) TestNoBaseSpecified(c *gc.C) {
   111  	charmDir := filepath.Join(s.repoPath, "sample-fail-no-os")
   112  	s.cloneCharmDir(s.repoPath, "sample-fail-no-os")
   113  	_, _, err := corecharm.NewCharmAtPath(charmDir, base.Base{})
   114  	c.Assert(err, gc.ErrorMatches, "base not specified and charm does not define any")
   115  }
   116  
   117  func (s *charmPathSuite) TestNoBaseSpecifiedForceStillFails(c *gc.C) {
   118  	charmDir := filepath.Join(s.repoPath, "sample-fail-no-os")
   119  	s.cloneCharmDir(s.repoPath, "sample-fail-no-os")
   120  	_, _, err := corecharm.NewCharmAtPathForceBase(charmDir, base.Base{}, true)
   121  	c.Assert(err, gc.ErrorMatches, "base not specified and charm does not define any")
   122  }
   123  
   124  func (s *charmPathSuite) TestMultiBaseDefault(c *gc.C) {
   125  	charmDir := filepath.Join(s.repoPath, "multi-series-charmpath")
   126  	s.cloneCharmDir(s.repoPath, "multi-series-charmpath")
   127  	ch, url, err := corecharm.NewCharmAtPath(charmDir, base.Base{})
   128  	c.Assert(err, gc.IsNil)
   129  	c.Assert(ch.Meta().Name, gc.Equals, "new-charm-with-multi-series")
   130  	c.Assert(ch.Revision(), gc.Equals, 7)
   131  	c.Assert(url, gc.DeepEquals, charm.MustParseURL("local:jammy/new-charm-with-multi-series-7"))
   132  }
   133  
   134  func (s *charmPathSuite) TestMultiBase(c *gc.C) {
   135  	charmDir := filepath.Join(s.repoPath, "multi-series-charmpath")
   136  	s.cloneCharmDir(s.repoPath, "multi-series-charmpath")
   137  	ch, url, err := corecharm.NewCharmAtPath(charmDir, base.MustParseBaseFromString("ubuntu@20.04"))
   138  	c.Assert(err, gc.IsNil)
   139  	c.Assert(ch.Meta().Name, gc.Equals, "new-charm-with-multi-series")
   140  	c.Assert(ch.Revision(), gc.Equals, 7)
   141  	c.Assert(url, gc.DeepEquals, charm.MustParseURL("local:focal/new-charm-with-multi-series-7"))
   142  }
   143  
   144  func (s *charmPathSuite) TestUnsupportedBase(c *gc.C) {
   145  	charmDir := filepath.Join(s.repoPath, "multi-series-charmpath")
   146  	s.cloneCharmDir(s.repoPath, "multi-series-charmpath")
   147  	_, _, err := corecharm.NewCharmAtPath(charmDir, base.MustParseBaseFromString("ubuntu@15.10"))
   148  	c.Assert(err, gc.ErrorMatches, `base "ubuntu@15.10" not supported by charm, the charm supported bases are.*`)
   149  }
   150  
   151  func (s *charmPathSuite) TestUnsupportedBaseNoForce(c *gc.C) {
   152  	charmDir := filepath.Join(s.repoPath, "multi-series-charmpath")
   153  	s.cloneCharmDir(s.repoPath, "multi-series-charmpath")
   154  	_, _, err := corecharm.NewCharmAtPathForceBase(charmDir, base.MustParseBaseFromString("ubuntu@15.10"), false)
   155  	c.Assert(err, gc.ErrorMatches, `base "ubuntu@15.10" not supported by charm, the charm supported bases are.*`)
   156  }
   157  
   158  func (s *charmPathSuite) TestUnsupportedBaseForce(c *gc.C) {
   159  	charmDir := filepath.Join(s.repoPath, "multi-series-charmpath")
   160  	s.cloneCharmDir(s.repoPath, "multi-series-charmpath")
   161  	ch, url, err := corecharm.NewCharmAtPathForceBase(charmDir, base.MustParseBaseFromString("ubuntu@15.10"), true)
   162  	c.Assert(err, jc.ErrorIsNil)
   163  	c.Assert(ch.Meta().Name, gc.Equals, "new-charm-with-multi-series")
   164  	c.Assert(ch.Revision(), gc.Equals, 7)
   165  	c.Assert(url, gc.DeepEquals, charm.MustParseURL("local:wily/new-charm-with-multi-series-7"))
   166  }
   167  
   168  func (s *charmPathSuite) TestFindsSymlinks(c *gc.C) {
   169  	realPath := testcharms.Repo.ClonedDirPath(c.MkDir(), "dummy")
   170  	charmsPath := c.MkDir()
   171  	linkPath := filepath.Join(charmsPath, "dummy")
   172  	err := os.Symlink(realPath, linkPath)
   173  	c.Assert(err, gc.IsNil)
   174  
   175  	ch, url, err := corecharm.NewCharmAtPath(filepath.Join(charmsPath, "dummy"), base.MustParseBaseFromString("ubuntu@12.10"))
   176  	c.Assert(err, gc.IsNil)
   177  	c.Assert(ch.Revision(), gc.Equals, 1)
   178  	c.Assert(ch.Meta().Name, gc.Equals, "dummy")
   179  	c.Assert(ch.Config().Options["title"].Default, gc.Equals, "My Title")
   180  	c.Assert(ch.(*charm.CharmDir).Path, gc.Equals, linkPath)
   181  	c.Assert(url, gc.DeepEquals, charm.MustParseURL("local:quantal/dummy-1"))
   182  }