本文整理汇总了Golang中github.com/juju/charm.ReadDir函数的典型用法代码示例。如果您正苦于以下问题:Golang ReadDir函数的具体用法?Golang ReadDir怎么用?Golang ReadDir使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了ReadDir函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestDirRevisionFile
func (s *DirSuite) TestDirRevisionFile(c *gc.C) {
charmDir := charmtesting.Charms.ClonedDirPath(c.MkDir(), "dummy")
revPath := filepath.Join(charmDir, "revision")
// Missing revision file
err := os.Remove(revPath)
c.Assert(err, gc.IsNil)
dir, err := charm.ReadDir(charmDir)
c.Assert(err, gc.IsNil)
c.Assert(dir.Revision(), gc.Equals, 0)
// Missing revision file with old revision in metadata
file, err := os.OpenFile(filepath.Join(charmDir, "metadata.yaml"), os.O_WRONLY|os.O_APPEND, 0)
c.Assert(err, gc.IsNil)
_, err = file.Write([]byte("\nrevision: 1234\n"))
c.Assert(err, gc.IsNil)
dir, err = charm.ReadDir(charmDir)
c.Assert(err, gc.IsNil)
c.Assert(dir.Revision(), gc.Equals, 1234)
// Revision file with bad content
err = ioutil.WriteFile(revPath, []byte("garbage"), 0666)
c.Assert(err, gc.IsNil)
dir, err = charm.ReadDir(charmDir)
c.Assert(err, gc.ErrorMatches, "invalid revision file")
c.Assert(dir, gc.IsNil)
}
示例2: TestDirSetDiskRevision
func (s *DirSuite) TestDirSetDiskRevision(c *gc.C) {
charmDir := charmtesting.Charms.ClonedDirPath(c.MkDir(), "dummy")
dir, err := charm.ReadDir(charmDir)
c.Assert(err, gc.IsNil)
c.Assert(dir.Revision(), gc.Equals, 1)
dir.SetDiskRevision(42)
c.Assert(dir.Revision(), gc.Equals, 42)
dir, err = charm.ReadDir(charmDir)
c.Assert(err, gc.IsNil)
c.Assert(dir.Revision(), gc.Equals, 42)
}
示例3: TestReadDirWithoutConfig
func (s *DirSuite) TestReadDirWithoutConfig(c *gc.C) {
path := charmtesting.Charms.DirPath("varnish")
dir, err := charm.ReadDir(path)
c.Assert(err, gc.IsNil)
// A lacking config.yaml file still causes a proper
// Config value to be returned.
c.Assert(dir.Config().Options, gc.HasLen, 0)
}
示例4: TestReadDirWithoutActions
func (s *DirSuite) TestReadDirWithoutActions(c *gc.C) {
path := charmtesting.Charms.DirPath("wordpress")
dir, err := charm.ReadDir(path)
c.Assert(err, gc.IsNil)
// A lacking actions.yaml file still causes a proper
// Actions value to be returned.
c.Assert(dir.Actions().ActionSpecs, gc.HasLen, 0)
}
示例5: bundleDir
func bundleDir(c *gc.C, dirpath string) *charm.Bundle {
dir, err := charm.ReadDir(dirpath)
c.Assert(err, gc.IsNil)
buf := new(bytes.Buffer)
err = dir.BundleTo(buf)
c.Assert(err, gc.IsNil)
bundle, err := charm.ReadBundleBytes(buf.Bytes())
c.Assert(err, gc.IsNil)
return bundle
}
示例6: TestRespectsLocalRevisionWhenPossible
func (s *UpgradeCharmSuccessSuite) TestRespectsLocalRevisionWhenPossible(c *gc.C) {
dir, err := charm.ReadDir(s.path)
c.Assert(err, gc.IsNil)
err = dir.SetDiskRevision(42)
c.Assert(err, gc.IsNil)
err = runUpgradeCharm(c, "riak")
c.Assert(err, gc.IsNil)
s.assertUpgraded(c, 42, false)
s.assertLocalRevision(c, 42, s.path)
}
示例7: TestExpandTo
func (s *BundleSuite) TestExpandTo(c *gc.C) {
bundle, err := charm.ReadBundle(s.bundlePath)
c.Assert(err, gc.IsNil)
path := filepath.Join(c.MkDir(), "charm")
err = bundle.ExpandTo(path)
c.Assert(err, gc.IsNil)
dir, err := charm.ReadDir(path)
c.Assert(err, gc.IsNil)
checkDummy(c, dir, path)
}
示例8: processUploadedArchive
// processUploadedArchive opens the given charm archive from path,
// inspects it to see if it has all files at the root of the archive
// or it has subdirs. It repackages the archive so it has all the
// files at the root dir, if necessary, replacing the original archive
// at path.
func (h *charmsHandler) processUploadedArchive(path string) error {
// Open the archive as a zip.
f, err := os.OpenFile(path, os.O_RDWR, 0644)
if err != nil {
return err
}
defer f.Close()
fi, err := f.Stat()
if err != nil {
return err
}
zipr, err := zip.NewReader(f, fi.Size())
if err != nil {
return errors.Annotate(err, "cannot open charm archive")
}
// Find out the root dir prefix from the archive.
rootDir, err := h.findArchiveRootDir(zipr)
if err != nil {
return errors.Annotate(err, "cannot read charm archive")
}
if rootDir == "." {
// Normal charm, just use charm.ReadBundle().
return nil
}
// There is one or more subdirs, so we need extract it to a temp
// dir and then read it as a charm dir.
tempDir, err := ioutil.TempDir("", "charm-extract")
if err != nil {
return errors.Annotate(err, "cannot create temp directory")
}
defer os.RemoveAll(tempDir)
if err := ziputil.Extract(zipr, tempDir, rootDir); err != nil {
return errors.Annotate(err, "cannot extract charm archive")
}
dir, err := charm.ReadDir(tempDir)
if err != nil {
return errors.Annotate(err, "cannot read extracted archive")
}
// Now repackage the dir as a bundle at the original path.
if err := f.Truncate(0); err != nil {
return err
}
if err := dir.BundleTo(f); err != nil {
return err
}
return nil
}
示例9: TestBundleToWithBadType
func (s *DirSuite) TestBundleToWithBadType(c *gc.C) {
charmDir := charmtesting.Charms.ClonedDirPath(c.MkDir(), "dummy")
badFile := filepath.Join(charmDir, "hooks", "badfile")
// Symlink targeting a path outside of the charm.
err := os.Symlink("../../target", badFile)
c.Assert(err, gc.IsNil)
dir, err := charm.ReadDir(charmDir)
c.Assert(err, gc.IsNil)
err = dir.BundleTo(&bytes.Buffer{})
c.Assert(err, gc.ErrorMatches, `symlink "hooks/badfile" links out of charm: "../../target"`)
// Symlink targeting an absolute path.
os.Remove(badFile)
err = os.Symlink("/target", badFile)
c.Assert(err, gc.IsNil)
dir, err = charm.ReadDir(charmDir)
c.Assert(err, gc.IsNil)
err = dir.BundleTo(&bytes.Buffer{})
c.Assert(err, gc.ErrorMatches, `symlink "hooks/badfile" is absolute: "/target"`)
// Can't bundle special files either.
os.Remove(badFile)
err = syscall.Mkfifo(badFile, 0644)
c.Assert(err, gc.IsNil)
dir, err = charm.ReadDir(charmDir)
c.Assert(err, gc.IsNil)
err = dir.BundleTo(&bytes.Buffer{})
c.Assert(err, gc.ErrorMatches, `file is a named pipe: "hooks/badfile"`)
}
示例10: TestUpgradeCharmDir
func (s *DeploySuite) TestUpgradeCharmDir(c *gc.C) {
// Add the charm, so the url will exist and a new revision will be
// picked in ServiceDeploy.
dummyCharm := s.AddTestingCharm(c, "dummy")
dirPath := charmtesting.Charms.ClonedDirPath(s.SeriesPath, "dummy")
err := runDeploy(c, "local:quantal/dummy")
c.Assert(err, gc.IsNil)
upgradedRev := dummyCharm.Revision() + 1
curl := dummyCharm.URL().WithRevision(upgradedRev)
s.AssertService(c, "dummy", curl, 1, 0)
// Check the charm dir was left untouched.
ch, err := charm.ReadDir(dirPath)
c.Assert(err, gc.IsNil)
c.Assert(ch.Revision(), gc.Equals, 1)
}
示例11: TestUpgradesWithBundle
func (s *UpgradeCharmSuccessSuite) TestUpgradesWithBundle(c *gc.C) {
dir, err := charm.ReadDir(s.path)
c.Assert(err, gc.IsNil)
dir.SetRevision(42)
buf := &bytes.Buffer{}
err = dir.BundleTo(buf)
c.Assert(err, gc.IsNil)
bundlePath := path.Join(s.SeriesPath, "riak.charm")
err = ioutil.WriteFile(bundlePath, buf.Bytes(), 0644)
c.Assert(err, gc.IsNil)
err = runUpgradeCharm(c, "riak")
c.Assert(err, gc.IsNil)
s.assertUpgraded(c, 42, false)
s.assertLocalRevision(c, 7, s.path)
}
示例12: TestBundleSetRevision
func (s *BundleSuite) TestBundleSetRevision(c *gc.C) {
bundle, err := charm.ReadBundle(s.bundlePath)
c.Assert(err, gc.IsNil)
c.Assert(bundle.Revision(), gc.Equals, 1)
bundle.SetRevision(42)
c.Assert(bundle.Revision(), gc.Equals, 42)
path := filepath.Join(c.MkDir(), "charm")
err = bundle.ExpandTo(path)
c.Assert(err, gc.IsNil)
dir, err := charm.ReadDir(path)
c.Assert(err, gc.IsNil)
c.Assert(dir.Revision(), gc.Equals, 42)
}
示例13: AddCustomBundle
func (br *bundleReader) AddCustomBundle(c *gc.C, url *corecharm.URL, customize func(path string)) charm.BundleInfo {
base := c.MkDir()
dirpath := charmtesting.Charms.ClonedDirPath(base, "dummy")
if customize != nil {
customize(dirpath)
}
dir, err := corecharm.ReadDir(dirpath)
c.Assert(err, gc.IsNil)
err = dir.SetDiskRevision(url.Revision)
c.Assert(err, gc.IsNil)
bunpath := filepath.Join(base, "bundle")
file, err := os.Create(bunpath)
c.Assert(err, gc.IsNil)
defer file.Close()
err = dir.BundleTo(file)
c.Assert(err, gc.IsNil)
bundle, err := corecharm.ReadBundle(bunpath)
c.Assert(err, gc.IsNil)
return br.AddBundle(c, url, bundle)
}
示例14: TestExpandToSetsHooksExecutable
func (s *BundleSuite) TestExpandToSetsHooksExecutable(c *gc.C) {
charmDir := charmtesting.Charms.ClonedDir(c.MkDir(), "all-hooks")
// Bundle manually, so we can check ExpandTo(), unaffected
// by BundleTo()'s behavior
bundlePath := filepath.Join(c.MkDir(), "bundle.charm")
s.prepareBundle(c, charmDir, bundlePath)
bundle, err := charm.ReadBundle(bundlePath)
c.Assert(err, gc.IsNil)
path := filepath.Join(c.MkDir(), "charm")
err = bundle.ExpandTo(path)
c.Assert(err, gc.IsNil)
_, err = charm.ReadDir(path)
c.Assert(err, gc.IsNil)
for name := range bundle.Meta().Hooks() {
hookName := string(name)
info, err := os.Stat(filepath.Join(path, "hooks", hookName))
c.Assert(err, gc.IsNil)
perm := info.Mode() & 0777
c.Assert(perm&0100 != 0, gc.Equals, true, gc.Commentf("hook %q is not executable", hookName))
}
}
示例15: assertBundleTo
func (s *DirSuite) assertBundleTo(c *gc.C, baseDir, charmDir string) {
var haveSymlinks = true
if err := os.Symlink("../target", filepath.Join(charmDir, "hooks/symlink")); err != nil {
haveSymlinks = false
}
dir, err := charm.ReadDir(charmDir)
c.Assert(err, gc.IsNil)
path := filepath.Join(baseDir, "bundle.charm")
file, err := os.Create(path)
c.Assert(err, gc.IsNil)
err = dir.BundleTo(file)
file.Close()
c.Assert(err, gc.IsNil)
zipr, err := zip.OpenReader(path)
c.Assert(err, gc.IsNil)
defer zipr.Close()
var metaf, instf, emptyf, revf, symf *zip.File
for _, f := range zipr.File {
c.Logf("Bundled file: %s", f.Name)
switch f.Name {
case "revision":
revf = f
case "metadata.yaml":
metaf = f
case "hooks/install":
instf = f
case "hooks/symlink":
symf = f
case "empty/":
emptyf = f
case "build/ignored":
c.Errorf("bundle includes build/*: %s", f.Name)
case ".ignored", ".dir/ignored":
c.Errorf("bundle includes .* entries: %s", f.Name)
}
}
c.Assert(revf, gc.NotNil)
reader, err := revf.Open()
c.Assert(err, gc.IsNil)
data, err := ioutil.ReadAll(reader)
reader.Close()
c.Assert(err, gc.IsNil)
c.Assert(string(data), gc.Equals, "1")
c.Assert(metaf, gc.NotNil)
reader, err = metaf.Open()
c.Assert(err, gc.IsNil)
meta, err := charm.ReadMeta(reader)
reader.Close()
c.Assert(err, gc.IsNil)
c.Assert(meta.Name, gc.Equals, "dummy")
c.Assert(instf, gc.NotNil)
// Despite it being 0751, we pack and unpack it as 0755.
c.Assert(instf.Mode()&0777, gc.Equals, os.FileMode(0755))
if haveSymlinks {
c.Assert(symf, gc.NotNil)
c.Assert(symf.Mode()&0777, gc.Equals, os.FileMode(0777))
reader, err = symf.Open()
c.Assert(err, gc.IsNil)
data, err = ioutil.ReadAll(reader)
reader.Close()
c.Assert(err, gc.IsNil)
c.Assert(string(data), gc.Equals, "../target")
} else {
c.Assert(symf, gc.IsNil)
}
c.Assert(emptyf, gc.NotNil)
c.Assert(emptyf.Mode()&os.ModeType, gc.Equals, os.ModeDir)
// Despite it being 0750, we pack and unpack it as 0755.
c.Assert(emptyf.Mode()&0777, gc.Equals, os.FileMode(0755))
}