def test_extract_to(self): filename = self.makeFile() charm = get_charm_from_path(self.filename) f2 = charm.extract_to(filename) # f2 should be a charm directory self.assertInstance(f2, CharmDirectory) self.assertInstance(f2.get_sha256(), basestring) self.assertEqual(f2.path, filename)
def test_charm_from_path(self): # from a directory charm = get_charm_from_path(sample_directory) assert charm.get_sha256() filename = self.makeFile(suffix=".charm") charm.make_archive(filename) # and from a bundle charm = get_charm_from_path(filename) self.assertEquals(charm.path, filename) self.assertInstance(charm.get_sha256(), basestring) # and validate the implementation detail that calling it twice # doesn't result in an error after caching the callable charm = get_charm_from_path(filename) self.assertEquals(charm.path, filename) self.assertInstance(charm.get_sha256(), basestring)
def test_as_directory(self): filename = self.makeFile() charm = get_charm_from_path(self.filename) f2 = charm.as_directory() # f2 should be a charm directory self.assertInstance(f2, CharmDirectory) self.assertInstance(f2.get_sha256(), basestring) # verify that it was extracted to a new temp dirname self.assertNotEqual(f2.path, filename) fn = os.path.split(f2.path)[1] # verify that it used the expected prefix self.assertStartsWith(fn, "tmp")
def find(self, charm_url): info = yield self._get_info(charm_url) revision = info["revision"] if charm_url.revision is None: charm_url = charm_url.with_revision(revision) else: assert revision == charm_url.revision, "bad url revision" cache_path = os.path.join(self.cache_path, _cache_key(charm_url)) cached = os.path.exists(cache_path) if not cached: yield self._download(charm_url, cache_path) charm = get_charm_from_path(cache_path) assert charm.get_revision() == revision, "bad charm revision" if charm.get_sha256() != info["sha256"]: os.remove(cache_path) name = "%s (%s)" % (charm_url, "cached" if cached else "downloaded") raise CharmError(name, "SHA256 mismatch") returnValue(charm)
def _collection(self, collection): path = os.path.join(self.path, collection.series) if not os.path.exists(path): return for dentry in os.listdir(path): dentry_path = os.path.join(path, dentry) try: yield get_charm_from_path(dentry_path) except FileNotFound: continue # There is a broken charm in the repo, but that # shouldn't stop us from continuing except yaml.YAMLError, e: # Log yaml errors for feedback to developers. log.warning("Charm %r has a YAML error: %s", dentry, e) continue except (ServiceConfigError, MetaDataError), e: # Log invalid config.yaml and metadata.yaml semantic errors log.warning("Charm %r has an error: %r %s", dentry, e, e) continue