class CharmBundle(CharmBase): """ZIP-archive that contains charm directory content.""" def __init__(self, path): self.path = isinstance(path, file) and path.name or path try: zf = ZipFile(path, 'r') except BadZipfile, exc: raise CharmError(path, "must be a zip file (%s)" % exc) if "metadata.yaml" not in zf.namelist(): raise CharmError( path, "charm does not contain required file 'metadata.yaml'") self.metadata = MetaData() self.metadata.parse(zf.read("metadata.yaml")) try: revision_content = zf.read("revision") except KeyError: revision_content = None self._revision = get_revision( revision_content, self.metadata, self.path) if self._revision is None: raise CharmError(self.path, "has no revision") self.config = ConfigOptions() if "config.yaml" in zf.namelist(): self.config.parse(zf.read("config.yaml"))
def __init__(self, client, charm_id, charm_data): self._client = client self._charm_url = CharmURL.parse(charm_id) self._charm_url.assert_revision() self._metadata = MetaData() self._metadata.parse_serialization_data(charm_data["metadata"]) self._config = ConfigOptions() self._config.parse(charm_data["config"]) # Just a health check: assert self._metadata.name == self.name self._sha256 = charm_data["sha256"] self._bundle_url = charm_data.get("url")
def test_load_file(self): sample_path = self.makeFile(sample_configuration) config = ConfigOptions() config.load(sample_path) self.assertEqual(config.get_serialization_data(), sample_yaml_data) # and an expected exception # on an empty file empty_file = self.makeFile("") error = self.assertRaises(ServiceConfigError, config.load, empty_file) self.assertEqual( str(error), ("Error processing %r: " "Missing required service options metadata") % empty_file) # a missing filename is allowed config = config.load("missing_file")
def __init__(self, path): self.path = path self.metadata = MetaData(os.path.join(path, "metadata.yaml")) revision_content = None revision_path = os.path.join(self.path, "revision") if os.path.exists(revision_path): with open(revision_path) as f: revision_content = f.read() self._revision = get_revision(revision_content, self.metadata, self.path) if self._revision is None: self.set_revision(0) elif revision_content is None: self.set_revision(self._revision) self.config = ConfigOptions() self.config.load(os.path.join(path, "config.yaml")) self._temp_bundle = None self._temp_bundle_file = None
def test_validate(self): sample_input = {"title": "Helpful Title", "outlook": "Peachy"} self.config.parse(sample_configuration) data = self.config.validate(sample_input) # This should include an overridden value, a default and a new value. self.assertEqual(data, {"username": "******", "outlook": "Peachy", "title": "Helpful Title"}) # now try to set a value outside the expected sample_input["bad"] = "value" error = self.assertRaises(ServiceConfigValueError, self.config.validate, sample_input) self.assertEqual(error.message, "bad is not a valid configuration option.") # validating with an empty instance # the service takes no options config = ConfigOptions() self.assertRaises( ServiceConfigValueError, config.validate, sample_input)
def setUp(self): self.config = ConfigOptions()